lean_imt/
lib.rs

1//! # Lean IMT
2//!
3//! LeanIMT provides an optimized incremental Merkle tree (IMT) implementation tailored for efficient, binary-based hashing without relying on zero values for incomplete nodes. It dynamically adjusts its depth based on leaf insertions, significantly reducing computational overhead.
4//!
5//! ## Quick Start
6//!
7//! Install the `zk-kit-lean-imt` crate with `cargo`:
8//!
9//! ```commandline
10//! cargo add zk-kit-lean-imt
11//! ```
12//!
13//! ## Example
14//! ```rust
15//! use lean_imt::hashed_tree::{HashedLeanIMT, LeanIMTHasher};
16//! use std::collections::hash_map::DefaultHasher;
17//! use std::hash::{Hash, Hasher};
18//!
19//! struct SampleHasher;
20//!
21//! impl LeanIMTHasher<32> for SampleHasher {
22//!     fn hash(input: &[u8]) -> [u8; 32] {
23//!         let mut hasher = DefaultHasher::new();
24//!         input.hash(&mut hasher);
25//!         let h = hasher.finish();
26//!
27//!         let mut result = [0u8; 32];
28//!         result[..8].copy_from_slice(&h.to_le_bytes());
29//!         result
30//!     }
31//! }
32//!
33//! let mut tree = HashedLeanIMT::<32, SampleHasher>::new(&[], SampleHasher).unwrap();
34//!
35//! tree.insert(&[1; 32]);
36//! tree.insert(&[2; 32]);
37//! tree.insert_many(&[[3; 32], [4; 32], [5; 32]]);
38//!
39//! println!("Tree root: {:?}", tree.root().unwrap());
40//! println!("Tree depth: {}", tree.depth());
41//!
42//! let proof = tree.generate_proof(3).unwrap();
43//! assert!(HashedLeanIMT::<32, SampleHasher>::verify_proof(&proof));
44//! ```
45
46pub mod hashed_tree;
47pub mod lean_imt;
48
49#[cfg(feature = "stateless")]
50pub mod stateless;