5个版本

0.0.5 2024年8月6日
0.0.4 2024年7月23日
0.0.3 2024年7月22日
0.0.2 2024年7月22日
0.0.1 2024年7月19日

#365密码学

Download history 97/week @ 2024-07-15 349/week @ 2024-07-22 8/week @ 2024-07-29 126/week @ 2024-08-05

580次每月下载

MIT许可证

14KB
271

zk-kit-imt

Rust中增量Merkle树的实现。

License Version Downloads

在此实现中,树以预定的深度构建,使用一个零列表(每个级别一个)来哈希缺少完全定义子节点的节点。树的分支因子,即每个节点的子节点数量,可以通过arity参数自定义。有关实现细节的详细信息,请参阅技术文档


🛠 安装

使用zk-kit-imtcrate和cargo安装

cargo add zk-kit-imt

📜 使用方法

use zk_kit_imt::imt::IMT;

fn hash_function(nodes: Vec<String>) -> String {
    nodes.join("-")
}

fn main() {
    const ZERO: &str = "zero";
    const DEPTH: usize = 3;
    const ARITY: usize = 2;

    /*
     *  To create an instance of an IMT, you need to provide a hash function,
     *  the depth of the tree, the zero value, the arity of the tree and an initial list of leaves.
     */
    let mut tree = IMT::new(hash_function, DEPTH, ZERO.to_string(), ARITY, vec![]).unwrap();

    // Insert (incrementally) a leaf with value "some-leaf"
    tree.insert("some-leaf".to_string()).unwrap();
    // Insert (incrementally) a leaf with value "another_leaf"
    tree.insert("another_leaf".to_string()).unwrap();

    let root = tree.root().unwrap();
    println!("imt tree root: {root}");
    assert!(root == "some-leaf-another_leaf-zero-zero-zero-zero-zero-zero");

    let depth = tree.depth();
    println!("imt tree depth: {depth}");
    assert!(depth == 3);

    let arity = tree.arity();
    println!("imt tree arity: {arity}");
    assert!(arity == 2);

    let leaves = tree.leaves();
    println!("imt tree leaves: {:?}", leaves);
    assert!(leaves == vec!["some-leaf", "another_leaf"]);

    // Delete the leaf at index 0
    assert!(tree.delete(0).is_ok());
    let root = tree.root().unwrap();
    println!("imt tree root: {root}");
    assert!(root == "zero-another_leaf-zero-zero-zero-zero-zero-zero");

    // Create a proof for the leaf at index 1
    let proof = tree.create_proof(1);
    assert!(proof.is_ok());
    let proof = proof.unwrap();
    assert!(tree.verify_proof(&proof));
}

依赖项

~76KB