#merkle-tree #tree #merkle

alloy-merkle-tree

最小默克尔树实现

7个版本 (破坏性)

0.6.0 2024年6月25日
0.5.0 2024年2月28日
0.4.0 2024年2月19日
0.3.0 2024年2月19日
0.1.1 2024年2月19日

#2323神奇豆

Download history 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 22/week @ 2024-05-27 20/week @ 2024-06-03 26/week @ 2024-06-10 22/week @ 2024-06-17 245/week @ 2024-06-24 81/week @ 2024-07-01 5/week @ 2024-07-08 25/week @ 2024-07-15 58/week @ 2024-07-22 67/week @ 2024-07-29

每月下载量 162
用于 5 个crate(3个直接使用)

自定义许可

26KB
519

alloy-merkle-tree

最小默克尔树实现

  • 各种树实现
    • 完美二叉默克尔树
    • 增量默克尔树
    • 标准二叉树
  • 与alloy-primitives类型兼容
  • keccak哈希作为本地哈希
  • 支持功能:插入、证明、验证

安装

 cargo add alloy-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));
}

增量默克尔树

用于 ETH2 存款合约

 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);
}

参考

依赖

~8.5MB
~163K SLoC