Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC of the Miden SDK and rollup account implementation #75

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target/
bin/midenc
bin/filecheck

.vscode
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"frontend-wasm",
"tests/rust-apps/*",
"tests/integration",
"sdk",
]
exclude = ["tests/rust-apps-wasm"]

Expand Down
8 changes: 7 additions & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ description = "Install wasm32-unknown-unknown target"
command = "rustup"
args = ["target", "add", "wasm32-unknown-unknown", "--toolchain", "${CARGO_MAKE_TOOLCHAIN}"]

[tasks.install-wasm-wasi-target]
category = "Test"
description = "Install wasm32-wasi target"
command = "rustup"
args = ["target", "add", "wasm32-wasi", "--toolchain", "${CARGO_MAKE_TOOLCHAIN}"]

[tasks.install-rust-src]
category = "Test"
description = "Install rust-src component"
Expand All @@ -180,7 +186,7 @@ category = "Test"
description = "Runs tests written in Rust"
command = "rustup"
args = ["run", "${CARGO_MAKE_TOOLCHAIN}", "cargo", "test", "@@remove-empty(CARGO_MAKE_CARGO_VERBOSE_FLAGS)", "@@split(CARGO_MAKE_CARGO_BUILD_TEST_FLAGS, )", "${@}"]
dependencies = ["install-wasm-target", "install-rust-src"]
dependencies = ["install-wasm-target", "install-wasm-wasi-target", "install-rust-src"]

[tasks.test-filecheck]
category = "Test"
Expand Down
4 changes: 3 additions & 1 deletion hir/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ impl Module {
/// Unlinks the given function from this module
pub fn unlink(&mut self, id: Ident) -> Box<Function> {
let mut cursor = self.cursor_mut_at(id);
cursor.remove().expect("invalid function id")
cursor
.remove()
.expect(format!("invalid function id {}", id).as_str())
}

/// Append `function` to the end of this module's body, returning the [FuncId]
Expand Down
16 changes: 16 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "miden"
description = "Miden SDK"
version.workspace = true
rust-version.workspace = true
authors.workspace = true
repository.workspace = true
categories.workspace = true
keywords.workspace = true
license.workspace = true
readme.workspace = true
edition.workspace = true

[dependencies]
derive_more = "0.99"
serde = { version = "1.0", default-features = false, features = ["derive"] }
27 changes: 27 additions & 0 deletions sdk/src/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::asset::Asset;
use crate::felt::Felt;

extern "C" {
#[link_name = "miden::sat::account::add_asset"]
pub fn add_asset_inner(asset: Asset) -> Asset;
#[link_name = "miden::sat::account::remove_asset"]
pub fn remove_asset_inner(asset: Asset) -> Asset;
}

pub fn add_asset(asset: Asset) -> Asset {
unsafe { add_asset_inner(asset) }
}

pub fn remove_asset(asset: Asset) -> Asset {
unsafe { remove_asset_inner(asset) }
}

#[repr(transparent)]
#[derive(Clone, derive_more::Into, serde::Serialize, serde::Deserialize)]
pub struct AccountId(Felt);

impl AccountId {
pub fn new(value: u64) -> Self {
Self(Felt::from(value))
}
}
32 changes: 32 additions & 0 deletions sdk/src/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::account::AccountId;
use crate::felt::Word;

#[derive(derive_more::From, serde::Serialize, serde::Deserialize)]
#[repr(C)]
pub enum Asset {
Fungible(FungibleAsset),
NonFungible(NonFungibleAsset),
}

impl From<Word> for Asset {
fn from(_value: Word) -> Self {
todo!()
}
}

#[repr(C)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct FungibleAsset {
pub asset: AccountId,
pub amount: u64,
}

impl FungibleAsset {
pub fn new(asset: AccountId, amount: u64) -> Self {
Self { asset, amount }
}
}

#[repr(transparent)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct NonFungibleAsset(Word);
23 changes: 23 additions & 0 deletions sdk/src/call_conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::Felt;

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum FuncArgPassingMedium {
Stack,
AdvProvider,
}

pub struct FuncArgPassingConv {
pub medium: FuncArgPassingMedium,
pub felt_count: u32,
}

impl FuncArgPassingConv {
pub fn to_felt(&self) -> Felt {
todo!()
}

pub fn from_felt(felt: Felt) -> Self {
let _ = felt;
todo!()
}
}
8 changes: 8 additions & 0 deletions sdk/src/eoa/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern "C" {
#[link_name = "miden::eoa::basic::auth_tx_rpo_falcon512"]
pub fn auth_tx_rpo_falcon512_inner();
}

pub fn auth_tx_rpo_falcon512() {
unsafe { auth_tx_rpo_falcon512_inner() }
}
1 change: 1 addition & 0 deletions sdk/src/eoa/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod basic;
38 changes: 38 additions & 0 deletions sdk/src/felt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// Number of field elements in a word.
pub const WORD_SIZE: usize = 4;

/// A group of four field elements in the Miden base field.
#[repr(transparent)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Word([Felt; WORD_SIZE]);

impl Word {
pub fn from_u64(f1: u64, f2: u64, f3: u64, f4: u64) -> Self {
Self([
Felt::from(f1),
Felt::from(f2),
Felt::from(f3),
Felt::from(f4),
])
}

pub fn from_felts(f1: Felt, f2: Felt, f3: Felt, f4: Felt) -> Self {
Self([f1, f2, f3, f4])
}
}

#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Felt(u64);

impl From<u64> for Felt {
fn from(value: u64) -> Self {
Self(value)
}
}

impl From<Felt> for u64 {
fn from(value: Felt) -> Self {
value.0
}
}
26 changes: 26 additions & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![no_std]
#![deny(warnings)]

extern crate alloc;

mod asset;
mod felt;
mod note;
mod serialization;

pub use asset::Asset;
pub use asset::FungibleAsset;
pub use asset::NonFungibleAsset;
pub use felt::Felt;
pub use felt::Word;
pub use note::Recipient;
pub use note::Tag;
pub use serialization::bytes_to_felts;
pub use serialization::felts_to_bytes;

pub mod call_conv;

pub mod account;

pub mod eoa;
pub mod sat;
28 changes: 28 additions & 0 deletions sdk/src/note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::felt::Felt;
use crate::felt::Word;

#[repr(transparent)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Recipient(Word);

impl From<Word> for Recipient {
fn from(value: Word) -> Self {
Self(value)
}
}

#[repr(transparent)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Tag(Felt);

impl Tag {
pub fn new(value: u64) -> Self {
Self(Felt::from(value))
}
}

impl From<Felt> for Tag {
fn from(value: Felt) -> Self {
Self(value)
}
}
2 changes: 2 additions & 0 deletions sdk/src/sat/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod note;
pub mod tx;
10 changes: 10 additions & 0 deletions sdk/src/sat/note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::asset::Asset;

extern "C" {
#[link_name = "miden::sat::note::get_assets"]
pub fn get_assets_inner() -> Asset;
}

pub fn get_assets() -> Asset {
unsafe { get_assets_inner() }
}
12 changes: 12 additions & 0 deletions sdk/src/sat/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::asset::Asset;
use crate::note::Recipient;
use crate::note::Tag;

extern "C" {
#[link_name = "miden::sat::tx::create_note"]
pub fn create_note_inner(asset: Asset, tag: Tag, recipient: Recipient);
}

pub fn create_note(asset: Asset, tag: Tag, recipient: Recipient) {
unsafe { create_note_inner(asset, tag, recipient) }
}
Loading
Loading