Skip to content

Commit

Permalink
feature(cargo-miden): support building Wasm component from a Cargo pr…
Browse files Browse the repository at this point in the history
…oject
  • Loading branch information
greenhat committed Oct 1, 2024
1 parent 8076326 commit 02f855d
Show file tree
Hide file tree
Showing 56 changed files with 4,152 additions and 1,151 deletions.
142 changes: 70 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ cargo-miden = { version = "0.0.7", path = "tools/cargo-miden" }
miden-integration-tests = { version = "0.0.0", path = "tests/integration" }
wat = "1.0.69"
blake3 = "1.5"
tokio = { version = "1.39.2", features = ["rt", "time", "macros"] }
tokio-util = "0.7.11"

[profile.dev]
lto = false
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/src/component/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ pub fn interface_type_to_ir(
InterfaceType::U32 => midenc_hir_type::Type::U32,
InterfaceType::S64 => midenc_hir_type::Type::I64,
InterfaceType::U64 => midenc_hir_type::Type::U64,
InterfaceType::Float32 => todo!(),
InterfaceType::Float32 => midenc_hir_type::Type::Felt,
InterfaceType::Float64 => todo!(),
InterfaceType::Char => todo!(),
InterfaceType::String => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/src/intrinsics/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use midenc_hir::{FunctionIdent, InstBuilder, SourceSpan, Type::*, Value};

use crate::module::function_builder_ext::FunctionBuilderExt;

pub(crate) const INTRINSICS_FELT_MODULE_NAME: &str = "miden:stdlib/intrinsics_felt";
pub(crate) const MODULE_ID: &str = "miden:core-import/[email protected]";

/// Convert a call to a felt op intrinsic function into instruction(s)
pub(crate) fn convert_felt_intrinsics(
Expand Down
4 changes: 3 additions & 1 deletion frontend-wasm/src/intrinsics/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use midenc_hir::{AbiParam, FunctionIdent, InstBuilder, Signature, SourceSpan, Ty

use crate::module::function_builder_ext::FunctionBuilderExt;

pub const MODULE_ID: &str = "miden:core-import/[email protected]";

/// Convert a call to a memory intrinsic function
pub(crate) fn convert_mem_intrinsics(
func_id: FunctionIdent,
Expand All @@ -10,7 +12,7 @@ pub(crate) fn convert_mem_intrinsics(
span: SourceSpan,
) -> Vec<Value> {
match func_id.function.as_symbol().as_str() {
"heap_base" => {
"heap-base" => {
assert_eq!(args.len(), 0, "{} takes no arguments", func_id);
if builder
.data_flow_graph()
Expand Down
10 changes: 4 additions & 6 deletions frontend-wasm/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn modules() -> &'static HashSet<&'static str> {
static MODULES: OnceLock<HashSet<&'static str>> = OnceLock::new();
MODULES.get_or_init(|| {
let mut s = HashSet::default();
s.insert("intrinsics::mem");
s.insert(felt::INTRINSICS_FELT_MODULE_NAME);
s.insert(mem::MODULE_ID);
s.insert(felt::MODULE_ID);
s
})
}
Expand All @@ -30,10 +30,8 @@ pub fn convert_intrinsics_call(
span: SourceSpan,
) -> Vec<Value> {
match func_id.module.as_symbol().as_str() {
"intrinsics::mem" => mem::convert_mem_intrinsics(func_id, args, builder, span),
felt::INTRINSICS_FELT_MODULE_NAME => {
felt::convert_felt_intrinsics(func_id, args, builder, span)
}
mem::MODULE_ID => mem::convert_mem_intrinsics(func_id, args, builder, span),
felt::MODULE_ID => felt::convert_felt_intrinsics(func_id, args, builder, span),
_ => panic!("No intrinsics found for {}", func_id),
}
}
8 changes: 4 additions & 4 deletions frontend-wasm/src/miden_abi/stdlib/crypto/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ use midenc_hir_type::Type::*;

use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};

pub(crate) const BLAKE3_HASH_1TO1: &str = "hash_1to1";
pub const MODULE_ID: &str = "miden:core-import/[email protected]";

pub(crate) const BLAKE3_HASH_1TO1: &str = "blake3-hash-one-to-one";
pub(crate) const BLAKE3_HASH_2TO1: &str = "hash_2to1";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
let mut blake3: FunctionTypeMap = Default::default();
blake3.insert(
BLAKE3_HASH_1TO1,
//Accepts and returns a 8 Felt elements
FunctionType::new(
[I32, I32, I32, I32, I32, I32, I32, I32],
[I32, I32, I32, I32, I32, I32, I32, I32],
),
);
blake3.insert(
BLAKE3_HASH_2TO1,
// Accepts 16 and returns a 8 Felt elements
FunctionType::new(
[I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32, I32],
[I32, I32, I32, I32, I32, I32, I32, I32],
),
);
m.insert("std::crypto::hashes::blake3", blake3);
m.insert(MODULE_ID, blake3);
m
}
6 changes: 3 additions & 3 deletions frontend-wasm/src/miden_abi/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn get_transform_strategy(module_id: &str, function_id: &str) -> TransformStrate
stdlib::mem::PIPE_DOUBLE_WORDS_TO_MEMORY => return TransformStrategy::ReturnViaPointer,
_ => (),
},
"std::crypto::hashes::blake3" => match function_id {
stdlib::crypto::hashes::MODULE_ID => match function_id {
stdlib::crypto::hashes::BLAKE3_HASH_1TO1 => return TransformStrategy::ReturnViaPointer,
stdlib::crypto::hashes::BLAKE3_HASH_2TO1 => return TransformStrategy::ReturnViaPointer,
_ => (),
Expand All @@ -40,13 +40,13 @@ fn get_transform_strategy(module_id: &str, function_id: &str) -> TransformStrate
tx_kernel::note::GET_INPUTS => return TransformStrategy::ListReturn,
_ => (),
},
"miden::account" => match function_id {
tx_kernel::account::MODULE_ID => match function_id {
tx_kernel::account::ADD_ASSET => return TransformStrategy::ReturnViaPointer,
tx_kernel::account::REMOVE_ASSET => return TransformStrategy::ReturnViaPointer,
tx_kernel::account::GET_ID => return TransformStrategy::NoTransform,
_ => (),
},
"miden::tx" => match function_id {
tx_kernel::tx::MODULE_ID => match function_id {
tx_kernel::tx::CREATE_NOTE => return TransformStrategy::NoTransform,
_ => (),
},
Expand Down
8 changes: 5 additions & 3 deletions frontend-wasm/src/miden_abi/tx_kernel/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use midenc_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 MODULE_ID: &str = "miden:core-import/[email protected]";

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 {
Expand All @@ -17,6 +19,6 @@ pub(crate) fn signatures() -> ModuleFunctionTypeMap {
FunctionType::new([Felt, Felt, Felt, Felt], [Felt, Felt, Felt, Felt]),
);
account.insert(GET_ID, FunctionType::new([], [Felt]));
m.insert("miden::account", account);
m.insert(MODULE_ID, account);
m
}
6 changes: 4 additions & 2 deletions frontend-wasm/src/miden_abi/tx_kernel/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use midenc_hir_type::Type::*;

use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};

pub const CREATE_NOTE: &str = "create_note";
pub const MODULE_ID: &str = "miden:core-import/[email protected]";

pub const CREATE_NOTE: &str = "create-note";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
Expand All @@ -12,6 +14,6 @@ pub(crate) fn signatures() -> ModuleFunctionTypeMap {
CREATE_NOTE,
FunctionType::new([Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt], [Felt]),
);
m.insert("miden::tx", note);
m.insert(MODULE_ID, note);
m
}
4 changes: 2 additions & 2 deletions midenc-debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ serde.workspace = true
ratatui = "0.28.0"
crossterm = { version = "0.28.1", features = ["event-stream"] }
tui-input = "0.10"
tokio = { version = "1.39.2", features = ["rt", "time", "macros"] }
tokio-util = "0.7.11"
tokio.workspace = true
tokio-util.workspace = true
futures = "0.3.30"
signal-hook = "0.3.17"
syntect = { version = "5.2.0", default-features = false, features = [
Expand Down
3 changes: 2 additions & 1 deletion sdk/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ unsafe impl GlobalAlloc for BumpAlloc {
}

#[cfg(target_family = "wasm")]
#[link(wasm_import_module = "intrinsics::mem")]
#[link(wasm_import_module = "miden:core-import/intrinsics-mem@1.0.0")]
extern "C" {
#[link_name = "heap-base"]
fn heap_base() -> *mut u8;
}
10 changes: 5 additions & 5 deletions sdk/base-sys/src/bindings/tx/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use miden_stdlib_sys::Felt;

use crate::bindings::tx::{AccountId, CoreAsset, NoteId, NoteType, Tag};

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

Expand All @@ -18,9 +18,9 @@ extern "C" {
pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize;
}

#[link(wasm_import_module = "miden::tx")]
#[link(wasm_import_module = "miden:core-import/[email protected]")]
extern "C" {
#[link_name = "create_note<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "create-note"]
pub fn extern_tx_create_note(
asset_f0: Felt,
asset_f1: Felt,
Expand Down
8 changes: 4 additions & 4 deletions sdk/base-sys/src/bindings/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ pub fn create_note(
asset.inner[3],
tag,
note_type,
recipient.0[0],
recipient.0[1],
recipient.0[2],
recipient.0[3],
recipient.inner[0],
recipient.inner[1],
recipient.inner[2],
recipient.inner[3],
)
}
}
14 changes: 10 additions & 4 deletions sdk/base-sys/src/bindings/tx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl From<AccountId> for Felt {

#[repr(transparent)]
pub struct CoreAsset {
pub(crate) inner: Word,
pub inner: Word,
}

impl CoreAsset {
Expand All @@ -26,13 +26,19 @@ impl CoreAsset {
}

#[repr(transparent)]
pub struct Recipient(pub(crate) Word);
pub struct Recipient {
pub inner: Word,
}

#[repr(transparent)]
pub struct Tag(pub(crate) Felt);
pub struct Tag {
pub inner: Felt,
}

#[repr(transparent)]
pub struct NoteId(pub(crate) Felt);

#[repr(transparent)]
pub struct NoteType(pub(crate) Felt);
pub struct NoteType {
pub inner: Felt,
}
24 changes: 17 additions & 7 deletions sdk/stdlib-sys/src/intrinsics/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

#[link(wasm_import_module = "miden:stdlib/intrinsics_felt")]
#[link(wasm_import_module = "miden:core-import/[email protected]")]
extern "C" {
#[link_name = "from_u64_unchecked"]
fn extern_from_u64_unchecked(value: u64) -> Felt;
Expand Down Expand Up @@ -80,8 +80,10 @@ pub enum FeltError {
}

#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Felt(f32);
#[derive(Copy, Clone, Debug)]
pub struct Felt {
pub inner: f32,
}

impl Felt {
/// Field modulus = 2^64 - 2^32 + 1
Expand Down Expand Up @@ -141,26 +143,34 @@ impl From<Felt> for u64 {

impl From<u32> for Felt {
fn from(value: u32) -> Self {
Self(unsafe { core::mem::transmute::<u32, f32>(value) })
Self {
inner: unsafe { core::mem::transmute::<u32, f32>(value) },
}
}
}

impl From<u16> for Felt {
fn from(value: u16) -> Self {
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
Self {
inner: unsafe { core::mem::transmute::<u32, f32>(value as u32) },
}
}
}

impl From<u8> for Felt {
fn from(value: u8) -> Self {
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
Self {
inner: unsafe { core::mem::transmute::<u32, f32>(value as u32) },
}
}
}

#[cfg(target_pointer_width = "32")]
impl From<usize> for Felt {
fn from(value: usize) -> Self {
Self(unsafe { core::mem::transmute(value as u32) })
Self {
inner: unsafe { core::mem::transmute(value as u32) },
}
}
}

Expand Down
53 changes: 34 additions & 19 deletions sdk/stdlib-sys/src/intrinsics/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@ use core::ops::{Index, IndexMut};

use crate::Felt;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(C, align(32))]
pub struct Word([Felt; 4]);
impl Word {
pub const fn new(word: [Felt; 4]) -> Self {
Self(word)
}
}
impl From<[Felt; 4]> for Word {
fn from(word: [Felt; 4]) -> Self {
Self(word)
}
}
impl From<Word> for [Felt; 4] {
#[inline(always)]
fn from(word: Word) -> Self {
word.0
}
// pub struct Word([Felt; 4]);
pub struct Word {
pub inner: (Felt, Felt, Felt, Felt),
}
// impl Word {
// pub const fn new(word: [Felt; 4]) -> Self {
// Self { inner: word }
// }
// }
// impl From<[Felt; 4]> for Word {
// fn from(word: [Felt; 4]) -> Self {
// Self { inner: word }
// }
// }
// impl From<Word> for [Felt; 4] {
// #[inline(always)]
// fn from(word: Word) -> Self {
// word.inner
// }
// }
impl Index<usize> for Word {
type Output = Felt;

#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
self.0.index(index)
match index {
0 => &self.inner.0,
1 => &self.inner.1,
2 => &self.inner.2,
3 => &self.inner.3,
_ => unreachable!(),
}
}
}
impl IndexMut<usize> for Word {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.0.index_mut(index)
match index {
0 => &mut self.inner.0,
1 => &mut self.inner.1,
2 => &mut self.inner.2,
3 => &mut self.inner.3,
_ => unreachable!(),
}
}
}
Loading

0 comments on commit 02f855d

Please sign in to comment.