Crates.io | alloy-merkle-tree |
lib.rs | alloy-merkle-tree |
version | 0.7.1 |
source | src |
created_at | 2024-02-19 01:16:01.269487 |
updated_at | 2024-10-16 03:17:35.162224 |
description | Minimal Merkle Tree implementation |
homepage | |
repository | https://github.com/rkdud007/alloy-merkle-tree |
max_upload_size | |
id | 1144575 |
size | 45,976 |
Minimal Merkle Tree implementation
❯ cargo add alloy-merkle-tree
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));
}
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));
}
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);
}