Skip to content

Commit

Permalink
Merge pull request #180 from LNP-BP/merkle_concealed
Browse files Browse the repository at this point in the history
MPC: Add MerkleConcealed type
  • Loading branch information
dr-orlovsky authored Aug 15, 2024
2 parents f80719a + 42f1035 commit 4fba57a
Show file tree
Hide file tree
Showing 11 changed files with 536 additions and 28 deletions.
433 changes: 433 additions & 0 deletions commit_verify/doc/MPC.drawio

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions commit_verify/doc/MPC.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions commit_verify/src/bin/commit-stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
43 changes: 41 additions & 2 deletions commit_verify/src/mpc/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ 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", rename_all = "camelCase")
)]
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)]
Expand Down Expand Up @@ -588,7 +616,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 {
Expand All @@ -597,7 +625,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,
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion commit_verify/src/mpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() }
}
Expand Down
3 changes: 2 additions & 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: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<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! {
Expand All @@ -33,6 +33,7 @@ fn _commit_verify_stl() -> Result<TypeLib, CompileError> {
.transpile::<ReservedBytes<1>>()
.transpile::<ReservedBytes<2>>()
.transpile::<ReservedBytes<4>>()
.transpile::<mpc::MerkleConcealed>()
.transpile::<mpc::MerkleTree>()
.transpile::<mpc::MerkleBlock>()
.transpile::<mpc::MerkleProof>()
Expand Down
41 changes: 22 additions & 19 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -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-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F|Rkm*bpSUudIqf?x<LRg@~V42^pIs*%m
1ZZJ%XaP8$Lxv|61vo|<S$`kJ6oIZx{|tq&1{dNqe!iO(;xhpbV{dJ3X>@I6Zgfm#VP*gZ0t#?)Z**^C
Z){0q0Wg(*<{e=)S-S-Y<l(P9Y9YVY`}-X+f~R@qMRed+u?KBsb8}&5WdSuesZkZk>V@1=_p5>Oab-~j
CR3C`SFec^=zG+gvC{

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

Binary file modified stl/[email protected]
Binary file not shown.
7 changes: 6 additions & 1 deletion stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-
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
Version: 0.1.0
Description: Client-side-validation deterministic commitments
Expand Down Expand Up @@ -28,6 +28,11 @@ data MerkleBlock : depth Std.U5
, crossSection [TreeNode ^ 1..0xffffffff]
, entropy U64?

@mnemonic(aztec-miracle-igloo)
data MerkleConcealed : depth Std.U5
, cofactor U16
, merkleRoot MerkleHash

@mnemonic(horse-popcorn-bundle)
data MerkleHash : [Byte ^ 32]

Expand Down
18 changes: 16 additions & 2 deletions stl/Merkle.vesper
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ Leaf union
pos is U32

Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31
MerkleBlock concealed concealed=MerkleBlock
MerkleConcealed serialized

MerkleConcealed rec
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
_16=16 _17=17 _18=18 _19=19 _20=20 _21=21 _22=22 _23=23
_24=24 _25=25 _26=26 _27=27 _28=28 _29=29 _30=30 _31=31

}
cofactor is U16
merkleRoot bytes len=32 aka=MerkleHash

Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31
MerkleConcealed concealed concealed=MerkleConcealed

MerkleBlock rec
depth enum {
Expand Down Expand Up @@ -63,7 +77,7 @@ MerkleBlock rec
some is U64 option wrapped tag=1

Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31
MerkleBlock concealed concealed=MerkleBlock
MerkleConcealed concealed concealed=MerkleConcealed

MerkleTree rec
depth enum {
Expand Down

0 comments on commit 4fba57a

Please sign in to comment.