pub trait HashFunction:
Send
+ Sync
+ 'static {
type Digest: AsRef<[u8]> + Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
// Required methods
fn hash(data: &[u8]) -> Self::Digest;
fn algorithm_name() -> &'static str;
fn digest_size() -> usize;
// Provided methods
fn hash_nodes(left: &Self::Digest, right: &Self::Digest) -> Self::Digest { ... }
fn empty() -> Self::Digest { ... }
}Expand description
Abstraction over a cryptographic hash function used by Merkle trees.
§Type parameter Digest
The associated type Digest represents a fixed-size hash output. It
must be:
Clone— nodes are cloned when building the tree.PartialEq + Eq— so roots and proof hashes can be compared.AsRef<[u8]>— for serialisation and concatenation before hashing.Debug— for test output and diagnostic messages.Send + Sync— so trees can be used across thread boundaries.
§Zero-cost abstractions
All methods are #[inline] by convention in implementors. Rust’s
monomorphisation ensures that no vtable dispatch overhead occurs when
calling through H: HashFunction bounds.
Required Associated Types§
Required Methods§
Sourcefn algorithm_name() -> &'static str
fn algorithm_name() -> &'static str
Human-readable name of the algorithm, e.g. "SHA-256" or "BLAKE3".
Sourcefn digest_size() -> usize
fn digest_size() -> usize
Output size of the digest in bytes.
Provided Methods§
Sourcefn hash_nodes(left: &Self::Digest, right: &Self::Digest) -> Self::Digest
fn hash_nodes(left: &Self::Digest, right: &Self::Digest) -> Self::Digest
Hash two child digests together to produce a parent digest.
The default implementation concatenates left || right and calls
Self::hash. Override this to use domain separation or a different
concatenation strategy.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.