Skip to content

Commit

Permalink
refactor: move in ReservedBytes type from RGB Core
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Mar 16, 2024
1 parent e963d5d commit 98ed745
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 30 deletions.
94 changes: 93 additions & 1 deletion commit_verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,102 @@ pub const LIB_NAME_COMMIT_VERIFY: &str = "CommitVerify";
pub trait CommitmentProtocol {}

/// Protocol defining commits created by using externally created hash value
/// *optionally pretagged).
/// *optionally pre-tagged*.
pub struct UntaggedProtocol;
impl CommitmentProtocol for UntaggedProtocol {}

/// Reserved bytes.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display("reserved")]
#[derive(StrictType, StrictEncode)]
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)]
pub struct ReservedBytes<const LEN: usize, const VAL: u8 = 0>([u8; LEN]);

impl<const LEN: usize, const VAL: u8> Default for ReservedBytes<LEN, VAL> {
fn default() -> Self { Self([VAL; LEN]) }
}

impl<const LEN: usize, const VAL: u8> From<[u8; LEN]> for ReservedBytes<LEN, VAL> {
fn from(value: [u8; LEN]) -> Self {
assert_eq!(value, [VAL; LEN]);
Self(value)
}
}

mod _reserved {
use strict_encoding::{DecodeError, ReadTuple, StrictDecode, TypedRead};

use crate::{CommitEncode, CommitEngine, ReservedBytes, StrictHash};

impl<const LEN: usize, const VAL: u8> CommitEncode for ReservedBytes<LEN, VAL> {
type CommitmentId = StrictHash;

fn commit_encode(&self, e: &mut CommitEngine) { e.commit_to_serialized(self) }
}

impl<const LEN: usize, const VAL: u8> StrictDecode for ReservedBytes<LEN, VAL> {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
let reserved = reader.read_tuple(|r| r.read_field().map(Self))?;
if reserved != ReservedBytes::<LEN, VAL>::default() {
Err(DecodeError::DataIntegrityError(format!(
"unsupported reserved byte value indicating a future RGB version. Please \
update your software, or, if the problem persists, contact your vendor \
providing the following version information: {reserved}"
)))
} else {
Ok(reserved)
}
}
}

#[cfg(feature = "serde")]
mod _serde {
use std::fmt;

use serde_crate::de::Visitor;
use serde_crate::{de, Deserialize, Deserializer, Serialize, Serializer};

use super::*;

impl<const LEN: usize, const VAL: u8> Serialize for ReservedBytes<LEN, VAL> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
// Doing nothing
serializer.serialize_unit()
}
}

impl<'de, const LEN: usize, const VAL: u8> Deserialize<'de> for ReservedBytes<LEN, VAL> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
#[derive(Default)]
pub struct UntaggedUnitVisitor;

impl<'de> Visitor<'de> for UntaggedUnitVisitor {
type Value = ();

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "reserved unit")
}

fn visit_none<E>(self) -> Result<(), E>
where E: de::Error {
Ok(())
}

fn visit_unit<E>(self) -> Result<(), E>
where E: de::Error {
Ok(())
}
}

deserializer.deserialize_unit(UntaggedUnitVisitor)?;
Ok(default!())
}
}
}
}

/// Helpers for writing test functions working with commit schemes
#[cfg(test)]
pub mod test_helpers {
Expand Down
7 changes: 5 additions & 2 deletions commit_verify/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@

use strict_types::{CompileError, LibBuilder, TypeLib};

use crate::{mpc, MerkleHash, MerkleNode, StrictHash, LIB_NAME_COMMIT_VERIFY};
use crate::{mpc, MerkleHash, MerkleNode, ReservedBytes, StrictHash, LIB_NAME_COMMIT_VERIFY};

pub const LIB_ID_COMMIT_VERIFY: &str =
"urn:ubideco:stl:7qvjR4HCwJKF3mxE5GqsAaADces5JDRwb8ajAse9mkz3#exhibit-karate-ritual";
"urn:ubideco:stl:7EpM1uymEteG4g4xmF5ntKYX1wXXpbQj5iLiDreH4jWa#protein-donald-cool";

fn _commit_verify_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! {
strict_types::stl::std_stl().to_dependency()
})
.transpile::<ReservedBytes<1>>()
.transpile::<ReservedBytes<2>>()
.transpile::<ReservedBytes<4>>()
.transpile::<mpc::MerkleTree>()
.transpile::<mpc::MerkleBlock>()
.transpile::<mpc::MerkleProof>()
Expand Down
21 changes: 11 additions & 10 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-----BEGIN STRICT TYPE LIB-----
Id: urn:ubideco:stl:7qvjR4HCwJKF3mxE5GqsAaADces5JDRwb8ajAse9mkz3#exhibit-karate-ritual
Id: urn:ubideco:stl:7EpM1uymEteG4g4xmF5ntKYX1wXXpbQj5iLiDreH4jWa#protein-donald-cool
Name: CommitVerify
Dependency: DzTvt9gGhPUKD8Dkkjk9PDBhkJ4gtWxXWQjxnmUYLNrs#voyage-kimono-disco
Checksum-SHA256: ab9c647839dade059c136e1259383094050afe96493376d9debca2dc05173a4a
Checksum-SHA256: 33bbc0d7f6f0068636ff89a975b4ca52e6ccb9811b22e0f7aecdb69e5023fb4c

3`1{iZE18?WpZg|c>%!$%tl@;WNu_lcw-g2$nLVb<*lb*ENIx0;35YAw}=B%bYuY
oQ*>kj0A^Tl*p6J$36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV3;+s4Z*6U9bZu
oQ*>kj0A^Tl*p6J$36SYb7g#;qpQBTpwL(~+!(f@;t~vt?k^)sV4*&{7Z*6U9bZu
pBbOiwb2LJ#-AOHkRWnpFn0uTvlZfId*X>?^|00sgGaB^>SZ)0z40Wg(*<{e=)S-
S-Y<l(P9Y9YVY`}-X+f~R@qMRed+u?KBsb8}&5WdSuesZkZk>V@1=_p5>Oab-~jC
R3C`SFec^=zG+gvC|O;Wo~qGZ*X}41_B3VZgg^QaCra#2m^3$a{vGY3r%HmYiwmg
Expand All @@ -24,13 +24,14 @@ u0Wg(*<{e=)S-S-Y<l(P9Y9YVY`}-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+MW?bl_I82W@3@b75y?0W~+NQ5DGQh1^
p2tAX-yWl;qtQ<OqiuZa`rd(@k;(*

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

Binary file modified stl/[email protected]
Binary file not shown.
11 changes: 10 additions & 1 deletion stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-
Id: urn:ubideco:stl:7qvjR4HCwJKF3mxE5GqsAaADces5JDRwb8ajAse9mkz3#exhibit-karate-ritual
Id: urn:ubideco:stl:7EpM1uymEteG4g4xmF5ntKYX1wXXpbQj5iLiDreH4jWa#protein-donald-cool
Name: CommitVerify
Version: 0.1.0
Description: Client-side-validation deterministic commitments
Expand Down Expand Up @@ -60,6 +60,15 @@ data NodeBranching : void | single | branch
@mnemonic(gamma-banjo-corona)
data ProtocolId : [Byte ^ 32]

@mnemonic(joker-lemon-mental)
data ReservedBytes1 : [Byte ^ 1]

@mnemonic(pocket-habitat-soviet)
data ReservedBytes2 : [Byte ^ 2]

@mnemonic(claudia-drink-digital)
data ReservedBytes4 : [Byte ^ 4]

@mnemonic(cheese-final-locate)
data StrictHash : [Byte ^ 32]

Expand Down
35 changes: 19 additions & 16 deletions stl/Merkle.vesper
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ MerkleHash commitment hasher=SHA256 tagged=urn:ubideco:merkle:node#2024-01-31
MerkleNode serialized

MerkleNode rec
branching enum void=0 single=1 branch=2 -- NodeBranching
branching enum NodeBranching void=0 single=1 branch=2
depth is U8
width is U256
node1 bytes len=32 aka=MerkleHash
Expand All @@ -39,21 +39,23 @@ Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31

MerkleBlock rec
depth enum {
_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
} -- U5
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
crossSection list len=1..MAX32
TreeNode union
concealedNode rec tag=0
depth enum {
_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
} -- U5
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

}
hash bytes len=32 aka=MerkleHash
commitmentLeaf rec tag=1
protocolId bytes len=32 aka=ProtocolId
Expand All @@ -65,11 +67,12 @@ Commitment commitment hasher=SHA256 tagged=urn:ubideco:mpc:commitment#2024-01-31

MerkleTree rec
depth enum {
_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
} -- U5
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

}
entropy is U64
cofactor is U16
messages map len=0..MAX24
Expand Down

0 comments on commit 98ed745

Please sign in to comment.