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/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(()) } 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); } 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(), } } } 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; 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"] 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(()) } }