Skip to content

Commit

Permalink
mpc: add support for static entropy. Closes #137
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 8, 2023
1 parent f122107 commit 979eeca
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 @@ -152,21 +152,33 @@ impl Commitment {
pub fn from_slice(slice: &[u8]) -> Option<Self> { Bytes32::from_slice(slice).map(Self) }
}

// 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,
}
}

Check warning on line 173 in commit_verify/src/mpc/atoms.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/mpc/atoms.rs#L171-L173

Added lines #L171 - L173 were not covered by tests
}

impl MultiSource {
#[inline]
pub fn with_static_entropy(static_entropy: u64) -> Self {
MultiSource {
static_entropy: Some(static_entropy),
..default!()

Check warning on line 181 in commit_verify/src/mpc/atoms.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/mpc/atoms.rs#L178-L181

Added lines #L178 - L181 were not covered by tests
}
}
}
Expand Down
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 979eeca

Please sign in to comment.