fuel_merkle/binary/
hash.rs

1use crate::common::{
2    empty_sum_sha256,
3    Bytes32,
4    Prefix,
5};
6
7use digest::Digest;
8use sha2::Sha256;
9
10type Hash = Sha256;
11
12// Merkle Tree hash of an empty list
13// MTH({}) = Hash()
14pub const fn empty_sum() -> &'static Bytes32 {
15    empty_sum_sha256()
16}
17
18// Merkle tree hash of an n-element list D[n]
19// MTH(D[n]) = Hash(0x01 || MTH(D[0:k]) || MTH(D[k:n])
20pub fn node_sum(lhs_data: &Bytes32, rhs_data: &Bytes32) -> Bytes32 {
21    let mut hash = Hash::new();
22
23    hash.update(Prefix::Node);
24    hash.update(lhs_data);
25    hash.update(rhs_data);
26
27    hash.finalize().into()
28}
29
30// Merkle tree hash of a list with one entry
31// MTH({d(0)}) = Hash(0x00 || d(0))
32pub fn leaf_sum(data: &[u8]) -> Bytes32 {
33    let mut hash = Hash::new();
34
35    hash.update(Prefix::Leaf);
36    hash.update(data);
37
38    hash.finalize().into()
39}