Skip to content

Commit

Permalink
refactor: make ProcessState a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Nov 19, 2024
1 parent af27954 commit 2b49880
Show file tree
Hide file tree
Showing 27 changed files with 231 additions and 234 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

#### Changes
- [BREAKING] `Process` no longer takes ownership of the `Host` (#1563)
- [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)

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
20 changes: 10 additions & 10 deletions processor/src/host/advice/injectors/adv_map_injectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use crate::ProcessState;
/// - `start_addr` is greater than or equal to 2^32.
/// - `end_addr` is greater than or equal to 2^32.
/// - `start_addr` > `end_addr`.
pub(crate) fn insert_mem_values_into_adv_map<S: ProcessState, A: AdviceProvider>(
pub(crate) fn insert_mem_values_into_adv_map<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let (start_addr, end_addr) = get_mem_addr_range(process, 4, 5)?;
let ctx = process.ctx();
Expand Down Expand Up @@ -61,9 +61,9 @@ pub(crate) fn insert_mem_values_into_adv_map<S: ProcessState, A: AdviceProvider>
///
/// Where KEY is computed as hash(A || B, domain), where domain is provided via the immediate
/// value.
pub(crate) fn insert_hdword_into_adv_map<S: ProcessState, A: AdviceProvider>(
pub(crate) fn insert_hdword_into_adv_map<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
domain: Felt,
) -> Result<HostResponse, ExecutionError> {
// get the top two words from the stack and hash them to compute the key value
Expand Down Expand Up @@ -94,9 +94,9 @@ pub(crate) fn insert_hdword_into_adv_map<S: ProcessState, A: AdviceProvider>(
///
/// Where KEY is computed by extracting the digest elements from hperm([C, A, B]). For example,
/// if C is [0, d, 0, 0], KEY will be set as hash(A || B, d).
pub(crate) fn insert_hperm_into_adv_map<S: ProcessState, A: AdviceProvider>(
pub(crate) fn insert_hperm_into_adv_map<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
// read the state from the stack
let mut state = [
Expand Down Expand Up @@ -145,9 +145,9 @@ pub(crate) fn insert_hperm_into_adv_map<S: ProcessState, A: AdviceProvider>(
/// provider (i.e., the input trees are not removed).
///
/// It is not checked whether the provided roots exist as Merkle trees in the advide providers.
pub(crate) fn merge_merkle_nodes<S: ProcessState, A: AdviceProvider>(
pub(crate) fn merge_merkle_nodes<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
// fetch the arguments from the stack
let lhs = process.get_stack_word(1);
Expand All @@ -164,8 +164,8 @@ pub(crate) fn merge_merkle_nodes<S: ProcessState, A: AdviceProvider>(

/// Reads (start_addr, end_addr) tuple from the specified elements of the operand stack (
/// without modifying the state of the stack), and verifies that memory range is valid.
fn get_mem_addr_range<S: ProcessState>(
process: &S,
fn get_mem_addr_range(
process: ProcessState,
start_idx: usize,
end_idx: usize,
) -> Result<(u32, u32), ExecutionError> {
Expand Down
48 changes: 24 additions & 24 deletions processor/src/host/advice/injectors/adv_stack_injectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type QuadFelt = QuadExtension<Felt>;
/// - The specified depth is either zero or greater than the depth of the Merkle tree identified by
/// the specified root.
/// - Value of the node at the specified depth and index is not known to the advice provider.
pub(crate) fn copy_merkle_node_to_adv_stack<S: ProcessState, A: AdviceProvider>(
pub(crate) fn copy_merkle_node_to_adv_stack<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
// read node depth, node index, and tree root from the stack
let depth = process.get_stack_item(0);
Expand Down Expand Up @@ -83,9 +83,9 @@ pub(crate) fn copy_merkle_node_to_adv_stack<S: ProcessState, A: AdviceProvider>(
/// # Errors
/// Returns an error if the required key was not found in the key-value map or if stack offset
/// is greater than 12.
pub(crate) fn copy_map_value_to_adv_stack<S: ProcessState, A: AdviceProvider>(
pub(crate) fn copy_map_value_to_adv_stack<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
include_len: bool,
key_offset: usize,
) -> Result<HostResponse, ExecutionError> {
Expand Down Expand Up @@ -122,9 +122,9 @@ pub(crate) fn copy_map_value_to_adv_stack<S: ProcessState, A: AdviceProvider>(
///
/// # Errors
/// Returns an error if the divisor is ZERO.
pub(crate) fn push_u64_div_result<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_u64_div_result<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let divisor_hi = process.get_stack_item(0).as_int();
let divisor_lo = process.get_stack_item(1).as_int();
Expand Down Expand Up @@ -168,9 +168,9 @@ pub(crate) fn push_u64_div_result<S: ProcessState, A: AdviceProvider>(
///
/// # Errors
/// Returns an error if the input is a zero element in the extension field.
pub(crate) fn push_ext2_inv_result<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_ext2_inv_result<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let coef0 = process.get_stack_item(1);
let coef1 = process.get_stack_item(0);
Expand Down Expand Up @@ -215,9 +215,9 @@ pub(crate) fn push_ext2_inv_result<S: ProcessState, A: AdviceProvider>(
/// - `output_size` is 0 or is greater than the `input_size`.
/// - `input_ptr` is greater than 2^32.
/// - `input_ptr + input_size / 2` is greater than 2^32.
pub(crate) fn push_ext2_intt_result<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_ext2_intt_result<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let output_size = process.get_stack_item(0).as_int() as usize;
let input_size = process.get_stack_item(1).as_int() as usize;
Expand Down Expand Up @@ -284,9 +284,9 @@ pub(crate) fn push_ext2_intt_result<S: ProcessState, A: AdviceProvider>(
/// - DATA is the needed data for signature verification in the VM.
///
/// The advice provider is expected to contain the private key associated to the public key PK.
pub(crate) fn push_signature<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_signature<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
kind: SignatureKind,
) -> Result<HostResponse, ExecutionError> {
let pub_key = process.get_stack_word(0);
Expand All @@ -307,9 +307,9 @@ pub(crate) fn push_signature<S: ProcessState, A: AdviceProvider>(
/// Outputs:
/// Operand stack: [n, ...]
/// Advice stack: [leading_zeros, ...]
pub(crate) fn push_leading_zeros<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_leading_zeros<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
push_transformed_stack_top(advice_provider, process, |stack_top| {
Felt::from(stack_top.leading_zeros())
Expand All @@ -325,9 +325,9 @@ pub(crate) fn push_leading_zeros<S: ProcessState, A: AdviceProvider>(
/// Outputs:
/// Operand stack: [n, ...]
/// Advice stack: [trailing_zeros, ...]
pub(crate) fn push_trailing_zeros<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_trailing_zeros<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
push_transformed_stack_top(advice_provider, process, |stack_top| {
Felt::from(stack_top.trailing_zeros())
Expand All @@ -343,9 +343,9 @@ pub(crate) fn push_trailing_zeros<S: ProcessState, A: AdviceProvider>(
/// Outputs:
/// Operand stack: [n, ...]
/// Advice stack: [leading_ones, ...]
pub(crate) fn push_leading_ones<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_leading_ones<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
push_transformed_stack_top(advice_provider, process, |stack_top| {
Felt::from(stack_top.leading_ones())
Expand All @@ -361,9 +361,9 @@ pub(crate) fn push_leading_ones<S: ProcessState, A: AdviceProvider>(
/// Outputs:
/// Operand stack: [n, ...]
/// Advice stack: [trailing_ones, ...]
pub(crate) fn push_trailing_ones<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_trailing_ones<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
push_transformed_stack_top(advice_provider, process, |stack_top| {
Felt::from(stack_top.trailing_ones())
Expand All @@ -381,9 +381,9 @@ pub(crate) fn push_trailing_ones<S: ProcessState, A: AdviceProvider>(
///
/// # Errors
/// Returns an error if the logarithm argument (top stack element) equals ZERO.
pub(crate) fn push_ilog2<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_ilog2<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let n = process.get_stack_item(0).as_int();
if n == 0 {
Expand All @@ -405,9 +405,9 @@ fn u64_to_u32_elements(value: u64) -> (Felt, Felt) {

/// Gets the top stack element, applies a provided function to it and pushes it to the advice
/// provider.
fn push_transformed_stack_top<S: ProcessState, A: AdviceProvider>(
fn push_transformed_stack_top<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
f: impl FnOnce(u32) -> Felt,
) -> Result<HostResponse, ExecutionError> {
let stack_top = process.get_stack_item(0);
Expand Down
4 changes: 2 additions & 2 deletions processor/src/host/advice/injectors/merkle_store_injectors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::super::{AdviceProvider, ExecutionError, HostResponse, ProcessState};

pub(crate) fn update_operand_stack_merkle_node<S: ProcessState, A: AdviceProvider>(
pub(crate) fn update_operand_stack_merkle_node<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let depth = process.get_stack_item(4);
let index = process.get_stack_item(5);
Expand Down
12 changes: 6 additions & 6 deletions processor/src/host/advice/injectors/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use crate::{AdviceProvider, ProcessState};
///
/// # Errors
/// Returns an error if the provided Merkle root doesn't exist on the advice provider.
pub(crate) fn push_smtpeek_result<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_smtpeek_result<A: AdviceProvider>(
advice_provider: &mut A,
process: &S,
process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
let empty_leaf = EmptySubtreeRoots::entry(SMT_DEPTH, SMT_DEPTH);
// fetch the arguments from the operand stack
Expand Down Expand Up @@ -68,17 +68,17 @@ pub(crate) fn push_smtpeek_result<S: ProcessState, A: AdviceProvider>(
}

/// Currently unimplemented
pub(crate) fn push_smtget_inputs<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_smtget_inputs<A: AdviceProvider>(
_advice_provider: &mut A,
_process: &S,
_process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
unimplemented!()
}

/// Currently unimplemented
pub(crate) fn push_smtset_inputs<S: ProcessState, A: AdviceProvider>(
pub(crate) fn push_smtset_inputs<A: AdviceProvider>(
_advice_provider: &mut A,
_process: &S,
_process: ProcessState,
) -> Result<HostResponse, ExecutionError> {
unimplemented!()
}
Expand Down
Loading

0 comments on commit 2b49880

Please sign in to comment.