Skip to content

Commit a876d8b

Browse files
committed
feature(cargo-miden): support building Wasm component from a Cargo project
1 parent 8076326 commit a876d8b

File tree

30 files changed

+1198
-1111
lines changed

30 files changed

+1198
-1111
lines changed

Cargo.lock

Lines changed: 66 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ cargo-miden = { version = "0.0.7", path = "tools/cargo-miden" }
108108
miden-integration-tests = { version = "0.0.0", path = "tests/integration" }
109109
wat = "1.0.69"
110110
blake3 = "1.5"
111+
tokio = { version = "1.39.2", features = ["rt", "time", "macros"] }
112+
tokio-util = "0.7.11"
111113

112114
[profile.dev]
113115
lto = false

midenc-debug/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ serde.workspace = true
3131
ratatui = "0.28.0"
3232
crossterm = { version = "0.28.1", features = ["event-stream"] }
3333
tui-input = "0.10"
34-
tokio = { version = "1.39.2", features = ["rt", "time", "macros"] }
35-
tokio-util = "0.7.11"
34+
tokio.workspace = true
35+
tokio-util.workspace = true
3636
futures = "0.3.30"
3737
signal-hook = "0.3.17"
3838
syntect = { version = "5.2.0", default-features = false, features = [

sdk/alloc/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ unsafe impl GlobalAlloc for BumpAlloc {
123123
}
124124

125125
#[cfg(target_family = "wasm")]
126-
#[link(wasm_import_module = "intrinsics::mem")]
127-
extern "C" {
128-
fn heap_base() -> *mut u8;
126+
#[inline(never)]
127+
#[no_mangle]
128+
fn miden_intrinsics_mem_heap_base() -> *mut u8 {
129+
unreachable!()
129130
}
131+
// #[link(wasm_import_module = "intrinsics::mem")]
132+
// extern "C" {
133+
// fn heap_base() -> *mut u8;
134+
// }

sdk/base-sys/src/bindings/tx/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ pub fn create_note(
107107
asset.inner[3],
108108
tag,
109109
note_type,
110-
recipient.0[0],
111-
recipient.0[1],
112-
recipient.0[2],
113-
recipient.0[3],
110+
recipient.inner[0],
111+
recipient.inner[1],
112+
recipient.inner[2],
113+
recipient.inner[3],
114114
)
115115
}
116116
}

sdk/base-sys/src/bindings/tx/types.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl From<AccountId> for Felt {
1212

1313
#[repr(transparent)]
1414
pub struct CoreAsset {
15-
pub(crate) inner: Word,
15+
pub inner: Word,
1616
}
1717

1818
impl CoreAsset {
@@ -26,13 +26,19 @@ impl CoreAsset {
2626
}
2727

2828
#[repr(transparent)]
29-
pub struct Recipient(pub(crate) Word);
29+
pub struct Recipient {
30+
pub inner: Word,
31+
}
3032

3133
#[repr(transparent)]
32-
pub struct Tag(pub(crate) Felt);
34+
pub struct Tag {
35+
pub inner: Felt,
36+
}
3337

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

3741
#[repr(transparent)]
38-
pub struct NoteType(pub(crate) Felt);
42+
pub struct NoteType {
43+
pub inner: Felt,
44+
}

sdk/stdlib-sys/src/intrinsics/felt.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ pub enum FeltError {
8080
}
8181

8282
#[repr(transparent)]
83-
#[derive(Copy, Clone)]
84-
pub struct Felt(f32);
83+
#[derive(Copy, Clone, Debug)]
84+
pub struct Felt {
85+
pub inner: f32,
86+
}
8587

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

142144
impl From<u32> for Felt {
143145
fn from(value: u32) -> Self {
144-
Self(unsafe { core::mem::transmute::<u32, f32>(value) })
146+
Self {
147+
inner: unsafe { core::mem::transmute::<u32, f32>(value) },
148+
}
145149
}
146150
}
147151

148152
impl From<u16> for Felt {
149153
fn from(value: u16) -> Self {
150-
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
154+
Self {
155+
inner: unsafe { core::mem::transmute::<u32, f32>(value as u32) },
156+
}
151157
}
152158
}
153159

154160
impl From<u8> for Felt {
155161
fn from(value: u8) -> Self {
156-
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
162+
Self {
163+
inner: unsafe { core::mem::transmute::<u32, f32>(value as u32) },
164+
}
157165
}
158166
}
159167

160168
#[cfg(target_pointer_width = "32")]
161169
impl From<usize> for Felt {
162170
fn from(value: usize) -> Self {
163-
Self(unsafe { core::mem::transmute(value as u32) })
171+
Self {
172+
inner: unsafe { core::mem::transmute(value as u32) },
173+
}
164174
}
165175
}
166176

sdk/stdlib-sys/src/intrinsics/word.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,49 @@ use crate::Felt;
44

55
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
66
#[repr(C, align(32))]
7-
pub struct Word([Felt; 4]);
8-
impl Word {
9-
pub const fn new(word: [Felt; 4]) -> Self {
10-
Self(word)
11-
}
12-
}
13-
impl From<[Felt; 4]> for Word {
14-
fn from(word: [Felt; 4]) -> Self {
15-
Self(word)
16-
}
17-
}
18-
impl From<Word> for [Felt; 4] {
19-
#[inline(always)]
20-
fn from(word: Word) -> Self {
21-
word.0
22-
}
7+
// pub struct Word([Felt; 4]);
8+
pub struct Word {
9+
pub inner: (Felt, Felt, Felt, Felt),
2310
}
11+
// impl Word {
12+
// pub const fn new(word: [Felt; 4]) -> Self {
13+
// Self { inner: word }
14+
// }
15+
// }
16+
// impl From<[Felt; 4]> for Word {
17+
// fn from(word: [Felt; 4]) -> Self {
18+
// Self { inner: word }
19+
// }
20+
// }
21+
// impl From<Word> for [Felt; 4] {
22+
// #[inline(always)]
23+
// fn from(word: Word) -> Self {
24+
// word.inner
25+
// }
26+
// }
2427
impl Index<usize> for Word {
2528
type Output = Felt;
2629

2730
#[inline(always)]
2831
fn index(&self, index: usize) -> &Self::Output {
29-
self.0.index(index)
32+
match index {
33+
0 => &self.inner.0,
34+
1 => &self.inner.1,
35+
2 => &self.inner.2,
36+
3 => &self.inner.3,
37+
_ => unreachable!(),
38+
}
3039
}
3140
}
3241
impl IndexMut<usize> for Word {
3342
#[inline(always)]
3443
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
35-
self.0.index_mut(index)
44+
match index {
45+
0 => &mut self.inner.0,
46+
1 => &mut self.inner.1,
47+
2 => &mut self.inner.2,
48+
3 => &mut self.inner.3,
49+
_ => unreachable!(),
50+
}
3651
}
3752
}

tests/integration/src/compiler_test.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ impl CompilerTestBuilder {
370370

371371
// Cargo-based source types share a lot of configuration in common
372372
match self.source {
373+
CompilerTestInputType::CargoMiden(ref config) => {
374+
let manifest_path = project_dir.join("Cargo.toml");
375+
command
376+
.arg("--manifest-path")
377+
.arg(manifest_path)
378+
.arg("--release")
379+
.arg("--target")
380+
.arg(config.target.as_ref());
381+
}
373382
CompilerTestInputType::CargoComponent(_) => {
374383
let manifest_path = project_dir.join("Cargo.toml");
375384
command.arg("--manifest-path").arg(manifest_path).arg("--release");
@@ -635,6 +644,23 @@ impl CompilerTestBuilder {
635644
builder
636645
}
637646

647+
/// Compile the Rust project using cargo-miden
648+
pub fn rust_source_cargo_miden(
649+
cargo_project_folder: impl AsRef<Path>,
650+
config: WasmTranslationConfig,
651+
) -> Self {
652+
let name = cargo_project_folder
653+
.as_ref()
654+
.file_stem()
655+
.map(|name| name.to_string_lossy().into_owned())
656+
.unwrap_or("".to_string());
657+
let mut builder = CompilerTestBuilder::new(CompilerTestInputType::CargoMiden(
658+
CargoTest::new(name, cargo_project_folder.as_ref().to_path_buf()),
659+
));
660+
builder.with_wasm_translation_config(config);
661+
builder
662+
}
663+
638664
/// Set the Rust source code to compile a library Cargo project to Wasm module
639665
pub fn rust_source_cargo_lib(
640666
cargo_project_folder: impl AsRef<Path>,
@@ -983,6 +1009,14 @@ impl CompilerTest {
9831009
CompilerTestBuilder::rust_source_cargo_component(cargo_project_folder, config).build()
9841010
}
9851011

1012+
/// Compile the Rust project using cargo-miden
1013+
pub fn rust_source_cargo_miden(
1014+
cargo_project_folder: impl AsRef<Path>,
1015+
config: WasmTranslationConfig,
1016+
) -> Self {
1017+
CompilerTestBuilder::rust_source_cargo_miden(cargo_project_folder, config).build()
1018+
}
1019+
9861020
/// Set the Rust source code to compile a library Cargo project to Wasm module
9871021
pub fn rust_source_cargo_lib(
9881022
cargo_project_folder: impl AsRef<Path>,

tests/integration/src/rust_masm_tests/rust_sdk.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::PathBuf;
22

33
use expect_test::expect_file;
4+
use midenc_frontend_wasm::WasmTranslationConfig;
45

56
use crate::{cargo_proj::project, compiler_test::sdk_crate_path, CompilerTest};
67

@@ -26,30 +27,15 @@ fn account() {
2627
}
2728

2829
#[test]
29-
fn basic_wallet() {
30+
fn rust_sdk_basic_wallet() {
31+
let _ = env_logger::builder().is_test(true).try_init();
3032
let project_name = "rust_sdk_basic_wallet";
31-
let source = r#"
32-
33-
pub struct Account;
34-
35-
impl Account {
36-
#[no_mangle]
37-
pub fn receive_asset(asset: CoreAsset) {
38-
add_asset(asset);
39-
}
40-
41-
#[no_mangle]
42-
pub fn send_asset(asset: CoreAsset, tag: Tag, note_type: NoteType, recipient: Recipient) {
43-
let asset = remove_asset(asset);
44-
create_note(asset, tag, note_type, recipient);
45-
}
46-
}
47-
"#;
48-
49-
let mut test = CompilerTest::rust_source_with_sdk(project_name, source, true, None, None);
33+
let config = WasmTranslationConfig::default();
34+
let mut test =
35+
CompilerTest::rust_source_cargo_miden("../rust-apps-wasm/rust-sdk/basic-wallet", config);
5036
let artifact_name = test.artifact_name();
51-
test.expect_wasm(expect_file![format!("../../expected/{project_name}/{artifact_name}.wat")]);
52-
test.expect_ir(expect_file![format!("../../expected/{project_name}/{artifact_name}.hir")]);
37+
test.expect_wasm(expect_file![format!("../../expected/rust_sdk/{artifact_name}.wat")]);
38+
test.expect_ir(expect_file![format!("../../expected/rust_sdk/{artifact_name}.hir")]);
5339
// TODO: fix flaky test, "exec."_ZN19miden_sdk_tx_kernel9add_asset17h6f4cff304c095ffc" is
5440
// changing the suffix on every n-th run test.expect_masm(expect_file![format!("../../
5541
// expected/{project_name}/{artifact_name}.masm" )]);

0 commit comments

Comments
 (0)