Skip to content

Commit

Permalink
mpc: add merklization method information
Browse files Browse the repository at this point in the history
to be used in a future for more zk-friendly merkjlization hash functions
  • Loading branch information
dr-orlovsky committed Oct 14, 2024
1 parent 4015f1f commit 23f69e9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 35 deletions.
17 changes: 17 additions & 0 deletions commit_verify/src/mpc/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProtocolId, Message>;

Expand Down Expand Up @@ -149,6 +164,7 @@ impl From<Sha256> 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
Expand All @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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()),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -44,6 +44,9 @@ type OrderedMap = MediumOrdMap<u32, (ProtocolId, Message)>;
#[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,

Expand Down Expand Up @@ -155,6 +158,7 @@ mod commit {
map.insert(pos, (*protocol, *message)).is_none()
}) {
return Ok(MerkleTree {
method: source.method,
depth,
entropy,
cofactor,
Expand Down Expand Up @@ -244,6 +248,7 @@ pub(crate) mod test_helpers {

pub fn make_random_tree(msgs: &BTreeMap<ProtocolId, Message>) -> 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,
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! {
Expand Down
50 changes: 26 additions & 24 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -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`NnJlR3`&dbYuYoQ*>kj0A^Tl*p6J$
36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV5C958Z*6U9bZupBbOiwb2LJ#-AOHkRWnpFn0uTvlZfId*
36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV5daE9Z*6U9bZupBbOiwb2LJ#-AOHkRWnpFn0uTvlZfId*
X>?^|00sgGaB^>SZ)0z40Wg(*<{e=)S-S-Y<l(P9Y9YVY`}-X+f~R@qMRed+u?KBsb8}&5WdSuesZkZk
>V@1=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d30rSG6ewu2LJ#-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`;qm44<OVKiC01qkHfuRUrZzt;Qv9WjEZdF4fP;8w8*ZDn(FVP|ClH8-hI70Bv^
+*0?ef%0)>Q3WPbltNdpi4*91)SI!>

-----END STRICT TYPE LIB-----

Binary file modified stl/[email protected]
Binary file not shown.
21 changes: 14 additions & 7 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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?
Expand All @@ -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}
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions stl/Merkle.vesper
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23f69e9

Please sign in to comment.