From c3d5a69d5904424fe360fa663913569c5518beed Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Wed, 3 Apr 2024 18:14:03 +0300 Subject: [PATCH 1/4] feat: add Miden SDK dependency and update the sample code --- library/template/Cargo.toml | 16 ++++++++++++++ library/template/rust-toolchain.toml | 5 +++++ library/template/src/lib.rs | 33 ++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 library/template/rust-toolchain.toml diff --git a/library/template/Cargo.toml b/library/template/Cargo.toml index 83cdca7..bee0d7d 100644 --- a/library/template/Cargo.toml +++ b/library/template/Cargo.toml @@ -4,7 +4,23 @@ version = "0.1.0" edition = "2021" [lib] +# Build this crate as a self-contained, C-style dynamic library +# This is required to emit the proper Wasm module type crate-type = ["cdylib"] +[dependencies] +# Miden SDK consists of a Prelude (intrinsic functions for VM opr, stdlib) and transaction kernel API for the Miden rollup +miden-sdk = { git = "https://github.com/0xPolygonMiden/compiler", rev = "581f639c6bf0bc8f6f508d970ee799f135b5798f" } +# Use a tiny allocator in place of the default one, if we want +# to make use of types in the `alloc` crate, e.g. String. We +# don't need that now, but its good information to have in hand. +wee_alloc = { version = "0.4.5", default-features = false} + [profile.release] +# optimize the output for size opt-level = "z" +# Explicitly disable panic infrastructure on Wasm, as +# there is no proper support for them anyway, and it +# ensures that panics do not pull in a bunch of standard +# library code unintentionally +panic = "abort" diff --git a/library/template/rust-toolchain.toml b/library/template/rust-toolchain.toml new file mode 100644 index 0000000..bc008c9 --- /dev/null +++ b/library/template/rust-toolchain.toml @@ -0,0 +1,5 @@ +[toolchain] +channel = "nightly-2024-03-10" +components = ["rustfmt", "rust-src"] +targets = ["wasm32-wasi"] +profile = "minimal" diff --git a/library/template/src/lib.rs b/library/template/src/lib.rs index 09afd8b..1e6fb8c 100644 --- a/library/template/src/lib.rs +++ b/library/template/src/lib.rs @@ -1,11 +1,40 @@ +// Do not link against libstd (i.e. anything defined in `std::`) #![no_std] +// However, we could still use some standard library types while +// remaining no-std compatible, if we uncommented the following lines: +// +// extern crate alloc; +// use alloc::vec::Vec; + +// Global allocator to use heap memory in no-std environment +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +// Required for no-std crates #[panic_handler] fn my_panic(_info: &core::panic::PanicInfo) -> ! { loop {} } +use miden_sdk::*; + +// Marking the function no_mangle ensures that it is exported +// from the compiled binary as `fib`, otherwise it would have +// a mangled name that has no stable form. +// +// You can specify a different name from the library than the +// name in the source code using the `#[export_name = "foo"]` +// attribute, which will make the function callable as `foo` +// externally (in this example) #[no_mangle] -pub fn some_func(n: u32) -> u32 { - n / 3 +pub fn fib(n: u32) -> Felt { + let mut a = felt!(0); + let mut b = felt!(1); + for _ in 0..n { + let c = a + b; + a = b; + b = c; + } + a } From b5fb4f25429d9f836b5f1bfd6be8202bae109543 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Thu, 4 Apr 2024 17:38:48 +0300 Subject: [PATCH 2/4] docs: update README.md with proper build instructions --- library/template/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/template/README.md b/library/template/README.md index 97cc88a..26b01d4 100644 --- a/library/template/README.md +++ b/library/template/README.md @@ -4,7 +4,7 @@ `{{crate_name}}` is built using the [Miden compiler](https://github.com/0xPolygonMiden/compiler). -`cargo miden` is a `cargo` cargo extension. Check out its [documentation](https://github.com/0xPolygonMiden/compiler/cargo-ext) +`cargo miden` is a `cargo` cargo extension. Check out its [documentation](https://github.com/0xPolygonMiden/compiler/tree/main/tools/cargo-miden) for more details. @@ -13,5 +13,7 @@ for more details. To build this project, run: ```bash -cargo miden compile -o ./target/{{crate_name}}.masm +cargo miden build --release ``` + +The compiled Miden artifact will be located in `target/miden/release`. From 93e61ba087a982f0b53d098e2581152c34bf801c Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Thu, 4 Apr 2024 17:44:15 +0300 Subject: [PATCH 3/4] rename `library/template` to `account/template` folder --- {library => account}/template/.gitignore | 0 {library => account}/template/Cargo.toml | 0 {library => account}/template/README.md | 0 {library => account}/template/cargo-generate.toml | 0 {library => account}/template/rust-toolchain.toml | 0 {library => account}/template/src/lib.rs | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename {library => account}/template/.gitignore (100%) rename {library => account}/template/Cargo.toml (100%) rename {library => account}/template/README.md (100%) rename {library => account}/template/cargo-generate.toml (100%) rename {library => account}/template/rust-toolchain.toml (100%) rename {library => account}/template/src/lib.rs (100%) diff --git a/library/template/.gitignore b/account/template/.gitignore similarity index 100% rename from library/template/.gitignore rename to account/template/.gitignore diff --git a/library/template/Cargo.toml b/account/template/Cargo.toml similarity index 100% rename from library/template/Cargo.toml rename to account/template/Cargo.toml diff --git a/library/template/README.md b/account/template/README.md similarity index 100% rename from library/template/README.md rename to account/template/README.md diff --git a/library/template/cargo-generate.toml b/account/template/cargo-generate.toml similarity index 100% rename from library/template/cargo-generate.toml rename to account/template/cargo-generate.toml diff --git a/library/template/rust-toolchain.toml b/account/template/rust-toolchain.toml similarity index 100% rename from library/template/rust-toolchain.toml rename to account/template/rust-toolchain.toml diff --git a/library/template/src/lib.rs b/account/template/src/lib.rs similarity index 100% rename from library/template/src/lib.rs rename to account/template/src/lib.rs From 8d14bbb08a11d525f220e7fc8b831f22550f8989 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Thu, 20 Jun 2024 11:54:02 +0300 Subject: [PATCH 4/4] update the miden-sdk version to include the SDK crates rename --- account/template/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account/template/Cargo.toml b/account/template/Cargo.toml index bee0d7d..9a2123a 100644 --- a/account/template/Cargo.toml +++ b/account/template/Cargo.toml @@ -10,7 +10,7 @@ crate-type = ["cdylib"] [dependencies] # Miden SDK consists of a Prelude (intrinsic functions for VM opr, stdlib) and transaction kernel API for the Miden rollup -miden-sdk = { git = "https://github.com/0xPolygonMiden/compiler", rev = "581f639c6bf0bc8f6f508d970ee799f135b5798f" } +miden-sdk = { git = "https://github.com/0xPolygonMiden/compiler", rev = "10a05d8fa76106325e2497c2d1b10f1042c77340" } # Use a tiny allocator in place of the default one, if we want # to make use of types in the `alloc` crate, e.g. String. We # don't need that now, but its good information to have in hand.