Skip to content

Commit

Permalink
Merge pull request #160 from LNP-BP/fix/159
Browse files Browse the repository at this point in the history
Fix bug with invalid order of args in merklization recursion
  • Loading branch information
dr-orlovsky authored Mar 18, 2024
2 parents e963d5d + 941eb39 commit 588e1a3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
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

0 comments on commit 588e1a3

Please sign in to comment.