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

Reintroduce moving pre-defined account components' source code into masm files #1097

Merged
merged 1 commit into from
Jan 23, 2025
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
64 changes: 36 additions & 28 deletions crates/miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ fn main() -> Result<()> {
)?;

// compile account components
compile_account_components(&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR), assembler)?;
compile_account_components(
&source_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
assembler,
)?;

generate_kernel_error_constants(&source_dir)?;

Expand Down Expand Up @@ -233,7 +237,7 @@ fn parse_proc_offsets(filename: impl AsRef<Path>) -> Result<BTreeMap<String, usi
// ================================================================================================

/// Reads the MASM files from "{source_dir}/miden" directory, compiles them into a Miden assembly
/// library, saves the library into "{target_dir}/miden.masl", and returns the complied library.
/// library, saves the library into "{target_dir}/miden.masl", and returns the compiled library.
fn compile_miden_lib(
source_dir: &Path,
target_dir: &Path,
Expand All @@ -258,7 +262,7 @@ fn compile_miden_lib(
// ================================================================================================

/// Reads all MASM files from the "{source_dir}", complies each file individually into a MASB
/// file, and stores the complied files into the "{target_dir}".
/// file, and stores the compiled files into the "{target_dir}".
///
/// The source files are expected to contain executable programs.
fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembler) -> Result<()> {
Expand All @@ -283,33 +287,37 @@ fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembl
Ok(())
}

// COMPILE DEFAULT ACCOUNT COMPONENTS
// COMPILE ACCOUNT COMPONENTS
// ================================================================================================

const BASIC_WALLET_CODE: &str = "
export.::miden::contracts::wallets::basic::receive_asset
export.::miden::contracts::wallets::basic::create_note
export.::miden::contracts::wallets::basic::move_asset_to_note
";

const RPO_FALCON_AUTH_CODE: &str = "
export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512
";

const BASIC_FUNGIBLE_FAUCET_CODE: &str = "
export.::miden::contracts::faucets::basic_fungible::distribute
export.::miden::contracts::faucets::basic_fungible::burn
";

/// Compiles the default account components into a MASL library and stores the complied files in
/// `target_dir`.
fn compile_account_components(target_dir: &Path, assembler: Assembler) -> Result<()> {
for (component_name, component_code) in [
("basic_wallet", BASIC_WALLET_CODE),
("rpo_falcon_512", RPO_FALCON_AUTH_CODE),
("basic_fungible_faucet", BASIC_FUNGIBLE_FAUCET_CODE),
] {
let component_library = assembler.clone().assemble_library([component_code])?;
/// Compiles the account components in `source_dir` into MASL libraries and stores the compiled
/// files in `target_dir`.
fn compile_account_components(
source_dir: &Path,
target_dir: &Path,
assembler: Assembler,
) -> Result<()> {
if !target_dir.exists() {
fs::create_dir_all(target_dir).unwrap();
}

for masm_file_path in get_masm_files(source_dir).unwrap() {
let component_name = masm_file_path
.file_stem()
.expect("masm file should have a file stem")
.to_str()
.expect("file stem should be valid UTF-8")
.to_owned();

// Read the source code to string instead of passing it to assemble_library directly since
// that would attempt to interpret the path as a LibraryPath which would fail.
let component_source_code = fs::read_to_string(masm_file_path)
.expect("reading the component's MASM source code should succeed");

let component_library = assembler
.clone()
.assemble_library([component_source_code])
.expect("library assembly should succeed");
let component_file_path =
target_dir.join(component_name).with_extension(Library::LIBRARY_EXTENSION);
component_library.write_to_file(component_file_path).into_diagnostic()?;
Expand Down
7 changes: 6 additions & 1 deletion crates/miden-lib/src/account/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use crate::account::components::rpo_falcon_512_library;
/// An [`AccountComponent`] implementing the RpoFalcon512 signature scheme for authentication of
/// transactions.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::auth::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// - `auth_tx_rpo_falcon512`, which can be used to verify a signature provided via the advice stack
/// to authenticate a transaction.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct RpoFalcon512 {
public_key: PublicKey,
}
Expand Down
7 changes: 6 additions & 1 deletion crates/miden-lib/src/account/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::account::{auth::RpoFalcon512, components::basic_fungible_faucet_libra

/// An [`AccountComponent`] implementing a basic fungible faucet.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::faucets::basic_fungible`. When linking
/// against this component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be
/// available to the assembler which is the case when using
/// [`TransactionKernel::assembler()`][kasm]. The procedures of this component are:
/// - `distribute`, which mints an assets and create a note for the provided recipient.
/// - `burn`, which burns the provided asset.
///
Expand All @@ -24,6 +27,8 @@ use crate::account::{auth::RpoFalcon512, components::basic_fungible_faucet_libra
/// authentication.
///
/// This component supports accounts of type [`AccountType::FungibleFaucet`].
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicFungibleFaucet {
symbol: TokenSymbol,
decimals: u8,
Expand Down
7 changes: 6 additions & 1 deletion crates/miden-lib/src/account/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::account::{auth::RpoFalcon512, components::basic_wallet_library};

/// An [`AccountComponent`] implementing a basic wallet.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::wallets::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// - `receive_asset`, which can be used to add an asset to the account.
/// - `create_note`, which can be used to create a new note without any assets attached to it.
/// - `move_asset_to_note`, which can be used to remove the specified asset from the account and add
Expand All @@ -25,6 +28,8 @@ use crate::account::{auth::RpoFalcon512, components::basic_wallet_library};
/// providing authentication.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicWallet;

impl From<BasicWallet> for AccountComponent {
Expand Down
Loading