Skip to content

Commit

Permalink
Merge pull request #165 from LNP-BP/v0.11
Browse files Browse the repository at this point in the history
Add proof match operation
  • Loading branch information
dr-orlovsky authored Apr 19, 2024
2 parents d32e1af + 4ca5963 commit f51e1e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
15 changes: 13 additions & 2 deletions commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ impl StrictDumb for MerkleBlock {
impl StrictSerialize for MerkleBlock {}
impl StrictDeserialize for MerkleBlock {}

impl Proof for MerkleBlock {}
impl Proof for MerkleBlock {
fn matches(&self, other: &Self) -> bool { self.commit_id() == other.commit_id() }
}

impl From<&MerkleTree> for MerkleBlock {
fn from(tree: &MerkleTree) -> Self {
Expand Down Expand Up @@ -621,7 +623,11 @@ pub struct MerkleProof {
path: Confined<Vec<MerkleHash>, 0, 32>,
}

impl Proof for MerkleProof {}
impl Proof for MerkleProof {
fn matches(&self, other: &Self) -> bool {
self.cofactor == other.cofactor && self.merkle_root() == other.merkle_root()
}
}

impl MerkleProof {
/// Computes the depth of the merkle tree.
Expand All @@ -639,6 +645,11 @@ impl MerkleProof {
/// Returns inner merkle path representation
pub fn as_path(&self) -> &[MerkleHash] { &self.path }

/// Returns the root of the merkle path.
///
/// If the MPC proof contains only a single message returns None
pub fn merkle_root(&self) -> Option<MerkleHash> { self.path.get(0).copied() }

/// Convolves the proof with the `message` under the given `protocol_id`,
/// producing [`Commitment`].
pub fn convolve(
Expand Down
6 changes: 6 additions & 0 deletions commit_verify/src/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ pub use tree::{Error, MerkleTree};
pub trait Proof:
strict_encoding::StrictEncode + strict_encoding::StrictDecode + Eq + std::fmt::Debug
{
/// Verifies whether one MPC proof matches another MPC proof.
///
/// This is not the same as `Eq`, since two proofs may reveal different
/// messages, and be non-equivalent, at the same time matching each other,
/// i.e. having the same merkle root and producing the same commitments.
fn matches(&self, other: &Self) -> bool;
}
6 changes: 4 additions & 2 deletions commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use self::commit::Error;
use crate::merkle::MerkleHash;
use crate::mpc::atoms::Leaf;
use crate::mpc::{Commitment, MerkleBlock, Message, MessageMap, Proof, ProtocolId};
use crate::{Conceal, LIB_NAME_COMMIT_VERIFY};
use crate::{CommitId, Conceal, LIB_NAME_COMMIT_VERIFY};

/// Number of cofactor variants tried before moving to the next tree depth.
#[allow(dead_code)]
Expand Down Expand Up @@ -58,7 +58,9 @@ pub struct MerkleTree {
pub(super) map: OrderedMap,
}

impl Proof for MerkleTree {}
impl Proof for MerkleTree {
fn matches(&self, other: &Self) -> bool { self.commit_id() == other.commit_id() }
}

impl MerkleTree {
pub fn root(&self) -> MerkleHash {
Expand Down

0 comments on commit f51e1e0

Please sign in to comment.