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 bug with invalid order of args in merklization recursion #160

Merged
merged 2 commits into from
Mar 18, 2024
Merged
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
4 changes: 2 additions & 2 deletions commit_verify/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ impl CommitEngine {
self.inner_commit_to::<_, 32>(&root);
}

pub fn commit_to_concealed<T: Conceal>(&mut self, value: &T)
pub fn commit_to_concealed<T>(&mut self, value: &T)
where
T: StrictType,
T: Conceal + StrictType,
T::Concealed: StrictEncode,
{
let fqn = commitment_fqn::<T>();
Expand Down
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
6 changes: 4 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,11 @@ 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 {}.\nSerialized \
length {} bytes.\nTakes {} msecs to generate",
tree.depth,
tree.cofactor,
tree.width(),
counter.unconfine().count,
elapsed_gen.as_millis(),
);
Expand Down
9 changes: 4 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,17 @@ pub trait SealResolver<Seal> {

#[cfg(test)]
mod test {
//! Tests use emulation of a simple client-side-validated state, consisting
//! of an array of data items, each of which has a name bound to a certain
//! bitcoin single-use-seal.
// Tests use emulation of a simple client-side-validated state, consisting
// of an array of data items, each of which has a name bound to a certain
// bitcoin single-use-seal.

use single_use_seals::{SealProtocol, SealStatus, SealWitness};

use super::*;

#[test]
#[allow(dead_code)]
fn test() {
#![allow(dead_code)]

#[derive(Clone, PartialEq, Eq, Hash, Debug, Default)]
#[derive(Serialize, Deserialize)]
#[serde(crate = "serde_crate")]
Expand Down
Loading