Skip to main content

HashFunction

Trait HashFunction 

Source
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§

Source

type Digest: AsRef<[u8]> + Clone + Debug + PartialEq + Eq + Send + Sync + 'static

The concrete digest type produced by this hash function.

Required Methods§

Source

fn hash(data: &[u8]) -> Self::Digest

Hash arbitrary bytes and return the digest.

Source

fn algorithm_name() -> &'static str

Human-readable name of the algorithm, e.g. "SHA-256" or "BLAKE3".

Source

fn digest_size() -> usize

Output size of the digest in bytes.

Provided Methods§

Source

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.

Source

fn empty() -> Self::Digest

Return the canonical “empty” digest used to pad trees to a power-of-two size, or to represent vacant slots in sparse trees.

The default is the hash of a zero-length byte slice.

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.

Implementors§