Skip to content

Commit

Permalink
Merge pull request #122 from LNP-BP/stl
Browse files Browse the repository at this point in the history
Strict type library updates
  • Loading branch information
dr-orlovsky authored May 15, 2023
2 parents 46a79d2 + d31990c commit 2cfa633
Show file tree
Hide file tree
Showing 24 changed files with 171 additions and 148 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

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

7 changes: 4 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.1"
version = "0.10.2"
description = "Client-side validation foundation library"
keywords = ["lnp-bp", "smart-contracts", "blockchain"]
categories = ["cryptography"]
Expand All @@ -40,13 +40,14 @@ name = "client_side_validation"
path = "src/lib.rs"

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

[features]
default = ["derive"]
all = ["serde", "rand", "async"]
all = ["serde", "rand", "async", "stl"]
stl = ["commit_verify/stl"]
async = ["single_use_seals/async"]
rand = ["commit_verify/rand"]
derive = ["commit_verify/derive"]
Expand Down
10 changes: 5 additions & 5 deletions 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.1"
version = "0.10.2"
description = "Commit-verify API for client-side validation"
keywords = ["lnp-bp", "smart-contracts", "blockchain", "commitments"]
categories = ["cryptography"]
Expand All @@ -22,10 +22,10 @@ name = "commit-stl"
required-features = ["stl"]

[dependencies]
amplify = { version = "4.0.0-beta.20", features = ["hex", "apfloat"] }
commit_encoding_derive = { version = "0.10.0-beta.1", path = "derive" }
strict_encoding = "2.0.0"
strict_types = { version = "1.0.0-rc.1", optional = true }
amplify = { version = "4.0.0", features = ["hex", "apfloat"] }
commit_encoding_derive = { version = "0.10.0", path = "derive" }
strict_encoding = "2.2.0"
strict_types = { version = "1.2.0", optional = true }
rand = { version = "0.8.5", optional = true }
serde_crate = { version = "1.0", package = "serde", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion commit_verify/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "commit_encoding_derive"
version = "0.10.0-beta.1"
version = "0.10.0"
description = "Commitment encoding derivation macros"
keywords = ["commitments", "proc-macro"]
categories = ["development-tools", "encoding"]
Expand Down
20 changes: 10 additions & 10 deletions commit_verify/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
// Coding conventions
#![recursion_limit = "256"]

//! Derivation macros for strict encoding. To learn more about the strict
//! encoding please check `strict_encoding` crate.
//! Derivation macros for commit encoding. To learn more about the strict
//! commit please check `commit_verify` crate.
//!
//! # Derivation macros
//!
//! Library exports derivation macros `#[derive(`[`StrictEncode`]`)]`,
//! `#[derive(`[`StrictDecode`]`)]`, which can be added on top of any structure
//! you'd like to support string encoding (see Example section below).
//! Library exports derivation macros `#[derive(`[`CommitEncode`]`)]`,
//! which can be added on top of any structure you'd like to support commitment
//! encoding.
//!
//! Encoding/decoding implemented by both of these macros may be configured at
//! type and individual field level using `#[strict_type(...)]` attributes.
//! type and individual field level using `#[commit_encode(...)]` attributes.
//!
//! # Attribute
//!
//! [`StrictEncode`] and [`StrictDecode`] behavior can be customized with
//! `#[strict_encoding(...)]` attribute, which accepts different arguments
//! depending to which part of the data type it is applied.
//! [`CommitEncode`] behavior can be customized with `#[commit_encoding(...)]`
//! attribute, which accepts different arguments depending to which part of the
//! data type it is applied.
//!
//! ## Attribute arguments at type declaration level
//!
//! Derivation macros accept `#[strict_encoding()]` attribute with the following
//! Derivation macros accept `#[commit_encoding()]` attribute with the following
//! arguments:
#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/derive/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl TryFrom<&Path> for StrategyAttr {
}

impl StrategyAttr {
pub fn to_ident(&self) -> Ident {
pub fn to_ident(self) -> Ident {
match self {
StrategyAttr::CommitEncoding => {
panic!("StrategyAttr::CommitEncoding must be derived manually")
Expand Down
2 changes: 2 additions & 0 deletions commit_verify/derive/tests/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn strategy_transparent() -> common::Result {
struct ShortLen(u16);

verify_commit(ShortLen(0), [0, 0]);
#[allow(clippy::mixed_case_hex_literals)]
verify_commit(ShortLen(0xFFde), [0xde, 0xFF]);

Ok(())
Expand All @@ -69,6 +70,7 @@ fn strategy_into_u8() -> common::Result {
B,
C,
}
#[allow(clippy::from_over_into)]
impl Into<u8> for Prim {
fn into(self) -> u8 { self as u8 }
}
Expand Down
76 changes: 16 additions & 60 deletions commit_verify/src/bin/commit-stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,23 @@
// You should have received a copy of the Apache 2.0 License along with this
// software. If not, see <https://opensource.org/licenses/Apache-2.0>.

#[macro_use]
extern crate amplify;
#[macro_use]
extern crate strict_types;
use commit_verify::stl;
use strict_types::typelib::parse_args;

use std::io::stdout;
use std::{env, fs, io};

use amplify::num::u24;
use commit_verify::{mpc, LIB_NAME_COMMIT_VERIFY};
use strict_encoding::{StrictEncode, StrictWriter};
use strict_types::typelib::LibBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();

let lib = LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY))
.process::<mpc::MerkleTree>()?
.process::<mpc::MerkleBlock>()?
.process::<mpc::MerkleProof>()?
.compile(none!())?;
let id = lib.id();

let ext = match args.get(1).map(String::as_str) {
Some("--stl") => "stl",
Some("--asc") => "asc.stl",
Some("--sty") => "sty",
_ => "sty",
};
let filename = args
.get(2)
.cloned()
.unwrap_or_else(|| format!("stl/CommitVerify.{ext}"));
let mut file = match args.len() {
1 => Box::new(stdout()) as Box<dyn io::Write>,
2 | 3 => Box::new(fs::File::create(filename)?) as Box<dyn io::Write>,
_ => panic!("invalid argument count"),
};
match ext {
"stl" => {
lib.strict_encode(StrictWriter::with(u24::MAX.into_usize(), file))?;
}
"asc.stl" => {
writeln!(file, "{lib:X}")?;
}
_ => {
writeln!(
file,
"{{-
Id: {id:+}
Name: CommitVerify
Description: Types for client-side-validation commits and verification
fn main() {
let lib = stl::commit_verify_stl();
let (format, dir) = parse_args();
lib.serialize(
format,
dir,
"0.1.0",
Some(
"
Description: Client-side-validation deterministic commitments
Author: Dr Maxim Orlovsky <[email protected]>
Copyright (C) 2023 LNP/BP Standards Association. All rights reserved.
License: Apache-2.0
-}}
"
)?;
writeln!(file, "{lib}")?;
}
}

Ok(())
License: Apache-2.0",
),
)
.expect("unable to write to the file");
}
2 changes: 1 addition & 1 deletion commit_verify/src/convolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
where
Self: VerifyEq,
{
let original = self.restore_original(&commitment);
let original = self.restore_original(commitment);
let suppl = self.extract_supplement();
let (commitment_prime, proof) = original.convolve_commit(suppl, msg)?;
Ok(commitment.verify_eq(&commitment_prime) && self.verify_eq(&proof))
Expand Down
4 changes: 2 additions & 2 deletions commit_verify/src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
{
let mut container_prime = proof.restore_original_container(self)?;
let proof_prime = container_prime.embed_commit(msg)?;
Ok(proof_prime.verify_eq(&proof) && self.verify_eq(&container_prime))
Ok(proof_prime.verify_eq(proof) && self.verify_eq(&container_prime))
}

/// Phantom method used to add `Protocol` generic parameter to the trait.
Expand Down Expand Up @@ -264,7 +264,7 @@ pub(crate) mod test_helpers {
acc.iter().for_each(|commitment| {
// Testing that verification against other commitments
// returns `false`
assert!(!SUPPLEMENT.verify(msg, &commitment).unwrap());
assert!(!SUPPLEMENT.verify(msg, commitment).unwrap());
});

// Detecting collision: each message should produce a unique
Expand Down
4 changes: 2 additions & 2 deletions commit_verify/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub mod strategies {
pub enum Strict {}

/// Commits to the value data by first converting them into confidential
/// version (*concealing*) by running [`CommitConceal::commit_conceal`]
/// version (*concealing*) by running [`Conceal::conceal`]
/// first and returning its result serialized with strict encoding
/// rules.
///
Expand All @@ -155,7 +155,7 @@ pub mod strategies {
/// Commits to the value via commitment id of the type, which is serialized
/// into the hasher.
///
/// Can apply only to types implementing [`CommitId`] trait.
/// Can apply only to types implementing [`CommitmentId`] trait.
pub enum Id {}

/// Commits to the value by merklizing data provided by [`MerkleLeaves`]
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{CommitEncode, Sha256};

/// High-level API used in client-side validation for producing a single
/// commitment to the data, which includes running all necessary procedures like
/// concealment with [`CommitConceal`], merklization, strict encoding,
/// concealment with [`crate::Conceal`], merklization, strict encoding,
/// wrapped into [`CommitEncode`], followed by the actual commitment to its
/// output.
pub trait CommitmentId: CommitEncode {
Expand Down
2 changes: 2 additions & 0 deletions commit_verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ mod convolve;
pub(self) mod embed;
mod encode;
mod id;
#[cfg(feature = "stl")]
pub mod stl;

pub mod merkle;
pub mod mpc;
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl From<&MerkleTree> for MerkleBlock {
fn from(tree: &MerkleTree) -> Self {
let map = &tree.map;

let iter = (0..tree.width()).into_iter().map(|pos| {
let iter = (0..tree.width()).map(|pos| {
map.get(&pos)
.map(|(protocol_id, message)| TreeNode::CommitmentLeaf {
protocol_id: *protocol_id,
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl CommitmentId for MerkleTree {

impl MerkleTree {
pub fn root(&self) -> MerkleNode {
let iter = (0..self.width()).into_iter().map(|pos| {
let iter = (0..self.width()).map(|pos| {
self.map
.get(&pos)
.map(|(protocol, msg)| Leaf::inhabited(*protocol, *msg))
Expand Down
Loading

0 comments on commit 2cfa633

Please sign in to comment.