lambdaworks_crypto/merkle_tree/
traits.rs

1use alloc::vec::Vec;
2#[cfg(feature = "parallel")]
3use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
4
5/// A backend for Merkle trees. This defines raw `Data` from which the Merkle
6/// tree is built from. It also defines the `Node` type and the hash function
7/// used to build parent nodes from children nodes.
8pub trait IsMerkleTreeBackend {
9    type Node: PartialEq + Eq + Clone + Sync + Send;
10    type Data: Sync + Send;
11
12    /// This function takes a single variable `Data` and converts it to a node.
13    fn hash_data(leaf: &Self::Data) -> Self::Node;
14
15    /// This function takes the list of data from which the Merkle
16    /// tree will be built from and converts it to a list of leaf nodes.
17    fn hash_leaves(unhashed_leaves: &[Self::Data]) -> Vec<Self::Node> {
18        #[cfg(feature = "parallel")]
19        let iter = unhashed_leaves.par_iter();
20        #[cfg(not(feature = "parallel"))]
21        let iter = unhashed_leaves.iter();
22
23        iter.map(|leaf| Self::hash_data(leaf)).collect()
24    }
25
26    /// This function takes to children nodes and builds a new parent node.
27    /// It will be used in the construction of the Merkle tree.
28    fn hash_new_parent(child_1: &Self::Node, child_2: &Self::Node) -> Self::Node;
29}