Skip to main content

MerkleHasher

Trait MerkleHasher 

Source
pub trait MerkleHasher {
    const OUT_LEN: usize;
    const PARALLEL_PAIRS: usize = 1;

    // Required method
    fn hash_pair(
        &self,
        pair: &[[u8; <Self as MerkleHasher>::OUT_LEN]; 2],
    ) -> [u8; <Self as MerkleHasher>::OUT_LEN];

    // Provided methods
    fn hash_pairs<const NUM_PAIRS: usize>(
        &self,
        pairs: &[[[u8; <Self as MerkleHasher>::OUT_LEN]; 2]; NUM_PAIRS],
    ) -> [[u8; <Self as MerkleHasher>::OUT_LEN]; NUM_PAIRS] { ... }
    fn hash_single_leaf(
        &self,
        leaf: &[u8; <Self as MerkleHasher>::OUT_LEN],
    ) -> [u8; <Self as MerkleHasher>::OUT_LEN] { ... }
}
Expand description

Hash function for inner nodes of Merkle Trees.

Hashing of many pairs at once is a part of the API, such that hash functions like BLAKE3 can use SIMD to hash multiple pairs in parallel.

Hashers used with SparseMerkleTree must never hash nodes into all zeroes, see its docs for details.

Required Associated Constants§

Source

const OUT_LEN: usize

Size of a node hash in bytes

Provided Associated Constants§

Source

const PARALLEL_PAIRS: usize = 1

Number of pairs Self::hash_pairs() hashes in parallel, 1 for hashers that hash pairs one by one.

Trees size batches of pairs and buffers for them accordingly, such that hashers without parallelism don’t pay for batching. Must be a power of two that is not larger than 64.

Required Methods§

Source

fn hash_pair( &self, pair: &[[u8; <Self as MerkleHasher>::OUT_LEN]; 2], ) -> [u8; <Self as MerkleHasher>::OUT_LEN]

Hash a pair of nodes together

Provided Methods§

Source

fn hash_pairs<const NUM_PAIRS: usize>( &self, pairs: &[[[u8; <Self as MerkleHasher>::OUT_LEN]; 2]; NUM_PAIRS], ) -> [[u8; <Self as MerkleHasher>::OUT_LEN]; NUM_PAIRS]

Similar to Self::hash_pair(), but hashes multiple pairs at once.

The default implementation hashes pairs one by one, hash functions that can do better should override it. An override must return the same as Self::hash_pair() for each pair, because trees mix both.

Source

fn hash_single_leaf( &self, leaf: &[u8; <Self as MerkleHasher>::OUT_LEN], ) -> [u8; <Self as MerkleHasher>::OUT_LEN]

Root of an UnbalancedMerkleTree or MerkleMountainRange with a single leaf.

The default implementation returns the leaf unchanged, which keeps a tree of trees the same as a single larger tree. Hashers that depend on the tree size should hash the leaf instead, otherwise the size isn’t committed to when the tree has a single leaf and no inner nodes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§