From 70fe42f8b153c011e4db08c9243d9277403e35ba Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 15 Oct 2024 17:55:00 +0300 Subject: [PATCH 1/2] refactor!: rename `miden-sdk` crate to `miden` #338 --- Cargo.lock | 18 ++--- sdk/sdk/Cargo.toml | 2 +- tests/integration/src/compiler_test.rs | 6 +- .../rust-sdk/account-test/Cargo.lock | 10 +-- .../rust-sdk/account-test/Cargo.toml | 2 +- .../rust-sdk/account-test/src/lib.rs | 2 +- .../rust-sdk/basic-wallet/Cargo.lock | 10 +-- .../rust-sdk/basic-wallet/Cargo.toml | 2 +- .../rust-sdk/basic-wallet/src/bindings.rs | 66 +++++++++---------- .../rust-sdk/basic-wallet/src/lib.rs | 4 +- tools/cargo-miden/src/commands/new_project.rs | 2 +- tools/cargo-miden/src/lib.rs | 14 ++-- 12 files changed, 69 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8cb7818c1..c8d0fdcd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2962,6 +2962,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miden" +version = "0.0.7" +dependencies = [ + "miden-base-sys", + "miden-sdk-alloc", + "miden-stdlib-sys", +] + [[package]] name = "miden-air" version = "0.10.5" @@ -3140,15 +3149,6 @@ dependencies = [ "winter-prover", ] -[[package]] -name = "miden-sdk" -version = "0.0.7" -dependencies = [ - "miden-base-sys", - "miden-sdk-alloc", - "miden-stdlib-sys", -] - [[package]] name = "miden-sdk-alloc" version = "0.0.7" diff --git a/sdk/sdk/Cargo.toml b/sdk/sdk/Cargo.toml index 4e8550ea6..de6848f3b 100644 --- a/sdk/sdk/Cargo.toml +++ b/sdk/sdk/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "miden-sdk" +name = "miden" description = "Miden SDK" version.workspace = true rust-version.workspace = true diff --git a/tests/integration/src/compiler_test.rs b/tests/integration/src/compiler_test.rs index f89ea49bd..6df94dbc0 100644 --- a/tests/integration/src/compiler_test.rs +++ b/tests/integration/src/compiler_test.rs @@ -842,7 +842,7 @@ authors = [] [dependencies] miden-sdk-alloc = {{ path = "{sdk_alloc_path}" }} -miden-sdk = {{ path = "{sdk_path}" }} +miden = {{ path = "{sdk_path}" }} [lib] crate-type = ["cdylib"] @@ -873,8 +873,8 @@ fn my_panic(_info: &core::panic::PanicInfo) -> ! {{ #[global_allocator] static ALLOC: miden_sdk_alloc::BumpAlloc = miden_sdk_alloc::BumpAlloc::new(); -extern crate miden_sdk; -use miden_sdk::*; +extern crate miden; +use miden::*; extern crate alloc; use alloc::vec::Vec; diff --git a/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.lock b/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.lock index f926dcfa4..46e62894d 100644 --- a/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.lock +++ b/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.lock @@ -21,18 +21,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" [[package]] -name = "miden-base-sys" +name = "miden" version = "0.0.7" dependencies = [ + "miden-base-sys", + "miden-sdk-alloc", "miden-stdlib-sys", ] [[package]] -name = "miden-sdk" +name = "miden-base-sys" version = "0.0.7" dependencies = [ - "miden-base-sys", - "miden-sdk-alloc", "miden-stdlib-sys", ] @@ -40,7 +40,7 @@ dependencies = [ name = "miden-sdk-account-test" version = "0.0.0" dependencies = [ - "miden-sdk", + "miden", "wee_alloc", ] diff --git a/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.toml b/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.toml index 2b8a9e198..fd45da9ad 100644 --- a/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.toml +++ b/tests/rust-apps-wasm/rust-sdk/account-test/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" crate-type = ["cdylib", "rlib"] [dependencies] -miden-sdk = { path = "../../../../sdk/sdk" } +miden = { path = "../../../../sdk/sdk" } wee_alloc = { version = "0.4.5", default-features = false } [profile.release] diff --git a/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs b/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs index e0c0602da..ebaea63fa 100644 --- a/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs +++ b/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs @@ -1,6 +1,6 @@ extern crate alloc; -use miden_sdk::*; +use miden::*; #[global_allocator] static ALLOC: BumpAlloc = BumpAlloc::new(); diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.lock b/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.lock index 12a87cc27..f457071dc 100644 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.lock +++ b/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.lock @@ -6,23 +6,23 @@ version = 3 name = "basic_wallet" version = "0.1.0" dependencies = [ - "miden-sdk", + "miden", "wit-bindgen-rt", ] [[package]] -name = "miden-base-sys" +name = "miden" version = "0.0.7" dependencies = [ + "miden-base-sys", + "miden-sdk-alloc", "miden-stdlib-sys", ] [[package]] -name = "miden-sdk" +name = "miden-base-sys" version = "0.0.7" dependencies = [ - "miden-base-sys", - "miden-sdk-alloc", "miden-stdlib-sys", ] diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.toml b/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.toml index d7e94a69b..56f8d6abe 100644 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.toml +++ b/tests/rust-apps-wasm/rust-sdk/basic-wallet/Cargo.toml @@ -11,7 +11,7 @@ crate-type = ["cdylib"] [dependencies] # Miden SDK consists of a stdlib (intrinsic functions for VM ops, stdlib functions and types) # and transaction kernel API for the Miden rollup -miden-sdk = { path = "../../../../sdk/sdk" } +miden = { path = "../../../../sdk/sdk" } wit-bindgen-rt = "0.28" diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/bindings.rs b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/bindings.rs index 55c9c577d..012d5d8a4 100644 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/bindings.rs +++ b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/bindings.rs @@ -64,13 +64,13 @@ pub mod exports { arg3: f32, ) { #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); - T::receive_asset(miden_sdk::CoreAsset { - inner: miden_sdk::Word { + T::receive_asset(miden::CoreAsset { + inner: miden::Word { inner: ( - miden_sdk::Felt { inner: arg0 }, - miden_sdk::Felt { inner: arg1 }, - miden_sdk::Felt { inner: arg2 }, - miden_sdk::Felt { inner: arg3 }, + miden::Felt { inner: arg0 }, + miden::Felt { inner: arg1 }, + miden::Felt { inner: arg2 }, + miden::Felt { inner: arg3 }, ), }, }); @@ -91,29 +91,29 @@ pub mod exports { ) { #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); T::send_asset( - miden_sdk::CoreAsset { - inner: miden_sdk::Word { + miden::CoreAsset { + inner: miden::Word { inner: ( - miden_sdk::Felt { inner: arg0 }, - miden_sdk::Felt { inner: arg1 }, - miden_sdk::Felt { inner: arg2 }, - miden_sdk::Felt { inner: arg3 }, + miden::Felt { inner: arg0 }, + miden::Felt { inner: arg1 }, + miden::Felt { inner: arg2 }, + miden::Felt { inner: arg3 }, ), }, }, - miden_sdk::Tag { - inner: miden_sdk::Felt { inner: arg4 }, + miden::Tag { + inner: miden::Felt { inner: arg4 }, }, - miden_sdk::NoteType { - inner: miden_sdk::Felt { inner: arg5 }, + miden::NoteType { + inner: miden::Felt { inner: arg5 }, }, - miden_sdk::Recipient { - inner: miden_sdk::Word { + miden::Recipient { + inner: miden::Word { inner: ( - miden_sdk::Felt { inner: arg6 }, - miden_sdk::Felt { inner: arg7 }, - miden_sdk::Felt { inner: arg8 }, - miden_sdk::Felt { inner: arg9 }, + miden::Felt { inner: arg6 }, + miden::Felt { inner: arg7 }, + miden::Felt { inner: arg8 }, + miden::Felt { inner: arg9 }, ), }, }, @@ -127,10 +127,10 @@ pub mod exports { ) -> f32 { #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); let result0 = T::test_felt_intrinsics( - miden_sdk::Felt { inner: arg0 }, - miden_sdk::Felt { inner: arg1 }, + miden::Felt { inner: arg0 }, + miden::Felt { inner: arg1 }, ); - let miden_sdk::Felt { inner: inner1 } = result0; + let miden::Felt { inner: inner1 } = result0; _rt::as_f32(inner1) } #[doc(hidden)] @@ -163,17 +163,17 @@ pub mod exports { _rt::cabi_dealloc(base2, len2 * 1, 1); } pub trait Guest { - fn receive_asset(core_asset: miden_sdk::CoreAsset); + fn receive_asset(core_asset: miden::CoreAsset); fn send_asset( - core_asset: miden_sdk::CoreAsset, - tag: miden_sdk::Tag, - note_type: miden_sdk::NoteType, - recipient: miden_sdk::Recipient, + core_asset: miden::CoreAsset, + tag: miden::Tag, + note_type: miden::NoteType, + recipient: miden::Recipient, ); fn test_felt_intrinsics( - a: miden_sdk::Felt, - b: miden_sdk::Felt, - ) -> miden_sdk::Felt; + a: miden::Felt, + b: miden::Felt, + ) -> miden::Felt; fn test_stdlib(input: _rt::Vec) -> _rt::Vec; } #[doc(hidden)] diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs index daf5c2ef5..d7d124932 100644 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs +++ b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs @@ -9,7 +9,7 @@ use alloc::vec::Vec; // Global allocator to use heap memory in no-std environment #[global_allocator] -static ALLOC: miden_sdk::BumpAlloc = miden_sdk::BumpAlloc::new(); +static ALLOC: miden::BumpAlloc = miden::BumpAlloc::new(); // Required for no-std crates #[panic_handler] @@ -22,7 +22,7 @@ bindings::export!(MyAccount with_types_in bindings); mod bindings; use bindings::exports::miden::basic_wallet::basic_wallet::Guest; -use miden_sdk::{ +use miden::{ add_asset, blake3_hash_1to1, create_note, remove_asset, CoreAsset, Felt, NoteType, Recipient, Tag, }; diff --git a/tools/cargo-miden/src/commands/new_project.rs b/tools/cargo-miden/src/commands/new_project.rs index c4c26faff..429648d09 100644 --- a/tools/cargo-miden/src/commands/new_project.rs +++ b/tools/cargo-miden/src/commands/new_project.rs @@ -74,7 +74,7 @@ impl NewCommand { }, None => TemplatePath { git: Some("https://github.com/0xPolygonMiden/rust-templates".into()), - tag: Some("v0.4.0".into()), + tag: Some("v0.5.0".into()), auto_path: Some("account".into()), ..Default::default() }, diff --git a/tools/cargo-miden/src/lib.rs b/tools/cargo-miden/src/lib.rs index 3f5b095f1..44c7290db 100644 --- a/tools/cargo-miden/src/lib.rs +++ b/tools/cargo-miden/src/lib.rs @@ -173,13 +173,13 @@ where for package in packages.iter_mut() { package.metadata.section.bindings.with = [ - ("miden:base/core-types@1.0.0/felt", "miden_sdk::Felt"), - ("miden:base/core-types@1.0.0/word", "miden_sdk::Word"), - ("miden:base/core-types@1.0.0/core-asset", "miden_sdk::CoreAsset"), - ("miden:base/core-types@1.0.0/account-id", "miden_sdk::AccountId"), - ("miden:base/core-types@1.0.0/tag", "miden_sdk::Tag"), - ("miden:base/core-types@1.0.0/note-type", "miden_sdk::NoteType"), - ("miden:base/core-types@1.0.0/recipient", "miden_sdk::Recipient"), + ("miden:base/core-types@1.0.0/felt", "miden::Felt"), + ("miden:base/core-types@1.0.0/word", "miden::Word"), + ("miden:base/core-types@1.0.0/core-asset", "miden::CoreAsset"), + ("miden:base/core-types@1.0.0/account-id", "miden::AccountId"), + ("miden:base/core-types@1.0.0/tag", "miden::Tag"), + ("miden:base/core-types@1.0.0/note-type", "miden::NoteType"), + ("miden:base/core-types@1.0.0/recipient", "miden::Recipient"), ] .into_iter() .map(|(k, v)| (k.to_string(), v.to_string())) From 6f3d32a4e41c5dfa33b49a9155360dbc686cd63e Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Wed, 16 Oct 2024 13:11:16 +0300 Subject: [PATCH 2/2] refactor!: revamp Miden SDK API and expose some modules; --- sdk/base-sys/src/bindings/account.rs | 63 +++++++ sdk/base-sys/src/bindings/mod.rs | 5 + sdk/base-sys/src/bindings/note.rs | 41 ++++ sdk/base-sys/src/bindings/tx.rs | 45 +++++ sdk/base-sys/src/bindings/tx/externs.rs | 37 ---- sdk/base-sys/src/bindings/tx/mod.rs | 116 ------------ sdk/base-sys/src/bindings/{tx => }/types.rs | 0 sdk/base-sys/src/lib.rs | 1 + sdk/sdk/src/lib.rs | 2 +- sdk/stdlib-sys/src/intrinsics/mod.rs | 7 +- sdk/stdlib-sys/src/intrinsics/word.rs | 2 +- sdk/stdlib-sys/src/lib.rs | 2 +- sdk/stdlib-sys/src/stdlib/crypto/dsa.rs | 2 +- sdk/stdlib-sys/src/stdlib/crypto/hashes.rs | 3 +- sdk/stdlib-sys/src/stdlib/mem.rs | 2 +- sdk/stdlib-sys/src/stdlib/mod.rs | 2 - .../abi_transform_tx_kernel_get_inputs_4.hir | 4 +- .../abi_transform_tx_kernel_get_inputs_4.masm | 4 +- .../abi_transform_tx_kernel_get_inputs_4.wat | 8 +- .../expected/rust_sdk/basic_wallet.hir | 8 +- .../expected/rust_sdk/basic_wallet.masm | 8 +- .../expected/rust_sdk/basic_wallet.wat | 20 +- .../miden_sdk_account_test.hir | 178 +++++++++--------- .../miden_sdk_account_test.wat | 80 ++++---- tests/integration/src/compiler_test.rs | 3 + .../abi_transform/tx_kernel.rs | 2 +- .../rust-sdk/account-test/src/lib.rs | 12 +- .../rust-sdk/basic-wallet/README.md | 19 -- .../rust-sdk/basic-wallet/rust-toolchain.toml | 5 - .../rust-sdk/basic-wallet/src/lib.rs | 11 +- 30 files changed, 335 insertions(+), 357 deletions(-) create mode 100644 sdk/base-sys/src/bindings/account.rs create mode 100644 sdk/base-sys/src/bindings/note.rs create mode 100644 sdk/base-sys/src/bindings/tx.rs delete mode 100644 sdk/base-sys/src/bindings/tx/externs.rs delete mode 100644 sdk/base-sys/src/bindings/tx/mod.rs rename sdk/base-sys/src/bindings/{tx => }/types.rs (100%) delete mode 100644 tests/rust-apps-wasm/rust-sdk/basic-wallet/README.md delete mode 100644 tests/rust-apps-wasm/rust-sdk/basic-wallet/rust-toolchain.toml diff --git a/sdk/base-sys/src/bindings/account.rs b/sdk/base-sys/src/bindings/account.rs new file mode 100644 index 000000000..ff21c0c8a --- /dev/null +++ b/sdk/base-sys/src/bindings/account.rs @@ -0,0 +1,63 @@ +use miden_stdlib_sys::Felt; + +use super::types::{AccountId, CoreAsset}; + +#[allow(improper_ctypes)] +#[link(wasm_import_module = "miden:core-import/account@1.0.0")] +extern "C" { + #[link_name = "get-id"] + pub fn extern_account_get_id() -> AccountId; + #[link_name = "add-asset"] + pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); + #[link_name = "remove-asset"] + pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); +} + +/// Get the account ID of the currently executing note account. +pub fn get_id() -> AccountId { + unsafe { extern_account_get_id() } +} + +/// Add the specified asset to the vault. +/// Returns the final asset in the account vault defined as follows: If asset is +/// a non-fungible asset, then returns the same as asset. If asset is a +/// fungible asset, then returns the total fungible asset in the account +/// vault after asset was added to it. +/// +/// Panics: +/// - If the asset is not valid. +/// - If the total value of two fungible assets is greater than or equal to 2^63. +/// - If the vault already contains the same non-fungible asset. +pub fn add_asset(asset: CoreAsset) -> CoreAsset { + unsafe { + let mut ret_area = ::core::mem::MaybeUninit::::uninit(); + extern_account_add_asset( + asset.inner[0], + asset.inner[1], + asset.inner[2], + asset.inner[3], + ret_area.as_mut_ptr(), + ); + ret_area.assume_init() + } +} + +/// Remove the specified asset from the vault. +/// +/// Panics: +/// - The fungible asset is not found in the vault. +/// - The amount of the fungible asset in the vault is less than the amount to be removed. +/// - The non-fungible asset is not found in the vault. +pub fn remove_asset(asset: CoreAsset) -> CoreAsset { + unsafe { + let mut ret_area = ::core::mem::MaybeUninit::::uninit(); + extern_account_remove_asset( + asset.inner[0], + asset.inner[1], + asset.inner[2], + asset.inner[3], + ret_area.as_mut_ptr(), + ); + ret_area.assume_init() + } +} diff --git a/sdk/base-sys/src/bindings/mod.rs b/sdk/base-sys/src/bindings/mod.rs index d7c32947c..36262b3ee 100644 --- a/sdk/base-sys/src/bindings/mod.rs +++ b/sdk/base-sys/src/bindings/mod.rs @@ -1 +1,6 @@ +pub mod account; +pub mod note; pub mod tx; +mod types; + +pub use types::*; diff --git a/sdk/base-sys/src/bindings/note.rs b/sdk/base-sys/src/bindings/note.rs new file mode 100644 index 000000000..149e040aa --- /dev/null +++ b/sdk/base-sys/src/bindings/note.rs @@ -0,0 +1,41 @@ +extern crate alloc; +use alloc::vec::Vec; + +use miden_stdlib_sys::Felt; + +#[link(wasm_import_module = "miden:core-import/note@1.0.0")] +extern "C" { + #[link_name = "get-inputs"] + pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize; +} + +/// Get the inputs of the currently executing note. +pub fn get_inputs() -> Vec { + const MAX_INPUTS: usize = 256; + let mut inputs: Vec = Vec::with_capacity(MAX_INPUTS); + let num_inputs = unsafe { + // Ensure the pointer is a valid Miden pointer + // + // NOTE: This relies on the fact that BumpAlloc makes all allocations + // minimally word-aligned. Each word consists of 4 elements of 4 bytes, + // so to get a Miden address from a Rust address, we divide by 16 to get + // the address in words (dividing by 4 gets us an address in elements, + // and by 4 again we get the word address). + let ptr = (inputs.as_mut_ptr() as usize) / 16; + // The MASM for this function is here: + // https://github.com/0xPolygonMiden/miden-base/blob/3cbe8d59dcf4ccc9c380b7c8417ac6178fc6b86a/miden-lib/asm/miden/note.masm#L69-L102 + // #! Writes the inputs of the currently execute note into memory starting at the specified + // address. #! + // #! Inputs: [dest_ptr] + // #! Outputs: [num_inputs, dest_ptr] + // #! + // #! - dest_ptr is the memory address to write the inputs. + // Compiler generated adapter code at call site will drop the returned dest_ptr + // and return the number of inputs + extern_note_get_inputs(ptr as *mut Felt) + }; + unsafe { + inputs.set_len(num_inputs); + } + inputs +} diff --git a/sdk/base-sys/src/bindings/tx.rs b/sdk/base-sys/src/bindings/tx.rs new file mode 100644 index 000000000..7db19c63c --- /dev/null +++ b/sdk/base-sys/src/bindings/tx.rs @@ -0,0 +1,45 @@ +use miden_stdlib_sys::Felt; + +use super::types::{CoreAsset, NoteId, NoteType, Recipient, Tag}; + +#[link(wasm_import_module = "miden:core-import/tx@1.0.0")] +extern "C" { + #[link_name = "create-note"] + pub fn extern_tx_create_note( + asset_f0: Felt, + asset_f1: Felt, + asset_f2: Felt, + asset_f3: Felt, + tag: Tag, + note_type: NoteType, + recipient_f0: Felt, + recipient_f1: Felt, + recipient_f2: Felt, + recipient_f3: Felt, + ) -> NoteId; +} + +/// Creates a new note. asset is the asset to be included in the note. tag is +/// the tag to be included in the note. recipient is the recipient of the note. +/// Returns the id of the created note. +pub fn create_note( + asset: CoreAsset, + tag: Tag, + note_type: NoteType, + recipient: Recipient, +) -> NoteId { + unsafe { + extern_tx_create_note( + asset.inner[0], + asset.inner[1], + asset.inner[2], + asset.inner[3], + tag, + note_type, + recipient.inner[0], + recipient.inner[1], + recipient.inner[2], + recipient.inner[3], + ) + } +} diff --git a/sdk/base-sys/src/bindings/tx/externs.rs b/sdk/base-sys/src/bindings/tx/externs.rs deleted file mode 100644 index 5ca963455..000000000 --- a/sdk/base-sys/src/bindings/tx/externs.rs +++ /dev/null @@ -1,37 +0,0 @@ -use miden_stdlib_sys::Felt; - -use crate::bindings::tx::{AccountId, CoreAsset, NoteId, NoteType, Tag}; - -#[allow(improper_ctypes)] -#[link(wasm_import_module = "miden:core-import/account@1.0.0")] -extern "C" { - #[link_name = "get-id"] - pub fn extern_account_get_id() -> AccountId; - #[link_name = "add-asset"] - pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); - #[link_name = "remove-asset"] - pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); -} - -#[link(wasm_import_module = "miden:core-import/note@1.0.0")] -extern "C" { - #[link_name = "get-inputs"] - pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize; -} - -#[link(wasm_import_module = "miden:core-import/tx@1.0.0")] -extern "C" { - #[link_name = "create-note"] - pub fn extern_tx_create_note( - asset_f0: Felt, - asset_f1: Felt, - asset_f2: Felt, - asset_f3: Felt, - tag: Tag, - note_type: NoteType, - recipient_f0: Felt, - recipient_f1: Felt, - recipient_f2: Felt, - recipient_f3: Felt, - ) -> NoteId; -} diff --git a/sdk/base-sys/src/bindings/tx/mod.rs b/sdk/base-sys/src/bindings/tx/mod.rs deleted file mode 100644 index f7b48cbc3..000000000 --- a/sdk/base-sys/src/bindings/tx/mod.rs +++ /dev/null @@ -1,116 +0,0 @@ -mod externs; -use externs::*; - -mod types; -pub use types::*; - -extern crate alloc; - -use alloc::vec::Vec; - -use miden_stdlib_sys::Felt; - -/// Get the account ID of the currently executing note account. -pub fn get_id() -> AccountId { - unsafe { extern_account_get_id() } -} - -/// Get the inputs of the currently executing note. -pub fn get_inputs() -> Vec { - const MAX_INPUTS: usize = 256; - let mut inputs: Vec = Vec::with_capacity(MAX_INPUTS); - let num_inputs = unsafe { - // Ensure the pointer is a valid Miden pointer - // - // NOTE: This relies on the fact that BumpAlloc makes all allocations - // minimally word-aligned. Each word consists of 4 elements of 4 bytes, - // so to get a Miden address from a Rust address, we divide by 16 to get - // the address in words (dividing by 4 gets us an address in elements, - // and by 4 again we get the word address). - let ptr = (inputs.as_mut_ptr() as usize) / 16; - // The MASM for this function is here: - // https://github.com/0xPolygonMiden/miden-base/blob/3cbe8d59dcf4ccc9c380b7c8417ac6178fc6b86a/miden-lib/asm/miden/note.masm#L69-L102 - // #! Writes the inputs of the currently execute note into memory starting at the specified - // address. #! - // #! Inputs: [dest_ptr] - // #! Outputs: [num_inputs, dest_ptr] - // #! - // #! - dest_ptr is the memory address to write the inputs. - // Compiler generated adapter code at call site will drop the returned dest_ptr - // and return the number of inputs - extern_note_get_inputs(ptr as *mut Felt) - }; - unsafe { - inputs.set_len(num_inputs); - } - inputs -} - -/// Add the specified asset to the vault. -/// Returns the final asset in the account vault defined as follows: If asset is -/// a non-fungible asset, then returns the same as asset. If asset is a -/// fungible asset, then returns the total fungible asset in the account -/// vault after asset was added to it. -/// -/// Panics: -/// - If the asset is not valid. -/// - If the total value of two fungible assets is greater than or equal to 2^63. -/// - If the vault already contains the same non-fungible asset. -pub fn add_asset(asset: CoreAsset) -> CoreAsset { - unsafe { - let mut ret_area = ::core::mem::MaybeUninit::::uninit(); - extern_account_add_asset( - asset.inner[0], - asset.inner[1], - asset.inner[2], - asset.inner[3], - ret_area.as_mut_ptr(), - ); - ret_area.assume_init() - } -} - -/// Remove the specified asset from the vault. -/// -/// Panics: -/// - The fungible asset is not found in the vault. -/// - The amount of the fungible asset in the vault is less than the amount to be removed. -/// - The non-fungible asset is not found in the vault. -pub fn remove_asset(asset: CoreAsset) -> CoreAsset { - unsafe { - let mut ret_area = ::core::mem::MaybeUninit::::uninit(); - extern_account_remove_asset( - asset.inner[0], - asset.inner[1], - asset.inner[2], - asset.inner[3], - ret_area.as_mut_ptr(), - ); - ret_area.assume_init() - } -} - -/// Creates a new note. asset is the asset to be included in the note. tag is -/// the tag to be included in the note. recipient is the recipient of the note. -/// Returns the id of the created note. -pub fn create_note( - asset: CoreAsset, - tag: Tag, - note_type: NoteType, - recipient: Recipient, -) -> NoteId { - unsafe { - extern_tx_create_note( - asset.inner[0], - asset.inner[1], - asset.inner[2], - asset.inner[3], - tag, - note_type, - recipient.inner[0], - recipient.inner[1], - recipient.inner[2], - recipient.inner[3], - ) - } -} diff --git a/sdk/base-sys/src/bindings/tx/types.rs b/sdk/base-sys/src/bindings/types.rs similarity index 100% rename from sdk/base-sys/src/bindings/tx/types.rs rename to sdk/base-sys/src/bindings/types.rs diff --git a/sdk/base-sys/src/lib.rs b/sdk/base-sys/src/lib.rs index c716a3018..539f25851 100644 --- a/sdk/base-sys/src/lib.rs +++ b/sdk/base-sys/src/lib.rs @@ -3,5 +3,6 @@ #[cfg(feature = "bindings")] pub mod bindings; + #[cfg(feature = "masl-lib")] pub mod masl; diff --git a/sdk/sdk/src/lib.rs b/sdk/sdk/src/lib.rs index 01a872d33..f38b38e2d 100644 --- a/sdk/sdk/src/lib.rs +++ b/sdk/sdk/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -pub use miden_base_sys::bindings::tx::*; +pub use miden_base_sys::bindings::*; pub use miden_sdk_alloc::BumpAlloc; pub use miden_stdlib_sys::*; diff --git a/sdk/stdlib-sys/src/intrinsics/mod.rs b/sdk/stdlib-sys/src/intrinsics/mod.rs index f27a9d615..d0bb0af3b 100644 --- a/sdk/stdlib-sys/src/intrinsics/mod.rs +++ b/sdk/stdlib-sys/src/intrinsics/mod.rs @@ -1,7 +1,10 @@ use core::ops::{Deref, DerefMut}; -pub(crate) mod felt; -pub(crate) mod word; +mod felt; +mod word; + +pub use felt::*; +pub use word::*; #[repr(C, align(32))] pub struct WordAligned(T); diff --git a/sdk/stdlib-sys/src/intrinsics/word.rs b/sdk/stdlib-sys/src/intrinsics/word.rs index 1c03a5c9f..0b65bf554 100644 --- a/sdk/stdlib-sys/src/intrinsics/word.rs +++ b/sdk/stdlib-sys/src/intrinsics/word.rs @@ -1,6 +1,6 @@ use core::ops::{Index, IndexMut}; -use crate::Felt; +use super::felt::Felt; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] #[repr(C, align(32))] diff --git a/sdk/stdlib-sys/src/lib.rs b/sdk/stdlib-sys/src/lib.rs index 55185e101..895c8ce58 100644 --- a/sdk/stdlib-sys/src/lib.rs +++ b/sdk/stdlib-sys/src/lib.rs @@ -3,5 +3,5 @@ mod intrinsics; mod stdlib; -pub use intrinsics::{felt::*, word::*, WordAligned}; +pub use intrinsics::*; pub use stdlib::*; diff --git a/sdk/stdlib-sys/src/stdlib/crypto/dsa.rs b/sdk/stdlib-sys/src/stdlib/crypto/dsa.rs index f48a066da..ce2821a83 100644 --- a/sdk/stdlib-sys/src/stdlib/crypto/dsa.rs +++ b/sdk/stdlib-sys/src/stdlib/crypto/dsa.rs @@ -1,4 +1,4 @@ -use crate::{Felt, Word}; +use crate::intrinsics::*; #[link(wasm_import_module = "miden:core-import/stdlib-crypto-dsa-rpo-falcon@1.0.0")] extern "C" { diff --git a/sdk/stdlib-sys/src/stdlib/crypto/hashes.rs b/sdk/stdlib-sys/src/stdlib/crypto/hashes.rs index 97bd8969a..a7d957131 100644 --- a/sdk/stdlib-sys/src/stdlib/crypto/hashes.rs +++ b/sdk/stdlib-sys/src/stdlib/crypto/hashes.rs @@ -102,8 +102,7 @@ fn hash_1to1( input: [u8; 32], extern_hash_1to1: unsafe extern "C" fn(u32, u32, u32, u32, u32, u32, u32, u32, *mut u8), ) -> [u8; 32] { - use crate::WordAligned; - + use crate::intrinsics::WordAligned; let input = unsafe { core::mem::transmute::<[u8; 32], [u32; 8]>(input) }; unsafe { let mut ret_area = ::core::mem::MaybeUninit::>::uninit(); diff --git a/sdk/stdlib-sys/src/stdlib/mem.rs b/sdk/stdlib-sys/src/stdlib/mem.rs index 6d0c7bf98..e3a794dcb 100644 --- a/sdk/stdlib-sys/src/stdlib/mem.rs +++ b/sdk/stdlib-sys/src/stdlib/mem.rs @@ -3,7 +3,7 @@ extern crate alloc; use alloc::vec::Vec; -use crate::{Felt, Word}; +use crate::intrinsics::{Felt, Word}; #[link(wasm_import_module = "miden:core-import/stdlib-mem@1.0.0")] extern "C" { diff --git a/sdk/stdlib-sys/src/stdlib/mod.rs b/sdk/stdlib-sys/src/stdlib/mod.rs index 2111d23da..000b317d7 100644 --- a/sdk/stdlib-sys/src/stdlib/mod.rs +++ b/sdk/stdlib-sys/src/stdlib/mod.rs @@ -3,5 +3,3 @@ mod mem; pub use crypto::*; pub use mem::*; - -// TODO: simplify bindings like in tx_kernel diff --git a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.hir b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.hir index 16cb9a16e..5218e876d 100644 --- a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.hir +++ b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.hir @@ -10,7 +10,7 @@ ;; Functions (func (export #entrypoint) (param i32) (block 0 (param v0 i32) - (call #miden_base_sys::bindings::tx::get_inputs v0) + (call #miden_base_sys::bindings::note::get_inputs v0) (br (block 1))) (block 1 @@ -151,7 +151,7 @@ (br (block 7 v66))) ) - (func (export #miden_base_sys::bindings::tx::get_inputs) + (func (export #miden_base_sys::bindings::note::get_inputs) (param i32) (block 0 (param v0 i32) (let (v1 i32) (const.i32 0)) diff --git a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.masm b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.masm index f96d217e3..9840e6587 100644 --- a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.masm +++ b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.masm @@ -4,11 +4,11 @@ use.miden::note use.intrinsics::mem export.entrypoint - exec."miden_base_sys::bindings::tx::get_inputs" + exec."miden_base_sys::bindings::note::get_inputs" end -export."miden_base_sys::bindings::tx::get_inputs" +export."miden_base_sys::bindings::note::get_inputs" mem_load.0x00011000 push.16 u32wrapping_sub diff --git a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.wat b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.wat index 97d866cb0..25c7d6af4 100644 --- a/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.wat +++ b/tests/integration/expected/abi_transform_tx_kernel_get_inputs_4.wat @@ -7,10 +7,10 @@ (type (;5;) (func (param i32 i32 i32))) (type (;6;) (func (param i32 i32))) (import "miden:core-import/intrinsics-mem@1.0.0" "heap-base" (func $miden_sdk_alloc::heap_base (;0;) (type 0))) - (import "miden:core-import/note@1.0.0" "get-inputs" (func $miden_base_sys::bindings::tx::externs::extern_note_get_inputs (;1;) (type 1))) + (import "miden:core-import/note@1.0.0" "get-inputs" (func $miden_base_sys::bindings::note::extern_note_get_inputs (;1;) (type 1))) (func $entrypoint (;2;) (type 2) (param i32) local.get 0 - call $miden_base_sys::bindings::tx::get_inputs + call $miden_base_sys::bindings::note::get_inputs ) (func $__rust_alloc (;3;) (type 3) (param i32 i32) (result i32) i32.const 1048576 @@ -102,7 +102,7 @@ end unreachable ) - (func $miden_base_sys::bindings::tx::get_inputs (;6;) (type 2) (param i32) + (func $miden_base_sys::bindings::note::get_inputs (;6;) (type 2) (param i32) (local i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -136,7 +136,7 @@ local.tee 3 i32.const 4 i32.shr_u - call $miden_base_sys::bindings::tx::externs::extern_note_get_inputs + call $miden_base_sys::bindings::note::extern_note_get_inputs i32.store offset=8 local.get 0 local.get 3 diff --git a/tests/integration/expected/rust_sdk/basic_wallet.hir b/tests/integration/expected/rust_sdk/basic_wallet.hir index a12511a66..14d68fe54 100644 --- a/tests/integration/expected/rust_sdk/basic_wallet.hir +++ b/tests/integration/expected/rust_sdk/basic_wallet.hir @@ -151,7 +151,7 @@ (store v25 v0) (let (v26 i32) (const.i32 32)) (let (v27 i32) (add.wrapping v9 v26)) - (call #miden_base_sys::bindings::tx::add_asset v27 v9) + (call #miden_base_sys::bindings::account::add_asset v27 v9) (let (v28 (ptr i32)) (global.symbol #__stack_pointer)) (store v28 v5) (br (block 1))) @@ -231,7 +231,7 @@ (store v47 v6) (let (v48 i32) (const.i32 64)) (let (v49 i32) (add.wrapping v15 v48)) - (call #miden_base_sys::bindings::tx::remove_asset v49 v15) + (call #miden_base_sys::bindings::account::remove_asset v49 v15) (let (v50 i32) (const.i32 64)) (let (v51 i32) (add.wrapping v15 v50)) (let (v52 i32) (const.i32 32)) @@ -676,7 +676,7 @@ (br (block 7 v66))) ) - (func (export #miden_base_sys::bindings::tx::add_asset) + (func (export #miden_base_sys::bindings::account::add_asset) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) (let (v2 u32) (bitcast v1)) @@ -721,7 +721,7 @@ (ret)) ) - (func (export #miden_base_sys::bindings::tx::remove_asset) + (func (export #miden_base_sys::bindings::account::remove_asset) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) (let (v2 u32) (bitcast v1)) diff --git a/tests/integration/expected/rust_sdk/basic_wallet.masm b/tests/integration/expected/rust_sdk/basic_wallet.masm index 773e20ada..e815020a0 100644 --- a/tests/integration/expected/rust_sdk/basic_wallet.masm +++ b/tests/integration/expected/rust_sdk/basic_wallet.masm @@ -2019,7 +2019,7 @@ export."miden:basic-wallet/basic-wallet@1.0.0#send-asset" u32wrapping_add dup.9 swap.1 - exec."miden_base_sys::bindings::tx::remove_asset" + exec."miden_base_sys::bindings::account::remove_asset" push.32 dup.9 swap.1 @@ -2070,7 +2070,7 @@ export."miden:basic-wallet/basic-wallet@1.0.0#send-asset" end -export."miden_base_sys::bindings::tx::remove_asset" +export."miden_base_sys::bindings::account::remove_asset" dup.1 add.12 u32assert @@ -2405,7 +2405,7 @@ export."miden:basic-wallet/basic-wallet@1.0.0#receive-asset" u32wrapping_add movup.5 swap.1 - exec."miden_base_sys::bindings::tx::add_asset" + exec."miden_base_sys::bindings::account::add_asset" push.1114112 movup.5 swap.1 @@ -2429,7 +2429,7 @@ export."miden:basic-wallet/basic-wallet@1.0.0#receive-asset" end -export."miden_base_sys::bindings::tx::add_asset" +export."miden_base_sys::bindings::account::add_asset" dup.1 add.12 u32assert diff --git a/tests/integration/expected/rust_sdk/basic_wallet.wat b/tests/integration/expected/rust_sdk/basic_wallet.wat index 91425a669..b44932871 100644 --- a/tests/integration/expected/rust_sdk/basic_wallet.wat +++ b/tests/integration/expected/rust_sdk/basic_wallet.wat @@ -73,9 +73,9 @@ (import "miden:core-import/intrinsics-felt@1.0.0" "add" (func $miden_stdlib_sys::intrinsics::felt::extern_add (;0;) (type 0))) (import "miden:core-import/stdlib-crypto-hashes-blake3@1.0.0" "hash-one-to-one" (func $miden_stdlib_sys::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;1;) (type 1))) (import "miden:core-import/intrinsics-mem@1.0.0" "heap-base" (func $miden_sdk_alloc::heap_base (;2;) (type 2))) - (import "miden:core-import/account@1.0.0" "add-asset" (func $miden_base_sys::bindings::tx::externs::extern_account_add_asset (;3;) (type 3))) - (import "miden:core-import/account@1.0.0" "remove-asset" (func $miden_base_sys::bindings::tx::externs::extern_account_remove_asset (;4;) (type 3))) - (import "miden:core-import/tx@1.0.0" "create-note" (func $miden_base_sys::bindings::tx::externs::extern_tx_create_note (;5;) (type 4))) + (import "miden:core-import/account@1.0.0" "add-asset" (func $miden_base_sys::bindings::account::extern_account_add_asset (;3;) (type 3))) + (import "miden:core-import/account@1.0.0" "remove-asset" (func $miden_base_sys::bindings::account::extern_account_remove_asset (;4;) (type 3))) + (import "miden:core-import/tx@1.0.0" "create-note" (func $miden_base_sys::bindings::tx::extern_tx_create_note (;5;) (type 4))) (func $__wasm_call_ctors (;6;) (type 5)) (func $basic_wallet::bindings::__link_custom_section_describing_imports (;7;) (type 5)) (func $__rust_alloc (;8;) (type 6) (param i32 i32) (result i32) @@ -149,7 +149,7 @@ i32.const 32 i32.add local.get 5 - call $miden_base_sys::bindings::tx::add_asset + call $miden_base_sys::bindings::account::add_asset local.get 4 global.set $__stack_pointer ) @@ -192,7 +192,7 @@ i32.const 64 i32.add local.get 11 - call $miden_base_sys::bindings::tx::remove_asset + call $miden_base_sys::bindings::account::remove_asset local.get 11 i32.const 64 i32.add @@ -473,7 +473,7 @@ end unreachable ) - (func $miden_base_sys::bindings::tx::add_asset (;21;) (type 13) (param i32 i32) + (func $miden_base_sys::bindings::account::add_asset (;21;) (type 13) (param i32 i32) local.get 1 f32.load local.get 1 @@ -483,9 +483,9 @@ local.get 1 f32.load offset=12 local.get 0 - call $miden_base_sys::bindings::tx::externs::extern_account_add_asset + call $miden_base_sys::bindings::account::extern_account_add_asset ) - (func $miden_base_sys::bindings::tx::remove_asset (;22;) (type 13) (param i32 i32) + (func $miden_base_sys::bindings::account::remove_asset (;22;) (type 13) (param i32 i32) local.get 1 f32.load local.get 1 @@ -495,7 +495,7 @@ local.get 1 f32.load offset=12 local.get 0 - call $miden_base_sys::bindings::tx::externs::extern_account_remove_asset + call $miden_base_sys::bindings::account::extern_account_remove_asset ) (func $miden_base_sys::bindings::tx::create_note (;23;) (type 14) (param i32 f32 f32 i32) (result f32) local.get 0 @@ -516,7 +516,7 @@ f32.load offset=8 local.get 3 f32.load offset=12 - call $miden_base_sys::bindings::tx::externs::extern_tx_create_note + call $miden_base_sys::bindings::tx::extern_tx_create_note ) (func $alloc::vec::Vec::into_boxed_slice (;24;) (type 13) (param i32 i32) (local i32 i32) diff --git a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.hir b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.hir index 66e75c258..7b37eaf25 100644 --- a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.hir +++ b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.hir @@ -42,7 +42,7 @@ (func (export #get_wallet_magic_number) (result felt) (block 0 (let (v1 felt) (const.felt 0)) - (let (v2 felt) (call #miden_base_sys::bindings::tx::get_id)) + (let (v2 felt) (call #miden_base_sys::bindings::account::get_id)) (let (v3 i64) (const.i64 42)) (let (v4 felt) (cast v3)) (let (v5 felt) (add.unchecked v4 v2)) @@ -96,7 +96,7 @@ (store v31 v10) (let (v32 i32) (const.i32 32)) (let (v33 i32) (add.wrapping v7 v32)) - (call #miden_base_sys::bindings::tx::add_asset v33 v7) + (call #miden_base_sys::bindings::account::add_asset v33 v7) (let (v34 u32) (bitcast v7)) (let (v35 u32) (add.checked v34 32)) (let (v36 u32) (mod.unchecked v35 4)) @@ -218,7 +218,7 @@ (let (v9 felt) (cast v8)) (let (v10 i32) (const.i32 4)) (let (v11 i32) (add.wrapping v6 v10)) - (call #miden_base_sys::bindings::tx::get_inputs v11) + (call #miden_base_sys::bindings::note::get_inputs v11) (let (v12 u32) (bitcast v6)) (let (v13 u32) (add.checked v12 12)) (let (v14 u32) (mod.unchecked v13 4)) @@ -565,7 +565,7 @@ (let (v8 i32) (band v6 v7)) (let (v9 (ptr i32)) (global.symbol #__stack_pointer)) (store v9 v8) - (call #miden_base_sys::bindings::tx::remove_asset v8 v0) + (call #miden_base_sys::bindings::account::remove_asset v8 v0) (let (v10 u32) (bitcast v8)) (let (v11 u32) (mod.unchecked v10 4)) (assertz 250 v11) @@ -712,7 +712,7 @@ (br (block 7 v66))) ) - (func (export #miden_base_sys::bindings::tx::get_id) + (func (export #miden_base_sys::bindings::account::get_id) (result felt) (block 0 (let (v1 felt) (call (#miden::account #get_id))) @@ -722,89 +722,7 @@ (ret v0)) ) - (func (export #miden_base_sys::bindings::tx::get_inputs) - (param i32) - (block 0 (param v0 i32) - (let (v1 i32) (const.i32 0)) - (let (v2 i32) (global.load i32 (global.symbol #__stack_pointer))) - (let (v3 i32) (const.i32 16)) - (let (v4 i32) (sub.wrapping v2 v3)) - (let (v5 (ptr i32)) (global.symbol #__stack_pointer)) - (store v5 v4) - (let (v6 i32) (const.i32 4)) - (let (v7 i32) (add.wrapping v4 v6)) - (let (v8 i32) (const.i32 256)) - (let (v9 i32) (const.i32 0)) - (call #alloc::raw_vec::RawVec::try_allocate_in v7 v8 v9) - (let (v10 u32) (bitcast v4)) - (let (v11 u32) (add.checked v10 8)) - (let (v12 u32) (mod.unchecked v11 4)) - (assertz 250 v12) - (let (v13 (ptr i32)) (inttoptr v11)) - (let (v14 i32) (load v13)) - (let (v15 u32) (bitcast v4)) - (let (v16 u32) (add.checked v15 4)) - (let (v17 u32) (mod.unchecked v16 4)) - (assertz 250 v17) - (let (v18 (ptr i32)) (inttoptr v16)) - (let (v19 i32) (load v18)) - (let (v20 i32) (const.i32 1)) - (let (v21 i1) (neq v19 v20)) - (let (v22 i32) (zext v21)) - (let (v23 i1) (neq v22 0)) - (condbr v23 (block 2) (block 3))) - - (block 1 - (ret)) - - (block 2 - (let (v29 u32) (bitcast v4)) - (let (v30 u32) (add.checked v29 12)) - (let (v31 u32) (mod.unchecked v30 4)) - (assertz 250 v31) - (let (v32 (ptr i32)) (inttoptr v30)) - (let (v33 i32) (load v32)) - (let (v34 i32) (const.i32 4)) - (let (v35 u32) (bitcast v33)) - (let (v36 u32) (bitcast v34)) - (let (v37 u32) (shr.wrapping v35 v36)) - (let (v38 i32) (bitcast v37)) - (let [(v39 i32) (v40 i32)] (call (#miden::note #get_inputs) v38)) - (let (v41 u32) (bitcast v0)) - (let (v42 u32) (add.checked v41 8)) - (let (v43 u32) (mod.unchecked v42 4)) - (assertz 250 v43) - (let (v44 (ptr i32)) (inttoptr v42)) - (store v44 v39) - (let (v45 u32) (bitcast v0)) - (let (v46 u32) (add.checked v45 4)) - (let (v47 u32) (mod.unchecked v46 4)) - (assertz 250 v47) - (let (v48 (ptr i32)) (inttoptr v46)) - (store v48 v33) - (let (v49 u32) (bitcast v0)) - (let (v50 u32) (mod.unchecked v49 4)) - (assertz 250 v50) - (let (v51 (ptr i32)) (inttoptr v49)) - (store v51 v14) - (let (v52 i32) (const.i32 16)) - (let (v53 i32) (add.wrapping v4 v52)) - (let (v54 (ptr i32)) (global.symbol #__stack_pointer)) - (store v54 v53) - (br (block 1))) - - (block 3 - (let (v24 u32) (bitcast v4)) - (let (v25 u32) (add.checked v24 12)) - (let (v26 u32) (mod.unchecked v25 4)) - (assertz 250 v26) - (let (v27 (ptr i32)) (inttoptr v25)) - (let (v28 i32) (load v27)) - (call #alloc::raw_vec::handle_error v14 v28) - (unreachable)) - ) - - (func (export #miden_base_sys::bindings::tx::add_asset) + (func (export #miden_base_sys::bindings::account::add_asset) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) (let (v2 u32) (bitcast v1)) @@ -849,7 +767,7 @@ (ret)) ) - (func (export #miden_base_sys::bindings::tx::remove_asset) + (func (export #miden_base_sys::bindings::account::remove_asset) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) (let (v2 u32) (bitcast v1)) @@ -894,6 +812,88 @@ (ret)) ) + (func (export #miden_base_sys::bindings::note::get_inputs) + (param i32) + (block 0 (param v0 i32) + (let (v1 i32) (const.i32 0)) + (let (v2 i32) (global.load i32 (global.symbol #__stack_pointer))) + (let (v3 i32) (const.i32 16)) + (let (v4 i32) (sub.wrapping v2 v3)) + (let (v5 (ptr i32)) (global.symbol #__stack_pointer)) + (store v5 v4) + (let (v6 i32) (const.i32 4)) + (let (v7 i32) (add.wrapping v4 v6)) + (let (v8 i32) (const.i32 256)) + (let (v9 i32) (const.i32 0)) + (call #alloc::raw_vec::RawVec::try_allocate_in v7 v8 v9) + (let (v10 u32) (bitcast v4)) + (let (v11 u32) (add.checked v10 8)) + (let (v12 u32) (mod.unchecked v11 4)) + (assertz 250 v12) + (let (v13 (ptr i32)) (inttoptr v11)) + (let (v14 i32) (load v13)) + (let (v15 u32) (bitcast v4)) + (let (v16 u32) (add.checked v15 4)) + (let (v17 u32) (mod.unchecked v16 4)) + (assertz 250 v17) + (let (v18 (ptr i32)) (inttoptr v16)) + (let (v19 i32) (load v18)) + (let (v20 i32) (const.i32 1)) + (let (v21 i1) (neq v19 v20)) + (let (v22 i32) (zext v21)) + (let (v23 i1) (neq v22 0)) + (condbr v23 (block 2) (block 3))) + + (block 1 + (ret)) + + (block 2 + (let (v29 u32) (bitcast v4)) + (let (v30 u32) (add.checked v29 12)) + (let (v31 u32) (mod.unchecked v30 4)) + (assertz 250 v31) + (let (v32 (ptr i32)) (inttoptr v30)) + (let (v33 i32) (load v32)) + (let (v34 i32) (const.i32 4)) + (let (v35 u32) (bitcast v33)) + (let (v36 u32) (bitcast v34)) + (let (v37 u32) (shr.wrapping v35 v36)) + (let (v38 i32) (bitcast v37)) + (let [(v39 i32) (v40 i32)] (call (#miden::note #get_inputs) v38)) + (let (v41 u32) (bitcast v0)) + (let (v42 u32) (add.checked v41 8)) + (let (v43 u32) (mod.unchecked v42 4)) + (assertz 250 v43) + (let (v44 (ptr i32)) (inttoptr v42)) + (store v44 v39) + (let (v45 u32) (bitcast v0)) + (let (v46 u32) (add.checked v45 4)) + (let (v47 u32) (mod.unchecked v46 4)) + (assertz 250 v47) + (let (v48 (ptr i32)) (inttoptr v46)) + (store v48 v33) + (let (v49 u32) (bitcast v0)) + (let (v50 u32) (mod.unchecked v49 4)) + (assertz 250 v50) + (let (v51 (ptr i32)) (inttoptr v49)) + (store v51 v14) + (let (v52 i32) (const.i32 16)) + (let (v53 i32) (add.wrapping v4 v52)) + (let (v54 (ptr i32)) (global.symbol #__stack_pointer)) + (store v54 v53) + (br (block 1))) + + (block 3 + (let (v24 u32) (bitcast v4)) + (let (v25 u32) (add.checked v24 12)) + (let (v26 u32) (mod.unchecked v25 4)) + (assertz 250 v26) + (let (v27 (ptr i32)) (inttoptr v25)) + (let (v28 i32) (load v27)) + (call #alloc::raw_vec::handle_error v14 v28) + (unreachable)) + ) + (func (export #miden_base_sys::bindings::tx::create_note) (param i32) (param felt) (param felt) (param i32) (result felt) (block 0 diff --git a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat index f64841636..6aca8e422 100644 --- a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat +++ b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat @@ -12,8 +12,8 @@ (type (;10;) (func (param f32 f32 f32 f32 f32 f32 f32 f32))) (type (;11;) (func (result i32))) (type (;12;) (func (result f32))) - (type (;13;) (func (param i32) (result i32))) - (type (;14;) (func (param f32 f32 f32 f32 i32))) + (type (;13;) (func (param f32 f32 f32 f32 i32))) + (type (;14;) (func (param i32) (result i32))) (type (;15;) (func (param f32 f32 f32 f32 f32 f32 f32 f32 f32 f32) (result f32))) (type (;16;) (func (param f32 i32 i32))) (type (;17;) (func (param f32 f32 f32 f32 f32 f32 f32 f32 f32 f32 f32 f32 i32 i32 i32))) @@ -49,11 +49,11 @@ (import "miden:core-import/stdlib-crypto-hashes-blake3@1.0.0" "hash-two-to-one" (func $miden_stdlib_sys::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;20;) (type 9))) (import "miden:core-import/stdlib-crypto-dsa-rpo-falcon@1.0.0" "rpo-falcon512-verify" (func $miden_stdlib_sys::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;21;) (type 10))) (import "miden:core-import/intrinsics-mem@1.0.0" "heap-base" (func $miden_sdk_alloc::heap_base (;22;) (type 11))) - (import "miden:core-import/account@1.0.0" "get-id" (func $miden_base_sys::bindings::tx::externs::extern_account_get_id (;23;) (type 12))) - (import "miden:core-import/note@1.0.0" "get-inputs" (func $miden_base_sys::bindings::tx::externs::extern_note_get_inputs (;24;) (type 13))) - (import "miden:core-import/account@1.0.0" "add-asset" (func $miden_base_sys::bindings::tx::externs::extern_account_add_asset (;25;) (type 14))) - (import "miden:core-import/account@1.0.0" "remove-asset" (func $miden_base_sys::bindings::tx::externs::extern_account_remove_asset (;26;) (type 14))) - (import "miden:core-import/tx@1.0.0" "create-note" (func $miden_base_sys::bindings::tx::externs::extern_tx_create_note (;27;) (type 15))) + (import "miden:core-import/account@1.0.0" "get-id" (func $miden_base_sys::bindings::account::extern_account_get_id (;23;) (type 12))) + (import "miden:core-import/account@1.0.0" "add-asset" (func $miden_base_sys::bindings::account::extern_account_add_asset (;24;) (type 13))) + (import "miden:core-import/account@1.0.0" "remove-asset" (func $miden_base_sys::bindings::account::extern_account_remove_asset (;25;) (type 13))) + (import "miden:core-import/note@1.0.0" "get-inputs" (func $miden_base_sys::bindings::note::extern_note_get_inputs (;26;) (type 14))) + (import "miden:core-import/tx@1.0.0" "create-note" (func $miden_base_sys::bindings::tx::extern_tx_create_note (;27;) (type 15))) (import "miden:core-import/stdlib-mem@1.0.0" "pipe-words-to-memory" (func $miden_stdlib_sys::stdlib::mem::extern_pipe_words_to_memory (;28;) (type 16))) (import "miden:core-import/stdlib-mem@1.0.0" "pipe-double-words-to-memory" (func $miden_stdlib_sys::stdlib::mem::extern_pipe_double_words_to_memory (;29;) (type 17))) (func $core::alloc::global::GlobalAlloc::alloc_zeroed (;30;) (type 18) (param i32 i32 i32) (result i32) @@ -74,7 +74,7 @@ ) (func $get_wallet_magic_number (;31;) (type 12) (result f32) (local f32) - call $miden_base_sys::bindings::tx::get_id + call $miden_base_sys::bindings::account::get_id local.set 0 i64.const 42 call $miden_stdlib_sys::intrinsics::felt::extern_from_u64_unchecked @@ -117,7 +117,7 @@ i32.const 32 i32.add local.get 1 - call $miden_base_sys::bindings::tx::add_asset + call $miden_base_sys::bindings::account::add_asset local.get 1 f32.load offset=32 local.set 2 @@ -227,7 +227,7 @@ local.get 0 i32.const 4 i32.add - call $miden_base_sys::bindings::tx::get_inputs + call $miden_base_sys::bindings::note::get_inputs local.get 0 i32.load offset=12 i32.const 2 @@ -393,7 +393,7 @@ global.set $__stack_pointer local.get 2 local.get 0 - call $miden_base_sys::bindings::tx::remove_asset + call $miden_base_sys::bindings::account::remove_asset local.get 2 f32.load local.set 3 @@ -488,10 +488,34 @@ end unreachable ) - (func $miden_base_sys::bindings::tx::get_id (;45;) (type 12) (result f32) - call $miden_base_sys::bindings::tx::externs::extern_account_get_id + (func $miden_base_sys::bindings::account::get_id (;45;) (type 12) (result f32) + call $miden_base_sys::bindings::account::extern_account_get_id ) - (func $miden_base_sys::bindings::tx::get_inputs (;46;) (type 24) (param i32) + (func $miden_base_sys::bindings::account::add_asset (;46;) (type 19) (param i32 i32) + local.get 1 + f32.load + local.get 1 + f32.load offset=4 + local.get 1 + f32.load offset=8 + local.get 1 + f32.load offset=12 + local.get 0 + call $miden_base_sys::bindings::account::extern_account_add_asset + ) + (func $miden_base_sys::bindings::account::remove_asset (;47;) (type 19) (param i32 i32) + local.get 1 + f32.load + local.get 1 + f32.load offset=4 + local.get 1 + f32.load offset=8 + local.get 1 + f32.load offset=12 + local.get 0 + call $miden_base_sys::bindings::account::extern_account_remove_asset + ) + (func $miden_base_sys::bindings::note::get_inputs (;48;) (type 24) (param i32) (local i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -525,7 +549,7 @@ local.tee 3 i32.const 4 i32.shr_u - call $miden_base_sys::bindings::tx::externs::extern_note_get_inputs + call $miden_base_sys::bindings::note::extern_note_get_inputs i32.store offset=8 local.get 0 local.get 3 @@ -538,30 +562,6 @@ i32.add global.set $__stack_pointer ) - (func $miden_base_sys::bindings::tx::add_asset (;47;) (type 19) (param i32 i32) - local.get 1 - f32.load - local.get 1 - f32.load offset=4 - local.get 1 - f32.load offset=8 - local.get 1 - f32.load offset=12 - local.get 0 - call $miden_base_sys::bindings::tx::externs::extern_account_add_asset - ) - (func $miden_base_sys::bindings::tx::remove_asset (;48;) (type 19) (param i32 i32) - local.get 1 - f32.load - local.get 1 - f32.load offset=4 - local.get 1 - f32.load offset=8 - local.get 1 - f32.load offset=12 - local.get 0 - call $miden_base_sys::bindings::tx::externs::extern_account_remove_asset - ) (func $miden_base_sys::bindings::tx::create_note (;49;) (type 22) (param i32 f32 f32 i32) (result f32) local.get 0 f32.load @@ -581,7 +581,7 @@ f32.load offset=8 local.get 3 f32.load offset=12 - call $miden_base_sys::bindings::tx::externs::extern_tx_create_note + call $miden_base_sys::bindings::tx::extern_tx_create_note ) (func $alloc::vec::Vec::with_capacity (;50;) (type 19) (param i32 i32) (local i32 i32) diff --git a/tests/integration/src/compiler_test.rs b/tests/integration/src/compiler_test.rs index 6df94dbc0..35946e2b5 100644 --- a/tests/integration/src/compiler_test.rs +++ b/tests/integration/src/compiler_test.rs @@ -789,6 +789,7 @@ impl CompilerTestBuilder { r#" #![no_std] #![no_main] + #![allow(unused_imports)] #[panic_handler] fn my_panic(_info: &core::panic::PanicInfo) -> ! {{ @@ -803,6 +804,7 @@ impl CompilerTestBuilder { use miden_stdlib_sys::*; #[no_mangle] + #[allow(improper_ctypes_definitions)] pub extern "C" fn entrypoint{} "#, source @@ -863,6 +865,7 @@ debug = true format!( r#"#![no_std] #![no_main] +#![allow(unused_imports)] #[panic_handler] fn my_panic(_info: &core::panic::PanicInfo) -> ! {{ diff --git a/tests/integration/src/rust_masm_tests/abi_transform/tx_kernel.rs b/tests/integration/src/rust_masm_tests/abi_transform/tx_kernel.rs index 8443c2a8f..76456537d 100644 --- a/tests/integration/src/rust_masm_tests/abi_transform/tx_kernel.rs +++ b/tests/integration/src/rust_masm_tests/abi_transform/tx_kernel.rs @@ -41,7 +41,7 @@ end expect3 = expected_inputs.get(2).map(|i| i.as_int()).unwrap_or(0), expect4 = expected_inputs.get(3).map(|i| i.as_int()).unwrap_or(0), ); - let main_fn = "() -> Vec { get_inputs() }"; + let main_fn = "() -> Vec { miden::note::get_inputs() }"; let artifact_name = format!("abi_transform_tx_kernel_get_inputs_{}", test_name); let mut test_builder = CompilerTestBuilder::rust_fn_body_with_sdk(artifact_name.clone(), main_fn, true, None); diff --git a/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs b/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs index ebaea63fa..f795ad315 100644 --- a/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs +++ b/tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs @@ -10,7 +10,7 @@ pub struct Account; impl Account { #[no_mangle] pub fn get_wallet_magic_number() -> Felt { - let acc_id = get_id(); + let acc_id = miden::account::get_id(); let magic = felt!(42); magic + acc_id.into() } @@ -18,7 +18,7 @@ impl Account { #[no_mangle] pub fn test_add_asset() -> Felt { let asset_in = CoreAsset::new([felt!(1), felt!(2), felt!(3), felt!(4)]); - let asset_out = add_asset(asset_in); + let asset_out = miden::account::add_asset(asset_in); asset_out.as_word()[0] } @@ -34,7 +34,7 @@ impl Account { } else if a >= b { b / a } else if a == b { - assert_eq(a, b); + miden::assert_eq(a, b); a + Felt::from_u64_unchecked(d) } else if a != b { -a @@ -54,7 +54,7 @@ impl Note { #[no_mangle] pub fn note_script() -> Felt { let mut sum = Felt::new(0).unwrap(); - for input in get_inputs() { + for input in miden::note::get_inputs() { sum = sum + input; } sum @@ -88,7 +88,7 @@ pub fn test_pipe_double_words_to_memory(num_words: Felt) -> (Word, Vec) { #[no_mangle] pub fn test_remove_asset(asset: CoreAsset) -> Felt { - let asset_out = remove_asset(asset); + let asset_out = miden::account::remove_asset(asset); asset_out.as_word()[0] } @@ -99,5 +99,5 @@ pub fn test_create_note( note_type: NoteType, recipient: Recipient, ) -> NoteId { - create_note(asset, tag, note_type, recipient) + miden::tx::create_note(asset, tag, note_type, recipient) } diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/README.md b/tests/rust-apps-wasm/rust-sdk/basic-wallet/README.md deleted file mode 100644 index c4e0f3098..000000000 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# basic_wallet - -## Useful commands - -`basic_wallet` is built using the [Miden compiler](https://github.com/0xPolygonMiden/compiler). - -`cargo miden` is a `cargo` cargo extension. Check out its [documentation](https://github.com/0xPolygonMiden/compiler/tree/main/tools/cargo-miden) -for more details. - - -### Build - -To build this project, run: - -```bash -cargo miden build --release -``` - -The compiled Miden artifact will be located in `target/miden/release`. diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/rust-toolchain.toml b/tests/rust-apps-wasm/rust-sdk/basic-wallet/rust-toolchain.toml deleted file mode 100644 index a4cb17f1f..000000000 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/rust-toolchain.toml +++ /dev/null @@ -1,5 +0,0 @@ -[toolchain] -channel = "nightly-2024-08-06" -components = ["rustfmt", "rust-src"] -targets = ["wasm32-wasi"] -profile = "minimal" diff --git a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs index d7d124932..1ed7a98df 100644 --- a/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs +++ b/tests/rust-apps-wasm/rust-sdk/basic-wallet/src/lib.rs @@ -22,21 +22,18 @@ bindings::export!(MyAccount with_types_in bindings); mod bindings; use bindings::exports::miden::basic_wallet::basic_wallet::Guest; -use miden::{ - add_asset, blake3_hash_1to1, create_note, remove_asset, CoreAsset, Felt, NoteType, Recipient, - Tag, -}; +use miden::{blake3_hash_1to1, CoreAsset, Felt, NoteType, Recipient, Tag}; struct MyAccount; impl Guest for MyAccount { fn receive_asset(asset: CoreAsset) { - add_asset(asset); + miden::account::add_asset(asset); } fn send_asset(asset: CoreAsset, tag: Tag, note_type: NoteType, recipient: Recipient) { - let asset = remove_asset(asset); - create_note(asset, tag, note_type, recipient); + let asset = miden::account::remove_asset(asset); + miden::tx::create_note(asset, tag, note_type, recipient); } fn test_felt_intrinsics(a: Felt, b: Felt) -> Felt {