From 6f96eb84ca574bd66d4848ebf54e62ff4a1501a9 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Fri, 15 Mar 2024 20:16:49 +0100 Subject: [PATCH 1/2] merkle: fixbug with invalid order of args in recursive call Closes #159 --- commit_verify/src/merkle.rs | 6 +++--- commit_verify/src/mpc/tree.rs | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/commit_verify/src/merkle.rs b/commit_verify/src/merkle.rs index 90e87d54..ed8e1933 100644 --- a/commit_verify/src/merkle.rs +++ b/commit_verify/src/merkle.rs @@ -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") @@ -192,8 +192,8 @@ impl MerkleHash { // TODO: Do this without allocation .collect::>() .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) } diff --git a/commit_verify/src/mpc/tree.rs b/commit_verify/src/mpc/tree.rs index a0984c1f..9cdc95c8 100644 --- a/commit_verify/src/mpc/tree.rs +++ b/commit_verify/src/mpc/tree.rs @@ -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) } } @@ -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(), ); From 941eb39cab1505575cd7208971c8286d52e0fcec Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Fri, 15 Mar 2024 20:22:02 +0100 Subject: [PATCH 2/2] chore: fix lints --- commit_verify/src/id.rs | 4 ++-- commit_verify/src/mpc/tree.rs | 5 ++--- src/api.rs | 9 ++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/commit_verify/src/id.rs b/commit_verify/src/id.rs index dd2f3c91..d41ad062 100644 --- a/commit_verify/src/id.rs +++ b/commit_verify/src/id.rs @@ -131,9 +131,9 @@ impl CommitEngine { self.inner_commit_to::<_, 32>(&root); } - pub fn commit_to_concealed(&mut self, value: &T) + pub fn commit_to_concealed(&mut self, value: &T) where - T: StrictType, + T: Conceal + StrictType, T::Concealed: StrictEncode, { let fqn = commitment_fqn::(); diff --git a/commit_verify/src/mpc/tree.rs b/commit_verify/src/mpc/tree.rs index 9cdc95c8..611c1dd7 100644 --- a/commit_verify/src/mpc/tree.rs +++ b/commit_verify/src/mpc/tree.rs @@ -299,9 +299,8 @@ mod test { let mut counter = StreamWriter::counter::<{ usize::MAX }>(); tree.strict_write(&mut counter).unwrap(); eprintln!( - "Tree with {count} protocol-messages: depth {}, cofactor {}, width {}.\n\ - Serialized length {} bytes.\n\ - 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(), diff --git a/src/api.rs b/src/api.rs index 002dfb2f..0b7bd54a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -392,18 +392,17 @@ pub trait SealResolver { #[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")]