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

Refactor Process and ProcessState #1571

Merged
merged 2 commits into from
Nov 19, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

#### Changes
- [BREAKING] `Process` no longer takes ownership of the `Host` (#1571)
- [BREAKING] `ProcessState` was converted from a trait to a struct (#1571)

## 0.11.0 (2024-11-04)

#### Enhancements
Expand Down
2 changes: 1 addition & 1 deletion miden/benches/program_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn program_execution(c: &mut Criterion) {
assembler.add_library(&stdlib).expect("failed to load stdlib");
let program = assembler.assemble_program(source).expect("Failed to compile test source.");
bench.iter(|| {
execute(&program, StackInputs::default(), host.clone(), ExecutionOptions::default())
execute(&program, StackInputs::default(), &mut host, ExecutionOptions::default())
});
});

Expand Down
2 changes: 1 addition & 1 deletion miden/src/cli/debug/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl DebugExecutor {
source_manager: Arc<dyn assembly::SourceManager>,
) -> Result<Self, String> {
let mut vm_state_iter =
processor::execute_iter(&program, stack_inputs, DefaultHost::new(advice_provider));
processor::execute_iter(&program, stack_inputs, &mut DefaultHost::new(advice_provider));
let vm_state = vm_state_iter
.next()
.ok_or(
Expand Down
9 changes: 5 additions & 4 deletions miden/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ impl ProveCmd {

// fetch the stack and program inputs from the arguments
let stack_inputs = input_data.parse_stack_inputs().map_err(Report::msg)?;
let host = DefaultHost::new(input_data.parse_advice_provider().map_err(Report::msg)?);
let mut host = DefaultHost::new(input_data.parse_advice_provider().map_err(Report::msg)?);

let proving_options =
self.get_proof_options().map_err(|err| Report::msg(format!("{err}")))?;

// execute program and generate proof
let (stack_outputs, proof) = prover::prove(&program, stack_inputs, host, proving_options)
.into_diagnostic()
.wrap_err("Failed to prove program")?;
let (stack_outputs, proof) =
prover::prove(&program, stack_inputs, &mut host, proving_options)
.into_diagnostic()
.wrap_err("Failed to prove program")?;

println!(
"Program with hash {} proved in {} ms",
Expand Down
4 changes: 2 additions & 2 deletions miden/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ fn run_program(params: &RunCmd) -> Result<(ExecutionTrace, [u8; 32]), Report> {

// fetch the stack and program inputs from the arguments
let stack_inputs = input_data.parse_stack_inputs().map_err(Report::msg)?;
let host = DefaultHost::new(input_data.parse_advice_provider().map_err(Report::msg)?);
let mut host = DefaultHost::new(input_data.parse_advice_provider().map_err(Report::msg)?);

let program_hash: [u8; 32] = program.hash().into();

// execute program and generate outputs
let trace = processor::execute(&program, stack_inputs, host, execution_options)
let trace = processor::execute(&program, stack_inputs, &mut host, execution_options)
.into_diagnostic()
.wrap_err("Failed to generate execution trace")?;

Expand Down
8 changes: 4 additions & 4 deletions miden/src/examples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl ExampleOptions {
let Example {
program,
stack_inputs,
host,
mut host,
num_outputs,
expected_result,
..
Expand All @@ -126,7 +126,7 @@ impl ExampleOptions {
// execute the program and generate the proof of execution
let now = Instant::now();
let (stack_outputs, proof) =
miden_vm::prove(&program, stack_inputs.clone(), host, proof_options).unwrap();
miden_vm::prove(&program, stack_inputs.clone(), &mut host, proof_options).unwrap();
println!("--------------------------------");

println!(
Expand Down Expand Up @@ -173,13 +173,13 @@ where
let Example {
program,
stack_inputs,
host,
mut host,
num_outputs,
expected_result,
} = example;

let (mut outputs, proof) =
miden_vm::prove(&program, stack_inputs.clone(), host, options).unwrap();
miden_vm::prove(&program, stack_inputs.clone(), &mut host, options).unwrap();

assert_eq!(
expected_result,
Expand Down
2 changes: 1 addition & 1 deletion miden/src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn execute(
host.load_mast_forest(library.mast_forest().clone());
}

let state_iter = processor::execute_iter(&program, stack_inputs, host);
let state_iter = processor::execute_iter(&program, stack_inputs, &mut host);
let (system, _, stack, chiplets, err) = state_iter.into_parts();
if let Some(err) = err {
return Err(format!("{err}"));
Expand Down
8 changes: 6 additions & 2 deletions miden/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ impl fmt::Display for ExecutionDetails {
}

/// Returns program analysis of a given program.
fn analyze<H>(program: &str, stack_inputs: StackInputs, host: H) -> Result<ExecutionDetails, Report>
fn analyze<H>(
program: &str,
stack_inputs: StackInputs,
mut host: H,
) -> Result<ExecutionDetails, Report>
where
H: Host,
{
Expand All @@ -220,7 +224,7 @@ where
.assemble_program(program)?;
let mut execution_details = ExecutionDetails::default();

let vm_state_iterator = processor::execute_iter(&program, stack_inputs, host);
let vm_state_iterator = processor::execute_iter(&program, stack_inputs, &mut host);
execution_details.set_trace_len_summary(vm_state_iterator.trace_len_summary());

for state in vm_state_iterator {
Expand Down
20 changes: 10 additions & 10 deletions miden/tests/integration/operations/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ impl Default for TestHost<MemAdviceProvider> {
}

impl<A: AdviceProvider> Host for TestHost<A> {
fn get_advice<S: ProcessState>(
fn get_advice(
&mut self,
process: &S,
process: ProcessState,
extractor: AdviceExtractor,
) -> Result<HostResponse, ExecutionError> {
self.adv_provider.get_advice(process, &extractor)
}

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

fn on_event<S: ProcessState>(
fn on_event(
&mut self,
_process: &S,
_process: ProcessState,
event_id: u32,
) -> Result<HostResponse, ExecutionError> {
self.event_handler.push(event_id);
Ok(HostResponse::None)
}

fn on_trace<S: ProcessState>(
fn on_trace(
&mut self,
_process: &S,
_process: ProcessState,
trace_id: u32,
) -> Result<HostResponse, ExecutionError> {
self.trace_handler.push(trace_id);
Ok(HostResponse::None)
}

fn on_debug<S: ProcessState>(
fn on_debug(
&mut self,
_process: &S,
_process: ProcessState,
_options: &DebugOptions,
) -> Result<HostResponse, ExecutionError> {
self.debug_handler.push(_options.to_string());
Expand Down
1 change: 1 addition & 0 deletions processor/src/chiplets/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const INIT_TRACE_CAPACITY: usize = 128;
/// significant 4-bit limbs of the input values. With every subsequent row, the next most
/// significant 4-bit limb of the result is appended to it. Thus, by the 8th row, column `z`
/// contains the full result of the bitwise operation.
#[derive(Debug)]
pub struct Bitwise {
trace: [Vec<Felt>; TRACE_WIDTH],
}
Expand Down
2 changes: 1 addition & 1 deletion processor/src/chiplets/hasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests;
/// the trace of a control or span block that can be copied to be used later for program blocks
/// encountered with the same digest instead of building it from scratch everytime. The hash of
/// the block is used as the key here after converting it to a bytes array.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Hasher {
trace: HasherTrace,
memoized_trace_map: BTreeMap<[u8; 32], (usize, usize)>,
Expand Down
2 changes: 1 addition & 1 deletion processor/src/chiplets/hasher/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{Felt, HasherState, Selectors, TraceFragment, STATE_WIDTH, TRACE_WIDT
/// - 3 selector columns.
/// - 12 columns describing hasher state.
/// - 1 node index column used for Merkle path related computations.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct HasherTrace {
selectors: [Vec<Felt>; 3],
hasher_state: [Vec<Felt>; STATE_WIDTH],
Expand Down
2 changes: 2 additions & 0 deletions processor/src/chiplets/kernel_rom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ProcHashBytes = [u8; 32];
/// change.
/// - `h0` - `h3` columns contain roots of procedures in a given kernel. Together with `idx` column,
/// these form tuples (index, procedure root) for all procedures in the kernel.
#[derive(Debug)]
pub struct KernelRom {
access_map: BTreeMap<ProcHashBytes, ProcAccessInfo>,
kernel: Kernel,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl KernelRom {
// ================================================================================================

/// Procedure access information for a given kernel procedure.
#[derive(Debug)]
struct ProcAccessInfo {
proc_hash: Word,
num_accesses: usize,
Expand Down
2 changes: 1 addition & 1 deletion processor/src/chiplets/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const INIT_MEM_VALUE: Word = EMPTY_WORD;
/// clock cycles computed as described above.
///
/// For the first row of the trace, values in `d0`, `d1`, and `d_inv` are set to zeros.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Memory {
/// Memory segment traces sorted by their execution context ID.
trace: BTreeMap<ContextId, MemorySegmentTrace>,
Expand Down
2 changes: 1 addition & 1 deletion processor/src/chiplets/memory/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{ContextId, ExecutionError};
/// A memory segment is an isolated address space accessible from a specific execution context.
/// Within each segment, the memory is word-addressable. That is, four field elements are located
/// at each memory address, and we can read and write elements to/from memory in batches of four.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct MemorySegmentTrace(BTreeMap<u32, Vec<MemorySegmentAccess>>);

impl MemorySegmentTrace {
Expand Down
1 change: 1 addition & 0 deletions processor/src/chiplets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ mod tests;
/// | 1 | 1 | 1 | 1 |---------------------------------------------------------|
/// +---+---+---+---+---------------------------------------------------------+
/// ```
#[derive(Debug)]
pub struct Chiplets {
/// Current clock cycle of the VM.
clk: RowIndex,
Expand Down
6 changes: 3 additions & 3 deletions processor/src/chiplets/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ fn build_trace(
kernel: Kernel,
) -> (ChipletsTrace, usize) {
let stack_inputs = StackInputs::try_from_ints(stack_inputs.iter().copied()).unwrap();
let host = DefaultHost::default();
let mut process = Process::new(kernel, stack_inputs, host, ExecutionOptions::default());
let mut host = DefaultHost::default();
let mut process = Process::new(kernel, stack_inputs, ExecutionOptions::default());
let program = {
let mut mast_forest = MastForest::new();

Expand All @@ -123,7 +123,7 @@ fn build_trace(

Program::new(mast_forest.into(), basic_block_id)
};
process.execute(&program).unwrap();
process.execute(&program, &mut host).unwrap();

let (trace, ..) = ExecutionTrace::test_finalize_trace(process);
let trace_len = trace.num_rows() - ExecutionTrace::NUM_RAND_ROWS;
Expand Down
9 changes: 3 additions & 6 deletions processor/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use vm_core::{AssemblyOp, Operation, StackOutputs, Word};

use crate::{
range::RangeChecker, system::ContextId, Chiplets, ChipletsLengths, Decoder, ExecutionError,
Felt, Host, Process, Stack, System, TraceLenSummary,
Felt, Process, Stack, System, TraceLenSummary,
};

/// VmState holds a current process state information at a specific clock cycle.
Expand Down Expand Up @@ -63,11 +63,8 @@ pub struct VmStateIterator {
}

impl VmStateIterator {
pub fn new<H>(process: Process<H>, result: Result<StackOutputs, ExecutionError>) -> Self
where
H: Host,
{
let (system, decoder, stack, mut range, chiplets, _) = process.into_parts();
pub fn new(process: Process, result: Result<StackOutputs, ExecutionError>) -> Self {
let (system, decoder, stack, mut range, chiplets) = process.into_parts();
let trace_len_summary = Self::build_trace_len_summary(&system, &mut range, &chiplets);

Self {
Expand Down
Loading
Loading