From c157f582d6e3b38c755193ccb5ce12efd9e806a1 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 28 Oct 2023 16:11:04 +0200 Subject: [PATCH 1/6] mpc: fix feature gate on tree::Error export --- commit_verify/src/mpc/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commit_verify/src/mpc/mod.rs b/commit_verify/src/mpc/mod.rs index 3ccd157b..7a0193b9 100644 --- a/commit_verify/src/mpc/mod.rs +++ b/commit_verify/src/mpc/mod.rs @@ -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; From 10bab9afdb07fa94beab1aa60e6eaf21e196fc45 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 28 Oct 2023 16:13:57 +0200 Subject: [PATCH 2/6] chore: bump version to 0.11.0 --- Cargo.lock | 6 +++--- Cargo.toml | 6 +++--- commit_verify/Cargo.toml | 2 +- single_use_seals/Cargo.toml | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 122467e1..a50434e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,7 +191,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "client_side_validation" -version = "0.10.6" +version = "0.11.0-beta.1" dependencies = [ "commit_verify", "serde", @@ -214,7 +214,7 @@ dependencies = [ [[package]] name = "commit_verify" -version = "0.10.6" +version = "0.11.0-beta.1" dependencies = [ "amplify", "commit_encoding_derive", @@ -693,7 +693,7 @@ dependencies = [ [[package]] name = "single_use_seals" -version = "0.10.1" +version = "0.11.0-beta.1" dependencies = [ "amplify_derive", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index a017d2e8..abcabfc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -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] diff --git a/commit_verify/Cargo.toml b/commit_verify/Cargo.toml index e68f99b4..2bad472e 100644 --- a/commit_verify/Cargo.toml +++ b/commit_verify/Cargo.toml @@ -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"] diff --git a/single_use_seals/Cargo.toml b/single_use_seals/Cargo.toml index 52c9b693..188035cf 100644 --- a/single_use_seals/Cargo.toml +++ b/single_use_seals/Cargo.toml @@ -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"] From bafc8bb1ddd6121766d8a64a1a84e068d36256aa Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 29 Oct 2023 12:02:25 +0100 Subject: [PATCH 3/6] merkle: improve commitment generation and API --- commit_verify/src/merkle.rs | 25 +++++++++++++++---------- commit_verify/src/mpc/atoms.rs | 4 ++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/commit_verify/src/merkle.rs b/commit_verify/src/merkle.rs index 69a3eb62..5a26b663 100644 --- a/commit_verify/src/merkle.rs +++ b/commit_verify/src/merkle.rs @@ -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; @@ -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, width: impl Into) -> 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, + width: impl Into, + 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, + width: impl Into, node1: MerkleNode, node2: MerkleNode, ) -> Self { @@ -108,16 +113,16 @@ impl MerkleNode { fn with( branching: NodeBranching, tag: [u8; 16], - depth: u5, - width: u32, + depth: impl Into, + width: impl Into, 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() diff --git a/commit_verify/src/mpc/atoms.rs b/commit_verify/src/mpc/atoms.rs index 493344c8..293979c1 100644 --- a/commit_verify/src/mpc/atoms.rs +++ b/commit_verify/src/mpc/atoms.rs @@ -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); } From 04c88651b9f0b970f708433ec9a4f2d89dd76b3d Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 29 Oct 2023 12:03:52 +0100 Subject: [PATCH 4/6] mpc: change TreeNode::to_merkle_node signature --- commit_verify/src/mpc/block.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/commit_verify/src/mpc/block.rs b/commit_verify/src/mpc/block.rs index 7cce1487..e6a044d3 100644 --- a/commit_verify/src/mpc/block.rs +++ b/commit_verify/src/mpc/block.rs @@ -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(), } } } From b853c229df404e7f15d1f2b94d8015c3aa0651ca Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 29 Oct 2023 12:34:03 +0100 Subject: [PATCH 5/6] convolve: improve error type --- commit_verify/src/convolve.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/commit_verify/src/convolve.rs b/commit_verify/src/convolve.rs index 0bd13168..3f743e07 100644 --- a/commit_verify/src/convolve.rs +++ b/commit_verify/src/convolve.rs @@ -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, @@ -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(()) } From 5d1e05803158dc6a59599dcf43657d31ff6911e8 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 29 Oct 2023 12:40:41 +0100 Subject: [PATCH 6/6] seals: fix verify API to avoid Result --- single_use_seals/src/lib.rs | 24 ++++++++---------------- src/api.rs | 4 ++-- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/single_use_seals/src/lib.rs b/single_use_seals/src/lib.rs index 3e27762e..49ef1dc0 100644 --- a/single_use_seals/src/lib.rs +++ b/single_use_seals/src/lib.rs @@ -264,7 +264,7 @@ pub trait SealWitness { /// 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; + fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error>; /// Performs batch verification of the seals. /// @@ -275,16 +275,14 @@ pub trait SealWitness { &self, seals: impl IntoIterator, msg: &Self::Message, - ) -> Result + ) -> 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(()) } } @@ -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; + async fn verify_seal_async(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error>; /// Performs batch verification of the seals. /// @@ -451,18 +445,16 @@ where Seal: Sync + Send &self, seals: I, msg: &Self::Message, - ) -> Result + ) -> Result<(), Self::Error> where I: IntoIterator + 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(()); } } diff --git a/src/api.rs b/src/api.rs index fb8db061..cbe75bdd 100644 --- a/src/api.rs +++ b/src/api.rs @@ -424,8 +424,8 @@ mod test { type Message = Vec; type Error = Issue; - fn verify_seal(&self, _seal: &Seal, _msg: &Self::Message) -> Result { - Ok(true) + fn verify_seal(&self, _seal: &Seal, _msg: &Self::Message) -> Result<(), Self::Error> { + Ok(()) } }