Skip to content

Commit

Permalink
refactor!: revamp Miden SDK API and expose some modules;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Oct 16, 2024
1 parent 70fe42f commit 6f3d32a
Show file tree
Hide file tree
Showing 30 changed files with 335 additions and 357 deletions.
63 changes: 63 additions & 0 deletions sdk/base-sys/src/bindings/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use miden_stdlib_sys::Felt;

use super::types::{AccountId, CoreAsset};

#[allow(improper_ctypes)]
#[link(wasm_import_module = "miden:core-import/account@1.0.0")]
extern "C" {
#[link_name = "get-id"]
pub fn extern_account_get_id() -> AccountId;
#[link_name = "add-asset"]
pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset);
#[link_name = "remove-asset"]
pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset);
}

/// Get the account ID of the currently executing note account.
pub fn get_id() -> AccountId {
unsafe { extern_account_get_id() }
}

/// Add the specified asset to the vault.
/// Returns the final asset in the account vault defined as follows: If asset is
/// a non-fungible asset, then returns the same as asset. If asset is a
/// fungible asset, then returns the total fungible asset in the account
/// vault after asset was added to it.
///
/// Panics:
/// - If the asset is not valid.
/// - If the total value of two fungible assets is greater than or equal to 2^63.
/// - If the vault already contains the same non-fungible asset.
pub fn add_asset(asset: CoreAsset) -> CoreAsset {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<CoreAsset>::uninit();
extern_account_add_asset(
asset.inner[0],
asset.inner[1],
asset.inner[2],
asset.inner[3],
ret_area.as_mut_ptr(),
);
ret_area.assume_init()
}
}

/// Remove the specified asset from the vault.
///
/// Panics:
/// - The fungible asset is not found in the vault.
/// - The amount of the fungible asset in the vault is less than the amount to be removed.
/// - The non-fungible asset is not found in the vault.
pub fn remove_asset(asset: CoreAsset) -> CoreAsset {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<CoreAsset>::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()
}
}
5 changes: 5 additions & 0 deletions sdk/base-sys/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
pub mod account;
pub mod note;
pub mod tx;
mod types;

pub use types::*;
41 changes: 41 additions & 0 deletions sdk/base-sys/src/bindings/note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extern crate alloc;
use alloc::vec::Vec;

use miden_stdlib_sys::Felt;

#[link(wasm_import_module = "miden:core-import/note@1.0.0")]
extern "C" {
#[link_name = "get-inputs"]
pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize;
}

/// Get the inputs of the currently executing note.
pub fn get_inputs() -> Vec<Felt> {
const MAX_INPUTS: usize = 256;
let mut inputs: Vec<Felt> = Vec::with_capacity(MAX_INPUTS);
let num_inputs = unsafe {
// Ensure the pointer is a valid Miden pointer
//
// NOTE: This relies on the fact that BumpAlloc makes all allocations
// minimally word-aligned. Each word consists of 4 elements of 4 bytes,
// so to get a Miden address from a Rust address, we divide by 16 to get
// the address in words (dividing by 4 gets us an address in elements,
// and by 4 again we get the word address).
let ptr = (inputs.as_mut_ptr() as usize) / 16;
// The MASM for this function is here:
// https://github.com/0xPolygonMiden/miden-base/blob/3cbe8d59dcf4ccc9c380b7c8417ac6178fc6b86a/miden-lib/asm/miden/note.masm#L69-L102
// #! Writes the inputs of the currently execute note into memory starting at the specified
// address. #!
// #! Inputs: [dest_ptr]
// #! Outputs: [num_inputs, dest_ptr]
// #!
// #! - dest_ptr is the memory address to write the inputs.
// Compiler generated adapter code at call site will drop the returned dest_ptr
// and return the number of inputs
extern_note_get_inputs(ptr as *mut Felt)
};
unsafe {
inputs.set_len(num_inputs);
}
inputs
}
45 changes: 45 additions & 0 deletions sdk/base-sys/src/bindings/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use miden_stdlib_sys::Felt;

use super::types::{CoreAsset, NoteId, NoteType, Recipient, Tag};

#[link(wasm_import_module = "miden:core-import/tx@1.0.0")]
extern "C" {
#[link_name = "create-note"]
pub fn extern_tx_create_note(
asset_f0: Felt,
asset_f1: Felt,
asset_f2: Felt,
asset_f3: Felt,
tag: Tag,
note_type: NoteType,
recipient_f0: Felt,
recipient_f1: Felt,
recipient_f2: Felt,
recipient_f3: Felt,
) -> NoteId;
}

/// Creates a new note. asset is the asset to be included in the note. tag is
/// the tag to be included in the note. recipient is the recipient of the note.
/// Returns the id of the created note.
pub fn create_note(
asset: CoreAsset,
tag: Tag,
note_type: NoteType,
recipient: Recipient,
) -> NoteId {
unsafe {
extern_tx_create_note(
asset.inner[0],
asset.inner[1],
asset.inner[2],
asset.inner[3],
tag,
note_type,
recipient.inner[0],
recipient.inner[1],
recipient.inner[2],
recipient.inner[3],
)
}
}
37 changes: 0 additions & 37 deletions sdk/base-sys/src/bindings/tx/externs.rs

This file was deleted.

116 changes: 0 additions & 116 deletions sdk/base-sys/src/bindings/tx/mod.rs

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions sdk/base-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

#[cfg(feature = "bindings")]
pub mod bindings;

#[cfg(feature = "masl-lib")]
pub mod masl;
2 changes: 1 addition & 1 deletion sdk/sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![no_std]

pub use miden_base_sys::bindings::tx::*;
pub use miden_base_sys::bindings::*;
pub use miden_sdk_alloc::BumpAlloc;
pub use miden_stdlib_sys::*;
7 changes: 5 additions & 2 deletions sdk/stdlib-sys/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::ops::{Deref, DerefMut};

pub(crate) mod felt;
pub(crate) mod word;
mod felt;
mod word;

pub use felt::*;
pub use word::*;

#[repr(C, align(32))]
pub struct WordAligned<T>(T);
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/intrinsics/word.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ops::{Index, IndexMut};

use crate::Felt;
use super::felt::Felt;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(C, align(32))]
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
mod intrinsics;
mod stdlib;

pub use intrinsics::{felt::*, word::*, WordAligned};
pub use intrinsics::*;
pub use stdlib::*;
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/stdlib/crypto/dsa.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Felt, Word};
use crate::intrinsics::*;

#[link(wasm_import_module = "miden:core-import/stdlib-crypto-dsa-rpo-falcon@1.0.0")]
extern "C" {
Expand Down
3 changes: 1 addition & 2 deletions sdk/stdlib-sys/src/stdlib/crypto/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ fn hash_1to1(
input: [u8; 32],
extern_hash_1to1: unsafe extern "C" fn(u32, u32, u32, u32, u32, u32, u32, u32, *mut u8),
) -> [u8; 32] {
use crate::WordAligned;

use crate::intrinsics::WordAligned;
let input = unsafe { core::mem::transmute::<[u8; 32], [u32; 8]>(input) };
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<WordAligned<[u8; 32]>>::uninit();
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/stdlib/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;
use alloc::vec::Vec;

use crate::{Felt, Word};
use crate::intrinsics::{Felt, Word};

#[link(wasm_import_module = "miden:core-import/stdlib-mem@1.0.0")]
extern "C" {
Expand Down
2 changes: 0 additions & 2 deletions sdk/stdlib-sys/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ mod mem;

pub use crypto::*;
pub use mem::*;

// TODO: simplify bindings like in tx_kernel
Loading

0 comments on commit 6f3d32a

Please sign in to comment.