From 98ed7451b89c83acca540d061cb134fd78290a7d Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 16 Mar 2024 18:24:35 +0100 Subject: [PATCH] refactor: move in ReservedBytes type from RGB Core --- commit_verify/src/lib.rs | 94 ++++++++++++++++++++++++++++++++++++- commit_verify/src/stl.rs | 7 ++- stl/CommitVerify@0.1.0.sta | 21 +++++---- stl/CommitVerify@0.1.0.stl | Bin 1333 -> 1405 bytes stl/CommitVerify@0.1.0.sty | 11 ++++- stl/Merkle.vesper | 35 +++++++------- 6 files changed, 138 insertions(+), 30 deletions(-) diff --git a/commit_verify/src/lib.rs b/commit_verify/src/lib.rs index 6e9d0e2c..9b509cb3 100644 --- a/commit_verify/src/lib.rs +++ b/commit_verify/src/lib.rs @@ -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([u8; LEN]); + +impl Default for ReservedBytes { + fn default() -> Self { Self([VAL; LEN]) } +} + +impl From<[u8; LEN]> for ReservedBytes { + 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 CommitEncode for ReservedBytes { + type CommitmentId = StrictHash; + + fn commit_encode(&self, e: &mut CommitEngine) { e.commit_to_serialized(self) } + } + + impl StrictDecode for ReservedBytes { + fn strict_decode(reader: &mut impl TypedRead) -> Result { + let reserved = reader.read_tuple(|r| r.read_field().map(Self))?; + if reserved != ReservedBytes::::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 Serialize for ReservedBytes { + fn serialize(&self, serializer: S) -> Result + where S: Serializer { + // Doing nothing + serializer.serialize_unit() + } + } + + impl<'de, const LEN: usize, const VAL: u8> Deserialize<'de> for ReservedBytes { + fn deserialize(deserializer: D) -> Result + 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(self) -> Result<(), E> + where E: de::Error { + Ok(()) + } + + fn visit_unit(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 { diff --git a/commit_verify/src/stl.rs b/commit_verify/src/stl.rs index 1240f76b..57c6596e 100644 --- a/commit_verify/src/stl.rs +++ b/commit_verify/src/stl.rs @@ -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 { LibBuilder::new(libname!(LIB_NAME_COMMIT_VERIFY), tiny_bset! { strict_types::stl::std_stl().to_dependency() }) + .transpile::>() + .transpile::>() + .transpile::>() .transpile::() .transpile::() .transpile::() diff --git a/stl/CommitVerify@0.1.0.sta b/stl/CommitVerify@0.1.0.sta index 1255495f..79adf775 100644 --- a/stl/CommitVerify@0.1.0.sta +++ b/stl/CommitVerify@0.1.0.sta @@ -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-YV@1=_p5>Oab-~jC R3C`SFec^=zG+gvC|O;Wo~qGZ*X}41_B3VZgg^QaCra#2m^3$a{vGY3r%HmYiwmg @@ -24,13 +24,14 @@ u0Wg(*<{e=)S-S-YmzVsZkZk>V@1=_p5>Oab- ~jCR3C`SFec^=zG+gvC{wm0000000960{{R30000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M0X;1R*Z=?k diff --git a/stl/CommitVerify@0.1.0.sty b/stl/CommitVerify@0.1.0.sty index 1e8050c7..3f6bc034 100644 --- a/stl/CommitVerify@0.1.0.sty +++ b/stl/CommitVerify@0.1.0.sty @@ -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 @@ -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] diff --git a/stl/Merkle.vesper b/stl/Merkle.vesper index dac8c267..0a33dc66 100644 --- a/stl/Merkle.vesper +++ b/stl/Merkle.vesper @@ -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 @@ -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 @@ -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