From 23f69e9b15e6fc6df79006d95bc3ce517778a906 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 14 Oct 2024 12:48:34 +0200 Subject: [PATCH] mpc: add merklization method information to be used in a future for more zk-friendly merkjlization hash functions --- commit_verify/src/mpc/atoms.rs | 17 +++++++++++ commit_verify/src/mpc/block.rs | 14 ++++++++- commit_verify/src/mpc/mod.rs | 2 +- commit_verify/src/mpc/tree.rs | 7 ++++- commit_verify/src/stl.rs | 2 +- stl/CommitVerify@0.1.0.sta | 50 +++++++++++++++++---------------- stl/CommitVerify@0.1.0.stl | Bin 1550 -> 1688 bytes stl/CommitVerify@0.1.0.sty | 21 +++++++++----- stl/Merkle.vesper | 2 ++ 9 files changed, 80 insertions(+), 35 deletions(-) diff --git a/commit_verify/src/mpc/atoms.rs b/commit_verify/src/mpc/atoms.rs index 00c03448..43a6745a 100644 --- a/commit_verify/src/mpc/atoms.rs +++ b/commit_verify/src/mpc/atoms.rs @@ -30,6 +30,21 @@ use crate::{CommitmentId, DigestExt}; pub const MPC_MINIMAL_DEPTH: u5 = u5::with(3); +#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, Default)] +#[display(lowercase)] +#[derive(StrictType, StrictEncode, StrictDecode)] +#[strict_type(lib = crate::LIB_NAME_COMMIT_VERIFY, tags = repr, try_from_u8, into_u8)] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", rename_all = "camelCase") +)] +#[repr(u8)] +pub enum Method { + #[default] + Sha256t = 0, +} + /// Map from protocol ids to commitment messages. pub type MessageMap = MediumOrdMap; @@ -149,6 +164,7 @@ impl From for Commitment { /// Structured source multi-message data for commitment creation #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct MultiSource { + pub method: Method, /// Minimal depth of the created LNPBP-4 commitment tree pub min_depth: u5, /// Map of the messages by their respective protocol ids @@ -160,6 +176,7 @@ impl Default for MultiSource { #[inline] fn default() -> Self { MultiSource { + method: Default::default(), min_depth: MPC_MINIMAL_DEPTH, messages: Default::default(), static_entropy: None, diff --git a/commit_verify/src/mpc/block.rs b/commit_verify/src/mpc/block.rs index 229d364e..962ef6aa 100644 --- a/commit_verify/src/mpc/block.rs +++ b/commit_verify/src/mpc/block.rs @@ -32,7 +32,7 @@ use crate::id::CommitId; use crate::merkle::{MerkleBuoy, MerkleHash}; use crate::mpc::atoms::Leaf; use crate::mpc::tree::protocol_id_pos; -use crate::mpc::{Commitment, MerkleTree, Message, MessageMap, Proof, ProtocolId}; +use crate::mpc::{Commitment, MerkleTree, Message, MessageMap, Method, Proof, ProtocolId}; use crate::{Conceal, LIB_NAME_COMMIT_VERIFY}; /// commitment under protocol id {0} is absent from the known part of a given @@ -165,6 +165,10 @@ impl Conceal for MerkleConcealed { #[commit_encode(crate = crate, strategy = conceal, id = Commitment)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] pub struct MerkleBlock { + /// Method used to construct MPC proof (hash function, merklization). + #[getter(as_copy)] + method: Method, + /// Tree depth (up to 16). #[getter(as_copy)] depth: u5, @@ -187,6 +191,7 @@ pub struct MerkleBlock { impl StrictDumb for MerkleBlock { fn strict_dumb() -> Self { MerkleBlock { + method: Method::Sha256t, depth: u5::ONE, cofactor: 0, cross_section: NonEmptyVec::with(TreeNode::strict_dumb()), @@ -221,6 +226,7 @@ impl From<&MerkleTree> for MerkleBlock { NonEmptyVec::try_from_iter(iter).expect("tree width guarantees are broken"); MerkleBlock { + method: tree.method, depth: tree.depth, cofactor: tree.cofactor, cross_section, @@ -281,6 +287,7 @@ impl MerkleBlock { NonEmptyVec::try_from(cross_section).expect("tree width guarantees are broken"); Ok(MerkleBlock { + method: proof.method, depth: u5::with(path.len() as u8), cofactor: proof.cofactor, cross_section, @@ -576,6 +583,7 @@ Changed commitment id: {}", "MerkleBlock conceal procedure is broken" ); Ok(MerkleProof { + method: self.method, pos: self.protocol_id_pos(protocol_id), cofactor: self.cofactor, path: Confined::try_from_iter(map.into_values()) @@ -650,6 +658,10 @@ impl Conceal for MerkleBlock { #[strict_type(lib = LIB_NAME_COMMIT_VERIFY)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] pub struct MerkleProof { + /// Method used to construct MPC proof (hash function, merklization). + #[getter(as_copy)] + method: Method, + /// Position of the leaf in the tree. /// /// Used to determine chirality of the node hashing partners on each step diff --git a/commit_verify/src/mpc/mod.rs b/commit_verify/src/mpc/mod.rs index c733263f..3044e0bd 100644 --- a/commit_verify/src/mpc/mod.rs +++ b/commit_verify/src/mpc/mod.rs @@ -28,7 +28,7 @@ mod tree; mod block; pub use atoms::{ - Commitment, Leaf, Message, MessageMap, MultiSource, ProtocolId, MPC_MINIMAL_DEPTH, + Commitment, Leaf, Message, MessageMap, Method, MultiSource, ProtocolId, MPC_MINIMAL_DEPTH, }; pub use block::{ InvalidProof, LeafNotKnown, MergeError, MerkleBlock, MerkleConcealed, MerkleProof, diff --git a/commit_verify/src/mpc/tree.rs b/commit_verify/src/mpc/tree.rs index 26c6d267..3a7debda 100644 --- a/commit_verify/src/mpc/tree.rs +++ b/commit_verify/src/mpc/tree.rs @@ -27,7 +27,7 @@ pub use self::commit::Error; use crate::merkle::MerkleHash; use crate::mpc::atoms::Leaf; use crate::mpc::{ - Commitment, MerkleBlock, MerkleConcealed, Message, MessageMap, Proof, ProtocolId, + Commitment, MerkleBlock, MerkleConcealed, Message, MessageMap, Method, Proof, ProtocolId, }; use crate::{CommitId, Conceal, LIB_NAME_COMMIT_VERIFY}; @@ -44,6 +44,9 @@ type OrderedMap = MediumOrdMap; #[derive(CommitEncode)] #[commit_encode(crate = crate, strategy = conceal, id = Commitment)] pub struct MerkleTree { + /// Method used to construct MPC proof (hash function, merklization). + pub(super) method: Method, + /// Tree depth (up to 32). pub(super) depth: u5, @@ -155,6 +158,7 @@ mod commit { map.insert(pos, (*protocol, *message)).is_none() }) { return Ok(MerkleTree { + method: source.method, depth, entropy, cofactor, @@ -244,6 +248,7 @@ pub(crate) mod test_helpers { pub fn make_random_tree(msgs: &BTreeMap) -> MerkleTree { let src = MultiSource { + method: Method::Sha256t, min_depth: u5::ZERO, messages: Confined::try_from_iter(msgs.iter().map(|(a, b)| (*a, *b))).unwrap(), static_entropy: None, diff --git a/commit_verify/src/stl.rs b/commit_verify/src/stl.rs index 4fcd398e..17a3a492 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:egMd32l9-y$Kod0o-$FRcNAV-Q4U$O5h-pskW9YM-$irF0yY#miller-pancake-elastic"; + "stl:t1xwwwIz-fgj0c!P-$Kh8oaL-qrthWrM-dxqldh6-qLoQ4Pk#escort-between-doctor"; fn _commit_verify_stl() -> Result { LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! { diff --git a/stl/CommitVerify@0.1.0.sta b/stl/CommitVerify@0.1.0.sta index 5af897f2..45d9439f 100644 --- a/stl/CommitVerify@0.1.0.sta +++ b/stl/CommitVerify@0.1.0.sta @@ -1,34 +1,36 @@ -----BEGIN STRICT TYPE LIB----- -Id: stl:egMd32l9-y$Kod0o-$FRcNAV-Q4U$O5h-pskW9YM-$irF0yY#miller-pancake-elastic +Id: stl:t1xwwwIz-fgj0c!P-$Kh8oaL-qrthWrM-dxqldh6-qLoQ4Pk#escort-between-doctor Name: CommitVerify Dependencies: Std#ralph-blue-lucky -Check-SHA256: ce5ec3f773efffb6535247e84c9da1bfc1656c1a35c616832abf5eecaa5feb44 +Check-SHA256: 4ec05430ba720c208d7a88e5bf0e66cd3660dbd2fd7695c6209acb19591e3a7d 3`1{iZE18?WpZg|c>&5S9&dx0-7pM3Z=O*v*GCA9fL-<|HrZsA`NnJ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cmOW?^G=Z*l+t +0t{nvZ*y}~Wn*+{Z*Bkx0ob*-9G+(AKhLw+s!eDmlO2>&}_PPHjCBJR{;P3000000RR90{{R30 +00(7mbaHQSc>n|g00eGtZe;)f009JZZ*64&1pxp62oFtVa%*g5LvLy0X3z7(9Z)Ra* +bZ>G100IhaWpZn5Wm0c%bOAV?Lxv|61vo|G100IbYWpi_3XJvB$3IQ;c +e&!uvG+Da^2;||fJ!&Dp*8BS%F@mRgpt-$m)gMQunKY@^NKR1twFJLRYVe6X<)?o3YaX +0000000030|Nj6000003ZDDW#3IG5E00ja8FqMAh9bq(Cy9Eg3;jcYvA-~r9`yDZYr+MW?bl_I80W~+N +Q5DGQh1^p2tAX-yWl;qtQ+pX>Mn1WdR0aa$#;`XaWjQa&L5RV{dFp +WCZ~L2LJ#-AOH?hWpib6c4cHjd30rSF$Dnt2LJ#-0RRqCWpib6c4cHjd30rSG6ewu2LJ#-0sszDWpib6 +c4cHjd30rSGz9?w2LJ#-1ON(CbaH89bVy-yXaxZP2LJ#-AOHwda%E*sZ)9Zz0sswTZ*F5{VQgh&PH$vo +00sgDWMy!4XadSC9&dx0-7pM3Z=O*v*GCA9fL-<|HrZsA`NnJlR3~OwaM+Gq(Fu_0Ocz)^+@GUUoV7w& +pu=F9->y0X3z7tAVRL8!IG#g>Clv)aMjKgwAH@`bu1x<7g|G$};xvA~n-$_S0S;qtZEb0EZDnqBOl4ta +00sgIaB^>SZ)0z4Nn`;qm44Q3WPbltNdpi4*91)SI!> -----END STRICT TYPE LIB----- diff --git a/stl/CommitVerify@0.1.0.stl b/stl/CommitVerify@0.1.0.stl index 6f83b3629b8871b2098e8ede1d13f5e8a235c548..452409e09198de3a7db513b83bb8febcb5761cdc 100644 GIT binary patch delta 178 zcmeCB`y?gEVOlhsU%%}Az+s$Lfyx@P*`;6-t zPfGuAs+%0c?7rETaT}v13(;Cwn8P*Mh*iBghFObIkj)qD9A-xL;*3NiQ?ruItt_34 E00mM*lmGw# delta 39 vcmbQi+s88@hEZT*>6$;_wRc diff --git a/stl/CommitVerify@0.1.0.sty b/stl/CommitVerify@0.1.0.sty index ab2f6ba6..d8d99ff3 100644 --- a/stl/CommitVerify@0.1.0.sty +++ b/stl/CommitVerify@0.1.0.sty @@ -1,5 +1,5 @@ {- - Id: stl:egMd32l9-y$Kod0o-$FRcNAV-Q4U$O5h-pskW9YM-$irF0yY#miller-pancake-elastic + Id: stl:t1xwwwIz-fgj0c!P-$Kh8oaL-qrthWrM-dxqldh6-qLoQ4Pk#escort-between-doctor Name: CommitVerify Version: 0.1.0 Description: Client-side-validation deterministic commitments @@ -22,8 +22,9 @@ data Commitment : [Byte ^ 32] data Leaf : inhabited#16 (protocol ProtocolId, message Message) | entropy (entropy U64, pos U32) -@mnemonic(snake-kitchen-june) -data MerkleBlock : depth Std.U5 +@mnemonic(gong-elite-seminar) +data MerkleBlock : method Method + , depth Std.U5 , cofactor U16 , crossSection [TreeNode ^ 1..0xffffffff] , entropy U64? @@ -43,13 +44,15 @@ data MerkleNode : branching NodeBranching , node1 MerkleHash , node2 MerkleHash -@mnemonic(potato-rubber-hobby) -data MerkleProof : pos U32 +@mnemonic(edison-pablo-orinoco) +data MerkleProof : method Method + , pos U32 , cofactor U16 , path [MerkleHash ^ ..0x20] -@mnemonic(horizon-pigment-dialog) -data MerkleTree : depth Std.U5 +@mnemonic(jump-respond-panda) +data MerkleTree : method Method + , depth Std.U5 , entropy U64 , cofactor U16 , messages {ProtocolId -> ^ ..0xffffff Message} @@ -58,6 +61,10 @@ data MerkleTree : depth Std.U5 @mnemonic(druid-blitz-rover) data Message : [Byte ^ 32] +@mnemonic(subject-justin-cowboy) +data Method : sha256t + + @mnemonic(member-dexter-price) data NodeBranching : void | single | branch diff --git a/stl/Merkle.vesper b/stl/Merkle.vesper index c4ee87a0..b8b7ea2e 100644 --- a/stl/Merkle.vesper +++ b/stl/Merkle.vesper @@ -52,6 +52,7 @@ Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31 MerkleConcealed concealed concealed=MerkleConcealed MerkleBlock rec + method enum Method sha256t=0 depth enum { U5 _0=0 _1=1 _2=2 _3=3 _4=4 _5=5 _6=6 _7=7 _8=8 _9=9 _10=10 _11=11 _12=12 _13=13 _14=14 _15=15 @@ -80,6 +81,7 @@ Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31 MerkleConcealed concealed concealed=MerkleConcealed MerkleTree rec + method enum Method sha256t=0 depth enum { U5 _0=0 _1=1 _2=2 _3=3 _4=4 _5=5 _6=6 _7=7 _8=8 _9=9 _10=10 _11=11 _12=12 _13=13 _14=14 _15=15