Skip to content

Commit

Permalink
Merge branch 'master' into mpc-u32
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 12, 2023
2 parents 6a57f22 + 99ab370 commit 9902847
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
61 changes: 48 additions & 13 deletions commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,43 @@ use crate::mpc::{
};
use crate::{Conceal, LIB_NAME_COMMIT_VERIFY};

/// commitment under protocol id {_0} is absent from the known part of a given
/// commitment under protocol id {0} is absent from the known part of a given
/// LNPBP-4 Merkle block.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error)]
#[display(doc_comments)]
pub struct LeafNotKnown(ProtocolId);

/// attempt to merge unrelated LNPBP-4 proof.
/// the provided merkle proof protocol id {protocol_id} position {actual}
/// doesn't match the expected position {expected} within the tree of width
/// {width}.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error)]
#[display(doc_comments)]
pub struct UnrelatedProof;
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct InvalidProof {
protocol_id: ProtocolId,
expected: u32,
actual: u32,
width: u32,
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum MergeError {
#[from]
#[display(inner)]
InvalidProof(InvalidProof),

/// attempt to merge two unrelated LNPBP-4 blocks with different Merkle
/// roots (base {base_root}, merged-in {merged_root}).
UnrelatedBlocks {
base_root: Commitment,
merged_root: Commitment,
},
}

/// LNPBP-4 Merkle tree node.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -170,13 +197,19 @@ impl MerkleBlock {
proof: &MerkleProof,
protocol_id: ProtocolId,
message: Message,
) -> Result<Self, UnrelatedProof> {
) -> Result<Self, InvalidProof> {
let path = proof.as_path();
let mut pos = proof.pos;
let mut width = proof.width();

if protocol_id_pos(protocol_id, proof.cofactor, width) != pos {
return Err(UnrelatedProof);
let expected = protocol_id_pos(protocol_id, proof.cofactor, width);
if expected != pos {
return Err(InvalidProof {
protocol_id,
expected,
actual: pos,
width,
});
}

let mut dir = Vec::with_capacity(path.len());
Expand Down Expand Up @@ -345,20 +378,22 @@ impl MerkleBlock {
proof: &MerkleProof,
protocol_id: ProtocolId,
message: Message,
) -> Result<u16, UnrelatedProof> {
) -> Result<u16, MergeError> {
let block = MerkleBlock::with(proof, protocol_id, message)?;
self.merge_reveal(block)
}

/// Merges two merkle blocks together, joining revealed information from
/// each one of them.
pub fn merge_reveal(&mut self, other: MerkleBlock) -> Result<u16, UnrelatedProof> {
pub fn merge_reveal(&mut self, other: MerkleBlock) -> Result<u16, MergeError> {
let orig = self.clone();

let base_root = self.commitment_id();

if base_root != other.commitment_id() {
return Err(UnrelatedProof);
let merged_root = other.commitment_id();
if base_root != merged_root {
return Err(MergeError::UnrelatedBlocks {
base_root,
merged_root,
});
}

let mut cross_section =
Expand Down Expand Up @@ -592,7 +627,7 @@ impl MerkleProof {
&self,
protocol_id: ProtocolId,
message: Message,
) -> Result<Commitment, UnrelatedProof> {
) -> Result<Commitment, InvalidProof> {
let block = MerkleBlock::with(self, protocol_id, message)?;
Ok(block.commitment_id())
}
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tree;
mod block;

pub use atoms::{Commitment, Leaf, MerkleBuoy, Message, MessageMap, MultiSource, ProtocolId};
pub use block::{LeafNotKnown, MerkleBlock, MerkleProof, UnrelatedProof};
pub use block::{InvalidProof, LeafNotKnown, MergeError, MerkleBlock, MerkleProof};
#[cfg(feature = "rand")]
pub use tree::Error;
pub use tree::MerkleTree;
Expand Down

0 comments on commit 9902847

Please sign in to comment.