From a2d5d20c2af29863f5e0fb7daf97600ddd074219 Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 15:08:30 +0800 Subject: [PATCH 01/10] init --- sdk/Cargo.lock | 33 ++++++++++++++++++++++ sdk/Cargo.toml | 2 +- sdk/src/common/system.rs | 4 +-- sdk/src/common/types/cross_program_call.rs | 1 + sdk/src/common/types/poseidon2hash.rs | 1 + sdk/src/common/types/program_identifier.rs | 1 + sdk/src/common/types/raw_message.rs | 1 + 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/sdk/Cargo.lock b/sdk/Cargo.lock index 51acf0663..505fc56c2 100644 --- a/sdk/Cargo.lock +++ b/sdk/Cargo.lock @@ -60,6 +60,29 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytecheck" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58167cc4244105c1b50a4d0a9f4672107138def79b73755b74d21c0b807e7755" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "rancor", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41412be9f547e2c7aa8e444c935958d711ec34189b42a24208635cab44c07cd1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -422,6 +445,9 @@ name = "rend" version = "0.5.0-pre6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f02fc3227b019649985d2f89e254e345f027cc58af7bbf5faa4f3f7271bc4cc" +dependencies = [ + "bytecheck", +] [[package]] name = "rkyv" @@ -430,6 +456,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f11954ea56d7bfee4022704c89d997ca15659bff852ab06dbee5cbeffdc723ed" dependencies = [ "bitvec", + "bytecheck", "hashbrown", "ptr_meta", "rancor", @@ -497,6 +524,12 @@ dependencies = [ "serde", ] +[[package]] +name = "simdutf8" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" + [[package]] name = "slice-group-by" version = "0.3.1" diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 640617f28..15ce7d269 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -14,7 +14,7 @@ version = "0.2.0" # as dependency. itertools = { version = "0.12", default-features = false } once_cell = { version = "1.19", default-features = false, features = ["race"] } -rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } +rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] } rkyv_derive = "=0.8.0-alpha.1" slice-group-by = { version = "0.3", default-features = false, features = ["nightly"] } diff --git a/sdk/src/common/system.rs b/sdk/src/common/system.rs index 279694fca..afd84b2e6 100644 --- a/sdk/src/common/system.rs +++ b/sdk/src/common/system.rs @@ -102,9 +102,7 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType { let mut buf = Vec::with_capacity(len); call_tape_read(buf.as_mut_ptr(), len); - let archived_cpc_messages = unsafe { - rkyv::access_unchecked::>(&*slice_from_raw_parts(buf.as_ptr(), len)) - }; + let archived_cpc_messages = rkyv::access::, Panic>(&buf[..]).unwrap(); let cast_list: Vec = archived_cpc_messages .iter() diff --git a/sdk/src/common/types/cross_program_call.rs b/sdk/src/common/types/cross_program_call.rs index 5c68d1340..22e1a76c5 100644 --- a/sdk/src/common/types/cross_program_call.rs +++ b/sdk/src/common/types/cross_program_call.rs @@ -1,6 +1,7 @@ #[derive( Default, Clone, Hash, PartialEq, PartialOrd, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, )] +#[archive(check_bytes)] #[cfg_attr( not(target_os = "mozakvm"), derive(Debug, serde::Serialize, serde::Deserialize) diff --git a/sdk/src/common/types/poseidon2hash.rs b/sdk/src/common/types/poseidon2hash.rs index 31b4a00bd..3c086355e 100644 --- a/sdk/src/common/types/poseidon2hash.rs +++ b/sdk/src/common/types/poseidon2hash.rs @@ -20,6 +20,7 @@ use crate::core::constants::DIGEST_BYTES; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct Poseidon2Hash( #[cfg_attr(not(target_os = "mozakvm"), serde(with = "SerHex::"))] pub [u8; DIGEST_BYTES], diff --git a/sdk/src/common/types/program_identifier.rs b/sdk/src/common/types/program_identifier.rs index 2bfca38ed..d373da16e 100644 --- a/sdk/src/common/types/program_identifier.rs +++ b/sdk/src/common/types/program_identifier.rs @@ -17,6 +17,7 @@ use crate::core::constants::DIGEST_BYTES; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct ProgramIdentifier(pub super::Poseidon2Hash); impl ProgramIdentifier { diff --git a/sdk/src/common/types/raw_message.rs b/sdk/src/common/types/raw_message.rs index 40226d26e..2e7a13a58 100644 --- a/sdk/src/common/types/raw_message.rs +++ b/sdk/src/common/types/raw_message.rs @@ -9,6 +9,7 @@ use serde_hex::{SerHexSeq, StrictPfx}; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct RawMessage( #[cfg_attr(not(target_os = "mozakvm"), serde(with = "SerHexSeq::"))] pub Vec, ); From 205100b44b622a045cbca631fe51f527cc2d7fe3 Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 16:00:51 +0800 Subject: [PATCH 02/10] chore: replace all usages of `access_unchecked` with `access` This requires us to enable the `bytecheck` features of `rkyv` and makes use of the `CheckBytes` trait to do byte validation when dealing with `rkyv` types. In future, if we wish to bring back the unsafe API for those who want performance, we could feature gate that in our sdk. --- Cargo.lock | 33 +++++++++++++++++++++++++++ examples/Cargo.lock | 33 +++++++++++++++++++++++++++ examples/wallet/Cargo.toml | 2 +- examples/wallet/core_logic.rs | 10 ++++++-- sdk/src/common/system.rs | 29 +++++++++++++++-------- sdk/src/common/traits.rs | 13 ++++++++--- sdk/src/common/types/event.rs | 3 +++ sdk/src/common/types/state_address.rs | 1 + sdk/src/mozakvm/calltape.rs | 20 ++++++++++------ 9 files changed, 121 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e9bca4e78..c3dc3539b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -205,6 +205,29 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytecheck" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58167cc4244105c1b50a4d0a9f4672107138def79b73755b74d21c0b807e7755" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "rancor", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41412be9f547e2c7aa8e444c935958d711ec34189b42a24208635cab44c07cd1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "cast" version = "0.3.0" @@ -1443,6 +1466,9 @@ name = "rend" version = "0.5.0-pre6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f02fc3227b019649985d2f89e254e345f027cc58af7bbf5faa4f3f7271bc4cc" +dependencies = [ + "bytecheck", +] [[package]] name = "rkyv" @@ -1451,6 +1477,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f11954ea56d7bfee4022704c89d997ca15659bff852ab06dbee5cbeffdc723ed" dependencies = [ "bitvec", + "bytecheck", "hashbrown 0.14.5", "ptr_meta", "rancor", @@ -1632,6 +1659,12 @@ dependencies = [ "sha3", ] +[[package]] +name = "simdutf8" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" + [[package]] name = "sized-chunks" version = "0.6.5" diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 762b1830b..55dd49c6e 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -76,6 +76,29 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytecheck" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58167cc4244105c1b50a4d0a9f4672107138def79b73755b74d21c0b807e7755" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "rancor", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.8.0-pre6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41412be9f547e2c7aa8e444c935958d711ec34189b42a24208635cab44c07cd1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -536,6 +559,9 @@ name = "rend" version = "0.5.0-pre6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f02fc3227b019649985d2f89e254e345f027cc58af7bbf5faa4f3f7271bc4cc" +dependencies = [ + "bytecheck", +] [[package]] name = "rkyv" @@ -544,6 +570,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f11954ea56d7bfee4022704c89d997ca15659bff852ab06dbee5cbeffdc723ed" dependencies = [ "bitvec", + "bytecheck", "hashbrown", "ptr_meta", "rancor", @@ -640,6 +667,12 @@ dependencies = [ "digest", ] +[[package]] +name = "simdutf8" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" + [[package]] name = "slice-group-by" version = "0.3.1" diff --git a/examples/wallet/Cargo.toml b/examples/wallet/Cargo.toml index 811ffa730..6e067c8a9 100644 --- a/examples/wallet/Cargo.toml +++ b/examples/wallet/Cargo.toml @@ -9,7 +9,7 @@ std = [] # TODO: 😱 why this black magick works? [dependencies] mozak-sdk = { path = "../../sdk" } -rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } +rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] } rkyv_derive = "=0.8.0-alpha.1" [target.'cfg(not(target_os="mozakvm"))'.dependencies] diff --git a/examples/wallet/core_logic.rs b/examples/wallet/core_logic.rs index 8c3a3cb3b..3a2f279a9 100644 --- a/examples/wallet/core_logic.rs +++ b/examples/wallet/core_logic.rs @@ -3,7 +3,7 @@ extern crate alloc; use mozak_sdk::common::types::{Poseidon2Hash, ProgramIdentifier, StateObject}; -use rkyv::rancor::{Panic, Strategy}; +use rkyv::rancor::{Failure, Panic, Strategy}; use rkyv::{Archive, Deserialize, Serialize}; /// A generic private key used by the wallet. @@ -15,6 +15,7 @@ pub struct PrivateKey(pub [u8; 32]); /// a poseidon2 hash. #[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub struct PublicKey(pub Poseidon2Hash); impl From<[u8; 32]> for PrivateKey { @@ -38,6 +39,7 @@ impl PrivateKey { /// `TokenObject`. #[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub struct Amount(u64); impl From for Amount { @@ -47,6 +49,7 @@ impl From for Amount { /// A token object is represented in the `data` section of a `StateObject`, and /// contains information about the token that is being used in a program. #[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] +#[archive(check_bytes)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] pub struct TokenObject { /// The public key that is the economic owner of this `TokenObject`. @@ -57,7 +60,7 @@ pub struct TokenObject { impl From for TokenObject { fn from(value: StateObject) -> Self { - let archived = unsafe { rkyv::access_unchecked::(&value.data[..]) }; + let archived = rkyv::access::(&value.data[..]).unwrap(); let token_object: TokenObject = archived .deserialize(Strategy::<_, Panic>::wrap(&mut ())) .unwrap(); @@ -73,6 +76,7 @@ impl From for TokenObject { /// differentiate between transactions. #[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub struct BlackBox { pub remitter_program: ProgramIdentifier, pub remittee_program: ProgramIdentifier, @@ -95,11 +99,13 @@ impl BlackBox { #[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub enum MethodArgs { ApproveSignature(PublicKey, BlackBox), } #[derive(Archive, Debug, Deserialize, Serialize, PartialEq, Eq, Clone)] +#[archive(check_bytes)] pub enum MethodReturns { ApproveSignature(()), } diff --git a/sdk/src/common/system.rs b/sdk/src/common/system.rs index afd84b2e6..c10904810 100644 --- a/sdk/src/common/system.rs +++ b/sdk/src/common/system.rs @@ -1,5 +1,7 @@ use once_cell::unsync::Lazy; -use rkyv::rancor::{Panic, Strategy}; +use rkyv::bytecheck::CheckBytes; +use rkyv::rancor::{Failure, Panic, Strategy}; +use rkyv::validation::validators::DefaultValidator; use rkyv::Deserialize; #[cfg(target_os = "mozakvm")] use { @@ -102,7 +104,10 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType { let mut buf = Vec::with_capacity(len); call_tape_read(buf.as_mut_ptr(), len); - let archived_cpc_messages = rkyv::access::, Panic>(&buf[..]).unwrap(); + let archived_cpc_messages = rkyv::access::, Panic>(unsafe { + &*slice_from_raw_parts(buf.as_ptr(), len) + }) + .unwrap(); let cast_list: Vec = archived_cpc_messages .iter() @@ -135,12 +140,11 @@ fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType { let mut buf = Vec::with_capacity(len); event_tape_read(buf.as_mut_ptr(), len); - let canonical_ordered_temporal_hints = unsafe { - rkyv::access_unchecked::>(&*slice_from_raw_parts( - buf.as_ptr(), - len, - )) - }; + let canonical_ordered_temporal_hints = + rkyv::access::, Failure>(unsafe { + &*slice_from_raw_parts(buf.as_ptr(), len) + }) + .unwrap(); EventTapeType { self_prog_id, @@ -168,7 +172,9 @@ where A: CallArgument + PartialEq, R: CallReturn, ::Archived: Deserialize>, - ::Archived: Deserialize>, { + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: CheckBytes>, { unsafe { SYSTEM_TAPE.call_tape.receive() } } @@ -184,8 +190,11 @@ pub fn call_send( where A: CallArgument + PartialEq, R: CallReturn, + ::Archived: Deserialize>, - ::Archived: Deserialize>, { + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: CheckBytes>, { unsafe { SYSTEM_TAPE .call_tape diff --git a/sdk/src/common/traits.rs b/sdk/src/common/traits.rs index 3bc0c67d2..846b6d397 100644 --- a/sdk/src/common/traits.rs +++ b/sdk/src/common/traits.rs @@ -1,7 +1,9 @@ -use rkyv::rancor::{Panic, Strategy}; +use rkyv::bytecheck::CheckBytes; +use rkyv::rancor::{Failure, Panic, Strategy}; use rkyv::ser::allocator::{AllocationTracker, GlobalAllocator}; use rkyv::ser::{AllocSerializer, Composite}; use rkyv::util::AlignedVec; +use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize, Serialize}; use crate::common::types::{Event, ProgramIdentifier}; @@ -36,7 +38,9 @@ pub trait Call: SelfIdentify { A: CallArgument + PartialEq, R: CallReturn, ::Archived: Deserialize>, - ::Archived: Deserialize>; + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: CheckBytes>; /// `receive` emulates a function call directed towards the /// program, presents back with a three tuple of the form @@ -50,7 +54,10 @@ pub trait Call: SelfIdentify { A: CallArgument + PartialEq, R: CallReturn, ::Archived: Deserialize>, - ::Archived: Deserialize>; + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: + CheckBytes>; } /// `EventEmit` trait provides method `emit` to use the underlying diff --git a/sdk/src/common/types/event.rs b/sdk/src/common/types/event.rs index 271095aa7..6541d0fa7 100644 --- a/sdk/src/common/types/event.rs +++ b/sdk/src/common/types/event.rs @@ -19,6 +19,7 @@ use crate::native::poseidon::poseidon2_hash_no_pad; derive(Debug, serde::Serialize, serde::Deserialize) )] #[allow(clippy::module_name_repetitions)] +#[archive(check_bytes)] #[repr(u8)] pub enum EventType { Write = 0, @@ -61,6 +62,7 @@ pub struct Event { not(target_os = "mozakvm"), derive(Debug, serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] #[allow(clippy::module_name_repetitions)] pub struct CanonicalEvent { pub address: super::StateAddress, @@ -117,4 +119,5 @@ impl CanonicalEvent { not(target_os = "mozakvm"), derive(Debug, serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct CanonicalOrderedTemporalHints(pub CanonicalEvent, pub u32); diff --git a/sdk/src/common/types/state_address.rs b/sdk/src/common/types/state_address.rs index 83fe02a41..89e341938 100644 --- a/sdk/src/common/types/state_address.rs +++ b/sdk/src/common/types/state_address.rs @@ -20,6 +20,7 @@ pub const STATE_TREE_DEPTH: usize = 8; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct StateAddress( #[cfg_attr(not(target_os = "mozakvm"), serde(with = "SerHex::"))] pub [u8; STATE_TREE_DEPTH], diff --git a/sdk/src/mozakvm/calltape.rs b/sdk/src/mozakvm/calltape.rs index e1d175ad0..5ed7414c4 100644 --- a/sdk/src/mozakvm/calltape.rs +++ b/sdk/src/mozakvm/calltape.rs @@ -1,4 +1,6 @@ -use rkyv::rancor::{Panic, Strategy}; +use rkyv::bytecheck::CheckBytes; +use rkyv::rancor::{Failure, Panic, Strategy}; +use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize}; use crate::common::traits::{Call, CallArgument, CallReturn, SelfIdentify}; @@ -37,7 +39,9 @@ impl Call for CallTape { A: CallArgument + PartialEq, R: CallReturn, ::Archived: Deserialize>, - ::Archived: Deserialize>, { + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: CheckBytes>, { // Ensure we aren't validating past the length of the event tape assert!(self.index < self.reader.as_ref().unwrap().len()); @@ -52,7 +56,7 @@ impl Call for CallTape { assert!(self.is_casted_actor(&recipient_program)); // Deserialize the `arguments` seen on the tape, and assert - let zcd_args = unsafe { rkyv::access_unchecked::(&cpcmsg.argument.0[..]) }; + let zcd_args = rkyv::access::(&cpcmsg.argument.0[..]).unwrap(); let deserialized_args = <::Archived as Deserialize>>::deserialize( zcd_args, @@ -67,7 +71,7 @@ impl Call for CallTape { // Return the claimed return value as seen on the tape // It remains that specific program's prerogative to ensure // that the return value used here is according to expectation - let zcd_ret = unsafe { rkyv::access_unchecked::(&cpcmsg.return_.0[..]) }; + let zcd_ret = rkyv::access::(&cpcmsg.return_.0[..]).unwrap(); <::Archived as Deserialize>>::deserialize( zcd_ret, Strategy::wrap(&mut ()), @@ -81,7 +85,9 @@ impl Call for CallTape { A: CallArgument + PartialEq, R: CallReturn, ::Archived: Deserialize>, - ::Archived: Deserialize>, { + ::Archived: CheckBytes>, + ::Archived: Deserialize>, + ::Archived: CheckBytes>, { // Loop until we completely traverse the call tape in the // worst case. Hopefully, we see a message directed towards us // before the end @@ -115,11 +121,11 @@ impl Call for CallTape { assert!(self.is_casted_actor(&caller)); let archived_args = - unsafe { rkyv::access_unchecked::(zcd_cpcmsg.argument.0.as_slice()) }; + rkyv::access::(zcd_cpcmsg.argument.0.as_slice()).unwrap(); let args: A = archived_args.deserialize(Strategy::wrap(&mut ())).unwrap(); let archived_ret = - unsafe { rkyv::access_unchecked::(zcd_cpcmsg.return_.0.as_slice()) }; + rkyv::access::(zcd_cpcmsg.return_.0.as_slice()).unwrap(); let ret: R = archived_ret.deserialize(Strategy::wrap(&mut ())).unwrap(); return Some((caller, args, ret)); From 7535673514f1b7bb1a21ab50ddac34ba04484b78 Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 16:35:04 +0800 Subject: [PATCH 03/10] cleaner where clauses --- sdk/src/common/traits.rs | 18 +++++++++--------- sdk/src/mozakvm/calltape.rs | 19 ++++++++----------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/sdk/src/common/traits.rs b/sdk/src/common/traits.rs index 846b6d397..681787f56 100644 --- a/sdk/src/common/traits.rs +++ b/sdk/src/common/traits.rs @@ -12,6 +12,11 @@ pub trait RkyvSerializable = rkyv::Serialize< Strategy, Panic>, Panic>, > + Serialize, Panic>>; pub trait CallArgument = Sized + RkyvSerializable; +pub trait ArchivedCallArgument = + Deserialize> + CheckBytes>; +pub trait ArchivedCallReturn = + Deserialize> + CheckBytes>; + pub trait CallReturn = ?Sized + Clone + Default + RkyvSerializable + Archive; /// A data struct that is aware of it's own ID @@ -37,10 +42,8 @@ pub trait Call: SelfIdentify { where A: CallArgument + PartialEq, R: CallReturn, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: CheckBytes>; + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn; /// `receive` emulates a function call directed towards the /// program, presents back with a three tuple of the form @@ -53,11 +56,8 @@ pub trait Call: SelfIdentify { where A: CallArgument + PartialEq, R: CallReturn, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: - CheckBytes>; + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn; } /// `EventEmit` trait provides method `emit` to use the underlying diff --git a/sdk/src/mozakvm/calltape.rs b/sdk/src/mozakvm/calltape.rs index 5ed7414c4..fd833794b 100644 --- a/sdk/src/mozakvm/calltape.rs +++ b/sdk/src/mozakvm/calltape.rs @@ -1,9 +1,9 @@ -use rkyv::bytecheck::CheckBytes; use rkyv::rancor::{Failure, Panic, Strategy}; -use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize}; -use crate::common::traits::{Call, CallArgument, CallReturn, SelfIdentify}; +use crate::common::traits::{ + ArchivedCallArgument, ArchivedCallReturn, Call, CallArgument, CallReturn, SelfIdentify, +}; use crate::common::types::{CrossProgramCall, ProgramIdentifier}; /// Represents the `CallTape` under `mozak-vm` @@ -38,10 +38,9 @@ impl Call for CallTape { where A: CallArgument + PartialEq, R: CallReturn, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, { + + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn, { // Ensure we aren't validating past the length of the event tape assert!(self.index < self.reader.as_ref().unwrap().len()); @@ -84,10 +83,8 @@ impl Call for CallTape { where A: CallArgument + PartialEq, R: CallReturn, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, { + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn, { // Loop until we completely traverse the call tape in the // worst case. Hopefully, we see a message directed towards us // before the end From 7ee0c6a7527dc094e12c9f107625d593e1981073 Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 16:38:39 +0800 Subject: [PATCH 04/10] use archived types --- sdk/src/common/system.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/sdk/src/common/system.rs b/sdk/src/common/system.rs index c10904810..bd24e5994 100644 --- a/sdk/src/common/system.rs +++ b/sdk/src/common/system.rs @@ -1,7 +1,5 @@ use once_cell::unsync::Lazy; -use rkyv::bytecheck::CheckBytes; use rkyv::rancor::{Failure, Panic, Strategy}; -use rkyv::validation::validators::DefaultValidator; use rkyv::Deserialize; #[cfg(target_os = "mozakvm")] use { @@ -17,7 +15,9 @@ use { #[cfg(not(target_os = "mozakvm"))] use {core::cell::RefCell, std::rc::Rc}; -use crate::common::traits::{Call, CallArgument, CallReturn, EventEmit}; +use crate::common::traits::{ + ArchivedCallArgument, ArchivedCallReturn, Call, CallArgument, CallReturn, EventEmit, +}; use crate::common::types::{ CallTapeType, Event, EventTapeType, PrivateInputTapeType, ProgramIdentifier, PublicInputTapeType, SystemTape, @@ -171,10 +171,8 @@ pub fn call_receive() -> Option<(ProgramIdentifier, A, R)> where A: CallArgument + PartialEq, R: CallReturn, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, { + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn, { unsafe { SYSTEM_TAPE.call_tape.receive() } } @@ -190,11 +188,8 @@ pub fn call_send( where A: CallArgument + PartialEq, R: CallReturn, - - ::Archived: Deserialize>, - ::Archived: CheckBytes>, - ::Archived: Deserialize>, - ::Archived: CheckBytes>, { + ::Archived: ArchivedCallArgument, + ::Archived: ArchivedCallReturn, { unsafe { SYSTEM_TAPE .call_tape From 6f277914b42c15b21ab9eeeecc8a8eaaf4dd6239 Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 16:41:59 +0800 Subject: [PATCH 05/10] derive check bytes in inputtape and token examples --- examples/inputtape/Cargo.toml | 2 +- examples/inputtape/core_logic.rs | 2 ++ examples/token/Cargo.toml | 2 +- examples/token/core_logic.rs | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/inputtape/Cargo.toml b/examples/inputtape/Cargo.toml index 7f7f0bee6..277235bdb 100644 --- a/examples/inputtape/Cargo.toml +++ b/examples/inputtape/Cargo.toml @@ -9,7 +9,7 @@ std = [] # TODO: 😱 why this black magick works? [dependencies] mozak-sdk = { path = "../../sdk" } -rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } +rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] } rkyv_derive = "=0.8.0-alpha.1" [target.'cfg(not(target_os="mozakvm"))'.dependencies.hex] diff --git a/examples/inputtape/core_logic.rs b/examples/inputtape/core_logic.rs index 1e2f98a2f..a59ad30a0 100644 --- a/examples/inputtape/core_logic.rs +++ b/examples/inputtape/core_logic.rs @@ -5,12 +5,14 @@ use rkyv::{Archive, Deserialize, Serialize}; #[derive(Archive, Deserialize, Serialize, PartialEq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub enum MethodArgs { RawTapesTest, } #[derive(Archive, Default, Deserialize, Serialize, PartialEq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub enum MethodReturns { #[default] Noop, diff --git a/examples/token/Cargo.toml b/examples/token/Cargo.toml index 9d1140c64..818c97f75 100644 --- a/examples/token/Cargo.toml +++ b/examples/token/Cargo.toml @@ -9,7 +9,7 @@ std = [] # TODO: 😱 why this black magick works? [dependencies] mozak-sdk = { path = "../../sdk" } -rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } +rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] } rkyv_derive = "=0.8.0-alpha.1" wallet = { path = "../wallet" } diff --git a/examples/token/core_logic.rs b/examples/token/core_logic.rs index aadce2282..4e9f6f042 100644 --- a/examples/token/core_logic.rs +++ b/examples/token/core_logic.rs @@ -7,6 +7,7 @@ use rkyv::{Archive, Deserialize, Serialize}; #[derive(Archive, Deserialize, Serialize, PartialEq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub enum MethodArgs { // Mint, // Burn, @@ -22,6 +23,7 @@ pub enum MethodArgs { #[derive(Archive, Default, Deserialize, Serialize, PartialEq, Clone)] #[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] +#[archive(check_bytes)] pub enum MethodReturns { // TODO: Remove later #[default] From 39aacd9446a915b8a275754e298a8060cdf5b0fa Mon Sep 17 00:00:00 2001 From: bing Date: Mon, 27 May 2024 16:45:17 +0800 Subject: [PATCH 06/10] sdk: fix more rkyv --- sdk/src/common/system.rs | 5 ++--- sdk/src/common/types/state_object.rs | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/src/common/system.rs b/sdk/src/common/system.rs index bd24e5994..f5e90960f 100644 --- a/sdk/src/common/system.rs +++ b/sdk/src/common/system.rs @@ -1,6 +1,4 @@ use once_cell::unsync::Lazy; -use rkyv::rancor::{Failure, Panic, Strategy}; -use rkyv::Deserialize; #[cfg(target_os = "mozakvm")] use { crate::common::merkle::merkleize, @@ -10,6 +8,7 @@ use { call_tape_read, event_tape_read, ioread_private, ioread_public, self_prog_id_tape_read, }, core::ptr::slice_from_raw_parts, + rkyv::rancor::{Panic, Strategy}, std::collections::BTreeSet, }; #[cfg(not(target_os = "mozakvm"))] @@ -141,7 +140,7 @@ fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType { event_tape_read(buf.as_mut_ptr(), len); let canonical_ordered_temporal_hints = - rkyv::access::, Failure>(unsafe { + rkyv::access::, Panic>(unsafe { &*slice_from_raw_parts(buf.as_ptr(), len) }) .unwrap(); diff --git a/sdk/src/common/types/state_object.rs b/sdk/src/common/types/state_object.rs index 91a879dc9..372fd7049 100644 --- a/sdk/src/common/types/state_object.rs +++ b/sdk/src/common/types/state_object.rs @@ -8,6 +8,7 @@ use serde_hex::{SerHexSeq, StrictPfx}; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] +#[archive(check_bytes)] pub struct StateObject { pub address: super::StateAddress, pub constraint_owner: super::ProgramIdentifier, From 74892c8a407acaa282f4f3b925809a7f6d7e3531 Mon Sep 17 00:00:00 2001 From: bing Date: Fri, 31 May 2024 10:35:35 +0800 Subject: [PATCH 07/10] fix derives --- sdk/src/common/types/poseidon2hash.rs | 1 - sdk/src/common/types/program_identifier.rs | 1 - sdk/src/common/types/raw_message.rs | 1 - 3 files changed, 3 deletions(-) diff --git a/sdk/src/common/types/poseidon2hash.rs b/sdk/src/common/types/poseidon2hash.rs index 59b78ad95..6351f01b8 100644 --- a/sdk/src/common/types/poseidon2hash.rs +++ b/sdk/src/common/types/poseidon2hash.rs @@ -21,7 +21,6 @@ use crate::core::constants::DIGEST_BYTES; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] -#[archive(check_bytes)] #[archive_attr(derive(Debug))] pub struct Poseidon2Hash( #[cfg_attr(not(target_os = "mozakvm"), serde(with = "SerHex::"))] diff --git a/sdk/src/common/types/program_identifier.rs b/sdk/src/common/types/program_identifier.rs index f9171999c..94cff0dfc 100644 --- a/sdk/src/common/types/program_identifier.rs +++ b/sdk/src/common/types/program_identifier.rs @@ -18,7 +18,6 @@ use crate::core::constants::DIGEST_BYTES; not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) )] -#[archive(check_bytes)] #[archive_attr(derive(Debug))] pub struct ProgramIdentifier(pub super::Poseidon2Hash); diff --git a/sdk/src/common/types/raw_message.rs b/sdk/src/common/types/raw_message.rs index 9aa904f6c..3db845a8d 100644 --- a/sdk/src/common/types/raw_message.rs +++ b/sdk/src/common/types/raw_message.rs @@ -5,7 +5,6 @@ use serde_hex::{SerHexSeq, StrictPfx}; #[derive( Default, Clone, Hash, PartialEq, PartialOrd, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, )] -#[archive(check_bytes)] #[cfg_attr( not(target_os = "mozakvm"), derive(serde::Serialize, serde::Deserialize) From ee63bb89b1d83af58861245c231c417e4300c00e Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Tue, 4 Jun 2024 15:04:34 +0800 Subject: [PATCH 08/10] Fix program ids --- examples/token/Cargo.toml | 2 +- examples/wallet/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/token/Cargo.toml b/examples/token/Cargo.toml index 818c97f75..ad3b1ba27 100644 --- a/examples/token/Cargo.toml +++ b/examples/token/Cargo.toml @@ -33,4 +33,4 @@ path = "core_logic.rs" # The following is read by `run_examples.py` [package.metadata.mozak] example_dependents = ["wallet"] -example_program_id = "MZK-b10da48cea4c09676b8e0efcd806941465060736032bb898420d0863dca72538" +example_program_id = "MZK-63236d3b0bc73b9cb18ab2aacbbcf741b84d0560e00172374ddfcffea7b409cc" diff --git a/examples/wallet/Cargo.toml b/examples/wallet/Cargo.toml index 6e067c8a9..c0438c859 100644 --- a/examples/wallet/Cargo.toml +++ b/examples/wallet/Cargo.toml @@ -32,4 +32,4 @@ path = "core_logic.rs" # The following is read by `run_examples.py` [package.metadata.mozak] -example_program_id = "MZK-c51b8a31c98b9fe13065b485c9f8658c194c430843570ccac2720a3b30b47adb" +example_program_id = "MZK-0308983e67e0432433d33ae9eb714a2ebd97c4e0044eb09d01a8ee02b72bbe27" From 75e7311d3a2b46a9582db389fa4f7da789e0e1b4 Mon Sep 17 00:00:00 2001 From: bing Date: Tue, 11 Jun 2024 14:26:35 +0800 Subject: [PATCH 09/10] fix relative path --- examples/token/mozakvm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/token/mozakvm/Cargo.toml b/examples/token/mozakvm/Cargo.toml index 6422dd707..904d74821 100644 --- a/examples/token/mozakvm/Cargo.toml +++ b/examples/token/mozakvm/Cargo.toml @@ -5,7 +5,7 @@ name = "token-mozakvm" version = "0.1.0" [dependencies] -mozak-sdk = { path = "../../sdk" } +mozak-sdk = { path = "../../../sdk" } rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc", "bytecheck"] } rkyv_derive = "=0.8.0-alpha.1" token-core-logic = { path = "../core-logic" } From f30b4dca54b348e52f06d668763b05307c6dc146 Mon Sep 17 00:00:00 2001 From: bing Date: Tue, 11 Jun 2024 14:35:45 +0800 Subject: [PATCH 10/10] remove tapes wrongfully added --- .../merkleproof-trustedroot/private_input.tape | Bin 464 -> 0 bytes .../merkleproof-trustedroot/public_input.tape | Bin 66 -> 0 bytes examples/wallet/mozakvm/Cargo.lock | 5 +++-- 3 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 examples/merkleproof-trustedroot/private_input.tape delete mode 100644 examples/merkleproof-trustedroot/public_input.tape diff --git a/examples/merkleproof-trustedroot/private_input.tape b/examples/merkleproof-trustedroot/private_input.tape deleted file mode 100644 index 834d93d765f5267562a7686ab11163d090f9e9af..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 464 zcmaFDz`(!^#4JGE9RGid%9|VKA~ppX-HnSWxGdWM1OM zy${^@H8Pwh1y<=e7cXUTP!ZrPJyytT$`Ep5=X5GWWRj5(7<{U5tqv1PgaDX?QKwO`D?67xS|#`Sm(KFRpm! g8M!w; diff --git a/examples/merkleproof-trustedroot/public_input.tape b/examples/merkleproof-trustedroot/public_input.tape deleted file mode 100644 index ec0cfb5e24904336d072a473764805b5ab7bba86..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 66 tcmZSBrtpO0;Ooaz)ECa#*QuuEer3(f{}aQs%Og_dLYKb|dc#0S0RUafA&~$8 diff --git a/examples/wallet/mozakvm/Cargo.lock b/examples/wallet/mozakvm/Cargo.lock index cd5540bd6..d2b962a8f 100644 --- a/examples/wallet/mozakvm/Cargo.lock +++ b/examples/wallet/mozakvm/Cargo.lock @@ -29,8 +29,9 @@ checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "array-concat" -version = "0.5.2" -source = "git+https://github.com/Daniel-Aaron-Bloom/array-concat.git#c929d49bc0f3c5f7e25d87fb940ece830e87e8da" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b4d2c47ea522f4135657904891e533727daca3d2d852f29f5e4cc50960c77c" [[package]] name = "array-init"