Skip to content

Commit fbb57a7

Browse files
committed
commit: add strict encoding
1 parent 3504108 commit fbb57a7

7 files changed

Lines changed: 48 additions & 32 deletions

File tree

Cargo.lock

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

commit_verify/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use convolve::{ConvolveCommit, ConvolveCommitProof};
3636
pub use embed::{EmbedCommitProof, EmbedCommitVerify, VerifyEq};
3737
pub use encode::CommitEncode;
3838

39-
pub const STY_LIB_NAME: &str = "CommitVerify";
39+
pub const LIB_NAME_COMMIT_VERIFY: &str = "CommitVerify";
4040

4141
/// Marker trait for specific commitment protocols.
4242
///

commit_verify/src/merkle.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ use amplify::confinement::Confined;
1919
use amplify::num::u4;
2020
use amplify::{Bytes32, Wrapper};
2121
use bitcoin_hashes::{sha256, Hash};
22-
use strict_encoding::{DecodeError, ReadTuple, StrictDecode, StrictEncode, TypedRead, TypedWrite};
2322

2423
use crate::encode::{strategies, CommitStrategy};
25-
use crate::{CommitEncode, STY_LIB_NAME};
24+
use crate::{CommitEncode, LIB_NAME_COMMIT_VERIFY};
2625

2726
/// Type of a merkle node branching.
2827
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
@@ -56,8 +55,8 @@ impl CommitStrategy for NodeBranching {
5655
/// [LNPBP-4]: https://github.com/LNP-BP/LNPBPs/blob/master/lnpbp-0004.md
5756
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
5857
#[wrapper(Deref, BorrowSlice, Display, FromStr, Hex, RangeOps)]
59-
#[derive(StrictDumb, StrictType)]
60-
#[strict_type(lib = STY_LIB_NAME, dumb = MerkleNode(default!()))]
58+
#[derive(StrictDumb, StrictType, StrictEncode, StrictDecode)]
59+
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY, dumb = MerkleNode(default!()))]
6160
#[cfg_attr(
6261
feature = "serde",
6362
derive(Serialize, Deserialize),
@@ -69,22 +68,6 @@ pub struct MerkleNode(
6968
Bytes32,
7069
);
7170

72-
impl StrictEncode for MerkleNode {
73-
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
74-
writer.write_newtype::<Self>(self.0.as_inner())
75-
}
76-
}
77-
78-
impl StrictDecode for MerkleNode {
79-
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
80-
reader.read_tuple(|r| {
81-
r.read_field::<[u8; 32]>()
82-
.map(Bytes32::from_inner)
83-
.map(MerkleNode)
84-
})
85-
}
86-
}
87-
8871
const VIRTUAL_LEAF: MerkleNode = MerkleNode(Bytes32::from_array([0xFF; 32]));
8972

9073
impl MerkleNode {

commit_verify/src/mpc/atoms.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ pub type MessageMap = SmallOrdMap<ProtocolId, Message>;
2929
/// procedure.
3030
///
3131
/// [LNPBP-4]: https://github.com/LNP-BP/LNPBPs/blob/master/lnpbp-0004.md
32-
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
32+
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Default)]
3333
#[wrapper(Deref, BorrowSlice, Display, FromStr, Hex, RangeOps)]
34+
#[derive(StrictType, StrictEncode, StrictDecode)]
35+
#[strict_type(lib = crate::LIB_NAME_COMMIT_VERIFY)]
3436
#[cfg_attr(
3537
feature = "serde",
3638
derive(Serialize, Deserialize),
@@ -49,8 +51,10 @@ impl CommitEncode for ProtocolId {
4951
/// Original message participating in multi-message commitment.
5052
///
5153
/// The message must be represented by a 32-byte hash.
52-
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
54+
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Default)]
5355
#[wrapper(Deref, BorrowSlice, Display, FromStr, Hex, RangeOps)]
56+
#[derive(StrictType, StrictEncode, StrictDecode)]
57+
#[strict_type(lib = crate::LIB_NAME_COMMIT_VERIFY)]
5458
#[cfg_attr(
5559
feature = "serde",
5660
derive(Serialize, Deserialize),

commit_verify/src/mpc/block.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// You should have received a copy of the Apache 2.0 License along with this
1313
// software. If not, see <https://opensource.org/licenses/Apache-2.0>.
1414

15+
#![allow(unused_braces)]
16+
1517
use std::cmp::Ordering;
1618
use std::collections::{BTreeMap, BTreeSet};
1719
use std::io::Write;
@@ -24,8 +26,8 @@ use crate::id::CommitmentId;
2426
use crate::merkle::MerkleNode;
2527
use crate::mpc::atoms::Leaf;
2628
use crate::mpc::tree::protocol_id_pos;
27-
use crate::mpc::{Commitment, MerkleTree, Message, ProtocolId, LNPBP4_TAG};
28-
use crate::{CommitEncode, Conceal};
29+
use crate::mpc::{Commitment, MerkleTree, Message, Proof, ProtocolId, LNPBP4_TAG};
30+
use crate::{CommitEncode, Conceal, LIB_NAME_COMMIT_VERIFY};
2931

3032
/// commitment under protocol id {_0} is absent from the known part of a given
3133
/// LNPBP-4 Merkle block.
@@ -40,6 +42,12 @@ pub struct UnrelatedProof;
4042

4143
/// LNPBP-4 Merkle tree node.
4244
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
45+
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
46+
#[strict_type(
47+
lib = LIB_NAME_COMMIT_VERIFY,
48+
tags = order,
49+
dumb = { TreeNode::ConcealedNode { depth: u4::ZERO, hash: [0u8; 32].into() } }
50+
)]
4351
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
4452
enum TreeNode {
4553
/// A node of the tree with concealed leaf or tree branch information.
@@ -90,6 +98,8 @@ impl TreeNode {
9098

9199
/// Partially-concealed merkle tree data.
92100
#[derive(Getters, Clone, PartialEq, Eq, Hash, Debug, Default)]
101+
#[derive(StrictType, StrictEncode, StrictDecode)]
102+
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)]
93103
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
94104
pub struct MerkleBlock {
95105
/// Tree depth (up to 16).
@@ -106,6 +116,8 @@ pub struct MerkleBlock {
106116
entropy: Option<u64>,
107117
}
108118

119+
impl Proof for MerkleBlock {}
120+
109121
impl From<&MerkleTree> for MerkleBlock {
110122
fn from(tree: &MerkleTree) -> Self {
111123
let map = &tree.map;
@@ -454,6 +466,8 @@ impl CommitmentId for MerkleBlock {
454466

455467
/// A proof of the merkle commitment.
456468
#[derive(Getters, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
469+
#[derive(StrictType, StrictEncode, StrictDecode)]
470+
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)]
457471
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
458472
pub struct MerkleProof {
459473
/// Position of the leaf in the tree.
@@ -468,6 +482,8 @@ pub struct MerkleProof {
468482
path: SmallVec<MerkleNode>,
469483
}
470484

485+
impl Proof for MerkleProof {}
486+
471487
impl MerkleProof {
472488
/// Computes the depth of the merkle tree.
473489
pub fn depth(&self) -> u8 { self.path.len() as u8 }

commit_verify/src/mpc/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ pub use tree::Error;
2727
pub use tree::{IntoIter, MerkleTree};
2828

2929
const LNPBP4_TAG: [u8; 16] = *b"urn:lnpbp:lnpbp4";
30+
31+
/// Marker trait for variates of LNPBP-4 commitment proofs, which differ by the
32+
/// amount of concealed information.
33+
pub trait Proof:
34+
strict_encoding::StrictEncode + strict_encoding::StrictDecode + Clone + Eq + std::fmt::Debug
35+
{
36+
}

commit_verify/src/mpc/tree.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414

1515
use std::collections::BTreeMap;
1616

17+
use amplify::confinement::SmallOrdMap;
1718
use amplify::num::{u256, u4};
1819
use amplify::Wrapper;
1920

2021
#[cfg(feature = "rand")]
2122
pub use self::commit::Error;
2223
use crate::merkle::{MerkleLeaves, MerkleNode};
2324
use crate::mpc::atoms::Leaf;
24-
use crate::mpc::{Message, MessageMap, ProtocolId, LNPBP4_TAG};
25-
use crate::Conceal;
25+
use crate::mpc::{Message, MessageMap, Proof, ProtocolId, LNPBP4_TAG};
26+
use crate::{Conceal, LIB_NAME_COMMIT_VERIFY};
2627

27-
type OrderedMap = BTreeMap<u16, (ProtocolId, Message)>;
28+
type OrderedMap = SmallOrdMap<u16, (ProtocolId, Message)>;
2829

2930
/// Complete information about LNPBP-4 merkle tree.
3031
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
32+
#[derive(StrictDumb, StrictType, StrictEncode, StrictDecode)]
33+
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)]
3134
pub struct MerkleTree {
3235
/// Tree depth (up to 16).
3336
pub(super) depth: u4,
@@ -41,6 +44,8 @@ pub struct MerkleTree {
4144
pub(super) map: OrderedMap,
4245
}
4346

47+
impl Proof for MerkleTree {}
48+
4449
pub struct IntoIter {
4550
width: u16,
4651
pos: u16,
@@ -100,6 +105,7 @@ impl Conceal for MerkleTree {
100105

101106
#[cfg(feature = "rand")]
102107
mod commit {
108+
use amplify::confinement::Confined;
103109
use rand::{thread_rng, RngCore};
104110

105111
use super::*;
@@ -157,7 +163,7 @@ mod commit {
157163
depth,
158164
messages: source.messages.clone(),
159165
entropy,
160-
map,
166+
map: Confined::try_from(map).expect("MultiSource type guarantees"),
161167
})
162168
}
163169
}

0 commit comments

Comments
 (0)