Skip to content

Commit

Permalink
bugfix: simple smt should use correct depth for count check
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto committed Nov 24, 2023
1 parent 7cd5b25 commit 3413d00
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/merkle/partial_mt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ impl PartialMerkleTree {
.or_insert(vec![node_index.value()]);
}

// check if the number of leaves can be accommodated by the tree's depth; we use a min
// depth of 63 because we consider passing in a vector of size 2^64 infeasible.
let max = (1_u64 << 63) as usize;
// Get maximum depth
let max_depth = *layers.keys().next_back().unwrap_or(&0);

// check if the number of leaves can be accommodated by the tree's depth
let max = (1_u64 << max_depth) as usize;
if layers.len() > max {
return Err(MerkleError::InvalidNumEntries(max, layers.len()));
}

// Get maximum depth
let max_depth = *layers.keys().next_back().unwrap_or(&0);

// fill layers without nodes with empty vector
for depth in 0..max_depth {
layers.entry(depth).or_default();
Expand Down
5 changes: 2 additions & 3 deletions src/merkle/simple_smt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ impl SimpleSmt {
// create an empty tree
let mut tree = Self::new(depth)?;

// check if the number of leaves can be accommodated by the tree's depth; we use a min
// depth of 63 because we consider passing in a vector of size 2^64 infeasible.
// check if the number of leaves can be accommodated by the tree's depth
let entries = entries.into_iter();
let max = 1 << tree.depth.min(63);
let max = 1 << tree.depth;
if entries.len() > max {
return Err(MerkleError::InvalidNumEntries(max, entries.len()));
}
Expand Down

0 comments on commit 3413d00

Please sign in to comment.