Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commitment and API changes for v0.11 #147

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// 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),

Check warning on line 31 in commit_verify/src/convolve.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/convolve.rs#L31

Added line #L31 was not covered by tests
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 @@
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::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 @@
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 {

Check warning on line 88 in commit_verify/src/merkle.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/merkle.rs#L88

Added line #L88 was not covered by tests
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 {

Check warning on line 98 in commit_verify/src/merkle.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/merkle.rs#L93-L98

Added lines #L93 - L98 were not covered by tests
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 @@
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 @@

/// 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 @@
&self,
seals: impl IntoIterator<Item = &'seal Seal>,
msg: &Self::Message,
) -> Result<bool, Self::Error>
) -> Result<(), Self::Error>

Check warning on line 278 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L278

Added line #L278 was not covered by tests
where
Seal: 'seal,
{
for seal in seals {
if !self.verify_seal(seal, msg)? {
return Ok(false);
}
self.verify_seal(seal, msg)?;

Check warning on line 283 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L283

Added line #L283 was not covered by tests
}
Ok(true)
Ok(())

Check warning on line 285 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L285

Added line #L285 was not covered by tests
}
}

Expand Down Expand Up @@ -436,11 +434,7 @@

/// 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 @@
&self,
seals: I,
msg: &Self::Message,
) -> Result<bool, Self::Error>
) -> Result<(), Self::Error>

Check warning on line 448 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L448

Added line #L448 was not covered by tests
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?;

Check warning on line 455 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L455

Added line #L455 was not covered by tests
}
return Ok(true);
return Ok(());

Check warning on line 457 in single_use_seals/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

single_use_seals/src/lib.rs#L457

Added line #L457 was not covered by tests
}
}

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 @@
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(())

Check warning on line 428 in src/api.rs

View check run for this annotation

Codecov / codecov/patch

src/api.rs#L428

Added line #L428 was not covered by tests
}
}

Expand Down
Loading