#merkle-tree #tree #merkle

alloy-merkle-tree

Minimal Merkle Tree implementation

6 releases (breaking)

0.5.0 Feb 28, 2024
0.4.0 Feb 19, 2024
0.3.0 Feb 19, 2024
0.2.0 Feb 19, 2024
0.1.1 Feb 19, 2024

#48 in #merkle

Download history 459/week @ 2024-02-19 146/week @ 2024-02-26 33/week @ 2024-03-04 19/week @ 2024-03-11 33/week @ 2024-03-18 19/week @ 2024-03-25 22/week @ 2024-04-01 6/week @ 2024-04-08 4/week @ 2024-04-15 14/week @ 2024-04-22 30/week @ 2024-05-06 9/week @ 2024-05-13 12/week @ 2024-05-20

52 downloads per month
Used in 2 crates (via hdp-core)

Apache-2.0

26KB
519 lines

alloy-merkle-tree

Minimal Merkle Tree implementation

  • various tree implementation
    • PerfectBinaryMerkleTree
    • IncrementalMerkleTree
    • StandardBinaryTree
  • type compatible with alloy-primitives
  • keccak hash as native hash
  • support features: insert, proof, verify

Install

 cargo add alloy-merkle-tree

Support

MerkleTree

Perfect Binary Merkle Tree

let mut tree = MerkleTree::new();
// Should be 2 ^ N leaves
let num_leaves = 16;
for i in 0..num_leaves {
    tree.insert(B256::from(U256::from(i)));
}
tree.finish();

for i in 0..num_leaves {
    let proof = tree.create_proof(&B256::from(U256::from(i))).unwrap();
    assert!(MerkleTree::verify_proof(&proof));
}

IncrementalMerkleTree

used in the ETH2 Deposit Contract

 let mut tree = IncrementalMerkleTree::<8>::new();
for i in 0..1 << (8 - 1) {
    tree.append([i as u8; 32].into()).unwrap();
}
for i in 0..1 << (8 - 1) {
    let leaf = [i as u8; 32].into();
    let proof = tree.proof_at_index(i).unwrap();
    assert!(tree.verify_proof(leaf, i, &proof));
}

StandardBinaryMerkleTree

StandardMerkleTree

let num_leaves = 1000;
let mut leaves = Vec::new();
for i in 0..num_leaves {
    leaves.push(i.to_string());
}
let tree = StandardMerkleTree::of(leaves.clone());

for leaf in leaves.iter() {
    let proof = tree.get_proof(leaf);
    let bool = tree.verify_proof(leaf.to_string(), proof);
    assert!(bool);
}

reference

Dependencies

~9MB
~174K SLoC