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

chore: update the Miden VM (with updated miden-package crate) #376

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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
244 changes: 129 additions & 115 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,23 @@ thiserror = { package = "miden-thiserror", version = "1.0" }
toml = { version = "0.8", features = ["preserve_order"] }
derive_more = "0.99"
indexmap = "2.2"
# miden-assembly = { version = "0.10.3" }
# miden-core = { version = "0.10.3" }
miden-formatting = { version = "0.1", default-features = false }
miden-parsing = "0.1"
# miden-processor = { version = "0.10.3" }
# miden-stdlib = { version = "0.10.3", features = ["with-debug-info"] }

# miden-assembly = { version = "0.11.0", path = "../vm-aux/assembly" }
# miden-core = { version = "0.11.0", path = "../vm-aux/core" }
# miden-processor = { version = "0.11.0", path = "../vm-aux/processor" }
# miden-stdlib = { version = "0.11.0", path = "../vm-aux/stdlib" }
# miden-package = { version = "0.11.0", path = "../vm-aux/package" }

miden-assembly = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "75bd636a6b83fd6cb134b0981663aa28e49a811c" }
miden-core = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "75bd636a6b83fd6cb134b0981663aa28e49a811c" }
miden-processor = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "75bd636a6b83fd6cb134b0981663aa28e49a811c" }
miden-stdlib = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "75bd636a6b83fd6cb134b0981663aa28e49a811c", features = [
miden-assembly = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "a1b44a7e28a64da99c2d0a7260498e1b84f57532" }
miden-core = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "a1b44a7e28a64da99c2d0a7260498e1b84f57532" }
miden-processor = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "a1b44a7e28a64da99c2d0a7260498e1b84f57532" }
miden-stdlib = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "a1b44a7e28a64da99c2d0a7260498e1b84f57532", features = [
"with-debug-info",
] }
miden-package = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "75bd636a6b83fd6cb134b0981663aa28e49a811c" }
miden-package = { version = "0.11.0", git = "https://github.com/0xPolygonMiden/miden-vm", rev = "a1b44a7e28a64da99c2d0a7260498e1b84f57532" }

#miden-assembly = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-core = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-processor = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
#miden-stdlib = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "828557c28ca1d159bfe42195e7ea73256ce4aa06" }
midenc-codegen-masm = { version = "0.0.7", path = "codegen/masm" }
midenc-dialect-hir = { version = "0.0.7", path = "dialects/hir" }
midenc-hir = { version = "0.0.7", path = "hir" }
Expand Down
5 changes: 1 addition & 4 deletions codegen/masm/src/emulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,12 +1418,9 @@ impl Emulator {
Op::AdvInjectInsertMem
| Op::AdvInjectInsertHperm
| Op::AdvInjectInsertHdword
| Op::AdvInjectInsertHdwordImm(_)
| Op::AdvInjectPushMTreeNode
| Op::AdvInjectPushMapVal
| Op::AdvInjectPushMapValImm(_)
| Op::AdvInjectPushMapValN
| Op::AdvInjectPushMapValNImm(_) => unimplemented!(),
| Op::AdvInjectPushMapValN => unimplemented!(),
Op::Assert => {
let cond = pop_bool!(self);
assert!(cond, "assertion failed: expected true, got false");
Expand Down
3 changes: 0 additions & 3 deletions hir/src/asm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,11 +1861,8 @@ fn apply_op_stack_effects(
MasmOp::AdvInjectInsertMem
| MasmOp::AdvInjectInsertHperm
| MasmOp::AdvInjectInsertHdword
| MasmOp::AdvInjectInsertHdwordImm(_)
| MasmOp::AdvInjectPushMapVal
| MasmOp::AdvInjectPushMapValImm(_)
| MasmOp::AdvInjectPushMapValN
| MasmOp::AdvInjectPushMapValNImm(_)
| MasmOp::AdvInjectPushU64Div
| MasmOp::AdvInjectPushMTreeNode
| MasmOp::AdvInjectPushSignature(_) => (),
Expand Down
7 changes: 0 additions & 7 deletions hir/src/asm/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,6 @@ impl PrettyPrint for DisplayOp<'_> {
| MasmOp::U32ShrImm(imm)
| MasmOp::U32RotlImm(imm)
| MasmOp::U32RotrImm(imm)) => text(format!("{op}")) + const_text(".") + display(*imm),
op @ (MasmOp::AdvInjectPushMapValImm(offset)
| MasmOp::AdvInjectPushMapValNImm(offset)) => {
text(format!("{op}")) + const_text(".") + display(*offset)
}
op @ MasmOp::AdvInjectInsertHdwordImm(domain) => {
text(format!("{op}")) + const_text(".") + display(*domain)
}
op @ MasmOp::DebugStackN(n) => text(format!("{op}")) + const_text(".") + display(*n),
op @ MasmOp::DebugMemoryAt(start) => {
text(format!("{op}")) + const_text(".") + display(*start)
Expand Down
100 changes: 22 additions & 78 deletions hir/src/asm/isa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{collections::BTreeSet, fmt};

use cranelift_entity::entity_impl;
pub use miden_assembly::ast::{AdviceInjectorNode, DebugOptions};
pub use miden_assembly::ast::DebugOptions;
use miden_assembly::ast::SystemEventNode;
use smallvec::{smallvec, SmallVec};

use crate::{
Expand Down Expand Up @@ -264,15 +265,6 @@ pub enum MasmOp {
/// [K]
/// ```
AdvInjectPushMapVal,
/// Pushes a list of field elements on the advice stack.
///
/// The list is looked up in the advice map using the word starting at `index` on the operand
/// stack.
///
/// ```text,ignore
/// [K]
/// ```
AdvInjectPushMapValImm(u8),
/// Pushes a list of field elements, along with the number of elements on the advice stack.
///
/// The list is looked up in the advice map using the word on top of the operand stack.
Expand All @@ -281,15 +273,6 @@ pub enum MasmOp {
/// [K]
/// ```
AdvInjectPushMapValN,
/// Pushes a list of field elements, along with the number of elements on the advice stack.
///
/// The list is looked up in the advice map using the word starting at `index` on the operand
/// stack.
///
/// ```text,ignore
/// [K]
/// ```
AdvInjectPushMapValNImm(u8),
/// Pushes a node of a Merkle tree with root `R` at depth `d` and index `i` from the Merkle
/// store onto the advice stack
///
Expand All @@ -311,16 +294,6 @@ pub enum MasmOp {
/// [B, A]
/// ```
AdvInjectInsertHdword,
/// Reads the top two words from the stack, and computes a key `K` as `hash(A || B, d)`.
///
/// `d` is a domain value which can be in the range 0..=255
///
/// The two words that were hashed are then saved into the advice map under `K` as `[A, B]`.
///
/// ```text,ignore
/// [B, A]
/// ```
AdvInjectInsertHdwordImm(u8),
/// Reads the top three words from the stack, and computes a key `K` as `permute(C, A,
/// B).digest`.
///
Expand Down Expand Up @@ -1119,13 +1092,10 @@ impl MasmOp {
| Self::Trace(_)
| Self::AdvInjectPushU64Div
| Self::AdvInjectPushMapVal
| Self::AdvInjectPushMapValImm(_)
| Self::AdvInjectPushMapValN
| Self::AdvInjectPushMapValNImm(_)
| Self::AdvInjectPushMTreeNode
| Self::AdvInjectInsertMem
| Self::AdvInjectInsertHdword
| Self::AdvInjectInsertHdwordImm(_)
| Self::AdvInjectInsertHperm
| Self::AdvInjectPushSignature(_)
| Self::DebugStack
Expand Down Expand Up @@ -1369,26 +1339,17 @@ impl MasmOp {
Instruction::AdvPipe => Self::AdvPipe,
Instruction::AdvPush(byte) => Self::AdvPush(unwrap_u8!(byte)),
Instruction::AdvLoadW => Self::AdvLoadw,
Instruction::AdvInject(AdviceInjectorNode::InsertMem) => Self::AdvInjectInsertMem,
Instruction::AdvInject(AdviceInjectorNode::InsertHperm) => Self::AdvInjectInsertHperm,
Instruction::AdvInject(AdviceInjectorNode::InsertHdword) => Self::AdvInjectInsertHdword,
Instruction::AdvInject(AdviceInjectorNode::InsertHdwordImm { domain }) => {
Self::AdvInjectInsertHdwordImm(unwrap_u8!(domain))
}
Instruction::AdvInject(AdviceInjectorNode::PushU64Div) => Self::AdvInjectPushU64Div,
Instruction::AdvInject(AdviceInjectorNode::PushMtNode) => Self::AdvInjectPushMTreeNode,
Instruction::AdvInject(AdviceInjectorNode::PushMapVal) => Self::AdvInjectPushMapVal,
Instruction::AdvInject(AdviceInjectorNode::PushMapValImm { offset }) => {
Self::AdvInjectPushMapValImm(unwrap_u8!(offset))
}
Instruction::AdvInject(AdviceInjectorNode::PushMapValN) => Self::AdvInjectPushMapValN,
Instruction::AdvInject(AdviceInjectorNode::PushMapValNImm { offset }) => {
Self::AdvInjectPushMapValNImm(unwrap_u8!(offset))
}
Instruction::AdvInject(AdviceInjectorNode::PushSignature { kind }) => {
Instruction::SysEvent(SystemEventNode::InsertMem) => Self::AdvInjectInsertMem,
Instruction::SysEvent(SystemEventNode::InsertHperm) => Self::AdvInjectInsertHperm,
Instruction::SysEvent(SystemEventNode::InsertHdword) => Self::AdvInjectInsertHdword,
Instruction::SysEvent(SystemEventNode::PushU64Div) => Self::AdvInjectPushU64Div,
Instruction::SysEvent(SystemEventNode::PushMtNode) => Self::AdvInjectPushMTreeNode,
Instruction::SysEvent(SystemEventNode::PushMapVal) => Self::AdvInjectPushMapVal,
Instruction::SysEvent(SystemEventNode::PushMapValN) => Self::AdvInjectPushMapValN,
Instruction::SysEvent(SystemEventNode::PushSignature { kind }) => {
Self::AdvInjectPushSignature(kind)
}
Instruction::AdvInject(injector) => {
Instruction::SysEvent(injector) => {
unimplemented!("unsupported advice injector: {injector:?}")
}
ref ix @ (Instruction::Exec(ref target)
Expand Down Expand Up @@ -1596,26 +1557,15 @@ impl MasmOp {
Self::AdvPipe => Instruction::AdvPipe,
Self::AdvPush(n) => Instruction::AdvPush(n.into()),
Self::AdvLoadw => Instruction::AdvLoadW,
Self::AdvInjectPushU64Div => Instruction::AdvInject(AdviceInjectorNode::PushU64Div),
Self::AdvInjectPushMTreeNode => Instruction::AdvInject(AdviceInjectorNode::PushMtNode),
Self::AdvInjectPushMapVal => Instruction::AdvInject(AdviceInjectorNode::PushMapVal),
Self::AdvInjectPushMapValImm(n) => {
Instruction::AdvInject(AdviceInjectorNode::PushMapValImm { offset: n.into() })
}
Self::AdvInjectPushMapValN => Instruction::AdvInject(AdviceInjectorNode::PushMapValN),
Self::AdvInjectPushMapValNImm(n) => {
Instruction::AdvInject(AdviceInjectorNode::PushMapValNImm { offset: n.into() })
}
Self::AdvInjectInsertMem => Instruction::AdvInject(AdviceInjectorNode::InsertMem),
Self::AdvInjectInsertHperm => Instruction::AdvInject(AdviceInjectorNode::InsertHperm),
Self::AdvInjectInsertHdword => Instruction::AdvInject(AdviceInjectorNode::InsertHdword),
Self::AdvInjectInsertHdwordImm(domain) => {
Instruction::AdvInject(AdviceInjectorNode::InsertHdwordImm {
domain: domain.into(),
})
}
Self::AdvInjectPushU64Div => Instruction::SysEvent(SystemEventNode::PushU64Div),
Self::AdvInjectPushMTreeNode => Instruction::SysEvent(SystemEventNode::PushMtNode),
Self::AdvInjectPushMapVal => Instruction::SysEvent(SystemEventNode::PushMapVal),
Self::AdvInjectPushMapValN => Instruction::SysEvent(SystemEventNode::PushMapValN),
Self::AdvInjectInsertMem => Instruction::SysEvent(SystemEventNode::InsertMem),
Self::AdvInjectInsertHperm => Instruction::SysEvent(SystemEventNode::InsertHperm),
Self::AdvInjectInsertHdword => Instruction::SysEvent(SystemEventNode::InsertHdword),
Self::AdvInjectPushSignature(kind) => {
Instruction::AdvInject(AdviceInjectorNode::PushSignature { kind })
Instruction::SysEvent(SystemEventNode::PushSignature { kind })
}
Self::If(..) | Self::While(_) | Self::Repeat(..) => {
panic!("control flow instructions are meant to be handled specially by the caller")
Expand Down Expand Up @@ -1871,17 +1821,11 @@ impl fmt::Display for MasmOp {
Self::AdvLoadw => f.write_str("adv_loadw"),
Self::AdvInjectPushU64Div => f.write_str("adv.push_u64div"),
Self::AdvInjectPushMTreeNode => f.write_str("adv.push_mtnode"),
Self::AdvInjectPushMapVal | Self::AdvInjectPushMapValImm(_) => {
f.write_str("adv.push_mapval")
}
Self::AdvInjectPushMapValN | Self::AdvInjectPushMapValNImm(_) => {
f.write_str("adv.push_mapvaln")
}
Self::AdvInjectPushMapVal => f.write_str("adv.push_mapval"),
Self::AdvInjectPushMapValN => f.write_str("adv.push_mapvaln"),
Self::AdvInjectInsertMem => f.write_str("adv.insert_mem"),
Self::AdvInjectInsertHperm => f.write_str("adv.insert_hperm"),
Self::AdvInjectInsertHdword | Self::AdvInjectInsertHdwordImm(_) => {
f.write_str("adv.insert_hdword")
}
Self::AdvInjectInsertHdword => f.write_str("adv.insert_hdword"),
Self::AdvInjectPushSignature(kind) => write!(f, "adv.push_sig.{kind}"),
Self::If(..) => f.write_str("if.true"),
Self::While(_) => f.write_str("while.true"),
Expand Down
6 changes: 3 additions & 3 deletions midenc-compile/src/stages/assemble.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use miden_assembly::ast::QualifiedProcedureName;
use miden_package::{Dependency, MastArtifact, Package};
use midenc_codegen_masm::MasmArtifact;

Expand Down Expand Up @@ -102,10 +103,9 @@ fn build_package(mast: MastArtifact, masm: &MasmArtifact, session: &Session) ->
unreachable!("expected MasmArtifact to be a library");
};
for module_info in lib.module_infos() {
let module_path = module_info.path().path();
for (_, proc_info) in module_info.procedures() {
let proc_name = proc_info.name.as_str();
let name = format!("{module_path}::{proc_name}");
let name =
QualifiedProcedureName::new(module_info.path().clone(), proc_info.name.clone());
let digest = proc_info.digest;
manifest.exports.insert(miden_package::PackageExport { name, digest });
}
Expand Down
14 changes: 8 additions & 6 deletions midenc-debug/src/exec/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{
use miden_assembly::Library as CompiledLibrary;
use miden_core::{Program, StackInputs, Word};
use miden_package::{
Dependency, DependencyName, DependencyResolution, DependencyResolver, LocalResolution,
MastArtifact, MemDependencyResolverByDigest, SystemLibraryId,
Dependency, DependencyName, DependencyResolver, LocalResolvedDependency, MastArtifact,
MemDependencyResolverByDigest, ResolvedDependency,
};
use miden_processor::{
AdviceInputs, ContextId, ExecutionError, Felt, MastForest, MemAdviceProvider, Process,
Expand Down Expand Up @@ -79,11 +79,11 @@ impl Executor {
log::debug!("dependency {:?} resolved to {:?}", dep, resolution);
log::debug!("loading library from package dependency: {:?}", dep);
match resolution {
DependencyResolution::Local(LocalResolution::Library(lib)) => {
ResolvedDependency::Local(LocalResolvedDependency::Library(lib)) => {
let library = lib.as_ref();
self.with_mast_forest(lib.mast_forest().clone());
}
DependencyResolution::Local(LocalResolution::Package(pkg)) => {
ResolvedDependency::Local(LocalResolvedDependency::Package(pkg)) => {
if let MastArtifact::Library(lib) = &pkg.mast {
self.with_mast_forest(lib.mast_forest().clone());
} else {
Expand Down Expand Up @@ -151,11 +151,13 @@ impl Executor {
let process_state: ProcessState = (&process).into();
let root_context = process_state.ctx();
let result = process.execute(program, &mut host);
let mut iter = VmStateIterator::new(process, result.clone());
#[allow(clippy::useless_asref)]
let stack_outputs = result.as_ref().map(|so| so.clone()).unwrap_or_default();
let mut iter = VmStateIterator::new(process, result);
let mut callstack = CallStack::new(trace_events);
DebugExecutor {
iter,
result,
stack_outputs,
contexts: Default::default(),
root_context,
current_context: root_context,
Expand Down
28 changes: 4 additions & 24 deletions midenc-debug/src/exec/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::{collections::BTreeMap, sync::Arc};

use miden_core::crypto::hash::RpoDigest;
use miden_processor::{
AdviceExtractor, AdviceInjector, AdviceProvider, ExecutionError, Host, HostResponse,
MastForest, MastForestStore, MemAdviceProvider, MemMastForestStore, ProcessState, RowIndex,
AdviceProvider, ExecutionError, Host, MastForest, MastForestStore, MemAdviceProvider,
MemMastForestStore, ProcessState, RowIndex,
};

use super::{TraceEvent, TraceHandler};
Expand Down Expand Up @@ -58,39 +58,19 @@ impl DebuggerHost {
impl Host for DebuggerHost {
type AdviceProvider = MemAdviceProvider;

fn get_advice(
&mut self,
process: ProcessState,
extractor: AdviceExtractor,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.get_advice(process, &extractor)
}

fn set_advice(
&mut self,
process: ProcessState,
injector: AdviceInjector,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.set_advice(process, &injector)
}

fn get_mast_forest(&self, node_digest: &RpoDigest) -> Option<Arc<MastForest>> {
self.store.get(node_digest)
}

fn on_trace(
&mut self,
process: ProcessState,
trace_id: u32,
) -> Result<HostResponse, ExecutionError> {
fn on_trace(&mut self, process: ProcessState, trace_id: u32) -> Result<(), ExecutionError> {
let event = TraceEvent::from(trace_id);
let clk = process.clk();
if let Some(handlers) = self.tracing_callbacks.get_mut(&trace_id) {
for handler in handlers.iter_mut() {
handler(clk, event);
}
}
Ok(HostResponse::None)
Ok(())
}

fn on_assert_failed(&mut self, process: ProcessState, err_code: u32) -> ExecutionError {
Expand Down
7 changes: 3 additions & 4 deletions midenc-debug/src/exec/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct DebugExecutor {
/// The underlying [VmStateIterator] being driven
pub iter: VmStateIterator,
/// The final outcome of the program being executed
pub result: Result<StackOutputs, ExecutionError>,
pub stack_outputs: StackOutputs,
/// The set of contexts allocated during execution so far
pub contexts: BTreeSet<ContextId>,
/// The root context
Expand Down Expand Up @@ -46,7 +46,7 @@ impl DebugExecutor {
/// Returns the call frame exited this cycle, if any
pub fn step(&mut self) -> Result<Option<CallFrame>, ExecutionError> {
if self.stopped {
return self.result.as_ref().map(|_| None).map_err(|err| err.clone());
return Ok(None);
}
match self.iter.next() {
Some(Ok(state)) => {
Expand Down Expand Up @@ -85,12 +85,11 @@ impl DebugExecutor {
let last_cycle = self.cycle;
let trace_len_summary = *self.iter.trace_len_summary();
let (_, _, _, chiplets, _) = self.iter.into_parts();
let outputs = self.result.unwrap_or_default();
ExecutionTrace {
root_context: self.root_context,
last_cycle: RowIndex::from(last_cycle),
chiplets: Chiplets::new(move |context, clk| chiplets.get_mem_state_at(context, clk)),
outputs,
outputs: self.stack_outputs,
trace_len_summary,
}
}
Expand Down
Loading
Loading