Skip to content

Commit

Permalink
remove digest-in-function-name encoding and MidenAbiImport::digest,
Browse files Browse the repository at this point in the history
change module and function names for core Wasm module imports for
stdlib and tx kernel.
  • Loading branch information
greenhat committed Oct 2, 2024
1 parent 02f855d commit 8915b27
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 1,933 deletions.
18 changes: 0 additions & 18 deletions frontend-wasm/src/miden_abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,12 @@ pub(crate) mod stdlib;
pub(crate) mod transform;
pub(crate) mod tx_kernel;

use miden_core::crypto::hash::RpoDigest;
use midenc_hir::{FunctionType, Symbol};
use rustc_hash::FxHashMap;

pub(crate) type FunctionTypeMap = FxHashMap<&'static str, FunctionType>;
pub(crate) type ModuleFunctionTypeMap = FxHashMap<&'static str, FunctionTypeMap>;

/// Parse the stable import function name and the hex encoded digest from the function name
pub fn parse_import_function_digest(import_name: &str) -> Result<(String, RpoDigest), String> {
// parse the hex encoded digest from the function name in the angle brackets
// and the function name (before the angle brackets) example:
// "miden:tx_kernel/note.get_inputs"
let mut parts = import_name.split('<');
let function_name = parts.next().unwrap();
let digest = parts
.next()
.and_then(|s| s.strip_suffix('>'))
.ok_or("Import name parsing error: missing closing angle bracket in import name")?;
Ok((
function_name.to_string(),
RpoDigest::try_from(digest).map_err(|e| e.to_string())?,
))
}

pub fn is_miden_abi_module(module_id: Symbol) -> bool {
is_miden_stdlib_module(module_id) || is_miden_sdk_module(module_id)
}
Expand Down
6 changes: 4 additions & 2 deletions frontend-wasm/src/miden_abi/stdlib/crypto/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use midenc_hir_type::Type::*;

use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};

pub(crate) const RPO_FALCON512_VERIFY: &str = "rpo_falcon512_verify";
pub(crate) const MODULE_ID: &str = "miden:core-import/[email protected]";

pub(crate) const RPO_FALCON512_VERIFY: &str = "rpo-falcon512-verify";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
Expand All @@ -12,6 +14,6 @@ pub(crate) fn signatures() -> ModuleFunctionTypeMap {
RPO_FALCON512_VERIFY,
FunctionType::new([Felt, Felt, Felt, Felt, Felt, Felt, Felt, Felt], []),
);
m.insert("std::crypto::dsa::rpo_falcon512", funcs);
m.insert(MODULE_ID, funcs);
m
}
2 changes: 1 addition & 1 deletion frontend-wasm/src/miden_abi/stdlib/crypto/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};
pub const MODULE_ID: &str = "miden:core-import/[email protected]";

pub(crate) const BLAKE3_HASH_1TO1: &str = "blake3-hash-one-to-one";
pub(crate) const BLAKE3_HASH_2TO1: &str = "hash_2to1";
pub(crate) const BLAKE3_HASH_2TO1: &str = "blake3-hash-two-to-one";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
Expand Down
8 changes: 5 additions & 3 deletions frontend-wasm/src/miden_abi/stdlib/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use midenc_hir_type::Type::*;

use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};

pub(crate) const PIPE_WORDS_TO_MEMORY: &str = "pipe_words_to_memory";
pub(crate) const PIPE_DOUBLE_WORDS_TO_MEMORY: &str = "pipe_double_words_to_memory";
pub(crate) const MODULE_ID: &str = "miden:core-import/[email protected]";

pub(crate) const PIPE_WORDS_TO_MEMORY: &str = "pipe-words-to-memory";
pub(crate) const PIPE_DOUBLE_WORDS_TO_MEMORY: &str = "pipe-double-words-to-memory";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
Expand Down Expand Up @@ -40,6 +42,6 @@ pub(crate) fn signatures() -> ModuleFunctionTypeMap {
],
),
);
m.insert("std::mem", funcs);
m.insert(MODULE_ID, funcs);
m
}
6 changes: 3 additions & 3 deletions frontend-wasm/src/miden_abi/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum TransformStrategy {
fn get_transform_strategy(module_id: &str, function_id: &str) -> TransformStrategy {
#[allow(clippy::single_match)]
match module_id {
"std::mem" => match function_id {
stdlib::mem::MODULE_ID => match function_id {
stdlib::mem::PIPE_WORDS_TO_MEMORY => return TransformStrategy::ReturnViaPointer,
stdlib::mem::PIPE_DOUBLE_WORDS_TO_MEMORY => return TransformStrategy::ReturnViaPointer,
_ => (),
Expand All @@ -32,11 +32,11 @@ fn get_transform_strategy(module_id: &str, function_id: &str) -> TransformStrate
stdlib::crypto::hashes::BLAKE3_HASH_2TO1 => return TransformStrategy::ReturnViaPointer,
_ => (),
},
"std::crypto::dsa::rpo_falcon512" => match function_id {
stdlib::crypto::dsa::MODULE_ID => match function_id {
stdlib::crypto::dsa::RPO_FALCON512_VERIFY => return TransformStrategy::NoTransform,
_ => (),
},
"miden::note" => match function_id {
tx_kernel::note::MODULE_ID => match function_id {
tx_kernel::note::GET_INPUTS => return TransformStrategy::ListReturn,
_ => (),
},
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/src/miden_abi/tx_kernel/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const MODULE_ID: &str = "miden:core-import/[email protected]";

pub const ADD_ASSET: &str = "add-asset";
pub const REMOVE_ASSET: &str = "remove-asset";
pub const GET_ID: &str = "get_id";
pub const GET_ID: &str = "get-id";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
Expand Down
6 changes: 4 additions & 2 deletions frontend-wasm/src/miden_abi/tx_kernel/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use midenc_hir_type::Type::*;

use crate::miden_abi::{FunctionTypeMap, ModuleFunctionTypeMap};

pub const GET_INPUTS: &str = "get_inputs";
pub const MODULE_ID: &str = "miden:core-import/[email protected]";

pub const GET_INPUTS: &str = "get-inputs";

pub(crate) fn signatures() -> ModuleFunctionTypeMap {
let mut m: ModuleFunctionTypeMap = Default::default();
let mut note: FunctionTypeMap = Default::default();
note.insert(GET_INPUTS, FunctionType::new([I32], [I32, I32]));
m.insert("miden::note", note);
m.insert(MODULE_ID, note);
m
}
8 changes: 1 addition & 7 deletions frontend-wasm/src/module/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,8 @@ pub fn translate_module_as_component(
ext_func.module.as_symbol(),
ext_func.function.as_symbol(),
);
let digest = *module_state.digest(ext_func).unwrap_or_else(|| {
panic!("failed to find MAST root hash for function {}", ext_func.function)
});
let component_import =
midenc_hir::ComponentImport::MidenAbiImport(MidenAbiImport {
function_ty,
digest,
});
midenc_hir::ComponentImport::MidenAbiImport(MidenAbiImport { function_ty });
cb.add_import(*ext_func, component_import);
}
}
Expand Down
30 changes: 5 additions & 25 deletions frontend-wasm/src/module/module_translation_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use miden_core::crypto::hash::RpoDigest;
use midenc_hir::{
diagnostics::{DiagnosticsHandler, Severity},
AbiParam, CallConv, DataFlowGraph, FunctionIdent, Ident, Linkage, Signature,
Expand All @@ -9,16 +8,14 @@ use super::{instance::ModuleArgument, ir_func_type, EntityIndex, FuncIndex, Modu
use crate::{
error::WasmResult,
intrinsics::is_miden_intrinsics_module,
miden_abi::{is_miden_abi_module, miden_abi_function_type, parse_import_function_digest},
miden_abi::{is_miden_abi_module, miden_abi_function_type},
translation_utils::sig_from_func_type,
};

pub struct ModuleTranslationState {
/// Imported and local functions
/// Stores both the function reference and its signature
functions: FxHashMap<FuncIndex, (FunctionIdent, Signature)>,
/// Parsed MAST root hash for imported functions for Miden SDK
digests: FxHashMap<FunctionIdent, RpoDigest>,
/// Number of imported or aliased functions in the module.
pub num_imported_funcs: usize,
// stable_imported_miden_abi_functions: FxHashMap<FunctionIdent, String>,
Expand Down Expand Up @@ -54,7 +51,6 @@ impl ModuleTranslationState {
}
}
let mut functions = FxHashMap::default();
let mut digests = FxHashMap::default();
for (index, func_type) in &module.functions {
let wasm_func_type = mod_types[func_type.signature].clone();
let ir_func_type = ir_func_type(&wasm_func_type, diagnostics).unwrap();
Expand All @@ -64,21 +60,11 @@ impl ModuleTranslationState {
} else if module.is_imported_function(index) {
assert!((index.as_u32() as usize) < module.num_imported_funcs);
let import = &module.imports[index.as_u32() as usize];
if let Ok((func_stable_name, digest)) = parse_import_function_digest(&import.field)
{
let func_id = FunctionIdent {
module: Ident::from(import.module.as_str()),
function: Ident::from(func_stable_name.as_str()),
};
functions.insert(index, (func_id, sig));
digests.insert(func_id, digest);
} else {
let func_id = FunctionIdent {
module: Ident::from(import.module.as_str()),
function: Ident::from(import.field.as_str()),
};
functions.insert(index, (func_id, sig));
let func_id = FunctionIdent {
module: Ident::from(import.module.as_str()),
function: Ident::from(import.field.as_str()),
};
functions.insert(index, (func_id, sig));
} else {
let func_name = module.func_name(index);
let func_id = FunctionIdent {
Expand All @@ -90,7 +76,6 @@ impl ModuleTranslationState {
}
Self {
functions,
digests,
num_imported_funcs: module.num_imported_funcs,
}
}
Expand All @@ -101,11 +86,6 @@ impl ModuleTranslationState {
&self.functions[&index].1
}

/// Returns parsed MAST root hash for the given function id (if it is imported and has one)
pub fn digest(&self, func_id: &FunctionIdent) -> Option<&RpoDigest> {
self.digests.get(func_id)
}

/// Get the `FunctionIdent` that should be used to make a direct call to function
/// `index`.
///
Expand Down
20 changes: 6 additions & 14 deletions hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ pub struct CanonAbiImport {
pub struct MidenAbiImport {
/// The Miden function type as it is defined in the MASM
pub function_ty: FunctionType,
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
}

/// A component import
Expand All @@ -56,13 +54,6 @@ pub enum ComponentImport {
}

impl ComponentImport {
pub fn digest(&self) -> RpoDigest {
match self {
ComponentImport::CanonAbiImport(import) => import.digest,
ComponentImport::MidenAbiImport(import) => import.digest,
}
}

pub fn unwrap_canon_abi_import(&self) -> &CanonAbiImport {
match self {
ComponentImport::CanonAbiImport(import) => import,
Expand Down Expand Up @@ -90,13 +81,14 @@ impl formatter::PrettyPrint for ComponentImport {
}
ComponentImport::MidenAbiImport(_import) => "".to_string(),
};

let digest = match self {
ComponentImport::CanonAbiImport(import) => format!("(digest {})", import.digest),
ComponentImport::MidenAbiImport(_) => "".to_string(),
};
const_text("(")
+ text(name)
+ const_text("(")
+ const_text("digest")
+ const_text(" ")
+ display(self.digest())
+ const_text(")")
+ text(digest)
+ const_text(" ")
+ const_text("(")
+ const_text("type")
Expand Down
6 changes: 3 additions & 3 deletions sdk/base-sys/src/bindings/tx/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use crate::bindings::tx::{AccountId, CoreAsset, NoteId, NoteType, Tag};

#[link(wasm_import_module = "miden:core-import/[email protected]")]
extern "C" {
#[link_name = "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "get-id"]
pub fn extern_account_get_id() -> AccountId;
#[link_name = "add-asset"]
pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset);
#[link_name = "remove-asset"]
pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset);
}

#[link(wasm_import_module = "miden::note")]
#[link(wasm_import_module = "miden:core-import/note@1.0.0")]
extern "C" {
#[link_name = "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "get-inputs"]
pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize;
}

Expand Down
37 changes: 20 additions & 17 deletions sdk/stdlib-sys/src/intrinsics/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ use crate::Felt;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(C, align(32))]
// pub struct Word([Felt; 4]);
pub struct Word {
pub inner: (Felt, Felt, Felt, Felt),
}
// impl Word {
// pub const fn new(word: [Felt; 4]) -> Self {
// Self { inner: word }
// }
// }
// impl From<[Felt; 4]> for Word {
// fn from(word: [Felt; 4]) -> Self {
// Self { inner: word }
// }
// }
// impl From<Word> for [Felt; 4] {
// #[inline(always)]
// fn from(word: Word) -> Self {
// word.inner
// }
// }
impl Word {
pub const fn new(word: [Felt; 4]) -> Self {
Self {
inner: (word[0], word[1], word[2], word[3]),
}
}
}
impl From<[Felt; 4]> for Word {
fn from(word: [Felt; 4]) -> Self {
Self {
inner: (word[0], word[1], word[2], word[3]),
}
}
}
impl From<Word> for [Felt; 4] {
#[inline(always)]
fn from(word: Word) -> Self {
[word.inner.0, word.inner.1, word.inner.2, word.inner.3]
}
}
impl Index<usize> for Word {
type Output = Felt;

Expand Down
4 changes: 2 additions & 2 deletions sdk/stdlib-sys/src/stdlib/crypto/dsa.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{Felt, Word};

#[link(wasm_import_module = "std::crypto::dsa::rpo_falcon512")]
#[link(wasm_import_module = "miden:core-import/stdlib-crypto-dsa@1.0.0")]
extern "C" {
#[link_name = "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "rpo-falcon512-verify"]
fn extern_rpo_falcon512_verify(
pk1: Felt,
pk2: Felt,
Expand Down
8 changes: 4 additions & 4 deletions sdk/stdlib-sys/src/stdlib/crypto/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
/// Input: 64-bytes stored in the first 16 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element)
/// The output is passed back to the caller via a pointer.
#[link_name = "hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "blake3-hash-two-to-one"]
fn extern_blake3_hash_2to1(
e1: u32,
e2: u32,
Expand All @@ -49,14 +49,14 @@ extern "C" {
);
}

#[link(wasm_import_module = "std::crypto::hashes::sha256")]
#[link(wasm_import_module = "miden:core-import/stdlib-crypto-hashes@1.0.0")]
extern "C" {
/// Computes SHA256 1-to-1 hash.
///
/// Input: 32-bytes stored in the first 8 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element).
/// The output is passed back to the caller via a pointer.
#[link_name = "sha256_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "sha256-hash-one-to-one"]
fn extern_sha256_hash_1to1(
e1: u32,
e2: u32,
Expand All @@ -74,7 +74,7 @@ extern "C" {
/// Input: 64-bytes stored in the first 16 elements of the stack (32 bits per element).
/// Output: A 32-byte digest stored in the first 8 elements of stack (32 bits per element).
/// The output is passed back to the caller via a pointer.
#[link_name = "sha256_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>"]
#[link_name = "sha256-hash-two-to-one"]
fn extern_sha256_hash_2to1(
e1: u32,
e2: u32,
Expand Down
Loading

0 comments on commit 8915b27

Please sign in to comment.