Skip to content

Commit

Permalink
Merge pull request #147 from LNP-BP/v0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Oct 30, 2023
2 parents b03c200 + 5d1e058 commit 672e628
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ license = "Apache-2.0"

[package]
name = "client_side_validation"
version = "0.10.6"
version = "0.11.0-beta.1"
description = "Client-side validation foundation library"
keywords = ["lnp-bp", "smart-contracts", "blockchain"]
categories = ["cryptography"]
Expand All @@ -40,8 +40,8 @@ name = "client_side_validation"
path = "src/lib.rs"

[dependencies]
commit_verify = { version = "0.10.6", path = "./commit_verify", default-features = false }
single_use_seals = { version = "0.10.1", path = "./single_use_seals" }
commit_verify = { version = "0.11.0-beta.1", path = "./commit_verify", default-features = false }
single_use_seals = { version = "0.11.0-beta.1", path = "./single_use_seals" }
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }

[features]
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "commit_verify"
version = "0.10.6"
version = "0.11.0-beta.1"
description = "Commit-verify API for client-side validation"
keywords = ["lnp-bp", "smart-contracts", "blockchain", "commitments"]
categories = ["cryptography"]
Expand Down
19 changes: 12 additions & 7 deletions commit_verify/src/convolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ use crate::{CommitEncode, CommitmentProtocol, VerifyEq};
/// Error during commitment verification
#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display(doc_comments)]
#[allow(clippy::enum_variant_names)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum ConvolveVerifyError {
/// The verified commitment doesn't commit to the provided message.
InvalidCommitment,
/// The message is invalid since a commitment to it can't be created /
/// exist.
InvalidMessage,
CommitmentMismatch,

/// The message is invalid since a valid commitment to it can't be created.
ImpossibleMessage,

/// The proof of the commitment is invalid and the commitment can't be
/// verified.
InvalidProof,
Expand Down Expand Up @@ -80,12 +85,12 @@ where
let suppl = self.extract_supplement();
let (commitment_prime, proof) = original
.convolve_commit(suppl, msg)
.map_err(|_| ConvolveVerifyError::InvalidMessage)?;
.map_err(|_| ConvolveVerifyError::ImpossibleMessage)?;
if !self.verify_eq(&proof) {
return Err(ConvolveVerifyError::InvalidProof);
}
if !commitment.verify_eq(&commitment_prime) {
return Err(ConvolveVerifyError::InvalidCommitment);
return Err(ConvolveVerifyError::CommitmentMismatch);
}
Ok(())
}
Expand Down
25 changes: 15 additions & 10 deletions commit_verify/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::io::Write;
use std::ops::SubAssign;

use amplify::confinement::Confined;
use amplify::num::u5;
use amplify::num::{u256, u5};
use amplify::{Bytes32, Wrapper};
use sha2::Sha256;

Expand Down Expand Up @@ -85,20 +85,25 @@ impl CommitmentId for MerkleNode {
const VIRTUAL_LEAF: MerkleNode = MerkleNode(Bytes32::from_array([0xFF; 32]));

impl MerkleNode {
pub fn void(tag: [u8; 16], depth: u5, width: u32) -> Self {
pub fn void(tag: [u8; 16], depth: impl Into<u8>, width: impl Into<u256>) -> Self {
let virt = VIRTUAL_LEAF;
Self::with(NodeBranching::Void, tag, depth, width, virt, virt)
}

pub fn single(tag: [u8; 16], depth: u5, width: u32, node: MerkleNode) -> Self {
pub fn single(
tag: [u8; 16],
depth: impl Into<u8>,
width: impl Into<u256>,
node: MerkleNode,
) -> Self {
let single = NodeBranching::Single;
Self::with(single, tag, depth, width, node, VIRTUAL_LEAF)
}

pub fn branches(
tag: [u8; 16],
depth: u5,
width: u32,
depth: impl Into<u8>,
width: impl Into<u256>,
node1: MerkleNode,
node2: MerkleNode,
) -> Self {
Expand All @@ -108,16 +113,16 @@ impl MerkleNode {
fn with(
branching: NodeBranching,
tag: [u8; 16],
depth: u5,
width: u32,
depth: impl Into<u8>,
width: impl Into<u256>,
node1: MerkleNode,
node2: MerkleNode,
) -> Self {
let mut engine = Sha256::default();
branching.commit_encode(&mut engine);
engine.write_all(&tag).ok();
depth.to_u8().commit_encode(&mut engine);
width.commit_encode(&mut engine);
depth.into().commit_encode(&mut engine);
width.into().commit_encode(&mut engine);
branching.commit_encode(&mut engine);
node1.commit_encode(&mut engine);
node2.commit_encode(&mut engine);
engine.finish().into()
Expand Down
4 changes: 4 additions & 0 deletions commit_verify/src/mpc/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ impl CommitEncode for Leaf {
fn commit_encode(&self, e: &mut impl Write) {
match self {
Leaf::Inhabited { protocol, message } => {
// We use this constant since we'd like to be distinct from NodeBranching values
0x10.commit_encode(e);
protocol.commit_encode(e);
message.commit_encode(e);
}
Leaf::Entropy { entropy, pos } => {
// We use this constant since we'd like to be distinct from NodeBranching values
0x11.commit_encode(e);
entropy.commit_encode(e);
pos.commit_encode(e);
}
Expand Down
8 changes: 3 additions & 5 deletions commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ impl TreeNode {

pub fn is_leaf(&self) -> bool { matches!(self, TreeNode::CommitmentLeaf { .. }) }

// TODO: Remove in v0.11 and change the function signature
#[allow(clippy::wrong_self_convention)]
pub fn to_merkle_node(&self) -> MerkleNode {
pub fn to_merkle_node(self) -> MerkleNode {
match self {
TreeNode::ConcealedNode { hash, .. } => *hash,
TreeNode::ConcealedNode { hash, .. } => hash,
TreeNode::CommitmentLeaf {
protocol_id,
message,
} => Leaf::inhabited(*protocol_id, *message).commitment_id(),
} => Leaf::inhabited(protocol_id, message).commitment_id(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions commit_verify/src/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ mod block;

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

#[deprecated(since = "0.10.6", note = "use commit_verify::merkle::MerkleBuoy instead")]
pub use crate::merkle::MerkleBuoy;
Expand Down
2 changes: 1 addition & 1 deletion single_use_seals/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "single_use_seals"
version = "0.10.1"
version = "0.11.0-beta.1"
description = "Single-use-seals foundation API"
keywords = ["lnp-bp", "smart-contracts", "blockchain", "single-use-seals"]
categories = ["cryptography"]
Expand Down
24 changes: 8 additions & 16 deletions single_use_seals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub trait SealWitness<Seal> {

/// Verifies that the seal was indeed closed over the message with the
/// provided seal closure witness.
fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<bool, Self::Error>;
fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error>;

/// Performs batch verification of the seals.
///
Expand All @@ -275,16 +275,14 @@ pub trait SealWitness<Seal> {
&self,
seals: impl IntoIterator<Item = &'seal Seal>,
msg: &Self::Message,
) -> Result<bool, Self::Error>
) -> Result<(), Self::Error>
where
Seal: 'seal,
{
for seal in seals {
if !self.verify_seal(seal, msg)? {
return Ok(false);
}
self.verify_seal(seal, msg)?;
}
Ok(true)
Ok(())
}
}

Expand Down Expand Up @@ -436,11 +434,7 @@ where Seal: Sync + Send

/// Verifies that the seal was indeed closed over the message with the
/// provided seal closure witness.
async fn verify_seal_async(
&self,
seal: &Seal,
msg: &Self::Message,
) -> Result<bool, Self::Error>;
async fn verify_seal_async(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error>;

/// Performs batch verification of the seals.
///
Expand All @@ -451,18 +445,16 @@ where Seal: Sync + Send
&self,
seals: I,
msg: &Self::Message,
) -> Result<bool, Self::Error>
) -> Result<(), Self::Error>
where
I: IntoIterator<Item = &'seal Seal> + Send,
I::IntoIter: Send,
Seal: 'seal,
{
for seal in seals {
if !self.verify_seal_async(seal, msg).await? {
return Ok(false);
}
self.verify_seal_async(seal, msg).await?;
}
return Ok(true);
return Ok(());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ mod test {
type Message = Vec<u8>;
type Error = Issue;

fn verify_seal(&self, _seal: &Seal, _msg: &Self::Message) -> Result<bool, Self::Error> {
Ok(true)
fn verify_seal(&self, _seal: &Seal, _msg: &Self::Message) -> Result<(), Self::Error> {
Ok(())
}
}

Expand Down

0 comments on commit 672e628

Please sign in to comment.