From 4c9a1ce36084550701b4b89df1d830abdd72ac59 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 9 Apr 2024 16:17:31 +0200 Subject: [PATCH] lib: bugfix serialization --- Cargo.lock | 1 + Cargo.toml | 2 +- src/isa/exec.rs | 12 ++---------- src/library/constants.rs | 6 +++--- src/library/lib.rs | 20 ++++++++++++++++---- src/stl.rs | 8 ++++++-- stl/AluVM@0.1.0.sta | 17 +++++++++++++---- stl/AluVM@0.1.0.stl | Bin 79 -> 510 bytes stl/AluVM@0.1.0.sty | 21 +++++++++++++++++++-- 9 files changed, 61 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8202f48..1d862d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,6 +538,7 @@ source = "git+https://github.com/strict-types/strict-encoding?branch=rstring#49d dependencies = [ "amplify", "half", + "serde", "strict_encoding_derive", ] diff --git a/Cargo.toml b/Cargo.toml index 675e99b..8f4ba3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ std = ["amplify/std"] log = ["std"] alloc = ["amplify/alloc"] curve25519 = ["curve25519-dalek"] -serde = ["serde_crate", "amplify/serde", "std"] +serde = ["serde_crate", "amplify/serde", "std", "strict_encoding/serde"] [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2" diff --git a/src/isa/exec.rs b/src/isa/exec.rs index 3c41b85..0795b90 100644 --- a/src/isa/exec.rs +++ b/src/isa/exec.rs @@ -1433,11 +1433,7 @@ impl InstructionSet for Secp256k1Op { #[cfg(feature = "secp256k1")] #[inline] - fn isa_ids() -> IsaSeg { - let mut set = IsaSeg::new(); - set.insert(constants::ISA_ID_SECP256K); - set - } + fn isa_ids() -> IsaSeg { IsaSeg::with(constants::ISA_ID_SECP256K) } fn src_regs(&self) -> BTreeSet { match self { @@ -1580,11 +1576,7 @@ impl InstructionSet for Curve25519Op { #[cfg(feature = "curve25519")] #[inline] - fn isa_ids() -> IsaSeg { - let mut set = IsaSeg::new(); - set.insert(constants::ISA_ID_ED25519); - set - } + fn isa_ids() -> IsaSeg { IsaSeg::with(constants::ISA_ID_ED25519) } fn src_regs(&self) -> BTreeSet { match self { diff --git a/src/library/constants.rs b/src/library/constants.rs index 713c989..4cb9a81 100644 --- a/src/library/constants.rs +++ b/src/library/constants.rs @@ -25,13 +25,13 @@ #![allow(missing_docs)] -pub const CODE_SEGMENT_MAX_LEN: usize = 1 << 16; +pub const CODE_SEGMENT_MAX_LEN: usize = 0xFFFF; -pub const DATA_SEGMENT_MAX_LEN: usize = 1 << 16; +pub const DATA_SEGMENT_MAX_LEN: usize = 0xFFFF; /// Maximum number of libraries that may be referenced (used by) any other library; i.e. limit for /// the number of records inside program segment. -pub const LIBS_SEGMENT_MAX_COUNT: usize = 1 << 8; +pub const LIBS_SEGMENT_MAX_COUNT: usize = 0xFF; /// Maximum total number of libraries which may be used by a single program; i.e. maximal number of /// nodes in a library dependency tree. diff --git a/src/library/lib.rs b/src/library/lib.rs index 3c6103c..dfec966 100644 --- a/src/library/lib.rs +++ b/src/library/lib.rs @@ -34,6 +34,7 @@ use amplify::confinement::SmallBlob; use amplify::{confinement, ByteArray, Bytes32}; use baid58::{Baid58ParseError, FromBaid58, ToBaid58}; use sha2::{Digest, Sha256}; +use strict_encoding::{StrictDeserialize, StrictSerialize}; #[cfg(feature = "ascii-armor")] pub use self::_armor::LibArmorError; @@ -137,6 +138,9 @@ pub struct Lib { pub libs: LibSeg, } +impl StrictSerialize for Lib {} +impl StrictDeserialize for Lib {} + impl Display for Lib { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { writeln!(f, "ISAE: {}", &self.isae)?; @@ -174,10 +178,11 @@ impl RustHash for Lib { #[cfg(feature = "ascii-armor")] mod _armor { + use amplify::confinement::{self, Confined, U24 as U24MAX}; use armor::{ArmorHeader, ArmorParseError, AsciiArmor, ASCII_ARMOR_ID}; + use strict_encoding::DeserializeError; use super::*; - use crate::data::encoding::{Decode, DecodeError, Encode}; const ASCII_ARMOR_ISAE: &str = "ISA-Extensions"; const ASCII_ARMOR_DEPENDENCY: &str = "Dependency"; @@ -190,9 +195,13 @@ mod _armor { #[from] Armor(ArmorParseError), + /// The provided data exceed maximum possible library size. + #[from(confinement::Error)] + TooLarge, + /// Library data deserialization error. #[from] - Decode(DecodeError), + Decode(DeserializeError), } impl AsciiArmor for Lib { @@ -210,11 +219,14 @@ mod _armor { headers } - fn to_ascii_armored_data(&self) -> Vec { self.serialize() } + fn to_ascii_armored_data(&self) -> Vec { + self.to_strict_serialized::().expect("type guarantees").to_vec() + } fn with_headers_data(_headers: Vec, data: Vec) -> Result { // TODO: check id, dependencies and ISAE - let me = Self::deserialize(data)?; + let data = Confined::try_from(data)?; + let me = Self::from_strict_serialized::(data)?; Ok(me) } } diff --git a/src/stl.rs b/src/stl.rs index af1932d..8e0d202 100644 --- a/src/stl.rs +++ b/src/stl.rs @@ -23,21 +23,25 @@ //! Strict types library generator methods. +use core::convert::TryFrom; + use strict_types::typelib::{CompileError, LibBuilder}; use strict_types::TypeLib; -use crate::library::LibSite; +use crate::library::{Lib, LibSite}; use crate::LIB_NAME_ALUVM; /// Strict type id for the library providing data types from this crate. pub const LIB_ID_ALUVM: &str = - "urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy"; + "urn:ubideco:stl:APYERRUMyWqLadwTv8tEFifHMPGpL3xGFSBxwaKYpmcV#square-mammal-uncle"; fn _aluvm_stl() -> Result { LibBuilder::new(libname!(LIB_NAME_ALUVM), tiny_bset! { + strict_types::stl::std_stl().to_dependency(), strict_types::stl::strict_types_stl().to_dependency() }) .transpile::() + .transpile::() .compile() } diff --git a/stl/AluVM@0.1.0.sta b/stl/AluVM@0.1.0.sta index 8dc829f..603e5a1 100644 --- a/stl/AluVM@0.1.0.sta +++ b/stl/AluVM@0.1.0.sta @@ -1,10 +1,19 @@ -----BEGIN STRICT TYPE LIB----- -Id: urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy +Id: urn:ubideco:stl:APYERRUMyWqLadwTv8tEFifHMPGpL3xGFSBxwaKYpmcV#square-mammal-uncle Name: AluVM -Checksum-SHA256: 225116c3d4fca2a386113db3fd9ffcd2952f3a5e7ea4f1391dd0a6fc139e97ec +Dependency: EcCNgrgLaygt3tCZNu2ZVEzMzSAZYEUeTNAVi5E81YWi#aspirin-mango-average +Checksum-SHA256: d0b37000d730beef023af7883b4664c10ecce81ab144c4ac0cc4a85687b96fac -1wm|eR!sl^0ssX}X<|ua1pxpD002NB00&HIVpC~!Wd;HRY-wTvr!Z9lE%{u?@QI^ -EqCb}2Q7OO^w+`_q*ddTXmHSf)18{G10006 +1wm|eR!srQEFN!zncXl9K5w2;FV{y1jDTJCC^p$-mHEbO0#qjhQ*>kj15cBr>9x-Cs#sheE97?nsOaXHkb)PY;b5{Lt$`p1^@?1b74+lZDj +=k00;ugEFN!zncXl9K5w2;FV{y1jDTJCC^p$-mHEbO0#qj_gwc#^4#qsMUl{*1zN +e>I^CwqAYJB+ZKALhJOg5MT00000000080000000006NpoRSWoHEe00{wQLP%5x# ++Iwz1XmoMZUe8>*JiqdOAW_7Xq?evcVyE5000000000$0000000003Ole{U1O#bw +VPydUTVY%eB()|DVs5)j?WBU==a=KzyTg3px>h43uaSoYV{c?-00;m8KmY&$0000 +00RR600000000d-VbYTDp002M$0000000030{{R3000004Y-wV10n*|aTPJY^=0y +A22XT>+T#q4YA)e%xCb6-wWELl;GBQRd#_+3bRpU*{+wJ{!Nz?u3(;wg5eYob~ zt}tbl^%L7!lJiqi8GyFJG&B7F4>5)%C9xzCyG%}IQZeJDM-tKMg{;q9e&1p*oH!*k z%xzNdo6SQ1=R_Syd13Z)1}o4No+&^NumcTOU|<7sVIEp;5Xz+cGv>sH&c&{a?5Eue w)I1=(o$sUi4aEtLQ-23@LH)r1bqNDINN;9IDjO3s&}v3P+L;UTiy0W0042|?U;qFB delta 32 ncmeyz>@UIUm{S_&%fP_I!0MBk