add Hash trait

This commit is contained in:
sinui0
2024-07-31 17:54:51 +09:00
parent ae7bc91b85
commit 8a2405218f

View File

@@ -1,6 +1,22 @@
use crate::prelude::*;
use core::convert::TryFrom;
use core::mem;
use core::{convert::TryFrom, mem};
/// This type is used as a hash type in the library.
///
/// It is recommended to use fixed size u8 array as a hash type. For example,
/// for sha256 the type would be `[u8; 32]`, representing 32 bytes,
/// which is the size of the sha256 digest. Also, fixed sized arrays of `u8`
/// by default satisfy all trait bounds required by this type.
///
/// # Trait bounds
/// `Copy` is required as the hash needs to be copied to be concatenated/propagated
/// when constructing nodes.
/// `PartialEq` is required to compare equality when verifying proof
/// `Into<Vec<u8>>` is required to be able to serialize proof
/// `TryFrom<Vec<u8>>` is required to parse hashes from a serialized proof
pub trait Hash: Copy + PartialEq + Into<Vec<u8>> + TryFrom<Vec<u8>> {}
impl<T> Hash for T where T: Copy + PartialEq + Into<Vec<u8>> + TryFrom<Vec<u8>> {}
/// Hasher is a trait used to provide a hashing algorithm for the library.
///
@@ -27,19 +43,7 @@ use core::mem;
/// }
/// ```
pub trait Hasher: Clone {
/// This type is used as a hash type in the library.
/// It is recommended to use fixed size u8 array as a hash type. For example,
/// for sha256 the type would be `[u8; 32]`, representing 32 bytes,
/// which is the size of the sha256 digest. Also, fixed sized arrays of `u8`
/// by default satisfy all trait bounds required by this type.
///
/// # Trait bounds
/// `Copy` is required as the hash needs to be copied to be concatenated/propagated
/// when constructing nodes.
/// `PartialEq` is required to compare equality when verifying proof
/// `Into<Vec<u8>>` is required to be able to serialize proof
/// `TryFrom<Vec<u8>>` is required to parse hashes from a serialized proof
type Hash: Copy + PartialEq + Into<Vec<u8>> + TryFrom<Vec<u8>>;
type Hash: Hash;
/// This associated function takes a slice of bytes and returns a hash of it.
/// Used by `concat_and_hash` function to build a tree from concatenated hashes