From f1bada5c03f475728b58e4dbd1dbdd39cbf2b00a Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 14 Aug 2024 20:00:26 +0200 Subject: [PATCH 1/2] mpc: add MerkleConcealed type --- commit_verify/src/bin/commit-stl.rs | 5 ++++ commit_verify/src/mpc/block.rs | 39 ++++++++++++++++++++++++-- commit_verify/src/mpc/mod.rs | 4 ++- commit_verify/src/mpc/tree.rs | 6 ++-- commit_verify/src/stl.rs | 3 +- stl/CommitVerify@0.1.0.sta | 41 +++++++++++++++------------- stl/CommitVerify@0.1.0.stl | Bin 1405 -> 1550 bytes stl/CommitVerify@0.1.0.sty | 7 ++++- stl/Merkle.vesper | 18 ++++++++++-- 9 files changed, 95 insertions(+), 28 deletions(-) diff --git a/commit_verify/src/bin/commit-stl.rs b/commit_verify/src/bin/commit-stl.rs index a13f01a5..22e9737d 100644 --- a/commit_verify/src/bin/commit-stl.rs +++ b/commit_verify/src/bin/commit-stl.rs @@ -79,6 +79,11 @@ Merklization vesper lexicon=types+commitments let tt = sys.type_tree("CommitVerify.Leaf").unwrap(); writeln!(file, "{tt}").unwrap(); + let layout = mpc::MerkleConcealed::commitment_layout(); + writeln!(file, "{layout}").unwrap(); + let tt = sys.type_tree("CommitVerify.MerkleConcealed").unwrap(); + writeln!(file, "{tt}").unwrap(); + let layout = mpc::MerkleBlock::commitment_layout(); writeln!(file, "{layout}").unwrap(); let tt = sys.type_tree("CommitVerify.MerkleBlock").unwrap(); diff --git a/commit_verify/src/mpc/block.rs b/commit_verify/src/mpc/block.rs index d5afa7fa..e1888985 100644 --- a/commit_verify/src/mpc/block.rs +++ b/commit_verify/src/mpc/block.rs @@ -129,6 +129,30 @@ impl TreeNode { } } +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] +#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] +#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)] +#[derive(CommitEncode)] +#[commit_encode(crate = crate, strategy = strict, id = Commitment)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] +pub struct MerkleConcealed { + /// Tree depth (up to 16). + depth: u5, + + /// Cofactor is used as an additive to the modulo divisor to improve packing + /// of protocols inside a tree of a given depth. + cofactor: u16, + + /// The root of the Merkle Tree + merkle_root: MerkleHash, +} + +impl Conceal for MerkleConcealed { + type Concealed = Self; + + fn conceal(&self) -> Self::Concealed { *self } +} + /// Partially-concealed merkle tree data. #[derive(Getters, Clone, PartialEq, Eq, Hash, Debug)] #[derive(StrictType, StrictEncode, StrictDecode)] @@ -588,7 +612,7 @@ Changed commitment id: {}", } impl Conceal for MerkleBlock { - type Concealed = Self; + type Concealed = MerkleConcealed; /// Reduces merkle tree into merkle tree root. fn conceal(&self) -> Self::Concealed { @@ -597,7 +621,18 @@ impl Conceal for MerkleBlock { .conceal_except([]) .expect("broken internal MerkleBlock structure"); debug_assert_eq!(concealed.cross_section.len(), 1); - concealed + let Some(TreeNode::ConcealedNode { + depth: u5::ZERO, + hash, + }) = concealed.cross_section.first() + else { + panic!("broken MerkleBlock conceal procedure") + }; + MerkleConcealed { + depth: self.depth, + cofactor: self.cofactor, + merkle_root: *hash, + } } } diff --git a/commit_verify/src/mpc/mod.rs b/commit_verify/src/mpc/mod.rs index f903f318..c733263f 100644 --- a/commit_verify/src/mpc/mod.rs +++ b/commit_verify/src/mpc/mod.rs @@ -30,7 +30,9 @@ mod block; pub use atoms::{ Commitment, Leaf, Message, MessageMap, MultiSource, ProtocolId, MPC_MINIMAL_DEPTH, }; -pub use block::{InvalidProof, LeafNotKnown, MergeError, MerkleBlock, MerkleProof}; +pub use block::{ + InvalidProof, LeafNotKnown, MergeError, MerkleBlock, MerkleConcealed, MerkleProof, +}; pub use tree::{Error, MerkleTree}; /// Marker trait for variates of LNPBP-4 commitment proofs, which differ by the diff --git a/commit_verify/src/mpc/tree.rs b/commit_verify/src/mpc/tree.rs index 51cb7d2a..b559f02d 100644 --- a/commit_verify/src/mpc/tree.rs +++ b/commit_verify/src/mpc/tree.rs @@ -26,7 +26,9 @@ use amplify::Wrapper; pub use self::commit::Error; use crate::merkle::MerkleHash; use crate::mpc::atoms::Leaf; -use crate::mpc::{Commitment, MerkleBlock, Message, MessageMap, Proof, ProtocolId}; +use crate::mpc::{ + Commitment, MerkleBlock, MerkleConcealed, Message, MessageMap, Proof, ProtocolId, +}; use crate::{CommitId, Conceal, LIB_NAME_COMMIT_VERIFY}; /// Number of cofactor variants tried before moving to the next tree depth. @@ -77,7 +79,7 @@ impl MerkleTree { } impl Conceal for MerkleTree { - type Concealed = MerkleBlock; + type Concealed = MerkleConcealed; fn conceal(&self) -> Self::Concealed { MerkleBlock::from(self.clone()).conceal() } } diff --git a/commit_verify/src/stl.rs b/commit_verify/src/stl.rs index 8ede6e42..4fcd398e 100644 --- a/commit_verify/src/stl.rs +++ b/commit_verify/src/stl.rs @@ -24,7 +24,7 @@ use strict_types::{CompileError, LibBuilder, TypeLib}; use crate::{mpc, MerkleHash, MerkleNode, ReservedBytes, StrictHash, LIB_NAME_COMMIT_VERIFY}; pub const LIB_ID_COMMIT_VERIFY: &str = - "stl:4dHdHDpB-17hoScv-Z8f00we-UaQ4D!G-tTV1vSN-JseSamU#tennis-peace-olympic"; + "stl:egMd32l9-y$Kod0o-$FRcNAV-Q4U$O5h-pskW9YM-$irF0yY#miller-pancake-elastic"; fn _commit_verify_stl() -> Result { LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! { @@ -33,6 +33,7 @@ fn _commit_verify_stl() -> Result { .transpile::>() .transpile::>() .transpile::>() + .transpile::() .transpile::() .transpile::() .transpile::() diff --git a/stl/CommitVerify@0.1.0.sta b/stl/CommitVerify@0.1.0.sta index 2cfbe4d6..5af897f2 100644 --- a/stl/CommitVerify@0.1.0.sta +++ b/stl/CommitVerify@0.1.0.sta @@ -1,31 +1,34 @@ -----BEGIN STRICT TYPE LIB----- -Id: stl:4dHdHDpB-17hoScv-Z8f00we-UaQ4D!G-tTV1vSN-JseSamU#tennis-peace-olympic +Id: stl:egMd32l9-y$Kod0o-$FRcNAV-Q4U$O5h-pskW9YM-$irF0yY#miller-pancake-elastic Name: CommitVerify Dependencies: Std#ralph-blue-lucky -Check-SHA256: 65c610ae37864b07fc960c412ea98cee2c9c528b0b0ef02d2c0b3ce4ba59290a +Check-SHA256: ce5ec3f773efffb6535247e84c9da1bfc1656c1a35c616832abf5eecaa5feb44 3`1{iZE18?WpZg|c>&5S9&dx0-7pM3Z=O*v*GCA9fL-<|HrZsA`NnJlR3`&dbYuYoQ*>kj0A^Tl*p6J$ -36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV4*&{7Z*6U9bZupBbOiwb2LJ#-AOHkRWnpFn0uTvlZfId* +36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV5C958Z*6U9bZupBbOiwb2LJ#-AOHkRWnpFn0uTvlZfId* X>?^|00sgGaB^>SZ)0z40Wg(*<{e=)S-S-YV@1=_p5>Oab-~jCR3C`SFec^=zG+gvC|O;Wo~qGZ*X}41_B3VZgg^QaCra#2m^3$a{vGY3r%HmYiwmg Y;R+01_T9UWpH$80?I5NZ-bfLFbqC#o>4E?M+l67UG^w8*<_XZ#%uyqCuUf1*p6J$36SYb7g#;qpQBTp wL(~+!(f@;t~vt?k_cmOW?^G=Z*l+t0t{nvZ*y}~Wn*+{Z*Bkx0ob*-9G+(AKhLw+s!eDmlO2> -&}_PPHjCBJR{;P3000000RR90{{R3000(7mbaHQSc>n|g00eGtZe;)f009JZZ*64&1pxp62ntPQa%*g5 -NMUnm1pxpD002NB018cIa%*g5PH$vo1_cRXa$#;`XlZU|0lQ3Y3mf3jZajR1t6%zV^Qs{9ch%g}Wy;2! -c7I|^!UbeyaCB$@009MeX=HS0001BbZf|5|F#$N9Lxv|61vo|sZfv!yd427@;7veO2zMB=|GYd^+a%*g5P;zf?W(ETTaBp(}00anQZ)Ra* -bZ>G100IPXVRUE!2mv^rLxv|61vo|y0X3z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-~j +CR3C`SFec^=zG+gvC{ -----END STRICT TYPE LIB----- diff --git a/stl/CommitVerify@0.1.0.stl b/stl/CommitVerify@0.1.0.stl index e191d7c7f72df8291c8f3ecd460292bf0a5ecf14..6f83b3629b8871b2098e8ede1d13f5e8a235c548 100644 GIT binary patch delta 60 zcmey%)yFd-hEZT+Yz(6izi(<$c225ueqM5FVoqub8}sDTOzx9?8F{$5a=}tT`S~T2 NWtkK=?_@l}3; Date: Thu, 15 Aug 2024 12:46:56 +0200 Subject: [PATCH 2/2] mpc: document commitment workflow --- commit_verify/doc/MPC.drawio | 433 +++++++++++++++++++++++++++++++++ commit_verify/doc/MPC.svg | 4 + commit_verify/src/mpc/block.rs | 6 +- 3 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 commit_verify/doc/MPC.drawio create mode 100644 commit_verify/doc/MPC.svg diff --git a/commit_verify/doc/MPC.drawio b/commit_verify/doc/MPC.drawio new file mode 100644 index 00000000..6bf8eb11 --- /dev/null +++ b/commit_verify/doc/MPC.drawio @@ -0,0 +1,433 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/commit_verify/doc/MPC.svg b/commit_verify/doc/MPC.svg new file mode 100644 index 00000000..79d705d9 --- /dev/null +++ b/commit_verify/doc/MPC.svg @@ -0,0 +1,4 @@ + + + +
0x10
protocol_id
message
1
32
32
Leaf::Inhabited
SHA256t(urn:ubideco:merkle:node#2024-01-31)
0x11
Leaf::Entropy
entropy
pos
8
4
1
MerkleHash
hash
32
branching
depth
32
1
width
node1
node2
1
32
32
MerkleNode
NodeBranching
u8
MerkleNode
u256
MerkleNode
98 bytes:
13 bytes:
65 bytes:
ProtocolId
Message
u64
u32
tag
tag
VIRTUAL_LEAF = u256::MAX
32
NodeBranching
0x00: empty (two virtual leafs)
0x01: single (one virtual leaf)
0x02: branch
tag
Commitment
hash
32
depth
cofactor
merkle_root
1
2
32
MerkleConcealed
65 bytes:
u16
MerkleHash
u5
SHA256t(urn:ubideco:mpc:commitment#2024-01-31)
Leaf::Inhabited
MerkleNode
NodeBranching::Single
Leaf::entropy
MerkleNode
NodeBranching::Empty
Leaf::Inhabited
MerkleNode
NodeBranching::Branch
Leaf::entropy
Leaf::entropy
cofactor=3
width=8
(2^depth)
depth=3
\ No newline at end of file diff --git a/commit_verify/src/mpc/block.rs b/commit_verify/src/mpc/block.rs index e1888985..6a7cdfae 100644 --- a/commit_verify/src/mpc/block.rs +++ b/commit_verify/src/mpc/block.rs @@ -134,7 +134,11 @@ impl TreeNode { #[strict_type(lib = LIB_NAME_COMMIT_VERIFY)] #[derive(CommitEncode)] #[commit_encode(crate = crate, strategy = strict, id = Commitment)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", rename_all = "camelCase") +)] pub struct MerkleConcealed { /// Tree depth (up to 16). depth: u5,