LeanIMT: A Lightweight Incremental Merkle Tree
A Lean implementation of an Incremental Merkle Tree (IMT). It supports insertion, deletion, and updating of leaves, all while maintaining the integrity and root hash of the tree. The tree is built using a customizable hash function.
[!WARNING] This library has not been audited, please use at your own risk.
Installation
To use LeanIMT, add the following to your Cargo.toml
:
[]
= "0.1.2"
If you are using a no_std environment, you can disable the default features:
[]
= { = "0.1.2", = false }
Usage
1. Import the necessary modules
use HashMap;
use ;
2. Define a simple hash function
3. Create a new LeanIMT instance
let hash_function: IMTHashFunction = simple_hash;
let mut imt = new;
4. Insert a single leaf
let leaf = "leaf1".to_string;
match imt.insert
5. Insert multiple leaves
let leaves = vec!;
match imt.insert_many
6. Update an existing leaf
let old_leaf = "leaf1".to_string;
let new_leaf = "leaf1_updated".to_string;
let sibling_nodes = vec!; // You would populate this based on the Merkle proof
match imt.update
7. Remove a leaf
let leaf_to_remove = "leaf1".to_string;
let sibling_nodes = vec!; // Populate this based on the Merkle proof
match imt.remove
8. Check if a leaf exists
if imt.has else
9. Get the root of the tree
match imt.root
10. Retrieve Tree Size and Depth
println!;
println!;
Example
Here's a full example using the library:
Testing
To run the test suite, use the following command:
Tests cover the basic operations of insertion, removal, and updates, as well as ensuring consistency across multiple tree operations.