Skip to content

Commit

Permalink
merkle: fixbug with invalid order of args in recursive call
Browse files Browse the repository at this point in the history
Closes #159
  • Loading branch information
dr-orlovsky committed Mar 15, 2024
1 parent e963d5d commit 6f96eb8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions commit_verify/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl MerkleHash {
pub fn merklize(leaves: &impl MerkleLeaves) -> Self {
let mut nodes = leaves.merkle_leaves().map(|leaf| leaf.commit_id());
let base_width =
u32::try_from(nodes.len()).expect("too many merkle leaves (more than 2^32)");
u32::try_from(nodes.len()).expect("too many merkle leaves (more than 2^31)");
if base_width == 1 {
// If we have just one leaf, it's MerkleNode value is the root
nodes.next().expect("length is 1")
Expand Down Expand Up @@ -192,8 +192,8 @@ impl MerkleHash {
// TODO: Do this without allocation
.collect::<Vec<_>>()
.into_iter();
let branch1 = Self::_merklize(slice, depth + 1, base_width, div);
let branch2 = Self::_merklize(iter, depth + 1, base_width, branch_width - div);
let branch1 = Self::_merklize(slice, depth + 1, div, base_width);
let branch2 = Self::_merklize(iter, depth + 1, branch_width - div, base_width);

MerkleHash::branches(depth, base_width, branch1, branch2)
}
Expand Down
7 changes: 5 additions & 2 deletions commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl MerkleTree {
.unwrap_or_else(|| Leaf::entropy(self.entropy, pos))
});
let leaves = LargeVec::try_from_iter(iter).expect("tree width has u32-bound size");
debug_assert_eq!(leaves.len_u32(), self.width());
MerkleHash::merklize(&leaves)
}
}
Expand Down Expand Up @@ -298,10 +299,12 @@ mod test {
let mut counter = StreamWriter::counter::<{ usize::MAX }>();
tree.strict_write(&mut counter).unwrap();
eprintln!(
"Tree with {count} protocol-messages: depth {}, cofactor {}. Serialized length {} \
bytes. Takes {} msecs to generate",
"Tree with {count} protocol-messages: depth {}, cofactor {}, width {}.\n\
Serialized length {} bytes.\n\
Takes {} msecs to generate",
tree.depth,
tree.cofactor,
tree.width(),
counter.unconfine().count,
elapsed_gen.as_millis(),
);
Expand Down

0 comments on commit 6f96eb8

Please sign in to comment.