Skip to content

Commit

Permalink
feat: add tx_kernel::tx::create_note tranrformation
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Apr 19, 2024
1 parent 1b76291 commit 120d55a
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 50 deletions.
1 change: 1 addition & 0 deletions frontend-wasm/src/miden_abi/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions frontend-wasm/src/miden_abi/tx_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub(crate) mod account;
pub(crate) mod note;
pub(crate) mod tx;

use std::sync::OnceLock;

Expand All @@ -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
})
}
17 changes: 17 additions & 0 deletions frontend-wasm/src/miden_abi/tx_kernel/tx.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 18 additions & 1 deletion sdk/tx-kernel/src/externs.rs
Original file line number Diff line number Diff line change
@@ -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" {
Expand All @@ -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;
}
25 changes: 25 additions & 0 deletions sdk/tx-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)
}
}
12 changes: 12 additions & 0 deletions sdk/tx-kernel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<T>::with_capacity)
(param i32) (param i32)
(block 0 (param v0 i32) (param v1 i32)
Expand Down Expand Up @@ -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))
)

)
Loading

0 comments on commit 120d55a

Please sign in to comment.