Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 32-bit sizing for MPC trees #132

Merged
merged 10 commits into from
Jul 12, 2023
13 changes: 5 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ serde = ["serde_crate", "commit_verify/serde"]

[package.metadata.docs.rs]
features = [ "all" ]

[patch.crates-io]
strict_encoding = { git = "https://github.com/strict-types/strict-encoding", branch = "u5" }
strict_types = { git = "https://github.com/strict-types/strict-types", branch = "u5" }
4 changes: 2 additions & 2 deletions commit_verify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ required-features = ["stl"]
[dependencies]
amplify = { version = "4.0.0", features = ["hex", "apfloat"] }
commit_encoding_derive = { version = "0.10.0", path = "derive" }
strict_encoding = "2.3.0"
strict_types = { version = "1.3.0", optional = true }
strict_encoding = "2.4.0"
strict_types = { version = "1.4.0", optional = true }
sha2 = "0.10.6"
rand = { version = "0.8.5", optional = true }
serde_crate = { version = "1.0", package = "serde", optional = true }
Expand Down
22 changes: 11 additions & 11 deletions commit_verify/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::collections::{btree_set, BTreeSet};
use std::io::Write;

use amplify::confinement::Confined;
use amplify::num::u4;
use amplify::num::u5;
use amplify::{Bytes32, Wrapper};
use sha2::Sha256;

Expand Down Expand Up @@ -84,20 +84,20 @@ impl CommitmentId for MerkleNode {
const VIRTUAL_LEAF: MerkleNode = MerkleNode(Bytes32::from_array([0xFF; 32]));

impl MerkleNode {
pub fn void(tag: [u8; 16], depth: u4, width: u16) -> Self {
pub fn void(tag: [u8; 16], depth: u5, width: u32) -> Self {
let virt = VIRTUAL_LEAF;
Self::with(NodeBranching::Void, tag, depth, width, virt, virt)
}

pub fn single(tag: [u8; 16], depth: u4, width: u16, node: MerkleNode) -> Self {
pub fn single(tag: [u8; 16], depth: u5, width: u32, node: MerkleNode) -> Self {
let single = NodeBranching::Single;
Self::with(single, tag, depth, width, node, VIRTUAL_LEAF)
}

pub fn branches(
tag: [u8; 16],
depth: u4,
width: u16,
depth: u5,
width: u32,
node1: MerkleNode,
node2: MerkleNode,
) -> Self {
Expand All @@ -107,8 +107,8 @@ impl MerkleNode {
fn with(
branching: NodeBranching,
tag: [u8; 16],
depth: u4,
width: u16,
depth: u5,
width: u32,
node1: MerkleNode,
node2: MerkleNode,
) -> Self {
Expand All @@ -130,20 +130,20 @@ impl MerkleNode {
/// [LNPBP-81]: https://github.com/LNP-BP/LNPBPs/blob/master/lnpbp-0081.md
pub fn merklize(tag: [u8; 16], leaves: &impl MerkleLeaves) -> Self {
let mut nodes = leaves.merkle_leaves().map(|leaf| leaf.commitment_id());
let len = nodes.len() as u16;
let len = nodes.len() as u32;
if len == 1 {
// If we have just one leaf, it's MerkleNode value is the root
nodes.next().expect("length is 1")
} else {
Self::_merklize(tag, nodes, u4::ZERO, len)
Self::_merklize(tag, nodes, u5::ZERO, len)
}
}

pub fn _merklize(
tag: [u8; 16],
mut iter: impl ExactSizeIterator<Item = MerkleNode>,
depth: u4,
width: u16,
depth: u5,
width: u32,
) -> Self {
let len = iter.len() as u16;

Expand Down
24 changes: 12 additions & 12 deletions commit_verify/src/mpc/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

use std::io::Write;

use amplify::confinement::SmallOrdMap;
use amplify::num::u4;
use amplify::confinement::MediumOrdMap;
use amplify::num::u5;
use amplify::{Bytes32, Wrapper};

use crate::id::CommitmentId;
use crate::merkle::MerkleNode;
use crate::{strategies, CommitEncode, CommitStrategy};

/// Map from protocol ids to commitment messages.
pub type MessageMap = SmallOrdMap<ProtocolId, Message>;
pub type MessageMap = MediumOrdMap<ProtocolId, Message>;

/// Source data for creation of multi-message commitments according to [LNPBP-4]
/// procedure.
Expand Down Expand Up @@ -91,12 +91,12 @@ pub enum Leaf {
},
Entropy {
entropy: u64,
pos: u16,
pos: u32,
},
}

impl Leaf {
pub fn entropy(entropy: u64, pos: u16) -> Self { Self::Entropy { entropy, pos } }
pub fn entropy(entropy: u64, pos: u32) -> Self { Self::Entropy { entropy, pos } }

pub fn inhabited(protocol: ProtocolId, message: Message) -> Self {
Self::Inhabited { protocol, message }
Expand Down Expand Up @@ -157,36 +157,36 @@ impl Commitment {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct MultiSource {
/// Minimal depth of the created LNPBP-4 commitment tree
pub min_depth: u4,
pub min_depth: u5,
/// Map of the messages by their respective protocol ids
pub messages: MessageMap,
}

impl Default for MultiSource {
fn default() -> Self {
MultiSource {
min_depth: u4::try_from(3).expect("hardcoded value"),
min_depth: u5::with(3),
messages: Default::default(),
}
}
}

/// Helper struct to track depth when merging two merkle blocks.
pub struct MerkleBuoy {
buoy: u4,
buoy: u5,
stack: Option<Box<MerkleBuoy>>,
}

impl MerkleBuoy {
pub fn new(top: u4) -> Self {
pub fn new(top: u5) -> Self {
Self {
buoy: top,
stack: None,
}
}

/// Measure the current buoy level.
pub fn level(&self) -> u4 {
pub fn level(&self) -> u5 {
self.stack
.as_ref()
.map(Box::as_ref)
Expand All @@ -200,8 +200,8 @@ impl MerkleBuoy {
///
/// The buoy surfaces each time the contents it has is reduced to two depth
/// of the same level.
pub fn push(&mut self, depth: u4) -> bool {
if depth == u4::ZERO {
pub fn push(&mut self, depth: u5) -> bool {
if depth == u5::ZERO {
return false;
}
match self
Expand Down
Loading
Loading