From 9009c524b8c8aa5a5fbaf8352973b6cc3c42d365 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Thu, 11 Apr 2024 16:17:18 +0300 Subject: [PATCH 1/5] fix use after free in Miden SDK `tx-kernel::get_inputs` --- sdk/tx-kernel/src/lib.rs | 29 ++- .../miden_sdk_account_test.hir | 166 +++++++++++++----- .../miden_sdk_account_test.wat | 139 ++++++++++----- 3 files changed, 231 insertions(+), 103 deletions(-) diff --git a/sdk/tx-kernel/src/lib.rs b/sdk/tx-kernel/src/lib.rs index fd1f2de9c..695d71cc8 100644 --- a/sdk/tx-kernel/src/lib.rs +++ b/sdk/tx-kernel/src/lib.rs @@ -38,25 +38,22 @@ pub fn get_id() -> AccountId { const MAX_INPUTS: usize = 256; -#[inline(always)] pub fn get_inputs() -> Vec { - // 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. unsafe { - #[repr(transparent)] - struct RetArea([Felt; MAX_INPUTS]); - let mut ret_area = ::core::mem::MaybeUninit::::uninit(); - let ptr = ret_area.as_mut_ptr(); - let num_inputs = extern_note_get_inputs(ptr as *mut Felt); - // Compiler generated adapter function will drop the returned dest_ptr + let mut inputs: Vec = Vec::with_capacity(MAX_INPUTS); + // 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 - Vec::from_raw_parts(ptr as *mut Felt, num_inputs, num_inputs) + let num_inputs = extern_note_get_inputs(inputs.as_mut_ptr()); + inputs.truncate(num_inputs); + inputs } } 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 ce6609f7e..e5c5c90dd 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 @@ -309,67 +309,75 @@ (let (v2 felt) (const.felt 0)) (let (v3 i32) (const.i32 0)) (let (v4 i32) (global.load i32 (global.symbol #__stack_pointer))) - (let (v5 i32) (const.i32 2048)) + (let (v5 i32) (const.i32 16)) (let (v6 i32) (sub.wrapping v4 v5)) (let (v7 (ptr i32)) (global.symbol #__stack_pointer)) (store v7 v6) (let (v8 i64) (const.i64 0)) (let (v9 felt) (cast v8)) - (let [(v10 i32) (v11 felt)] (call (#miden:tx_kernel/note #get_inputs) v6)) - (let (v12 i32) (const.i32 3)) - (let (v13 i32) (shl.wrapping v10 v12)) - (let (v14 i32) (add.wrapping v6 v13)) - (let (v15 u32) (cast v6)) - (let (v16 u32) (add.checked v15 12)) - (let (v17 (ptr i32)) (inttoptr v16)) - (store v17 v14) - (let (v18 u32) (cast v6)) - (let (v19 u32) (add.checked v18 8)) - (let (v20 (ptr i32)) (inttoptr v19)) - (store v20 v10) - (let (v21 u32) (cast v6)) - (let (v22 u32) (add.checked v21 4)) - (let (v23 (ptr i32)) (inttoptr v22)) - (store v23 v6) + (call #miden_sdk_tx_kernel::get_inputs v6) + (let (v10 u32) (cast v6)) + (let (v11 (ptr i32)) (inttoptr v10)) + (let (v12 i32) (load v11)) + (let (v13 u32) (cast v6)) + (let (v14 u32) (add.checked v13 4)) + (let (v15 (ptr i32)) (inttoptr v14)) + (let (v16 i32) (load v15)) + (let (v17 u32) (cast v6)) + (let (v18 u32) (add.checked v17 8)) + (let (v19 (ptr i32)) (inttoptr v18)) + (let (v20 i32) (load v19)) + (let (v21 i32) (const.i32 3)) + (let (v22 i32) (shl.wrapping v20 v21)) + (let (v23 i32) (add.wrapping v16 v22)) (let (v24 u32) (cast v6)) - (let (v25 (ptr i32)) (inttoptr v24)) - (store v25 v6) - (br (block 2 v13 v6 v9 v6))) + (let (v25 u32) (add.checked v24 12)) + (let (v26 (ptr i32)) (inttoptr v25)) + (store v26 v23) + (let (v27 u32) (cast v6)) + (let (v28 u32) (add.checked v27 8)) + (let (v29 (ptr i32)) (inttoptr v28)) + (store v29 v12) + (let (v30 u32) (cast v6)) + (let (v31 (ptr i32)) (inttoptr v30)) + (store v31 v16) + (br (block 2 v22 v6 v23 v9 v16))) (block 1 (param v0 felt)) (block 2 - (param v27 i32) - (param v29 i32) - (param v33 felt) - (param v34 i32) - (let (v28 i1) (neq v27 0)) - (condbr v28 (block 4) (block 5))) + (param v33 i32) + (param v35 i32) + (param v36 i32) + (param v43 felt) + (param v46 i32) + (let (v34 i1) (neq v33 0)) + (condbr v34 (block 4) (block 5))) - (block 3 (param v26 felt)) + (block 3 (param v32 felt)) (block 4 - (let (v35 i32) (const.i32 8)) - (let (v36 i32) (add.wrapping v34 v35)) - (let (v37 u32) (cast v29)) + (let (v44 i32) (const.i32 -8)) + (let (v45 i32) (add.wrapping v33 v44)) + (let (v47 u32) (cast v46)) + (let (v48 (ptr felt)) (inttoptr v47)) + (let (v49 felt) (load v48)) + (let (v50 felt) (add.unchecked v43 v49)) + (let (v51 i32) (const.i32 8)) + (let (v52 i32) (add.wrapping v46 v51)) + (br (block 2 v45 v35 v36 v50 v52))) + + (block 5 + (let (v37 u32) (cast v35)) (let (v38 u32) (add.checked v37 4)) (let (v39 (ptr i32)) (inttoptr v38)) (store v39 v36) - (let (v40 i32) (const.i32 -8)) - (let (v41 i32) (add.wrapping v27 v40)) - (let (v42 u32) (cast v34)) - (let (v43 (ptr felt)) (inttoptr v42)) - (let (v44 felt) (load v43)) - (let (v45 felt) (add.unchecked v33 v44)) - (br (block 2 v41 v29 v45 v36))) - - (block 5 - (call # as core::ops::drop::Drop>::drop v29) - (let (v30 i32) (const.i32 2048)) - (let (v31 i32) (add.wrapping v29 v30)) - (let (v32 (ptr i32)) (global.symbol #__stack_pointer)) - (store v32 v31) - (ret v33)) + (call # as core::ops::drop::Drop>::drop v35) + (let (v40 i32) (const.i32 16)) + (let (v41 i32) (add.wrapping v35 v40)) + (let (v42 (ptr i32)) (global.symbol #__stack_pointer)) + (store v42 v41) + (ret v43)) ) (func (export #test_blake3_hash_1to1) (param i32) (param i32) @@ -1824,6 +1832,74 @@ (br (block 12 v78 v80 v100 v91))) ) + (func (export #miden_sdk_tx_kernel::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) (cast v4)) + (let (v11 u32) (add.checked v10 12)) + (let (v12 (ptr i32)) (inttoptr v11)) + (let (v13 i32) (load v12)) + (let (v14 u32) (cast v4)) + (let (v15 u32) (add.checked v14 8)) + (let (v16 (ptr i32)) (inttoptr v15)) + (let (v17 i32) (load v16)) + (let (v18 u32) (cast v4)) + (let (v19 u32) (add.checked v18 4)) + (let (v20 (ptr i32)) (inttoptr v19)) + (let (v21 i32) (load v20)) + (let (v22 i1) (eq v21 0)) + (let (v23 i32) (cast v22)) + (let (v24 i1) (neq v23 0)) + (condbr v24 (block 3) (block 4))) + + (block 1) + + (block 2 + (call #alloc::raw_vec::capacity_overflow) + (unreachable)) + + (block 3 + (let [(v28 i32) (v29 felt)] (call (#miden:tx_kernel/note #get_inputs) v13)) + (let (v30 i32) (const.i32 0)) + (let (v31 u32) (cast v0)) + (let (v32 u32) (add.checked v31 8)) + (let (v33 (ptr i32)) (inttoptr v32)) + (store v33 v30) + (let (v34 u32) (cast v0)) + (let (v35 u32) (add.checked v34 4)) + (let (v36 (ptr i32)) (inttoptr v35)) + (store v36 v13) + (let (v37 u32) (cast v0)) + (let (v38 (ptr i32)) (inttoptr v37)) + (store v38 v17) + (let (v39 i32) (const.i32 16)) + (let (v40 i32) (add.wrapping v4 v39)) + (let (v41 (ptr i32)) (global.symbol #__stack_pointer)) + (store v41 v40) + (ret)) + + (block 4 + (let (v25 i1) (eq v17 0)) + (let (v26 i32) (cast v25)) + (let (v27 i1) (neq v26 0)) + (condbr v27 (block 2) (block 5))) + + (block 5 + (call #alloc::alloc::handle_alloc_error v17 v13) + (unreachable)) + ) + (func (export #miden_sdk_tx_kernel::add_assets) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) 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 b42bf1f7b..46496f7ee 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 @@ -8,10 +8,10 @@ (type (;6;) (func (param f64))) (type (;7;) (func (param f64) (result f64))) (type (;8;) (func (param f64 f64))) - (type (;9;) (func (param i32) (result i32))) - (type (;10;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 i32))) - (type (;11;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) - (type (;12;) (func (param f64 f64 f64 f64 f64 f64 f64 f64))) + (type (;9;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;10;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;11;) (func (param f64 f64 f64 f64 f64 f64 f64 f64))) + (type (;12;) (func (param i32) (result i32))) (type (;13;) (func (param f64 f64 f64 f64 i32))) (type (;14;) (func (param f64 f64 i32))) (type (;15;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) @@ -45,10 +45,10 @@ (import "miden:prelude/intrinsics_felt" "div" (func $miden_prelude::intrinsics::felt::extern_div (;17;) (type 2))) (import "miden:prelude/intrinsics_felt" "assert_eq" (func $miden_prelude::intrinsics::felt::extern_assert_eq (;18;) (type 8))) (import "miden:prelude/intrinsics_felt" "neg" (func $miden_prelude::intrinsics::felt::extern_neg (;19;) (type 7))) - (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_note_get_inputs (;20;) (type 9))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;21;) (type 10))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;22;) (type 11))) - (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;23;) (type 12))) + (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;20;) (type 9))) + (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;21;) (type 10))) + (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;22;) (type 11))) + (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_note_get_inputs (;23;) (type 12))) (import "miden:tx_kernel/account" "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_account_add_asset (;24;) (type 13))) (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;25;) (type 14))) (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;26;) (type 15))) @@ -279,9 +279,9 @@ call $miden_prelude::intrinsics::felt::extern_neg ) (func $note_script (;35;) (type 0) (result f64) - (local i32 f64 i32 i32 i32) + (local i32 f64 i32 i32 i32 i32) global.get $__stack_pointer - i32.const 2048 + i32.const 16 i32.sub local.tee 0 global.set $__stack_pointer @@ -289,56 +289,57 @@ call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked local.set 1 local.get 0 + call $miden_sdk_tx_kernel::get_inputs + local.get 0 + i32.load + local.set 2 local.get 0 local.get 0 - call $miden_sdk_tx_kernel::extern_note_get_inputs - local.tee 2 + i32.load offset=4 + local.tee 3 + local.get 0 + i32.load offset=8 i32.const 3 i32.shl - local.tee 3 + local.tee 4 i32.add + local.tee 5 i32.store offset=12 local.get 0 local.get 2 i32.store offset=8 local.get 0 - local.get 0 - i32.store offset=4 - local.get 0 - local.get 0 + local.get 3 i32.store - local.get 0 - local.set 2 loop (result f64) ;; label = @1 block ;; label = @2 - local.get 3 + local.get 4 br_if 0 (;@2;) local.get 0 + local.get 5 + i32.store offset=4 + local.get 0 call $ as core::ops::drop::Drop>::drop local.get 0 - i32.const 2048 + i32.const 16 i32.add global.set $__stack_pointer local.get 1 return end - local.get 0 - local.get 2 - i32.const 8 - i32.add - local.tee 4 - i32.store offset=4 - local.get 3 + local.get 4 i32.const -8 i32.add - local.set 3 + local.set 4 local.get 1 - local.get 2 + local.get 3 f64.load call $miden_prelude::intrinsics::felt::extern_add local.set 1 - local.get 4 - local.set 2 + local.get 3 + i32.const 8 + i32.add + local.set 3 br 0 (;@1;) end ) @@ -1381,7 +1382,61 @@ i32.store end ) - (func $miden_sdk_tx_kernel::add_assets (;48;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::get_inputs (;48;) (type 16) (param i32) + (local i32 i32 i32) + global.get $__stack_pointer + i32.const 16 + i32.sub + local.tee 1 + global.set $__stack_pointer + local.get 1 + i32.const 4 + i32.add + i32.const 256 + i32.const 0 + call $alloc::raw_vec::RawVec::try_allocate_in + local.get 1 + i32.load offset=12 + local.set 2 + local.get 1 + i32.load offset=8 + local.set 3 + block ;; label = @1 + block ;; label = @2 + local.get 1 + i32.load offset=4 + i32.eqz + br_if 0 (;@2;) + local.get 3 + i32.eqz + br_if 1 (;@1;) + local.get 3 + local.get 2 + call $alloc::alloc::handle_alloc_error + unreachable + end + local.get 2 + call $miden_sdk_tx_kernel::extern_note_get_inputs + drop + local.get 0 + i32.const 0 + i32.store offset=8 + local.get 0 + local.get 2 + i32.store offset=4 + local.get 0 + local.get 3 + i32.store + local.get 1 + i32.const 16 + i32.add + global.set $__stack_pointer + return + end + call $alloc::raw_vec::capacity_overflow + unreachable + ) + (func $miden_sdk_tx_kernel::add_assets (;49;) (type 19) (param i32 i32) (local i32) global.get $__stack_pointer i32.const 32 @@ -1431,7 +1486,7 @@ i32.add global.set $__stack_pointer ) - (func $alloc::vec::Vec::with_capacity (;49;) (type 19) (param i32 i32) + (func $alloc::vec::Vec::with_capacity (;50;) (type 19) (param i32 i32) (local i32 i32) global.get $__stack_pointer i32.const 16 @@ -1482,7 +1537,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $alloc::raw_vec::RawVec::try_allocate_in (;50;) (type 18) (param i32 i32 i32) + (func $alloc::raw_vec::RawVec::try_allocate_in (;51;) (type 18) (param i32 i32 i32) (local i32) block ;; label = @1 block ;; label = @2 @@ -1556,12 +1611,12 @@ local.get 1 i32.store ) - (func $>::from (;51;) (type 23) (param i32) (result f64) + (func $>::from (;52;) (type 23) (param i32) (result f64) local.get 0 i64.extend_i32_u call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked ) - (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;52;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;53;) (type 20) (param i32 f64) (local i32) global.get $__stack_pointer i32.const 64 @@ -1618,7 +1673,7 @@ i32.add global.set $__stack_pointer ) - (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;53;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;54;) (type 20) (param i32 f64) (local i32 i32 f64 f64) global.get $__stack_pointer i32.const 128 @@ -1696,19 +1751,19 @@ i32.add global.set $__stack_pointer ) - (func $alloc::alloc::handle_alloc_error (;54;) (type 19) (param i32 i32) + (func $alloc::alloc::handle_alloc_error (;55;) (type 19) (param i32 i32) unreachable unreachable ) - (func $alloc::raw_vec::capacity_overflow (;55;) (type 24) + (func $alloc::raw_vec::capacity_overflow (;56;) (type 24) unreachable unreachable ) - (func $core::slice::::copy_from_slice::len_mismatch_fail (;56;) (type 18) (param i32 i32 i32) + (func $core::slice::::copy_from_slice::len_mismatch_fail (;57;) (type 18) (param i32 i32 i32) unreachable unreachable ) - (func $core::slice::::copy_from_slice (;57;) (type 25) (param i32 i32 i32 i32 i32) + (func $core::slice::::copy_from_slice (;58;) (type 25) (param i32 i32 i32 i32 i32) block ;; label = @1 local.get 1 local.get 3 From 8187532202c07f20cd69668189330b170aee63c5 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Thu, 11 Apr 2024 16:24:54 +0300 Subject: [PATCH 2/5] fix `tx_kernel::add_asset` function name --- sdk/tx-kernel/src/lib.rs | 2 +- .../expected/rust_sdk_account_test/miden_sdk_account_test.hir | 4 ++-- .../expected/rust_sdk_account_test/miden_sdk_account_test.wat | 4 ++-- tests/rust-apps-wasm/rust-sdk/account-test/src/lib.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/tx-kernel/src/lib.rs b/sdk/tx-kernel/src/lib.rs index 695d71cc8..9554ea43f 100644 --- a/sdk/tx-kernel/src/lib.rs +++ b/sdk/tx-kernel/src/lib.rs @@ -72,7 +72,7 @@ impl CoreAsset { } } -pub fn add_assets(asset: CoreAsset) -> CoreAsset { +pub fn add_asset(asset: CoreAsset) -> CoreAsset { unsafe { let mut ret_area = ::core::mem::MaybeUninit::::uninit(); extern_account_add_asset( 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 e5c5c90dd..86cf97628 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 @@ -195,7 +195,7 @@ (store v25 v8) (let (v26 i32) (const.i32 32)) (let (v27 i32) (add.wrapping v5 v26)) - (call #miden_sdk_tx_kernel::add_assets v27 v5) + (call #miden_sdk_tx_kernel::add_asset v27 v5) (let (v28 u32) (cast v5)) (let (v29 u32) (add.checked v28 32)) (let (v30 (ptr felt)) (inttoptr v29)) @@ -1900,7 +1900,7 @@ (unreachable)) ) - (func (export #miden_sdk_tx_kernel::add_assets) + (func (export #miden_sdk_tx_kernel::add_asset) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) (let (v2 i32) (const.i32 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 46496f7ee..6a8fb6cf7 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 @@ -179,7 +179,7 @@ i32.const 32 i32.add local.get 0 - call $miden_sdk_tx_kernel::add_assets + call $miden_sdk_tx_kernel::add_asset local.get 0 f64.load offset=32 local.set 1 @@ -1436,7 +1436,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $miden_sdk_tx_kernel::add_assets (;49;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::add_asset (;49;) (type 19) (param i32 i32) (local i32) global.get $__stack_pointer i32.const 32 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 5b71e89a7..0ac296839 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 @@ -26,7 +26,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_assets(asset_in); + let asset_out = add_asset(asset_in); asset_out.as_word()[0] } From 672a7aea968b89ee1612430fbc7a9ffd2d502ec7 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Fri, 12 Apr 2024 15:06:56 +0300 Subject: [PATCH 3/5] refactor: tx-kernel: move externs and types to separate files --- sdk/tx-kernel/src/externs.rs | 17 +++++ sdk/tx-kernel/src/lib.rs | 63 +++++-------------- sdk/tx-kernel/src/types.rs | 26 ++++++++ .../miden_sdk_account_test.wat | 12 ++-- 4 files changed, 63 insertions(+), 55 deletions(-) create mode 100644 sdk/tx-kernel/src/externs.rs create mode 100644 sdk/tx-kernel/src/types.rs diff --git a/sdk/tx-kernel/src/externs.rs b/sdk/tx-kernel/src/externs.rs new file mode 100644 index 000000000..20a0b6863 --- /dev/null +++ b/sdk/tx-kernel/src/externs.rs @@ -0,0 +1,17 @@ +use miden_prelude::Felt; + +use crate::{AccountId, CoreAsset}; + +#[link(wasm_import_module = "miden:tx_kernel/account")] +extern "C" { + #[link_name = "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>"] + pub fn extern_account_get_id() -> AccountId; + #[link_name = "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>"] + pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); +} + +#[link(wasm_import_module = "miden:tx_kernel/note")] +extern "C" { + #[link_name = "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>"] + pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize; +} diff --git a/sdk/tx-kernel/src/lib.rs b/sdk/tx-kernel/src/lib.rs index 9554ea43f..95eb37c04 100644 --- a/sdk/tx-kernel/src/lib.rs +++ b/sdk/tx-kernel/src/lib.rs @@ -1,46 +1,26 @@ #![no_std] -#![allow(dead_code)] -extern crate alloc; - -use alloc::vec::Vec; +mod externs; +use externs::*; -use miden_prelude::{Felt, Word}; +mod types; +pub use types::*; -#[repr(transparent)] -#[derive(Copy, Clone)] -pub struct AccountId(Felt); - -impl From for Felt { - fn from(account_id: AccountId) -> Felt { - account_id.0 - } -} +extern crate alloc; -#[link(wasm_import_module = "miden:tx_kernel/account")] -extern "C" { - #[link_name = "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>"] - fn extern_account_get_id() -> AccountId; - #[link_name = "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>"] - fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); -} +use alloc::vec::Vec; -#[link(wasm_import_module = "miden:tx_kernel/note")] -extern "C" { - #[link_name = "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>"] - fn extern_note_get_inputs(ptr: *mut Felt) -> usize; -} +use miden_prelude::Felt; #[inline(always)] pub fn get_id() -> AccountId { unsafe { extern_account_get_id() } } -const MAX_INPUTS: usize = 256; - pub fn get_inputs() -> Vec { - unsafe { - let mut inputs: Vec = Vec::with_capacity(MAX_INPUTS); + const MAX_INPUTS: usize = 256; + let mut inputs: Vec = Vec::with_capacity(MAX_INPUTS); + let num_inputs = unsafe { // 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 @@ -51,25 +31,10 @@ pub fn get_inputs() -> Vec { // #! - 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 - let num_inputs = extern_note_get_inputs(inputs.as_mut_ptr()); - inputs.truncate(num_inputs); - inputs - } -} - -#[repr(transparent)] -pub struct CoreAsset { - inner: Word, -} - -impl CoreAsset { - pub fn new(word: Word) -> Self { - CoreAsset { inner: word } - } - - pub fn as_word(&self) -> Word { - self.inner - } + extern_note_get_inputs(inputs.as_mut_ptr()) + }; + inputs.truncate(num_inputs); + inputs } pub fn add_asset(asset: CoreAsset) -> CoreAsset { diff --git a/sdk/tx-kernel/src/types.rs b/sdk/tx-kernel/src/types.rs new file mode 100644 index 000000000..5e5193558 --- /dev/null +++ b/sdk/tx-kernel/src/types.rs @@ -0,0 +1,26 @@ +use miden_prelude::{Felt, Word}; + +#[repr(transparent)] +#[derive(Copy, Clone)] +pub struct AccountId(Felt); + +impl From for Felt { + fn from(account_id: AccountId) -> Felt { + account_id.0 + } +} + +#[repr(transparent)] +pub struct CoreAsset { + pub(crate) inner: Word, +} + +impl CoreAsset { + pub fn new(word: Word) -> Self { + CoreAsset { inner: word } + } + + pub fn as_word(&self) -> Word { + self.inner + } +} 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 6a8fb6cf7..4ed7f6422 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 @@ -25,7 +25,7 @@ (type (;23;) (func (param i32) (result f64))) (type (;24;) (func)) (type (;25;) (func (param i32 i32 i32 i32 i32))) - (import "miden:tx_kernel/account" "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_account_get_id (;0;) (type 0))) + (import "miden:tx_kernel/account" "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_get_id (;0;) (type 0))) (import "miden:prelude/intrinsics_felt" "from_u64_unchecked" (func $miden_prelude::intrinsics::felt::extern_from_u64_unchecked (;1;) (type 1))) (import "miden:prelude/intrinsics_felt" "add" (func $miden_prelude::intrinsics::felt::extern_add (;2;) (type 2))) (import "miden:prelude/intrinsics_felt" "as_u64" (func $miden_prelude::intrinsics::felt::extern_as_u64 (;3;) (type 3))) @@ -48,8 +48,8 @@ (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;20;) (type 9))) (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;21;) (type 10))) (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;22;) (type 11))) - (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_note_get_inputs (;23;) (type 12))) - (import "miden:tx_kernel/account" "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::extern_account_add_asset (;24;) (type 13))) + (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_note_get_inputs (;23;) (type 12))) + (import "miden:tx_kernel/account" "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_add_asset (;24;) (type 13))) (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;25;) (type 14))) (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;26;) (type 15))) (func $< as core::ops::drop::Drop>::drop::DropGuard as core::ops::drop::Drop>::drop (;27;) (type 16) (param i32) @@ -139,7 +139,7 @@ ) (func $get_wallet_magic_number (;32;) (type 0) (result f64) (local f64) - call $miden_sdk_tx_kernel::extern_account_get_id + call $miden_sdk_tx_kernel::externs::extern_account_get_id local.set 0 i64.const 42 call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked @@ -1416,7 +1416,7 @@ unreachable end local.get 2 - call $miden_sdk_tx_kernel::extern_note_get_inputs + call $miden_sdk_tx_kernel::externs::extern_note_get_inputs drop local.get 0 i32.const 0 @@ -1452,7 +1452,7 @@ local.get 1 f64.load offset=24 local.get 2 - call $miden_sdk_tx_kernel::extern_account_add_asset + call $miden_sdk_tx_kernel::externs::extern_account_add_asset local.get 0 i32.const 24 i32.add From 1b76291418947610bf5da443fc179be837bb9b9a Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Fri, 12 Apr 2024 15:30:38 +0300 Subject: [PATCH 4/5] feat: add tx_kernel::account::remove_asset() transformation, refactor tx_kernel function types into separate module --- frontend-wasm/src/miden_abi/transform.rs | 7 +- frontend-wasm/src/miden_abi/tx_kernel.rs | 31 +-- .../src/miden_abi/tx_kernel/account.rs | 22 ++ frontend-wasm/src/miden_abi/tx_kernel/note.rs | 14 ++ sdk/tx-kernel/src/externs.rs | 2 + sdk/tx-kernel/src/lib.rs | 33 ++- .../miden_sdk_account_test.hir | 121 +++++++++- .../miden_sdk_account_test.wat | 220 ++++++++++++------ .../rust-sdk/account-test/src/lib.rs | 6 + 9 files changed, 352 insertions(+), 104 deletions(-) create mode 100644 frontend-wasm/src/miden_abi/tx_kernel/account.rs create mode 100644 frontend-wasm/src/miden_abi/tx_kernel/note.rs diff --git a/frontend-wasm/src/miden_abi/transform.rs b/frontend-wasm/src/miden_abi/transform.rs index d79b08350..eeaf1b07d 100644 --- a/frontend-wasm/src/miden_abi/transform.rs +++ b/frontend-wasm/src/miden_abi/transform.rs @@ -17,9 +17,10 @@ enum TransformStrategy { /// Get the transformation strategy for a function name fn get_transform_strategy(function_id: &str) -> TransformStrategy { match function_id { - tx_kernel::NOTE_GET_INPUTS => TransformStrategy::ListReturn, - tx_kernel::ACCOUNT_ADD_ASSET => TransformStrategy::ReturnViaPointer, - tx_kernel::ACCOUNT_GET_ID => TransformStrategy::NoTransform, + tx_kernel::note::GET_INPUTS => TransformStrategy::ListReturn, + tx_kernel::account::ADD_ASSET => TransformStrategy::ReturnViaPointer, + tx_kernel::account::REMOVE_ASSET => TransformStrategy::ReturnViaPointer, + tx_kernel::account::GET_ID => TransformStrategy::NoTransform, stdlib::crypto::hashes::BLAKE3_HASH_1TO1 => TransformStrategy::ReturnViaPointer, stdlib::crypto::hashes::BLAKE3_HASH_2TO1 => TransformStrategy::ReturnViaPointer, stdlib::crypto::dsa::RPO_FALCON512_VERIFY => TransformStrategy::NoTransform, diff --git a/frontend-wasm/src/miden_abi/tx_kernel.rs b/frontend-wasm/src/miden_abi/tx_kernel.rs index 637327bc4..6ff924f5a 100644 --- a/frontend-wasm/src/miden_abi/tx_kernel.rs +++ b/frontend-wasm/src/miden_abi/tx_kernel.rs @@ -1,39 +1,18 @@ //! Function types and lowering for tx kernel API functions -#![allow(missing_docs)] -use std::sync::OnceLock; +pub(crate) mod account; +pub(crate) mod note; -use miden_hir::FunctionType; -use miden_hir_type::Type::*; +use std::sync::OnceLock; use super::ModuleFunctionTypeMap; -use crate::miden_abi::FunctionTypeMap; - -pub const NOTE_MODULE_NAME: &str = "miden:tx_kernel/note"; -pub const NOTE_GET_INPUTS: &str = "get_inputs"; - -pub const ACCOUNT_MODULE_NAME: &str = "miden:tx_kernel/account"; -pub const ACCOUNT_ADD_ASSET: &str = "add_asset"; -pub const ACCOUNT_GET_ID: &str = "get_id"; pub(crate) fn signatures() -> &'static ModuleFunctionTypeMap { static TYPES: OnceLock = OnceLock::new(); TYPES.get_or_init(|| { let mut m: ModuleFunctionTypeMap = Default::default(); - - let mut note: FunctionTypeMap = Default::default(); - note.insert(NOTE_GET_INPUTS, FunctionType::new([Felt], [I32, Felt])); - m.insert(NOTE_MODULE_NAME, note); - - let mut account: FunctionTypeMap = Default::default(); - account.insert( - ACCOUNT_ADD_ASSET, - // Accepts and returns word - FunctionType::new([Felt, Felt, Felt, Felt], [Felt, Felt, Felt, Felt]), - ); - account.insert(ACCOUNT_GET_ID, FunctionType::new([], [Felt])); - m.insert(ACCOUNT_MODULE_NAME, account); - + m.extend(account::signatures()); + m.extend(note::signatures()); m }) } diff --git a/frontend-wasm/src/miden_abi/tx_kernel/account.rs b/frontend-wasm/src/miden_abi/tx_kernel/account.rs new file mode 100644 index 000000000..71bdffc8c --- /dev/null +++ b/frontend-wasm/src/miden_abi/tx_kernel/account.rs @@ -0,0 +1,22 @@ +use miden_hir::FunctionType; +use miden_hir_type::Type::*; + +use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap}; + +pub const ADD_ASSET: &str = "add_asset"; +pub const REMOVE_ASSET: &str = "remove_asset"; +pub const GET_ID: &str = "get_id"; + +pub(crate) fn signatures() -> ModuleFunctionTypeMap { + let mut m: ModuleFunctionTypeMap = Default::default(); + let mut account: FunctionTypeMap = Default::default(); + account + .insert(ADD_ASSET, FunctionType::new([Felt, Felt, Felt, Felt], [Felt, Felt, Felt, Felt])); + account.insert( + REMOVE_ASSET, + FunctionType::new([Felt, Felt, Felt, Felt], [Felt, Felt, Felt, Felt]), + ); + account.insert(GET_ID, FunctionType::new([], [Felt])); + m.insert("miden:tx_kernel/account", account); + m +} diff --git a/frontend-wasm/src/miden_abi/tx_kernel/note.rs b/frontend-wasm/src/miden_abi/tx_kernel/note.rs new file mode 100644 index 000000000..b05019a52 --- /dev/null +++ b/frontend-wasm/src/miden_abi/tx_kernel/note.rs @@ -0,0 +1,14 @@ +use miden_hir::FunctionType; +use miden_hir_type::Type::*; + +use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap}; + +pub const GET_INPUTS: &str = "get_inputs"; + +pub(crate) fn signatures() -> ModuleFunctionTypeMap { + let mut m: ModuleFunctionTypeMap = Default::default(); + let mut note: FunctionTypeMap = Default::default(); + note.insert(GET_INPUTS, FunctionType::new([Felt], [I32, Felt])); + m.insert("miden:tx_kernel/note", note); + m +} diff --git a/sdk/tx-kernel/src/externs.rs b/sdk/tx-kernel/src/externs.rs index 20a0b6863..c7e35bbdc 100644 --- a/sdk/tx-kernel/src/externs.rs +++ b/sdk/tx-kernel/src/externs.rs @@ -8,6 +8,8 @@ extern "C" { pub fn extern_account_get_id() -> AccountId; #[link_name = "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>"] pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); + #[link_name = "remove_asset<0x0000000000000000000000000000000000000000000000000000000000000000>"] + pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); } #[link(wasm_import_module = "miden:tx_kernel/note")] diff --git a/sdk/tx-kernel/src/lib.rs b/sdk/tx-kernel/src/lib.rs index 95eb37c04..a10acc603 100644 --- a/sdk/tx-kernel/src/lib.rs +++ b/sdk/tx-kernel/src/lib.rs @@ -12,11 +12,12 @@ use alloc::vec::Vec; use miden_prelude::Felt; -#[inline(always)] +/// 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); @@ -37,6 +38,16 @@ pub fn get_inputs() -> Vec { 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(); @@ -50,3 +61,23 @@ pub fn add_asset(asset: CoreAsset) -> CoreAsset { 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/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 86cf97628..9547eae0c 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 @@ -7,6 +7,7 @@ (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (param felt) (result felt felt felt felt felt)))) (#miden:prelude/std_mem #pipe_words_to_memory) (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)))) (#miden:tx_kernel/account #add_asset) (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (result felt)))) (#miden:tx_kernel/account #get_id) + (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)))) (#miden:tx_kernel/account #remove_asset) (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (result i32 felt)))) (#miden:tx_kernel/note #get_inputs) ;; Modules @@ -151,7 +152,7 @@ (func (export #get_wallet_magic_number) (result felt) (block 0 (let (v1 felt) (const.felt 0)) - (let (v2 felt) (call (#miden:tx_kernel/account #get_id))) + (let (v2 felt) (call #miden_sdk_tx_kernel::get_id)) (let (v3 i64) (const.i64 42)) (let (v4 felt) (cast v3)) (let (v5 felt) (add.unchecked v4 v2)) @@ -1067,6 +1068,29 @@ (ret)) ) + (func (export #test_remove_asset) (param i32) (result felt) + (block 0 (param v0 i32) + (let (v2 i32) (const.i32 0)) + (let (v3 felt) (const.felt 0)) + (let (v4 i32) (global.load i32 (global.symbol #__stack_pointer))) + (let (v5 i32) (const.i32 32)) + (let (v6 i32) (sub.wrapping v4 v5)) + (let (v7 (ptr i32)) (global.symbol #__stack_pointer)) + (store v7 v6) + (call #miden_sdk_tx_kernel::remove_asset v6 v0) + (let (v8 u32) (cast v6)) + (let (v9 (ptr felt)) (inttoptr v8)) + (let (v10 felt) (load v9)) + (let (v11 i32) (const.i32 32)) + (let (v12 i32) (add.wrapping v6 v11)) + (let (v13 (ptr i32)) (global.symbol #__stack_pointer)) + (store v13 v12) + (br (block 1 v10))) + + (block 1 (param v1 felt) + (ret v1)) + ) + (func (export #__rust_alloc) (param i32) (param i32) (result i32) (block 0 (param v0 i32) (param v1 i32) (let (v3 i32) (const.i32 1048652)) @@ -1832,6 +1856,16 @@ (br (block 12 v78 v80 v100 v91))) ) + (func (export #miden_sdk_tx_kernel::get_id) + (result felt) + (block 0 + (let (v1 felt) (call (#miden:tx_kernel/account #get_id))) + (br (block 1 v1))) + + (block 1 (param v0 felt) + (ret v0)) + ) + (func (export #miden_sdk_tx_kernel::get_inputs) (param i32) (block 0 (param v0 i32) @@ -1983,6 +2017,89 @@ (ret)) ) + (func (export #miden_sdk_tx_kernel::remove_asset) + (param i32) (param i32) + (block 0 (param v0 i32) (param v1 i32) + (let (v2 i32) (const.i32 0)) + (let (v3 i32) (global.load i32 (global.symbol #__stack_pointer))) + (let (v4 i32) (const.i32 32)) + (let (v5 i32) (sub.wrapping v3 v4)) + (let (v6 (ptr i32)) (global.symbol #__stack_pointer)) + (store v6 v5) + (let (v7 u32) (cast v1)) + (let (v8 (ptr felt)) (inttoptr v7)) + (let (v9 felt) (load v8)) + (let (v10 u32) (cast v1)) + (let (v11 u32) (add.checked v10 8)) + (let (v12 (ptr felt)) (inttoptr v11)) + (let (v13 felt) (load v12)) + (let (v14 u32) (cast v1)) + (let (v15 u32) (add.checked v14 16)) + (let (v16 (ptr felt)) (inttoptr v15)) + (let (v17 felt) (load v16)) + (let (v18 u32) (cast v1)) + (let (v19 u32) (add.checked v18 24)) + (let (v20 (ptr felt)) (inttoptr v19)) + (let (v21 felt) (load v20)) + (let [(v22 felt) (v23 felt) (v24 felt) (v25 felt)] (call (#miden:tx_kernel/account #remove_asset) v9 v13 v17 v21)) + (let (v26 u32) (cast v5)) + (let (v27 (ptr felt)) (inttoptr v26)) + (store v27 v22) + (let (v28 u32) (add.checked v26 8)) + (let (v29 (ptr felt)) (inttoptr v28)) + (store v29 v23) + (let (v30 u32) (add.checked v26 16)) + (let (v31 (ptr felt)) (inttoptr v30)) + (store v31 v24) + (let (v32 u32) (add.checked v26 24)) + (let (v33 (ptr felt)) (inttoptr v32)) + (store v33 v25) + (let (v34 i32) (const.i32 24)) + (let (v35 i32) (add.wrapping v0 v34)) + (let (v36 i32) (const.i32 24)) + (let (v37 i32) (add.wrapping v5 v36)) + (let (v38 u32) (cast v37)) + (let (v39 (ptr i64)) (inttoptr v38)) + (let (v40 i64) (load v39)) + (let (v41 u32) (cast v35)) + (let (v42 (ptr i64)) (inttoptr v41)) + (store v42 v40) + (let (v43 i32) (const.i32 16)) + (let (v44 i32) (add.wrapping v0 v43)) + (let (v45 i32) (const.i32 16)) + (let (v46 i32) (add.wrapping v5 v45)) + (let (v47 u32) (cast v46)) + (let (v48 (ptr i64)) (inttoptr v47)) + (let (v49 i64) (load v48)) + (let (v50 u32) (cast v44)) + (let (v51 (ptr i64)) (inttoptr v50)) + (store v51 v49) + (let (v52 i32) (const.i32 8)) + (let (v53 i32) (add.wrapping v0 v52)) + (let (v54 i32) (const.i32 8)) + (let (v55 i32) (add.wrapping v5 v54)) + (let (v56 u32) (cast v55)) + (let (v57 (ptr i64)) (inttoptr v56)) + (let (v58 i64) (load v57)) + (let (v59 u32) (cast v53)) + (let (v60 (ptr i64)) (inttoptr v59)) + (store v60 v58) + (let (v61 u32) (cast v5)) + (let (v62 (ptr i64)) (inttoptr v61)) + (let (v63 i64) (load v62)) + (let (v64 u32) (cast v0)) + (let (v65 (ptr i64)) (inttoptr v64)) + (store v65 v63) + (let (v66 i32) (const.i32 32)) + (let (v67 i32) (add.wrapping v5 v66)) + (let (v68 (ptr i32)) (global.symbol #__stack_pointer)) + (store v68 v67) + (br (block 1))) + + (block 1 + (ret)) + ) + (func (export #alloc::vec::Vec::with_capacity) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) @@ -2456,6 +2573,8 @@ (func (import #miden:tx_kernel/account #add_asset) (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)) (func (import #miden:tx_kernel/account #get_id) (result felt)) + (func (import #miden:tx_kernel/account #remove_asset) + (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)) (func (import #miden:tx_kernel/note #get_inputs) (param felt) (result i32 felt)) ) 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 4ed7f6422..532a7e6c0 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 @@ -1,16 +1,16 @@ (module $miden_sdk_account_test.wasm - (type (;0;) (func (result f64))) - (type (;1;) (func (param i64) (result f64))) - (type (;2;) (func (param f64 f64) (result f64))) - (type (;3;) (func (param f64) (result i64))) - (type (;4;) (func (param f64 f64) (result i32))) - (type (;5;) (func (param f64) (result i32))) - (type (;6;) (func (param f64))) - (type (;7;) (func (param f64) (result f64))) - (type (;8;) (func (param f64 f64))) - (type (;9;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 i32))) - (type (;10;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) - (type (;11;) (func (param f64 f64 f64 f64 f64 f64 f64 f64))) + (type (;0;) (func (param i64) (result f64))) + (type (;1;) (func (param f64 f64) (result f64))) + (type (;2;) (func (param f64) (result i64))) + (type (;3;) (func (param f64 f64) (result i32))) + (type (;4;) (func (param f64) (result i32))) + (type (;5;) (func (param f64))) + (type (;6;) (func (param f64) (result f64))) + (type (;7;) (func (param f64 f64))) + (type (;8;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;9;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;10;) (func (param f64 f64 f64 f64 f64 f64 f64 f64))) + (type (;11;) (func (result f64))) (type (;12;) (func (param i32) (result i32))) (type (;13;) (func (param f64 f64 f64 f64 i32))) (type (;14;) (func (param f64 f64 i32))) @@ -20,39 +20,40 @@ (type (;18;) (func (param i32 i32 i32))) (type (;19;) (func (param i32 i32))) (type (;20;) (func (param i32 f64))) - (type (;21;) (func (param i32 i32) (result i32))) - (type (;22;) (func (param i32 i32 i32 i32))) - (type (;23;) (func (param i32) (result f64))) + (type (;21;) (func (param i32) (result f64))) + (type (;22;) (func (param i32 i32) (result i32))) + (type (;23;) (func (param i32 i32 i32 i32))) (type (;24;) (func)) (type (;25;) (func (param i32 i32 i32 i32 i32))) - (import "miden:tx_kernel/account" "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_get_id (;0;) (type 0))) - (import "miden:prelude/intrinsics_felt" "from_u64_unchecked" (func $miden_prelude::intrinsics::felt::extern_from_u64_unchecked (;1;) (type 1))) - (import "miden:prelude/intrinsics_felt" "add" (func $miden_prelude::intrinsics::felt::extern_add (;2;) (type 2))) - (import "miden:prelude/intrinsics_felt" "as_u64" (func $miden_prelude::intrinsics::felt::extern_as_u64 (;3;) (type 3))) - (import "miden:prelude/intrinsics_felt" "gt" (func $miden_prelude::intrinsics::felt::extern_gt (;4;) (type 4))) - (import "miden:prelude/intrinsics_felt" "lt" (func $miden_prelude::intrinsics::felt::extern_lt (;5;) (type 4))) - (import "miden:prelude/intrinsics_felt" "le" (func $miden_prelude::intrinsics::felt::extern_le (;6;) (type 4))) - (import "miden:prelude/intrinsics_felt" "ge" (func $miden_prelude::intrinsics::felt::extern_ge (;7;) (type 4))) - (import "miden:prelude/intrinsics_felt" "eq" (func $miden_prelude::intrinsics::felt::extern_eq (;8;) (type 4))) - (import "miden:prelude/intrinsics_felt" "is_odd" (func $miden_prelude::intrinsics::felt::extern_is_odd (;9;) (type 5))) - (import "miden:prelude/intrinsics_felt" "assertz" (func $miden_prelude::intrinsics::felt::extern_assertz (;10;) (type 6))) - (import "miden:prelude/intrinsics_felt" "assert" (func $miden_prelude::intrinsics::felt::extern_assert (;11;) (type 6))) - (import "miden:prelude/intrinsics_felt" "inv" (func $miden_prelude::intrinsics::felt::extern_inv (;12;) (type 7))) - (import "miden:prelude/intrinsics_felt" "exp" (func $miden_prelude::intrinsics::felt::extern_exp (;13;) (type 2))) - (import "miden:prelude/intrinsics_felt" "sub" (func $miden_prelude::intrinsics::felt::extern_sub (;14;) (type 2))) - (import "miden:prelude/intrinsics_felt" "pow2" (func $miden_prelude::intrinsics::felt::extern_pow2 (;15;) (type 7))) - (import "miden:prelude/intrinsics_felt" "mul" (func $miden_prelude::intrinsics::felt::extern_mul (;16;) (type 2))) - (import "miden:prelude/intrinsics_felt" "div" (func $miden_prelude::intrinsics::felt::extern_div (;17;) (type 2))) - (import "miden:prelude/intrinsics_felt" "assert_eq" (func $miden_prelude::intrinsics::felt::extern_assert_eq (;18;) (type 8))) - (import "miden:prelude/intrinsics_felt" "neg" (func $miden_prelude::intrinsics::felt::extern_neg (;19;) (type 7))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;20;) (type 9))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;21;) (type 10))) - (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;22;) (type 11))) + (import "miden:prelude/intrinsics_felt" "from_u64_unchecked" (func $miden_prelude::intrinsics::felt::extern_from_u64_unchecked (;0;) (type 0))) + (import "miden:prelude/intrinsics_felt" "add" (func $miden_prelude::intrinsics::felt::extern_add (;1;) (type 1))) + (import "miden:prelude/intrinsics_felt" "as_u64" (func $miden_prelude::intrinsics::felt::extern_as_u64 (;2;) (type 2))) + (import "miden:prelude/intrinsics_felt" "gt" (func $miden_prelude::intrinsics::felt::extern_gt (;3;) (type 3))) + (import "miden:prelude/intrinsics_felt" "lt" (func $miden_prelude::intrinsics::felt::extern_lt (;4;) (type 3))) + (import "miden:prelude/intrinsics_felt" "le" (func $miden_prelude::intrinsics::felt::extern_le (;5;) (type 3))) + (import "miden:prelude/intrinsics_felt" "ge" (func $miden_prelude::intrinsics::felt::extern_ge (;6;) (type 3))) + (import "miden:prelude/intrinsics_felt" "eq" (func $miden_prelude::intrinsics::felt::extern_eq (;7;) (type 3))) + (import "miden:prelude/intrinsics_felt" "is_odd" (func $miden_prelude::intrinsics::felt::extern_is_odd (;8;) (type 4))) + (import "miden:prelude/intrinsics_felt" "assertz" (func $miden_prelude::intrinsics::felt::extern_assertz (;9;) (type 5))) + (import "miden:prelude/intrinsics_felt" "assert" (func $miden_prelude::intrinsics::felt::extern_assert (;10;) (type 5))) + (import "miden:prelude/intrinsics_felt" "inv" (func $miden_prelude::intrinsics::felt::extern_inv (;11;) (type 6))) + (import "miden:prelude/intrinsics_felt" "exp" (func $miden_prelude::intrinsics::felt::extern_exp (;12;) (type 1))) + (import "miden:prelude/intrinsics_felt" "sub" (func $miden_prelude::intrinsics::felt::extern_sub (;13;) (type 1))) + (import "miden:prelude/intrinsics_felt" "pow2" (func $miden_prelude::intrinsics::felt::extern_pow2 (;14;) (type 6))) + (import "miden:prelude/intrinsics_felt" "mul" (func $miden_prelude::intrinsics::felt::extern_mul (;15;) (type 1))) + (import "miden:prelude/intrinsics_felt" "div" (func $miden_prelude::intrinsics::felt::extern_div (;16;) (type 1))) + (import "miden:prelude/intrinsics_felt" "assert_eq" (func $miden_prelude::intrinsics::felt::extern_assert_eq (;17;) (type 7))) + (import "miden:prelude/intrinsics_felt" "neg" (func $miden_prelude::intrinsics::felt::extern_neg (;18;) (type 6))) + (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;19;) (type 8))) + (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;20;) (type 9))) + (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;21;) (type 10))) + (import "miden:tx_kernel/account" "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_get_id (;22;) (type 11))) (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_note_get_inputs (;23;) (type 12))) (import "miden:tx_kernel/account" "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_add_asset (;24;) (type 13))) - (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;25;) (type 14))) - (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;26;) (type 15))) - (func $< as core::ops::drop::Drop>::drop::DropGuard as core::ops::drop::Drop>::drop (;27;) (type 16) (param i32) + (import "miden:tx_kernel/account" "remove_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_remove_asset (;25;) (type 13))) + (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;26;) (type 14))) + (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;27;) (type 15))) + (func $< as core::ops::drop::Drop>::drop::DropGuard as core::ops::drop::Drop>::drop (;28;) (type 16) (param i32) (local i32) global.get $__stack_pointer i32.const 16 @@ -78,7 +79,7 @@ i32.add global.set $__stack_pointer ) - (func $ as core::ops::drop::Drop>::drop (;28;) (type 16) (param i32) + (func $ as core::ops::drop::Drop>::drop (;29;) (type 16) (param i32) (local i32) block ;; label = @1 local.get 0 @@ -95,7 +96,7 @@ call $__rust_dealloc end ) - (func $core::alloc::global::GlobalAlloc::alloc_zeroed (;29;) (type 17) (param i32 i32 i32) (result i32) + (func $core::alloc::global::GlobalAlloc::alloc_zeroed (;30;) (type 17) (param i32 i32 i32) (result i32) block ;; label = @1 local.get 0 local.get 1 @@ -111,14 +112,14 @@ end local.get 1 ) - (func $__rust_dealloc (;30;) (type 18) (param i32 i32 i32) + (func $__rust_dealloc (;31;) (type 18) (param i32 i32 i32) i32.const 1048652 local.get 0 local.get 2 local.get 1 call $::dealloc ) - (func $ as core::ops::drop::Drop>::drop (;31;) (type 16) (param i32) + (func $ as core::ops::drop::Drop>::drop (;32;) (type 16) (param i32) (local i32) global.get $__stack_pointer i32.const 16 @@ -137,16 +138,16 @@ i32.add global.set $__stack_pointer ) - (func $get_wallet_magic_number (;32;) (type 0) (result f64) + (func $get_wallet_magic_number (;33;) (type 11) (result f64) (local f64) - call $miden_sdk_tx_kernel::externs::extern_account_get_id + call $miden_sdk_tx_kernel::get_id local.set 0 i64.const 42 call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked local.get 0 call $miden_prelude::intrinsics::felt::extern_add ) - (func $test_add_asset (;33;) (type 0) (result f64) + (func $test_add_asset (;34;) (type 11) (result f64) (local i32 f64 f64 f64) global.get $__stack_pointer i32.const 64 @@ -189,7 +190,7 @@ global.set $__stack_pointer local.get 1 ) - (func $test_felt_ops_smoke (;34;) (type 2) (param f64 f64) (result f64) + (func $test_felt_ops_smoke (;35;) (type 1) (param f64 f64) (result f64) (local i64) local.get 0 call $miden_prelude::intrinsics::felt::extern_as_u64 @@ -278,7 +279,7 @@ local.get 0 call $miden_prelude::intrinsics::felt::extern_neg ) - (func $note_script (;35;) (type 0) (result f64) + (func $note_script (;36;) (type 11) (result f64) (local i32 f64 i32 i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -343,7 +344,7 @@ br 0 (;@1;) end ) - (func $test_blake3_hash_1to1 (;36;) (type 19) (param i32 i32) + (func $test_blake3_hash_1to1 (;37;) (type 19) (param i32 i32) (local i32 i32 f64) global.get $__stack_pointer i32.const 240 @@ -531,7 +532,7 @@ br 0 (;@1;) end ) - (func $test_blake3_hash_2to1 (;37;) (type 18) (param i32 i32 i32) + (func $test_blake3_hash_2to1 (;38;) (type 18) (param i32 i32 i32) (local i32 i32 f64) global.get $__stack_pointer i32.const 432 @@ -788,7 +789,7 @@ br 0 (;@1;) end ) - (func $test_rpo_falcon512_verify (;38;) (type 19) (param i32 i32) + (func $test_rpo_falcon512_verify (;39;) (type 19) (param i32 i32) local.get 0 f64.load local.get 0 @@ -807,29 +808,48 @@ f64.load offset=24 call $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify ) - (func $test_pipe_words_to_memory (;39;) (type 20) (param i32 f64) + (func $test_pipe_words_to_memory (;40;) (type 20) (param i32 f64) local.get 0 local.get 1 call $miden_prelude::stdlib::mem::pipe_words_to_memory ) - (func $test_pipe_double_words_to_memory (;40;) (type 20) (param i32 f64) + (func $test_pipe_double_words_to_memory (;41;) (type 20) (param i32 f64) local.get 0 local.get 1 call $miden_prelude::stdlib::mem::pipe_double_words_to_memory ) - (func $__rust_alloc (;41;) (type 21) (param i32 i32) (result i32) + (func $test_remove_asset (;42;) (type 21) (param i32) (result f64) + (local i32 f64) + global.get $__stack_pointer + i32.const 32 + i32.sub + local.tee 1 + global.set $__stack_pointer + local.get 1 + local.get 0 + call $miden_sdk_tx_kernel::remove_asset + local.get 1 + f64.load + local.set 2 + local.get 1 + i32.const 32 + i32.add + global.set $__stack_pointer + local.get 2 + ) + (func $__rust_alloc (;43;) (type 22) (param i32 i32) (result i32) i32.const 1048652 local.get 1 local.get 0 call $::alloc ) - (func $__rust_alloc_zeroed (;42;) (type 21) (param i32 i32) (result i32) + (func $__rust_alloc_zeroed (;44;) (type 22) (param i32 i32) (result i32) i32.const 1048652 local.get 1 local.get 0 call $core::alloc::global::GlobalAlloc::alloc_zeroed ) - (func $wee_alloc::neighbors::Neighbors::remove (;43;) (type 16) (param i32) + (func $wee_alloc::neighbors::Neighbors::remove (;45;) (type 16) (param i32) (local i32 i32 i32) block ;; label = @1 local.get 0 @@ -896,7 +916,7 @@ i32.and i32.store ) - (func $::new_cell_for_free_list (;44;) (type 22) (param i32 i32 i32 i32) + (func $::new_cell_for_free_list (;46;) (type 23) (param i32 i32 i32 i32) block ;; label = @1 block ;; label = @2 local.get 2 @@ -954,7 +974,7 @@ local.get 3 i32.store ) - (func $wee_alloc::alloc_first_fit (;45;) (type 17) (param i32 i32 i32) (result i32) + (func $wee_alloc::alloc_first_fit (;47;) (type 17) (param i32 i32 i32) (result i32) (local i32 i32 i32 i32 i32 i32) local.get 1 i32.const -1 @@ -1186,7 +1206,7 @@ br 0 (;@1;) end ) - (func $::alloc (;46;) (type 17) (param i32 i32 i32) (result i32) + (func $::alloc (;48;) (type 17) (param i32 i32 i32) (result i32) (local i32 i32) global.get $__stack_pointer i32.const 16 @@ -1280,7 +1300,7 @@ global.set $__stack_pointer local.get 1 ) - (func $::dealloc (;47;) (type 22) (param i32 i32 i32 i32) + (func $::dealloc (;49;) (type 23) (param i32 i32 i32 i32) (local i32 i32 i32) block ;; label = @1 local.get 1 @@ -1382,7 +1402,10 @@ i32.store end ) - (func $miden_sdk_tx_kernel::get_inputs (;48;) (type 16) (param i32) + (func $miden_sdk_tx_kernel::get_id (;50;) (type 11) (result f64) + call $miden_sdk_tx_kernel::externs::extern_account_get_id + ) + (func $miden_sdk_tx_kernel::get_inputs (;51;) (type 16) (param i32) (local i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -1436,7 +1459,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $miden_sdk_tx_kernel::add_asset (;49;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::add_asset (;52;) (type 19) (param i32 i32) (local i32) global.get $__stack_pointer i32.const 32 @@ -1486,7 +1509,57 @@ i32.add global.set $__stack_pointer ) - (func $alloc::vec::Vec::with_capacity (;50;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::remove_asset (;53;) (type 19) (param i32 i32) + (local i32) + global.get $__stack_pointer + i32.const 32 + i32.sub + local.tee 2 + global.set $__stack_pointer + local.get 1 + f64.load + local.get 1 + f64.load offset=8 + local.get 1 + f64.load offset=16 + local.get 1 + f64.load offset=24 + local.get 2 + call $miden_sdk_tx_kernel::externs::extern_account_remove_asset + local.get 0 + i32.const 24 + i32.add + local.get 2 + i32.const 24 + i32.add + i64.load + i64.store + local.get 0 + i32.const 16 + i32.add + local.get 2 + i32.const 16 + i32.add + i64.load + i64.store + local.get 0 + i32.const 8 + i32.add + local.get 2 + i32.const 8 + i32.add + i64.load + i64.store + local.get 0 + local.get 2 + i64.load + i64.store + local.get 2 + i32.const 32 + i32.add + global.set $__stack_pointer + ) + (func $alloc::vec::Vec::with_capacity (;54;) (type 19) (param i32 i32) (local i32 i32) global.get $__stack_pointer i32.const 16 @@ -1537,7 +1610,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $alloc::raw_vec::RawVec::try_allocate_in (;51;) (type 18) (param i32 i32 i32) + (func $alloc::raw_vec::RawVec::try_allocate_in (;55;) (type 18) (param i32 i32 i32) (local i32) block ;; label = @1 block ;; label = @2 @@ -1611,12 +1684,12 @@ local.get 1 i32.store ) - (func $>::from (;52;) (type 23) (param i32) (result f64) + (func $>::from (;56;) (type 21) (param i32) (result f64) local.get 0 i64.extend_i32_u call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked ) - (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;53;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;57;) (type 20) (param i32 f64) (local i32) global.get $__stack_pointer i32.const 64 @@ -1673,7 +1746,7 @@ i32.add global.set $__stack_pointer ) - (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;54;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;58;) (type 20) (param i32 f64) (local i32 i32 f64 f64) global.get $__stack_pointer i32.const 128 @@ -1751,19 +1824,19 @@ i32.add global.set $__stack_pointer ) - (func $alloc::alloc::handle_alloc_error (;55;) (type 19) (param i32 i32) + (func $alloc::alloc::handle_alloc_error (;59;) (type 19) (param i32 i32) unreachable unreachable ) - (func $alloc::raw_vec::capacity_overflow (;56;) (type 24) + (func $alloc::raw_vec::capacity_overflow (;60;) (type 24) unreachable unreachable ) - (func $core::slice::::copy_from_slice::len_mismatch_fail (;57;) (type 18) (param i32 i32 i32) + (func $core::slice::::copy_from_slice::len_mismatch_fail (;61;) (type 18) (param i32 i32 i32) unreachable unreachable ) - (func $core::slice::::copy_from_slice (;58;) (type 25) (param i32 i32 i32 i32 i32) + (func $core::slice::::copy_from_slice (;62;) (type 25) (param i32 i32 i32 i32 i32) block ;; label = @1 local.get 1 local.get 3 @@ -1794,5 +1867,6 @@ (export "test_rpo_falcon512_verify" (func $test_rpo_falcon512_verify)) (export "test_pipe_words_to_memory" (func $test_pipe_words_to_memory)) (export "test_pipe_double_words_to_memory" (func $test_pipe_double_words_to_memory)) + (export "test_remove_asset" (func $test_remove_asset)) (data $.rodata (;0;) (i32.const 1048576) "~/sdk/prelude/src/stdlib/crypto/hashes.rs\00\00\00\00\00\10\00)\00\00\00\8a\00\00\00(\00\00\00\00\00\10\00)\00\00\00\d0\00\00\00(\00\00\00") ) \ No newline at end of file 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 0ac296839..1b77da7b2 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 @@ -93,3 +93,9 @@ pub fn test_pipe_words_to_memory(num_words: Felt) -> (Word, Vec) { pub fn test_pipe_double_words_to_memory(num_words: Felt) -> (Word, Vec) { pipe_double_words_to_memory(num_words) } + +#[no_mangle] +pub fn test_remove_asset(asset: CoreAsset) -> Felt { + let asset_out = remove_asset(asset); + asset_out.as_word()[0] +} From 120d55ac839ccee010a0638e616f33a32e830f10 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Fri, 12 Apr 2024 16:09:56 +0300 Subject: [PATCH 5/5] feat: add `tx_kernel::tx::create_note` tranrformation --- frontend-wasm/src/miden_abi/transform.rs | 1 + frontend-wasm/src/miden_abi/tx_kernel.rs | 2 + frontend-wasm/src/miden_abi/tx_kernel/tx.rs | 17 +++ sdk/tx-kernel/src/externs.rs | 19 ++- sdk/tx-kernel/src/lib.rs | 25 ++++ sdk/tx-kernel/src/types.rs | 12 ++ .../miden_sdk_account_test.hir | 61 ++++++++ .../miden_sdk_account_test.wat | 130 +++++++++++------- .../rust-sdk/account-test/src/lib.rs | 10 ++ 9 files changed, 227 insertions(+), 50 deletions(-) create mode 100644 frontend-wasm/src/miden_abi/tx_kernel/tx.rs diff --git a/frontend-wasm/src/miden_abi/transform.rs b/frontend-wasm/src/miden_abi/transform.rs index eeaf1b07d..c0df9cee5 100644 --- a/frontend-wasm/src/miden_abi/transform.rs +++ b/frontend-wasm/src/miden_abi/transform.rs @@ -21,6 +21,7 @@ fn get_transform_strategy(function_id: &str) -> TransformStrategy { tx_kernel::account::ADD_ASSET => TransformStrategy::ReturnViaPointer, tx_kernel::account::REMOVE_ASSET => TransformStrategy::ReturnViaPointer, tx_kernel::account::GET_ID => TransformStrategy::NoTransform, + tx_kernel::tx::CREATE_NOTE => TransformStrategy::NoTransform, stdlib::crypto::hashes::BLAKE3_HASH_1TO1 => TransformStrategy::ReturnViaPointer, stdlib::crypto::hashes::BLAKE3_HASH_2TO1 => TransformStrategy::ReturnViaPointer, stdlib::crypto::dsa::RPO_FALCON512_VERIFY => TransformStrategy::NoTransform, diff --git a/frontend-wasm/src/miden_abi/tx_kernel.rs b/frontend-wasm/src/miden_abi/tx_kernel.rs index 6ff924f5a..df92dd5be 100644 --- a/frontend-wasm/src/miden_abi/tx_kernel.rs +++ b/frontend-wasm/src/miden_abi/tx_kernel.rs @@ -2,6 +2,7 @@ pub(crate) mod account; pub(crate) mod note; +pub(crate) mod tx; use std::sync::OnceLock; @@ -13,6 +14,7 @@ pub(crate) fn signatures() -> &'static ModuleFunctionTypeMap { let mut m: ModuleFunctionTypeMap = Default::default(); m.extend(account::signatures()); m.extend(note::signatures()); + m.extend(tx::signatures()); m }) } diff --git a/frontend-wasm/src/miden_abi/tx_kernel/tx.rs b/frontend-wasm/src/miden_abi/tx_kernel/tx.rs new file mode 100644 index 000000000..983241829 --- /dev/null +++ b/frontend-wasm/src/miden_abi/tx_kernel/tx.rs @@ -0,0 +1,17 @@ +use miden_hir::FunctionType; +use miden_hir_type::Type::*; + +use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap}; + +pub const CREATE_NOTE: &str = "create_note"; + +pub(crate) fn signatures() -> ModuleFunctionTypeMap { + let mut m: ModuleFunctionTypeMap = Default::default(); + let mut note: FunctionTypeMap = Default::default(); + note.insert( + CREATE_NOTE, + FunctionType::new([Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt], [Felt]), + ); + m.insert("miden:tx_kernel/tx", note); + m +} diff --git a/sdk/tx-kernel/src/externs.rs b/sdk/tx-kernel/src/externs.rs index c7e35bbdc..9721a8fa2 100644 --- a/sdk/tx-kernel/src/externs.rs +++ b/sdk/tx-kernel/src/externs.rs @@ -1,6 +1,6 @@ use miden_prelude::Felt; -use crate::{AccountId, CoreAsset}; +use crate::{AccountId, CoreAsset, NoteId, NoteType, Tag}; #[link(wasm_import_module = "miden:tx_kernel/account")] extern "C" { @@ -17,3 +17,20 @@ extern "C" { #[link_name = "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>"] pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize; } + +#[link(wasm_import_module = "miden:tx_kernel/tx")] +extern "C" { + #[link_name = "create_note<0x0000000000000000000000000000000000000000000000000000000000000000>"] + 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/tx-kernel/src/lib.rs b/sdk/tx-kernel/src/lib.rs index a10acc603..fe2fe3bc3 100644 --- a/sdk/tx-kernel/src/lib.rs +++ b/sdk/tx-kernel/src/lib.rs @@ -81,3 +81,28 @@ pub fn remove_asset(asset: CoreAsset) -> CoreAsset { 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.0[0], + recipient.0[1], + recipient.0[2], + recipient.0[3], + ) + } +} diff --git a/sdk/tx-kernel/src/types.rs b/sdk/tx-kernel/src/types.rs index 5e5193558..b1c2c5755 100644 --- a/sdk/tx-kernel/src/types.rs +++ b/sdk/tx-kernel/src/types.rs @@ -24,3 +24,15 @@ impl CoreAsset { self.inner } } + +#[repr(transparent)] +pub struct Recipient(pub(crate) Word); + +#[repr(transparent)] +pub struct Tag(pub(crate) Felt); + +#[repr(transparent)] +pub struct NoteId(pub(crate) Felt); + +#[repr(transparent)] +pub struct NoteType(pub(crate) Felt); 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 9547eae0c..76dbf97c5 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 @@ -9,6 +9,7 @@ (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (result felt)))) (#miden:tx_kernel/account #get_id) (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)))) (#miden:tx_kernel/account #remove_asset) (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (result i32 felt)))) (#miden:tx_kernel/note #get_inputs) + (lower ((digest 0x0000000000000000000000000000000000000000000000000000000000000000) (type (func (abi canon) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (result felt)))) (#miden:tx_kernel/tx #create_note) ;; Modules (module #miden_sdk_account_test.wasm @@ -1091,6 +1092,20 @@ (ret v1)) ) + (func (export #test_create_note) + (param i32) (param felt) (param felt) (param i32) (result felt) + (block 0 + (param v0 i32) + (param v1 felt) + (param v2 felt) + (param v3 i32) + (let (v5 felt) (call #miden_sdk_tx_kernel::create_note v0 v1 v2 v3)) + (br (block 1 v5))) + + (block 1 (param v4 felt) + (ret v4)) + ) + (func (export #__rust_alloc) (param i32) (param i32) (result i32) (block 0 (param v0 i32) (param v1 i32) (let (v3 i32) (const.i32 1048652)) @@ -2100,6 +2115,50 @@ (ret)) ) + (func (export #miden_sdk_tx_kernel::create_note) + (param i32) (param felt) (param felt) (param i32) (result felt) + (block 0 + (param v0 i32) + (param v1 felt) + (param v2 felt) + (param v3 i32) + (let (v5 u32) (cast v0)) + (let (v6 (ptr felt)) (inttoptr v5)) + (let (v7 felt) (load v6)) + (let (v8 u32) (cast v0)) + (let (v9 u32) (add.checked v8 8)) + (let (v10 (ptr felt)) (inttoptr v9)) + (let (v11 felt) (load v10)) + (let (v12 u32) (cast v0)) + (let (v13 u32) (add.checked v12 16)) + (let (v14 (ptr felt)) (inttoptr v13)) + (let (v15 felt) (load v14)) + (let (v16 u32) (cast v0)) + (let (v17 u32) (add.checked v16 24)) + (let (v18 (ptr felt)) (inttoptr v17)) + (let (v19 felt) (load v18)) + (let (v20 u32) (cast v3)) + (let (v21 (ptr felt)) (inttoptr v20)) + (let (v22 felt) (load v21)) + (let (v23 u32) (cast v3)) + (let (v24 u32) (add.checked v23 8)) + (let (v25 (ptr felt)) (inttoptr v24)) + (let (v26 felt) (load v25)) + (let (v27 u32) (cast v3)) + (let (v28 u32) (add.checked v27 16)) + (let (v29 (ptr felt)) (inttoptr v28)) + (let (v30 felt) (load v29)) + (let (v31 u32) (cast v3)) + (let (v32 u32) (add.checked v31 24)) + (let (v33 (ptr felt)) (inttoptr v32)) + (let (v34 felt) (load v33)) + (let (v35 felt) (call (#miden:tx_kernel/tx #create_note) v7 v11 v15 v19 v1 v2 v22 v26 v30 v34)) + (br (block 1 v35))) + + (block 1 (param v4 felt) + (ret v4)) + ) + (func (export #alloc::vec::Vec::with_capacity) (param i32) (param i32) (block 0 (param v0 i32) (param v1 i32) @@ -2577,6 +2636,8 @@ (param felt) (param felt) (param felt) (param felt) (result felt felt felt felt)) (func (import #miden:tx_kernel/note #get_inputs) (param felt) (result i32 felt)) + (func (import #miden:tx_kernel/tx #create_note) + (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (result felt)) ) ) 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 532a7e6c0..08edfd54a 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 @@ -13,18 +13,20 @@ (type (;11;) (func (result f64))) (type (;12;) (func (param i32) (result i32))) (type (;13;) (func (param f64 f64 f64 f64 i32))) - (type (;14;) (func (param f64 f64 i32))) - (type (;15;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) - (type (;16;) (func (param i32))) - (type (;17;) (func (param i32 i32 i32) (result i32))) - (type (;18;) (func (param i32 i32 i32))) - (type (;19;) (func (param i32 i32))) - (type (;20;) (func (param i32 f64))) - (type (;21;) (func (param i32) (result f64))) - (type (;22;) (func (param i32 i32) (result i32))) - (type (;23;) (func (param i32 i32 i32 i32))) - (type (;24;) (func)) - (type (;25;) (func (param i32 i32 i32 i32 i32))) + (type (;14;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64) (result f64))) + (type (;15;) (func (param f64 f64 i32))) + (type (;16;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;17;) (func (param i32))) + (type (;18;) (func (param i32 i32 i32) (result i32))) + (type (;19;) (func (param i32 i32 i32))) + (type (;20;) (func (param i32 i32))) + (type (;21;) (func (param i32 f64))) + (type (;22;) (func (param i32) (result f64))) + (type (;23;) (func (param i32 f64 f64 i32) (result f64))) + (type (;24;) (func (param i32 i32) (result i32))) + (type (;25;) (func (param i32 i32 i32 i32))) + (type (;26;) (func)) + (type (;27;) (func (param i32 i32 i32 i32 i32))) (import "miden:prelude/intrinsics_felt" "from_u64_unchecked" (func $miden_prelude::intrinsics::felt::extern_from_u64_unchecked (;0;) (type 0))) (import "miden:prelude/intrinsics_felt" "add" (func $miden_prelude::intrinsics::felt::extern_add (;1;) (type 1))) (import "miden:prelude/intrinsics_felt" "as_u64" (func $miden_prelude::intrinsics::felt::extern_as_u64 (;2;) (type 2))) @@ -51,9 +53,10 @@ (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_note_get_inputs (;23;) (type 12))) (import "miden:tx_kernel/account" "add_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_add_asset (;24;) (type 13))) (import "miden:tx_kernel/account" "remove_asset<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_remove_asset (;25;) (type 13))) - (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;26;) (type 14))) - (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;27;) (type 15))) - (func $< as core::ops::drop::Drop>::drop::DropGuard as core::ops::drop::Drop>::drop (;28;) (type 16) (param i32) + (import "miden:tx_kernel/tx" "create_note<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_tx_create_note (;26;) (type 14))) + (import "miden:prelude/std_mem" "pipe_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_words_to_memory (;27;) (type 15))) + (import "miden:prelude/std_mem" "pipe_double_words_to_memory<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::mem::extern_pipe_double_words_to_memory (;28;) (type 16))) + (func $< as core::ops::drop::Drop>::drop::DropGuard as core::ops::drop::Drop>::drop (;29;) (type 17) (param i32) (local i32) global.get $__stack_pointer i32.const 16 @@ -79,7 +82,7 @@ i32.add global.set $__stack_pointer ) - (func $ as core::ops::drop::Drop>::drop (;29;) (type 16) (param i32) + (func $ as core::ops::drop::Drop>::drop (;30;) (type 17) (param i32) (local i32) block ;; label = @1 local.get 0 @@ -96,7 +99,7 @@ call $__rust_dealloc end ) - (func $core::alloc::global::GlobalAlloc::alloc_zeroed (;30;) (type 17) (param i32 i32 i32) (result i32) + (func $core::alloc::global::GlobalAlloc::alloc_zeroed (;31;) (type 18) (param i32 i32 i32) (result i32) block ;; label = @1 local.get 0 local.get 1 @@ -112,14 +115,14 @@ end local.get 1 ) - (func $__rust_dealloc (;31;) (type 18) (param i32 i32 i32) + (func $__rust_dealloc (;32;) (type 19) (param i32 i32 i32) i32.const 1048652 local.get 0 local.get 2 local.get 1 call $::dealloc ) - (func $ as core::ops::drop::Drop>::drop (;32;) (type 16) (param i32) + (func $ as core::ops::drop::Drop>::drop (;33;) (type 17) (param i32) (local i32) global.get $__stack_pointer i32.const 16 @@ -138,7 +141,7 @@ i32.add global.set $__stack_pointer ) - (func $get_wallet_magic_number (;33;) (type 11) (result f64) + (func $get_wallet_magic_number (;34;) (type 11) (result f64) (local f64) call $miden_sdk_tx_kernel::get_id local.set 0 @@ -147,7 +150,7 @@ local.get 0 call $miden_prelude::intrinsics::felt::extern_add ) - (func $test_add_asset (;34;) (type 11) (result f64) + (func $test_add_asset (;35;) (type 11) (result f64) (local i32 f64 f64 f64) global.get $__stack_pointer i32.const 64 @@ -190,7 +193,7 @@ global.set $__stack_pointer local.get 1 ) - (func $test_felt_ops_smoke (;35;) (type 1) (param f64 f64) (result f64) + (func $test_felt_ops_smoke (;36;) (type 1) (param f64 f64) (result f64) (local i64) local.get 0 call $miden_prelude::intrinsics::felt::extern_as_u64 @@ -279,7 +282,7 @@ local.get 0 call $miden_prelude::intrinsics::felt::extern_neg ) - (func $note_script (;36;) (type 11) (result f64) + (func $note_script (;37;) (type 11) (result f64) (local i32 f64 i32 i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -344,7 +347,7 @@ br 0 (;@1;) end ) - (func $test_blake3_hash_1to1 (;37;) (type 19) (param i32 i32) + (func $test_blake3_hash_1to1 (;38;) (type 20) (param i32 i32) (local i32 i32 f64) global.get $__stack_pointer i32.const 240 @@ -532,7 +535,7 @@ br 0 (;@1;) end ) - (func $test_blake3_hash_2to1 (;38;) (type 18) (param i32 i32 i32) + (func $test_blake3_hash_2to1 (;39;) (type 19) (param i32 i32 i32) (local i32 i32 f64) global.get $__stack_pointer i32.const 432 @@ -789,7 +792,7 @@ br 0 (;@1;) end ) - (func $test_rpo_falcon512_verify (;39;) (type 19) (param i32 i32) + (func $test_rpo_falcon512_verify (;40;) (type 20) (param i32 i32) local.get 0 f64.load local.get 0 @@ -808,17 +811,17 @@ f64.load offset=24 call $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify ) - (func $test_pipe_words_to_memory (;40;) (type 20) (param i32 f64) + (func $test_pipe_words_to_memory (;41;) (type 21) (param i32 f64) local.get 0 local.get 1 call $miden_prelude::stdlib::mem::pipe_words_to_memory ) - (func $test_pipe_double_words_to_memory (;41;) (type 20) (param i32 f64) + (func $test_pipe_double_words_to_memory (;42;) (type 21) (param i32 f64) local.get 0 local.get 1 call $miden_prelude::stdlib::mem::pipe_double_words_to_memory ) - (func $test_remove_asset (;42;) (type 21) (param i32) (result f64) + (func $test_remove_asset (;43;) (type 22) (param i32) (result f64) (local i32 f64) global.get $__stack_pointer i32.const 32 @@ -837,19 +840,26 @@ global.set $__stack_pointer local.get 2 ) - (func $__rust_alloc (;43;) (type 22) (param i32 i32) (result i32) + (func $test_create_note (;44;) (type 23) (param i32 f64 f64 i32) (result f64) + local.get 0 + local.get 1 + local.get 2 + local.get 3 + call $miden_sdk_tx_kernel::create_note + ) + (func $__rust_alloc (;45;) (type 24) (param i32 i32) (result i32) i32.const 1048652 local.get 1 local.get 0 call $::alloc ) - (func $__rust_alloc_zeroed (;44;) (type 22) (param i32 i32) (result i32) + (func $__rust_alloc_zeroed (;46;) (type 24) (param i32 i32) (result i32) i32.const 1048652 local.get 1 local.get 0 call $core::alloc::global::GlobalAlloc::alloc_zeroed ) - (func $wee_alloc::neighbors::Neighbors::remove (;45;) (type 16) (param i32) + (func $wee_alloc::neighbors::Neighbors::remove (;47;) (type 17) (param i32) (local i32 i32 i32) block ;; label = @1 local.get 0 @@ -916,7 +926,7 @@ i32.and i32.store ) - (func $::new_cell_for_free_list (;46;) (type 23) (param i32 i32 i32 i32) + (func $::new_cell_for_free_list (;48;) (type 25) (param i32 i32 i32 i32) block ;; label = @1 block ;; label = @2 local.get 2 @@ -974,7 +984,7 @@ local.get 3 i32.store ) - (func $wee_alloc::alloc_first_fit (;47;) (type 17) (param i32 i32 i32) (result i32) + (func $wee_alloc::alloc_first_fit (;49;) (type 18) (param i32 i32 i32) (result i32) (local i32 i32 i32 i32 i32 i32) local.get 1 i32.const -1 @@ -1206,7 +1216,7 @@ br 0 (;@1;) end ) - (func $::alloc (;48;) (type 17) (param i32 i32 i32) (result i32) + (func $::alloc (;50;) (type 18) (param i32 i32 i32) (result i32) (local i32 i32) global.get $__stack_pointer i32.const 16 @@ -1300,7 +1310,7 @@ global.set $__stack_pointer local.get 1 ) - (func $::dealloc (;49;) (type 23) (param i32 i32 i32 i32) + (func $::dealloc (;51;) (type 25) (param i32 i32 i32 i32) (local i32 i32 i32) block ;; label = @1 local.get 1 @@ -1402,10 +1412,10 @@ i32.store end ) - (func $miden_sdk_tx_kernel::get_id (;50;) (type 11) (result f64) + (func $miden_sdk_tx_kernel::get_id (;52;) (type 11) (result f64) call $miden_sdk_tx_kernel::externs::extern_account_get_id ) - (func $miden_sdk_tx_kernel::get_inputs (;51;) (type 16) (param i32) + (func $miden_sdk_tx_kernel::get_inputs (;53;) (type 17) (param i32) (local i32 i32 i32) global.get $__stack_pointer i32.const 16 @@ -1459,7 +1469,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $miden_sdk_tx_kernel::add_asset (;52;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::add_asset (;54;) (type 20) (param i32 i32) (local i32) global.get $__stack_pointer i32.const 32 @@ -1509,7 +1519,7 @@ i32.add global.set $__stack_pointer ) - (func $miden_sdk_tx_kernel::remove_asset (;53;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::remove_asset (;55;) (type 20) (param i32 i32) (local i32) global.get $__stack_pointer i32.const 32 @@ -1559,7 +1569,28 @@ i32.add global.set $__stack_pointer ) - (func $alloc::vec::Vec::with_capacity (;54;) (type 19) (param i32 i32) + (func $miden_sdk_tx_kernel::create_note (;56;) (type 23) (param i32 f64 f64 i32) (result f64) + local.get 0 + f64.load + local.get 0 + f64.load offset=8 + local.get 0 + f64.load offset=16 + local.get 0 + f64.load offset=24 + local.get 1 + local.get 2 + local.get 3 + f64.load + local.get 3 + f64.load offset=8 + local.get 3 + f64.load offset=16 + local.get 3 + f64.load offset=24 + call $miden_sdk_tx_kernel::externs::extern_tx_create_note + ) + (func $alloc::vec::Vec::with_capacity (;57;) (type 20) (param i32 i32) (local i32 i32) global.get $__stack_pointer i32.const 16 @@ -1610,7 +1641,7 @@ call $alloc::raw_vec::capacity_overflow unreachable ) - (func $alloc::raw_vec::RawVec::try_allocate_in (;55;) (type 18) (param i32 i32 i32) + (func $alloc::raw_vec::RawVec::try_allocate_in (;58;) (type 19) (param i32 i32 i32) (local i32) block ;; label = @1 block ;; label = @2 @@ -1684,12 +1715,12 @@ local.get 1 i32.store ) - (func $>::from (;56;) (type 21) (param i32) (result f64) + (func $>::from (;59;) (type 22) (param i32) (result f64) local.get 0 i64.extend_i32_u call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked ) - (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;57;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_words_to_memory (;60;) (type 21) (param i32 f64) (local i32) global.get $__stack_pointer i32.const 64 @@ -1746,7 +1777,7 @@ i32.add global.set $__stack_pointer ) - (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;58;) (type 20) (param i32 f64) + (func $miden_prelude::stdlib::mem::pipe_double_words_to_memory (;61;) (type 21) (param i32 f64) (local i32 i32 f64 f64) global.get $__stack_pointer i32.const 128 @@ -1824,19 +1855,19 @@ i32.add global.set $__stack_pointer ) - (func $alloc::alloc::handle_alloc_error (;59;) (type 19) (param i32 i32) + (func $alloc::alloc::handle_alloc_error (;62;) (type 20) (param i32 i32) unreachable unreachable ) - (func $alloc::raw_vec::capacity_overflow (;60;) (type 24) + (func $alloc::raw_vec::capacity_overflow (;63;) (type 26) unreachable unreachable ) - (func $core::slice::::copy_from_slice::len_mismatch_fail (;61;) (type 18) (param i32 i32 i32) + (func $core::slice::::copy_from_slice::len_mismatch_fail (;64;) (type 19) (param i32 i32 i32) unreachable unreachable ) - (func $core::slice::::copy_from_slice (;62;) (type 25) (param i32 i32 i32 i32 i32) + (func $core::slice::::copy_from_slice (;65;) (type 27) (param i32 i32 i32 i32 i32) block ;; label = @1 local.get 1 local.get 3 @@ -1868,5 +1899,6 @@ (export "test_pipe_words_to_memory" (func $test_pipe_words_to_memory)) (export "test_pipe_double_words_to_memory" (func $test_pipe_double_words_to_memory)) (export "test_remove_asset" (func $test_remove_asset)) + (export "test_create_note" (func $test_create_note)) (data $.rodata (;0;) (i32.const 1048576) "~/sdk/prelude/src/stdlib/crypto/hashes.rs\00\00\00\00\00\10\00)\00\00\00\8a\00\00\00(\00\00\00\00\00\10\00)\00\00\00\d0\00\00\00(\00\00\00") ) \ No newline at end of file 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 1b77da7b2..f54a66832 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 @@ -99,3 +99,13 @@ pub fn test_remove_asset(asset: CoreAsset) -> Felt { let asset_out = remove_asset(asset); asset_out.as_word()[0] } + +#[no_mangle] +pub fn test_create_note( + asset: CoreAsset, + tag: Tag, + note_type: NoteType, + recipient: Recipient, +) -> NoteId { + create_note(asset, tag, note_type, recipient) +}