Skip to content

Commit 2e941ea

Browse files
authored
Merge pull request #199 from LNP-BP/develop
Improve commitment Vesper representation
2 parents dbfcb73 + a471665 commit 2e941ea

File tree

7 files changed

+201
-113
lines changed

7 files changed

+201
-113
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ default-members = [
1313
]
1414

1515
[workspace.package]
16-
version = "0.12.0"
16+
version = "0.12.1"
1717
authors = ["Dr Maxim Orlovsky <[email protected]>"]
1818
homepage = "https://github.com/LNP-BP"
1919
repository = "https://github.com/LNP-BP/client_side_validation"
@@ -24,7 +24,7 @@ license = "Apache-2.0"
2424
[workspace.dependencies]
2525
amplify = "~4.9.0"
2626
strict_encoding = "~2.9.1"
27-
strict_types = "~2.9.0"
27+
strict_types = "~2.9.1"
2828
serde = { version = "1", features = ["derive"] }
2929

3030
[package]

commit_verify/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ required-features = ["stl", "vesper"]
2525
amplify = { workspace = true }
2626
strict_encoding = { workspace = true }
2727
strict_types = { workspace = true }
28-
vesper-lang = "0.2.1"
28+
vesper-lang = "0.2.3"
2929
commit_encoding_derive = { version = "0.12.0", path = "derive" }
3030
sha2 = "0.10.8"
3131
ripemd = "0.1.3"
@@ -40,8 +40,8 @@ default = ["derive"]
4040
all = ["rand", "derive", "stl", "vesper", "serde"]
4141
derive = []
4242
rand = ["dep:rand"]
43-
stl = ["strict_types/armor"]
44-
vesper = []
43+
stl = ["strict_types/armor", "strict_types/stl"]
44+
vesper = ["strict_types/vesper"]
4545
serde = ["dep:serde", "amplify/serde"]
4646

4747
[target.'cfg(target_arch = "wasm32")'.dependencies]

commit_verify/derive/src/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl CommitDerive {
4141
},
4242
StrategyAttr::ConcealStrict => quote! {
4343
use #trait_crate::Conceal;
44-
engine.commit_to_concealed(&self.conceal());
44+
engine.commit_to_concealed(self);
4545
},
4646
StrategyAttr::Transparent => quote! {
4747
use amplify::Wrapper;

commit_verify/src/id.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ pub enum CommitStep {
7777
Merklized(TypeFqn),
7878

7979
/// Serialization with [`CommitEngine::commit_to_concealed`].
80-
Concealed(TypeFqn),
80+
Concealed {
81+
/// The source type to which the concealment is applied.
82+
src: TypeFqn,
83+
/// The destination type of the concealment.
84+
dst: TypeFqn,
85+
},
8186
}
8287

8388
/// A helper engine used in computing commitment ids.
@@ -183,9 +188,13 @@ impl CommitEngine {
183188
T: Conceal + StrictType,
184189
T::Concealed: StrictEncode,
185190
{
186-
let fqn = commitment_fqn::<T>();
191+
let src = commitment_fqn::<T>();
192+
let dst = commitment_fqn::<T::Concealed>();
187193
self.layout
188-
.push(CommitStep::Concealed(fqn))
194+
.push(CommitStep::Concealed {
195+
src,
196+
dst: dst.clone(),
197+
})
189198
.expect("too many fields for commitment");
190199

191200
let concealed = value.conceal();
@@ -311,6 +320,7 @@ pub trait CommitEncode {
311320
#[derive(Getters, Clone, Eq, PartialEq, Hash, Debug)]
312321
pub struct CommitLayout {
313322
idty: TypeFqn,
323+
ty: TypeFqn,
314324
#[getter(as_copy)]
315325
tag: &'static str,
316326
fields: TinyVec<CommitStep>,
@@ -339,12 +349,13 @@ pub trait CommitmentLayout: CommitEncode {
339349
}
340350

341351
impl<T> CommitmentLayout for T
342-
where T: CommitEncode + StrictDumb
352+
where T: CommitEncode + StrictType + StrictDumb
343353
{
344354
fn commitment_layout() -> CommitLayout {
345355
let dumb = Self::strict_dumb();
346356
let fields = dumb.commit().into_layout();
347357
CommitLayout {
358+
ty: commitment_fqn::<T>(),
348359
idty: TypeFqn::with(
349360
libname!(Self::CommitmentId::STRICT_LIB_NAME),
350361
Self::CommitmentId::strict_name()
@@ -407,14 +418,14 @@ impl From<Sha256> for StrictHash {
407418
}
408419

409420
#[cfg(test)]
410-
mod tests {
421+
pub(crate) mod tests {
411422
#![cfg_attr(coverage_nightly, coverage(off))]
412423
use super::*;
413424

414425
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
415426
#[derive(StrictType, StrictEncode, StrictDecode)]
416427
#[strict_type(lib = "Test")]
417-
struct DumbConceal(u8);
428+
pub struct DumbConceal(u8);
418429

419430
impl Conceal for DumbConceal {
420431
type Concealed = DumbHash;
@@ -426,14 +437,14 @@ mod tests {
426437
#[strict_type(lib = "Test")]
427438
#[derive(CommitEncode)]
428439
#[commit_encode(crate = self, strategy = strict, id = StrictHash)]
429-
struct DumbHash(u8);
440+
pub struct DumbHash(u8);
430441

431442
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
432443
#[derive(StrictType, StrictEncode, StrictDecode)]
433444
#[strict_type(lib = "Test")]
434445
#[derive(CommitEncode)]
435446
#[commit_encode(crate = self, strategy = strict, id = MerkleHash)]
436-
struct DumbMerkle(u8);
447+
pub struct DumbMerkle(u8);
437448

438449
#[test]
439450
fn commit_engine_strict() {
@@ -474,7 +485,10 @@ mod tests {
474485
engine.commit_to_concealed(&val);
475486
engine.set_finished();
476487
let (id, layout) = engine.finish_layout();
477-
assert_eq!(layout, tiny_vec![CommitStep::Concealed(TypeFqn::from("Test.DumbConceal"))]);
488+
assert_eq!(layout, tiny_vec![CommitStep::Concealed {
489+
src: TypeFqn::from("Test.DumbConceal"),
490+
dst: TypeFqn::from("Test.DumbHash")
491+
},]);
478492
assert_eq!(
479493
id.finish(),
480494
Sha256::from_tag("test")

commit_verify/src/vesper.rs

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ pub type VesperCommit = TExpr<Pred>;
3535
#[display(lowercase)]
3636
pub enum Pred {
3737
Commitment,
38-
Serialized,
39-
Hashed,
40-
Merklized,
41-
Concealed,
38+
Serialize,
39+
Hash,
40+
Merklize,
41+
Conceal,
4242
List,
4343
Set,
4444
Element,
@@ -55,6 +55,7 @@ impl Predicate for Pred {
5555

5656
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
5757
pub enum Attr {
58+
For(TypeFqn),
5859
Tagged(&'static str),
5960
Concealed(TypeFqn),
6061
LenRange(LenRange),
@@ -74,15 +75,17 @@ impl Attribute for Attr {
7475

7576
fn name(&self) -> Option<Ident> {
7677
match self {
78+
Attr::For(_) => Some(ident!("for")),
7779
Attr::Tagged(_) => Some(ident!("tagged")),
78-
Attr::Concealed(_) => Some(ident!("concealed")),
80+
Attr::Concealed { .. } => Some(ident!("to")),
7981
Attr::LenRange(_) => Some(ident!("len")),
8082
Attr::Hasher => Some(ident!("hasher")),
8183
}
8284
}
8385

8486
fn value(&self) -> AttrVal<Self::Expression> {
8587
match self {
88+
Attr::For(fqn) => AttrVal::Ident(fqn.name.to_ident()),
8689
Attr::Tagged(tag) => AttrVal::Expr(AttrExpr::Tag(tag)),
8790
Attr::Concealed(fqn) => AttrVal::Ident(fqn.name.to_ident()),
8891
Attr::LenRange(range) => AttrVal::Expr(AttrExpr::LenRange(range.clone())),
@@ -98,28 +101,28 @@ impl CommitStep {
98101
CommitStep::Collection(_, _, fqn) => fqn,
99102
CommitStep::Hashed(fqn) => fqn,
100103
CommitStep::Merklized(fqn) => fqn,
101-
CommitStep::Concealed(fqn) => fqn,
104+
CommitStep::Concealed { src, dst: _ } => src,
102105
}
103106
.name
104107
.to_ident()
105108
}
106109

107110
fn predicate(&self) -> Pred {
108111
match self {
109-
CommitStep::Serialized(_) => Pred::Serialized,
112+
CommitStep::Serialized(_) => Pred::Serialize,
110113
CommitStep::Collection(CommitColType::List, _, _) => Pred::List,
111114
CommitStep::Collection(CommitColType::Set, _, _) => Pred::Set,
112115
CommitStep::Collection(CommitColType::Map { .. }, _, _) => Pred::Map,
113-
CommitStep::Hashed(_) => Pred::Hashed,
114-
CommitStep::Merklized(_) => Pred::Merklized,
115-
CommitStep::Concealed(_) => Pred::Concealed,
116+
CommitStep::Hashed(_) => Pred::Hash,
117+
CommitStep::Merklized(_) => Pred::Merklize,
118+
CommitStep::Concealed { .. } => Pred::Conceal,
116119
}
117120
}
118121

119122
fn attributes(&self) -> SmallVec<Attr> {
120123
match self {
121124
CommitStep::Collection(_, sizing, _) => small_vec![Attr::LenRange((*sizing).into())],
122-
CommitStep::Concealed(from) => small_vec![Attr::Concealed(from.clone())],
125+
CommitStep::Concealed { src: _, dst } => small_vec![Attr::Concealed(dst.clone())],
123126
CommitStep::Serialized(_) | CommitStep::Hashed(_) | CommitStep::Merklized(_) => none!(),
124127
}
125128
}
@@ -154,10 +157,17 @@ impl CommitStep {
154157
})
155158
]
156159
}
157-
CommitStep::Serialized(_) |
158-
CommitStep::Hashed(_) |
159-
CommitStep::Merklized(_) |
160-
CommitStep::Concealed(_) => empty!(),
160+
CommitStep::Serialized(_) => none!(),
161+
162+
CommitStep::Hashed(subj) |
163+
CommitStep::Merklized(subj) |
164+
CommitStep::Concealed { src: _, dst: subj } => tiny_vec![Box::new(VesperCommit {
165+
subject: subj.name.to_ident(),
166+
predicate: Pred::Serialize,
167+
attributes: none!(),
168+
content: none!(),
169+
comment: None,
170+
})],
161171
}
162172
}
163173
}
@@ -182,9 +192,79 @@ impl CommitLayout {
182192
VesperCommit {
183193
subject,
184194
predicate: Pred::Commitment,
185-
attributes: small_vec![Attr::Hasher, Attr::Tagged(self.tag())],
195+
attributes: small_vec![
196+
Attr::For(self.ty().clone()),
197+
Attr::Hasher,
198+
Attr::Tagged(self.tag())
199+
],
186200
content: Confined::from_iter_checked(content),
187201
comment: None,
188202
}
189203
}
190204
}
205+
206+
#[cfg(test)]
207+
mod tests {
208+
#![cfg_attr(coverage_nightly, coverage(off))]
209+
210+
use amplify::confinement::{LargeString, SmallOrdMap, SmallOrdSet};
211+
use strict_encoding::{StrictDecode, StrictEncode};
212+
213+
use super::*;
214+
use crate::id::tests::*;
215+
use crate::{CommitEncode, CommitEngine, CommitmentLayout, StrictHash};
216+
217+
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
218+
#[derive(StrictType, StrictEncode, StrictDecode)]
219+
#[strict_type(lib = "Test")]
220+
struct NamedWrapper<T: Default + StrictEncode + StrictDecode>(T);
221+
222+
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
223+
#[derive(StrictType, StrictEncode, StrictDecode)]
224+
#[strict_type(lib = "Test")]
225+
struct Test {
226+
serialized: NamedWrapper<LargeString>,
227+
concealed: DumbConceal,
228+
hash: DumbHash,
229+
merkle_list: TinyVec<DumbMerkle>,
230+
set: SmallOrdSet<DumbHash>,
231+
map: SmallOrdMap<u8, u64>,
232+
}
233+
impl CommitEncode for Test {
234+
type CommitmentId = StrictHash;
235+
fn commit_encode(&self, e: &mut CommitEngine) {
236+
e.commit_to_serialized(&self.serialized);
237+
e.commit_to_concealed(&self.concealed);
238+
e.commit_to_hash(&self.hash);
239+
e.commit_to_merkle(&self.merkle_list);
240+
e.commit_to_linear_set(&self.set);
241+
e.commit_to_linear_map(&self.map);
242+
}
243+
}
244+
245+
#[test]
246+
fn display() {
247+
let layout = Test::commitment_layout();
248+
assert_eq!(
249+
layout
250+
.to_vesper()
251+
.display()
252+
.to_string()
253+
.replace(" \n", "\n"),
254+
r#"commitment StrictHash: for Test, hasher SHA256, tagged urn:ubideco:strict-types:value-hash#2024-02-10
255+
serialize NamedWrapperConfinedString04294967295
256+
conceal DumbConceal: to DumbHash
257+
serialize DumbHash
258+
hash DumbHash
259+
serialize DumbHash
260+
merklize DumbMerkle
261+
serialize DumbMerkle
262+
set DumbHash: len 0..<2^16
263+
element DumbHash
264+
map U64: len 0..<2^16
265+
mapKey U8
266+
mapValue U64
267+
"#
268+
);
269+
}
270+
}

0 commit comments

Comments
 (0)