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

fix: add tx kernel library with stubs and link it on -l base #277

Merged
merged 7 commits into from
Aug 15, 2024
Merged
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
13 changes: 13 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 @@ -13,6 +13,7 @@ members = [
"midenc-driver",
"midenc-session",
"midenc-runner",
"sdk/base-sys",
"tools/*",
"frontend-wasm",
"tests/integration",
Expand Down
1 change: 1 addition & 0 deletions midenc-session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ miden-stdlib.workspace = true
miden-diagnostics.workspace = true
midenc-hir-symbol.workspace = true
midenc-hir-macros.workspace = true
miden-base-sys = { version = "0.0.0", path = "../sdk/base-sys", features = ["masl-lib"] }
thiserror.workspace = true
8 changes: 2 additions & 6 deletions midenc-session/src/libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use miden_assembly::{Library as CompiledLibrary, LibraryNamespace};
use miden_base_sys::masl::tx::MidenTxKernelLibrary;
use miden_stdlib::StdLibrary;

use crate::{
Expand Down Expand Up @@ -62,12 +63,7 @@ impl LinkLibrary {
// Handle libraries shipped with the compiler, or via Miden crates
match self.name.as_ref() {
"std" => return Ok(StdLibrary::default().into()),
"base" => {
// TODO(pauls): Handle properly once we have miden-base-sys
return Err(Report::msg(
"invalid link library 'base': miden-base-sys is unimplemented",
));
}
"base" => return Ok(MidenTxKernelLibrary::default().into()),
_ => (),
}

Expand Down
22 changes: 0 additions & 22 deletions sdk/Cargo.lock

This file was deleted.

27 changes: 0 additions & 27 deletions sdk/Cargo.toml

This file was deleted.

29 changes: 29 additions & 0 deletions sdk/base-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "miden-base-sys"
description = "Miden rollup Rust bingings and MASM library"
version = "0.0.0"
rust-version.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true
documentation.workspace = true
categories.workspace = true
keywords.workspace = true
license.workspace = true
readme.workspace = true
edition.workspace = true

[dependencies]
miden-assembly.workspace = true
miden-stdlib-sys = { version = "0.0.1", path = "../stdlib-sys", optional = true }

[build-dependencies]
miden-assembly.workspace = true

[features]
default = []

# User facing Rust bindings
"bindings" = ["miden-stdlib-sys"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simply make the masl-lib parts of this use #[cfg(not(target_family = "wasm"))], rather than a Cargo feature. This keeps it out of the Wasm builds that would cause this to end up in code processed by the compiler, but otherwise keeps all of the code together.

In the future we may need to add a feature that allows those items to exist in a Wasm build as well (for building the compiler to run in the browser), but we're a way away from that point yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or vice versa, make the bindings conditional on #[cfg(target_family = "wasm")] - that might make more sense, since the bindings only are needed when targeting Miden.

# MASL library for Miden rollup (tx kernel, etc.) used by the compiler in the link phase
"masl-lib" = []
30 changes: 30 additions & 0 deletions sdk/base-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::{env, path::Path, sync::Arc};

use miden_assembly::{
diagnostics::{IntoDiagnostic, Result},
Assembler, Library as CompiledLibrary, LibraryNamespace,
};

/// Read and parse the contents from `./masm/*` and compile it to MASL.
fn main() -> Result<()> {
// re-build the `[OUT_DIR]/assets/` file iff something in the `./masm` directory
// or its builder changed:
println!("cargo:rerun-if-changed=masm");

let build_dir = env::var("OUT_DIR").unwrap();
let build_dir = Path::new(&build_dir);
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let source_manager = Arc::new(miden_assembly::DefaultSourceManager::default());
let namespace = "miden".parse::<LibraryNamespace>().expect("invalid base namespace");

let tx_asm_dir = Path::new(manifest_dir).join("masm").join("tx");
let asm = Assembler::new(source_manager);
let txlib = CompiledLibrary::from_dir(tx_asm_dir, namespace, asm)?;
let tx_masl_path = build_dir
.join("assets")
.join("tx")
.with_extension(CompiledLibrary::LIBRARY_EXTENSION);
txlib.write_to_file(tx_masl_path).into_diagnostic()?;

Ok(())
}
11 changes: 11 additions & 0 deletions sdk/base-sys/masm/tx/account.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Stubs for miden::account tx kernel module

export.remove_asset
push.1
assertz
end

export.add_asset
push.1
assertz
end
6 changes: 6 additions & 0 deletions sdk/base-sys/masm/tx/tx.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Stubs for miden::tx tx kernel module

export.create_note
push.1
assertz
end
1 change: 1 addition & 0 deletions sdk/base-sys/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tx;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use miden_stdlib_sys::Felt;

use crate::{AccountId, CoreAsset, NoteId, NoteType, Tag};
use crate::bindings::tx::{AccountId, CoreAsset, NoteId, NoteType, Tag};

#[link(wasm_import_module = "miden::account")]
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![no_std]

mod externs;
use externs::*;

Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions sdk/base-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Enable no_std for the bindings module
#![cfg_attr(feature = "bindings", no_std)]

#[cfg(feature = "bindings")]
pub mod bindings;
#[cfg(feature = "masl-lib")]
pub mod masl;
1 change: 1 addition & 0 deletions sdk/base-sys/src/masl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tx;
24 changes: 24 additions & 0 deletions sdk/base-sys/src/masl/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use miden_assembly::{utils::Deserializable, Library as CompiledLibrary};

/// Stubs for the Miden rollup tx kernel
pub struct MidenTxKernelLibrary(CompiledLibrary);

impl AsRef<CompiledLibrary> for MidenTxKernelLibrary {
fn as_ref(&self) -> &CompiledLibrary {
&self.0
}
}

impl From<MidenTxKernelLibrary> for CompiledLibrary {
fn from(lib: MidenTxKernelLibrary) -> Self {
lib.0
}
}

impl Default for MidenTxKernelLibrary {
fn default() -> Self {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/tx.masl"));
let contents = CompiledLibrary::read_from_bytes(bytes).expect("failed to read std masl!");
Self(contents)
}
}
2 changes: 1 addition & 1 deletion sdk/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ crate-type = ["rlib"]

[dependencies]
miden-stdlib-sys = { path = "../stdlib-sys" }
miden-tx-kernel-sys = { path = "../tx-kernel-sys" }
miden-base-sys = { path = "../base-sys", features = ["bindings"] }
2 changes: 1 addition & 1 deletion sdk/sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]

pub use miden_base_sys::bindings::tx::*;
pub use miden_stdlib_sys::*;
pub use miden_tx_kernel_sys::*;
24 changes: 13 additions & 11 deletions sdk/stdlib-sys/src/intrinsics/felt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::transmute_int_to_float)]

use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

#[link(wasm_import_module = "miden:stdlib/intrinsics_felt")]
Expand Down Expand Up @@ -139,19 +141,19 @@ impl From<Felt> for u64 {

impl From<u32> for Felt {
fn from(value: u32) -> Self {
Self(unsafe { core::mem::transmute(value) })
Self(unsafe { core::mem::transmute::<u32, f32>(value) })
}
}

impl From<u16> for Felt {
fn from(value: u16) -> Self {
Self(unsafe { core::mem::transmute(value as u32) })
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
}
}

impl From<u8> for Felt {
fn from(value: u8) -> Self {
Self(unsafe { core::mem::transmute(value as u32) })
Self(unsafe { core::mem::transmute::<u32, f32>(value as u32) })
}
}

Expand Down Expand Up @@ -308,12 +310,12 @@ pub fn assert_eq(a: Felt, b: Felt) {
}
}

#[cfg(test)]
mod tests {
use super::*;
// #[cfg(test)]
// mod tests {
// use super::*;

#[test]
fn felt_macro_smoke_test() {
let _ = felt!(1);
}
}
// #[test]
// fn felt_macro_smoke_test() {
// let _ = felt!(1);
// }
// }
4 changes: 2 additions & 2 deletions sdk/stdlib-sys/src/stdlib/crypto/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn hash_1to1(
input: [u8; 32],
extern_hash_1to1: unsafe extern "C" fn(u32, u32, u32, u32, u32, u32, u32, u32, *mut u8),
) -> [u8; 32] {
let input = unsafe { core::mem::transmute::<_, [u32; 8]>(input) };
let input = unsafe { core::mem::transmute::<[u8; 32], [u32; 8]>(input) };
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<[u8; 32]>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
Expand Down Expand Up @@ -137,7 +137,7 @@ fn hash_2to1(
*mut u8,
),
) -> [u8; 32] {
let input = unsafe { core::mem::transmute::<_, [u32; 16]>(input) };
let input = unsafe { core::mem::transmute::<[u8; 64], [u32; 16]>(input) };
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<[u8; 32]>::uninit();
let ptr = ret_area.as_mut_ptr() as *mut u8;
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/stdlib/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn pipe_double_words_to_memory(num_words: Felt) -> (Word, Vec<Felt>) {
}

let num_words_in_felts = num_words.as_u64() as usize * 4;
let mut buf: Vec<Felt> = Vec::with_capacity(num_words_in_felts as usize);
let mut buf: Vec<Felt> = Vec::with_capacity(num_words_in_felts);
let write_ptr = buf.as_mut_ptr();
let end_ptr = unsafe { write_ptr.add(num_words_in_felts) };
// Place for returned C, B, A, write_ptr
Expand Down
15 changes: 0 additions & 15 deletions sdk/tx-kernel-sys/CHANGELOG.md

This file was deleted.

18 changes: 0 additions & 18 deletions sdk/tx-kernel-sys/Cargo.toml

This file was deleted.

Loading
Loading