Skip to content

Commit

Permalink
Merge pull request #140 from LNP-BP/static_entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Oct 10, 2023
2 parents 55aece9 + 979eeca commit d9161f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
14 changes: 13 additions & 1 deletion commit_verify/src/mpc/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,33 @@ impl Commitment {
}
}

// TODO: Either this type or [`MerkleTree`] should remain
/// Structured source multi-message data for commitment creation
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct MultiSource {
/// Minimal depth of the created LNPBP-4 commitment tree
pub min_depth: u5,
/// Map of the messages by their respective protocol ids
pub messages: MessageMap,
pub static_entropy: Option<u64>,
}

impl Default for MultiSource {
#[inline]
fn default() -> Self {
MultiSource {
min_depth: u5::with(3),
messages: Default::default(),
static_entropy: None,
}
}
}

impl MultiSource {
#[inline]
pub fn with_static_entropy(static_entropy: u64) -> Self {
MultiSource {
static_entropy: Some(static_entropy),
..default!()
}
}
}
23 changes: 18 additions & 5 deletions commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use amplify::confinement::{MediumOrdMap, SmallVec};
use amplify::num::{u256, u5};
use amplify::Wrapper;

#[cfg(feature = "rand")]
pub use self::commit::Error;
use crate::merkle::MerkleNode;
use crate::mpc::atoms::Leaf;
Expand Down Expand Up @@ -85,10 +84,10 @@ impl Conceal for MerkleTree {
fn conceal(&self) -> Self::Concealed { self.root() }
}

#[cfg(feature = "rand")]
mod commit {
use std::collections::BTreeMap;

use amplify::confinement::Confined;
use rand::{thread_rng, RngCore};

use super::*;
use crate::mpc::MultiSource;
Expand All @@ -112,11 +111,16 @@ mod commit {
CantFitInMaxSlots(usize),
}

/// # Panics
///
/// Panics if the crate is compiled without `rand` feature enabled and the
/// MultiSource doesn't contain a static entropy.
impl TryCommitVerify<MultiSource, UntaggedProtocol> for MerkleTree {
type Error = Error;

fn try_commit(source: &MultiSource) -> Result<Self, Error> {
use std::collections::BTreeMap;
#[cfg(feature = "rand")]
use rand::{thread_rng, RngCore};

let msg_count = source.messages.len();

Expand All @@ -127,7 +131,15 @@ mod commit {
return Err(Error::TooManyMessages(msg_count));
}

let entropy = thread_rng().next_u64();
#[cfg(feature = "rand")]
let entropy = source
.static_entropy
.unwrap_or_else(|| thread_rng().next_u64());
#[cfg(not(feature = "rand"))]
let entropy = source.static_entropy.expect(
"use must use `rand` feature for crate commit_verify if you do not provide with a \
static entropy information in `MultiSource`",
);

let mut map = BTreeMap::<u32, (ProtocolId, Message)>::new();

Expand Down Expand Up @@ -226,6 +238,7 @@ pub(crate) mod test_helpers {
let src = MultiSource {
min_depth: u5::ZERO,
messages: Confined::try_from_iter(msgs.iter().map(|(a, b)| (*a, *b))).unwrap(),
static_entropy: None,
};
MerkleTree::try_commit(&src).unwrap()
}
Expand Down

0 comments on commit d9161f0

Please sign in to comment.