Skip to content

Commit 1d475e4

Browse files
authored
feat: nodes accessor for lean-imt (#77)
## Description This PR adds a `nodes` method to the `LeanIMT` struct, exposing the internal nodes structure as a slice (`&[Vec<[u8; N]>]`). It also fixes minor clippy warnings by allowing `manual_div_ceil` and reformat the example in `lib.rs`. - **What kind of change?** Feature addition - **Current behavior**: No method exists to access the internal nodes structure of `LeanIMT`. - **New behavior**: The `nodes` method returns a reference to the internal nodes, allowing read-only access. - **Breaking change?** No. ## Related Issue(s) Resolves #76 ## Other information The change is minimal and does not affect existing functionality. No new tests were added as the method is a simple accessor, but existing tests pass. ## Checklist - [x] I have read and understand the [contributor guidelines](https://github.com/privacy-scaling-explorations/zk-kit.rust/blob/main/CONTRIBUTING.md) and [code of conduct](https://github.com/privacy-scaling-explorations/zk-kit.rust/blob/main/CODE_OF_CONDUCT.md). - [x] I have performed a self-review of my code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] My changes generate no new warnings. - [x] I have run `cargo fmt` without getting any errors. - [ ] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes.
1 parent 1014994 commit 1d475e4

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

crates/lean-imt/src/lean_imt.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! Lean Incremental Merkle Tree implementation.
44
//!
55
//! Specifications can be found here:
6-
//! - https://github.com/privacy-scaling-explorations/zk-kit/blob/main/papers/leanimt/paper/leanimt-paper.pdf
6+
//! - <https://github.com/privacy-scaling-explorations/zk-kit/blob/main/papers/leanimt/paper/leanimt-paper.pdf>
7+
8+
#![allow(clippy::manual_div_ceil)]
79

810
use thiserror::Error;
911

@@ -268,6 +270,11 @@ impl<const N: usize> LeanIMT<N> {
268270
.ok_or(LeanIMTError::IndexOutOfBounds)
269271
}
270272

273+
/// Returns the internal nodes structure.
274+
pub fn nodes(&self) -> &[Vec<[u8; N]>] {
275+
&self.nodes
276+
}
277+
271278
/// Retrieves the node at a specified level and index.
272279
pub fn get_node(&self, level: usize, index: usize) -> Result<[u8; N], LeanIMTError> {
273280
let level_vec = self

crates/lean-imt/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030
//! }
3131
//! }
3232
//!
33-
//! fn main() {
34-
//! let mut tree = HashedLeanIMT::<32, SampleHasher>::new(&[], SampleHasher).unwrap();
33+
//! let mut tree = HashedLeanIMT::<32, SampleHasher>::new(&[], SampleHasher).unwrap();
3534
//!
36-
//! tree.insert(&[1; 32]);
37-
//! tree.insert(&[2; 32]);
38-
//! tree.insert_many(&[[3; 32], [4; 32], [5; 32]]);
35+
//! tree.insert(&[1; 32]);
36+
//! tree.insert(&[2; 32]);
37+
//! tree.insert_many(&[[3; 32], [4; 32], [5; 32]]);
3938
//!
40-
//! println!("Tree root: {:?}", tree.root().unwrap());
41-
//! println!("Tree depth: {}", tree.depth());
39+
//! println!("Tree root: {:?}", tree.root().unwrap());
40+
//! println!("Tree depth: {}", tree.depth());
4241
//!
43-
//! let proof = tree.generate_proof(3).unwrap();
44-
//! assert!(HashedLeanIMT::<32, SampleHasher>::verify_proof(&proof));
45-
//! }
42+
//! let proof = tree.generate_proof(3).unwrap();
43+
//! assert!(HashedLeanIMT::<32, SampleHasher>::verify_proof(&proof));
4644
//! ```
4745
4846
pub mod hashed_tree;

0 commit comments

Comments
 (0)