1use crate::{MerkleHasher, balanced, mmr, sparse, unbalanced};
4use ab_blake3::{BLOCK_LEN, KEY_LEN, MAX_SIMD_DEGREE, OUT_LEN};
5use array_reshape::{Flatten, FlattenEach};
6
7pub type BalancedMerkleTree<'a, const N: usize> =
9 balanced::BalancedMerkleTree<'a, N, Blake3MerkleHasher>;
10pub type MerkleMountainRange<const MAX_N: u64> =
12 mmr::MerkleMountainRange<MAX_N, Blake3MerkleHasher>;
13pub type SparseMerkleTree<const BITS: u8> = sparse::SparseMerkleTree<BITS, Blake3MerkleHasher>;
15pub type UnbalancedMerkleTree = unbalanced::UnbalancedMerkleTree<Blake3MerkleHasher>;
17
18#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
21pub struct Blake3MerkleHasher;
22
23const impl Default for Blake3MerkleHasher {
24 #[inline(always)]
25 fn default() -> Self {
26 Self
27 }
28}
29
30impl Blake3MerkleHasher {
31 pub const KEY: [u8; KEY_LEN] = ab_blake3::const_hash(b"merkle-tree-inner-node");
33}
34
35impl MerkleHasher for Blake3MerkleHasher {
36 const OUT_LEN: usize = OUT_LEN;
37 const PARALLEL_PAIRS: usize = MAX_SIMD_DEGREE;
38
39 #[inline(always)]
40 #[cfg_attr(feature = "no-panic", no_panic::no_panic)]
41 fn hash_pair(&self, pair: &[[u8; OUT_LEN]; 2]) -> [u8; OUT_LEN] {
42 ab_blake3::single_block_keyed_hash(&Self::KEY, pair.flatten_ref::<BLOCK_LEN>())
43 .expect("Exactly one block worth of data; qed")
44 }
45
46 #[inline(always)]
47 #[cfg_attr(feature = "no-panic", no_panic::no_panic)]
48 fn hash_pairs<const NUM_PAIRS: usize>(
49 &self,
50 pairs: &[[[u8; OUT_LEN]; 2]; NUM_PAIRS],
51 ) -> [[u8; OUT_LEN]; NUM_PAIRS] {
52 let mut hashes = [[0; OUT_LEN]; NUM_PAIRS];
53 ab_blake3::single_block_keyed_hash_many_exact(
54 &Self::KEY,
55 pairs.flatten_each_ref::<BLOCK_LEN>(),
56 &mut hashes,
57 );
58 hashes
59 }
60}