Replies: 3 comments 4 replies
-
Great write up! Thank you! A few preliminary comments below. I wonder if we need to separate For For exposing MASM libraries to Rust code, I'd prefer to have Rust wrappers (rather than using use miden::{
account,
asset::Asset,
note::Recipient,
tx,
};
#[miden_account]
pub struct MyWallet;
#[miden_account]
impl MyWallet {
pub fn recieve_asset(&mut self, asset: Asset) {
account::add_asset(asset);
}
pub fn send_asset(&mut self, asset: Asset, recipient: Recipient) {
account::remove_asset(asset);
tx::create_note(asset, recipient);
}
} Where In my mind, the first milestone for
Totally understand that the code output by the compiler would not look as nice or be as optimal - but would be great to support same general functionality. Beyond this initial milestone, there are a few things which we can try to support in the longer term. A couple of examples: Using
|
Beta Was this translation helpful? Give feedback.
-
i think it makes sense to prioritise smart contract devs need - that will also provide useful information on what api/sdk devs building solely on the vm need |
Beta Was this translation helpful? Give feedback.
-
I made #72 to test how we can compile the above account code. |
Beta Was this translation helpful? Give feedback.
-
Goals
Provide libraries and tools for developers to write, compile, and test:
Implementation details
miden-vm-sdk
crateThis is the crate the developers writing Miden VM programs and libraries in Rust will use. Provides an API and macros to write and test Miden VM programs and libraries. Contains no rollup-specific features.
Miden VM ops not represented in Wasm (asserts, advice provider ops,
hash
,mtree
) could be implemented as Rust functions (sort of intrinsic) with an extern C functions under the hood. The Miden compiler later transforms these function calls into VM ops. Ops parameters could be exposed as high-level Rust types via adapters.For example, the high-level RPO hash function might look like this:
and is implemented using a low-level intrinsic
hmerge
function:that is converted to
hmerge
VM op by the compiler.The crate also provides an API for running and testing the compiled Miden code. For Miden programs, it should be possible to provide inputs and check outputs. For Miden libraries, it should be possible to call functions from the library and check the result.
To support inter-context procedure calls (
call
andsyscall
) we can pass arguments that don't fit in the stack via the advice provider. We can generate the marshaling code for these arguments on the caller and the callee sides with macros (using WIT's ABI). This would make the callee only callable viacall
andsyscall
. We should consider marking such functions with a special attribute in MASM to convey that they can be called only viacall
andsyscall
.Public inputs and output for Miden programs are provided via the stack before and after the program execution, which is hard to expose in Rust API. We could load them into some reserved global memory and serve on the Rust side with some
read
write
functions with implementation generated by the Miden IR compiler. @bobbinth @frisitano Do we have any alternative way to provide inputs and capture outputs for a Miden program?Using Miden libraries in the Rust code
To be able to use Miden libraries in the Rust code, we need to know the high-level type signatures of the functions in the library. We could try to leverage WIT and Wasm component model to provide a high-level interface description for the compiled Miden libraries. There is an actively developed ecosystem for WIT, and we could either adopt it as a whole or take pieces that fit us.
See wit-bindgen and WIT format for more details.
The code for calling functions from the Miden library into Rust code might look like this:
Where 'awesome_library.wit' is a WIT file is:
In the Miden compiler, we import the 'awesome_library' module and convert all calls to
some_hash_func
to the imported function using eitherexec
orcall
depending on the special marks we put on inter-context callable functions. In the case ofcall
, we also generate marshaling of the non-integer arguments to the advice provider before the call. If we could not find a way to mark inter-context callable functions in WIT, we could write acall
macro to call such functions. So such calls would look likecall!(sat::account::add_asset(asset))
.cargo-miden
cargo extensionCompiles Rust code written using
miden-vm-sdk
crate to Miden VM programs and libraries.The following commands are to be implemented:
compile
to compile Rust code (crate) to Miden VM programs and libraries and to Miden rollup accounts and note scripts.Here is an example of a command to compile the current crate's binary target to the Miden VM program -
cargo miden compile --output app.masm
.miden-sdk
crateHigh-level SDK for the smart contract developers. Provides an API and macros to develop and test Miden rollup accounts and note scripts.
A developer should be able to:
The account definition for a wallet might look like this:
In the Miden compiler, we import the 'sat' module and convert all calls to its functions to the imported ones with either
exec
orcall
depending on the special marks we put on inter-context callable functions. In the case ofcall
, we also generate marshaling of the non-integer arguments to the advice provider before the call.[miden_account]
attribute generates the WIT file forMyWallet
, which can be used in the note scripts like this:This crate could be combined with an SDK that constructs transactions and deploys smart contracts.
Beta Was this translation helpful? Give feedback.
All reactions