Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : Add .leaves() to MerkleTree #155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crypto-primitives/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,12 @@ impl<P: Config> MerkleTree<P> {
}
Ok(true)
}

/// Returns a reference to the leaf nodes of the Merkle tree.
/// The leaves are returned in their original order from left to right.
pub fn leaves(&self) -> &[P::LeafDigest] {
&self.leaf_nodes
}
}

/// Returns the height of the tree, given the number of leaves.
Expand Down
17 changes: 17 additions & 0 deletions crypto-primitives/src/merkle_tree/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,21 @@ mod field_mt_tests {
],
)
}

#[test]
fn test_leaves_access() {
let tree = MerkleTree::blank().unwrap();

// Get leaf digests
let leaf_digests = tree.leaves();

// Verify the number of leaves matches
assert_eq!(leaf_digests.len(), 0);

// Verify each leaf digest matches what we expect
for (i, leaf) in leaves.iter().enumerate() {
let expected_digest = P::LeafHash::evaluate(&leaf_hash_param, leaf).unwrap();
assert_eq!(leaf_digests[i], expected_digest);
}
}
}