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/account/template/Cargo.toml b/account/template/Cargo.toml new file mode 100644 index 0000000..9a2123a --- /dev/null +++ b/account/template/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "{{crate_name}}" +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 = "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. +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/README.md b/account/template/README.md similarity index 60% rename from library/template/README.md rename to account/template/README.md index 97cc88a..26b01d4 100644 --- a/library/template/README.md +++ b/account/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`. 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/account/template/rust-toolchain.toml b/account/template/rust-toolchain.toml new file mode 100644 index 0000000..bc008c9 --- /dev/null +++ b/account/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/account/template/src/lib.rs b/account/template/src/lib.rs new file mode 100644 index 0000000..1e6fb8c --- /dev/null +++ b/account/template/src/lib.rs @@ -0,0 +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 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 +} diff --git a/library/template/Cargo.toml b/library/template/Cargo.toml deleted file mode 100644 index 83cdca7..0000000 --- a/library/template/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "{{crate_name}}" -version = "0.1.0" -edition = "2021" - -[lib] -crate-type = ["cdylib"] - -[profile.release] -opt-level = "z" diff --git a/library/template/src/lib.rs b/library/template/src/lib.rs deleted file mode 100644 index 09afd8b..0000000 --- a/library/template/src/lib.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![no_std] - -#[panic_handler] -fn my_panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[no_mangle] -pub fn some_func(n: u32) -> u32 { - n / 3 -}