miden-processor 0.23.0

Miden VM processor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#[cfg(test)]
use alloc::rc::Rc;
use alloc::{boxed::Box, sync::Arc, vec, vec::Vec};
#[cfg(test)]
use core::cell::Cell;
use core::{cmp::min, ops::ControlFlow};

use miden_air::{Felt, trace::RowIndex};
use miden_core::{
    EMPTY_WORD, WORD_SIZE, Word, ZERO,
    mast::{MastForest, MastNodeExt, MastNodeId},
    operations::Decorator,
    precompile::PrecompileTranscript,
    program::{MIN_STACK_DEPTH, Program, StackInputs, StackOutputs},
    utils::range,
};

use crate::{
    AdviceInputs, AdviceProvider, BaseHost, ContextId, ExecutionError, ExecutionOptions,
    ProcessorState,
    advice::AdviceError,
    continuation_stack::{Continuation, ContinuationStack},
    errors::MapExecErrNoCtx,
    tracer::{OperationHelperRegisters, Tracer},
};

mod basic_block;
mod call_and_dyn;
mod execution_api;
mod external;
mod memory;
mod operation;
mod step;

pub use basic_block::SystemEventError;
pub use memory::Memory;
pub use step::{BreakReason, ResumeContext};

#[cfg(test)]
mod tests;

// CONSTANTS
// ================================================================================================

/// The initial size of the stack buffer.
///
/// Note: This value is much larger than it needs to be for the majority of programs. However, some
/// existing programs need it, so we're forced to push it up (though this should be double-checked).
/// At this high a value, we're starting to see some performance degradation on benchmarks. For
/// example, the blake3 benchmark went from 285 MHz to 250 MHz (~10% degradation). Perhaps a better
/// solution would be to make this value much smaller (~1000), and then fallback to a `Vec` if the
/// stack overflows.
const INITIAL_STACK_BUFFER_SIZE: usize = 6850;

/// The initial position of the top of the stack in the stack buffer.
///
/// We place this value close to 0 because if a program hits the limit, it's much more likely to hit
/// the upper bound than the lower bound, since hitting the lower bound only occurs when you drop
/// 0's that were generated automatically to keep the stack depth at 16. In practice, if this
/// occurs, it is most likely a bug.
const INITIAL_STACK_TOP_IDX: usize = 250;

/// Default maximum operand stack depth preserving the previous fixed-buffer ceiling.
const DEFAULT_MAX_STACK_DEPTH: usize =
    INITIAL_STACK_BUFFER_SIZE - INITIAL_STACK_TOP_IDX - 1 + MIN_STACK_DEPTH;

const _: [(); 1] =
    [(); (ExecutionOptions::DEFAULT_MAX_STACK_DEPTH == DEFAULT_MAX_STACK_DEPTH) as usize];

/// The stack buffer index where the logical operand stack starts after reset/recenter.
const STACK_BUFFER_BASE_IDX: usize = INITIAL_STACK_TOP_IDX - MIN_STACK_DEPTH;

// FAST PROCESSOR
// ================================================================================================

/// A fast processor which doesn't generate any trace.
///
/// This processor is designed to be as fast as possible. Hence, it only keeps track of the current
/// state of the processor (i.e. the stack, current clock cycle, current memory context, and free
/// memory pointer).
///
/// # Stack Management
/// A few key points about how the stack was designed for maximum performance:
///
/// - The stack starts with a fixed buffer size defined by `INITIAL_STACK_BUFFER_SIZE`.
///     - This was observed to increase performance by at least 2x compared to using a `Vec` with
///       `push()` & `pop()`.
///     - We track the stack top and bottom using indices `stack_top_idx` and `stack_bot_idx`,
///       respectively.
/// - Since we are using a fixed-size buffer, we need to ensure that stack buffer accesses are not
///   out of bounds. Naively, we could check for this on every access. However, every operation
///   alters the stack depth by a predetermined amount, allowing us to precisely determine the
///   minimum number of operations required to reach a stack buffer boundary, whether at the top or
///   bottom.
///     - For example, if the stack top is 10 elements away from the top boundary, and the stack
///       bottom is 15 elements away from the bottom boundary, then we can safely execute 10
///       operations that modify the stack depth with no bounds check.
/// - When switching contexts (e.g., during a call or syscall), all elements past the first 16 are
///   stored in an `ExecutionContextInfo` struct, and the stack is truncated to 16 elements. This
///   will be restored when returning from the call or syscall.
///
/// # Clock Cycle Management
/// - The clock cycle (`clk`) is managed in the same way as in `Process`. That is, it is incremented
///   by 1 for every row that `Process` adds to the main trace.
///     - It is important to do so because the clock cycle is used to determine the context ID for
///       new execution contexts when using `call` or `dyncall`.
#[derive(Debug)]
pub struct FastProcessor {
    /// The stack is stored in reverse order, so that the last element is at the top of the stack.
    stack: Box<[Felt]>,
    /// The index of the top of the stack.
    stack_top_idx: usize,
    /// The index of the bottom of the stack.
    stack_bot_idx: usize,

    /// The current clock cycle.
    clk: RowIndex,

    /// The current context ID.
    ctx: ContextId,

    /// The hash of the function that called into the current context, or `[ZERO, ZERO, ZERO,
    /// ZERO]` if we are in the first context (i.e. when `call_stack` is empty).
    caller_hash: Word,

    /// The advice provider to be used during execution.
    advice: AdviceProvider,

    /// A map from (context_id, word_address) to the word stored starting at that memory location.
    memory: Memory,

    /// The call stack is used when starting a new execution context (from a `call`, `syscall` or
    /// `dyncall`) to keep track of the information needed to return to the previous context upon
    /// return. It is a stack since calls can be nested.
    call_stack: Vec<ExecutionContextInfo>,

    /// Options for execution, including but not limited to whether debug or tracing is enabled,
    /// the size of core trace fragments during execution, etc.
    options: ExecutionOptions,

    /// Transcript used to record commitments via `log_precompile` instruction (implemented via
    /// Poseidon2 sponge).
    pc_transcript: PrecompileTranscript,

    /// Tracks decorator retrieval calls for testing.
    #[cfg(test)]
    pub decorator_retrieval_count: Rc<Cell<usize>>,
}

impl FastProcessor {
    /// Packages the processor state after successful execution into a public result type.
    #[inline(always)]
    fn into_execution_output(self, stack: StackOutputs) -> ExecutionOutput {
        ExecutionOutput {
            stack,
            advice: self.advice,
            memory: self.memory,
            final_precompile_transcript: self.pc_transcript,
        }
    }

    /// Converts the terminal result of a full execution run into [`ExecutionOutput`].
    #[inline(always)]
    fn execution_result_from_flow(
        flow: ControlFlow<BreakReason, StackOutputs>,
        processor: Self,
    ) -> Result<ExecutionOutput, ExecutionError> {
        match flow {
            ControlFlow::Continue(stack_outputs) => {
                Ok(processor.into_execution_output(stack_outputs))
            },
            ControlFlow::Break(break_reason) => match break_reason {
                BreakReason::Err(err) => Err(err),
                BreakReason::Stopped(_) => {
                    unreachable!("Execution never stops prematurely with NeverStopper")
                },
            },
        }
    }

    /// Converts a testing-only execution result into stack outputs.
    #[cfg(any(test, feature = "testing"))]
    #[inline(always)]
    fn stack_result_from_flow(
        flow: ControlFlow<BreakReason, StackOutputs>,
    ) -> Result<StackOutputs, ExecutionError> {
        match flow {
            ControlFlow::Continue(stack_outputs) => Ok(stack_outputs),
            ControlFlow::Break(break_reason) => match break_reason {
                BreakReason::Err(err) => Err(err),
                BreakReason::Stopped(_) => {
                    unreachable!("Execution never stops prematurely with NeverStopper")
                },
            },
        }
    }

    // CONSTRUCTORS
    // ----------------------------------------------------------------------------------------------

    /// Creates a new `FastProcessor` instance with the given stack inputs.
    ///
    /// By default, advice inputs are empty and execution options use their defaults
    /// (debugging and tracing disabled).
    ///
    /// # Example
    /// ```ignore
    /// use miden_processor::FastProcessor;
    ///
    /// let processor = FastProcessor::new(stack_inputs)
    ///     .with_advice(advice_inputs)
    ///     .expect("advice inputs should fit advice map limits")
    ///     .with_debugging(true)
    ///     .with_tracing(true);
    /// ```
    ///
    /// When using non-default advice map limits, prefer [`Self::new_with_options`] so the advice
    /// inputs are validated against the intended execution options.
    pub fn new(stack_inputs: StackInputs) -> Self {
        Self::new_with_options(stack_inputs, AdviceInputs::default(), ExecutionOptions::default())
            .expect("empty advice inputs should fit default advice map limits")
    }

    /// Sets the advice inputs for the processor.
    ///
    /// Advice inputs are loaded into the live advice provider immediately and are validated against
    /// the processor's current [`ExecutionOptions`]. If the advice map needs non-default limits,
    /// construct the processor with [`Self::new_with_options`] or call [`Self::with_options`]
    /// before calling this method.
    pub fn with_advice(mut self, advice_inputs: AdviceInputs) -> Result<Self, AdviceError> {
        self.advice = AdviceProvider::new(advice_inputs, &self.options)?;
        Ok(self)
    }

    /// Sets the execution options for the processor.
    ///
    /// This will override any previously set debugging or tracing settings.
    ///
    /// Existing advice inputs are revalidated against the new options before they are applied. To
    /// load advice inputs that require non-default advice map limits, call this before
    /// [`Self::with_advice`] or use [`Self::new_with_options`].
    pub fn with_options(mut self, options: ExecutionOptions) -> Result<Self, AdviceError> {
        self.advice.set_options(&options)?;
        self.options = options;
        Ok(self)
    }

    /// Enables or disables debugging mode.
    ///
    /// When debugging is enabled, debug decorators will be executed during program execution.
    pub fn with_debugging(mut self, enabled: bool) -> Self {
        self.options = self.options.with_debugging(enabled);
        self
    }

    /// Enables or disables tracing mode.
    ///
    /// When tracing is enabled, trace decorators will be executed during program execution.
    pub fn with_tracing(mut self, enabled: bool) -> Self {
        self.options = self.options.with_tracing(enabled);
        self
    }

    /// Constructor for creating a `FastProcessor` with all options specified at once.
    ///
    /// For a more fluent API, consider using `FastProcessor::new()` with builder methods.
    pub fn new_with_options(
        stack_inputs: StackInputs,
        advice_inputs: AdviceInputs,
        options: ExecutionOptions,
    ) -> Result<Self, AdviceError> {
        let stack_top_idx = INITIAL_STACK_TOP_IDX;
        let stack = {
            // Note: we use `Vec::into_boxed_slice()` here, since `Box::new([T; N])` first allocates
            // the array on the stack, and then moves it to the heap. This might cause a
            // stack overflow on some systems.
            let mut stack = vec![ZERO; INITIAL_STACK_BUFFER_SIZE].into_boxed_slice();

            // Copy inputs in reverse order so first element ends up at top of stack
            for (i, &input) in stack_inputs.iter().enumerate() {
                stack[stack_top_idx - 1 - i] = input;
            }
            stack
        };

        Ok(Self {
            advice: AdviceProvider::new(advice_inputs, &options)?,
            stack,
            stack_top_idx,
            stack_bot_idx: stack_top_idx - MIN_STACK_DEPTH,
            clk: 0_u32.into(),
            ctx: 0_u32.into(),
            caller_hash: EMPTY_WORD,
            memory: Memory::new(),
            call_stack: Vec::new(),
            options,
            pc_transcript: PrecompileTranscript::new(),
            #[cfg(test)]
            decorator_retrieval_count: Rc::new(Cell::new(0)),
        })
    }

    /// Returns the resume context to be used with the first call to `step_sync()`.
    pub fn get_initial_resume_context(
        &mut self,
        program: &Program,
    ) -> Result<ResumeContext, ExecutionError> {
        self.advice
            .extend_map(program.mast_forest().advice_map())
            .map_exec_err_no_ctx()?;

        Ok(ResumeContext {
            current_forest: program.mast_forest().clone(),
            continuation_stack: ContinuationStack::new(program),
            kernel: program.kernel().clone(),
        })
    }

    // ACCESSORS
    // -------------------------------------------------------------------------------------------

    /// Returns whether the processor is executing in debug mode.
    #[inline(always)]
    pub fn in_debug_mode(&self) -> bool {
        self.options.enable_debugging()
    }

    /// Returns true if decorators should be executed.
    ///
    /// This corresponds to either being in debug mode (for debug decorators) or having tracing
    /// enabled (for trace decorators).
    #[inline(always)]
    fn should_execute_decorators(&self) -> bool {
        self.in_debug_mode() || self.options.enable_tracing()
    }

    #[cfg(test)]
    #[inline(always)]
    fn record_decorator_retrieval(&self) {
        self.decorator_retrieval_count.set(self.decorator_retrieval_count.get() + 1);
    }

    /// Returns the size of the stack.
    #[inline(always)]
    fn stack_size(&self) -> usize {
        self.stack_top_idx - self.stack_bot_idx
    }

    /// Returns the stack, such that the top of the stack is at the last index of the returned
    /// slice.
    pub fn stack(&self) -> &[Felt] {
        &self.stack[self.stack_bot_idx..self.stack_top_idx]
    }

    /// Returns the top 16 elements of the stack.
    pub fn stack_top(&self) -> &[Felt] {
        &self.stack[self.stack_top_idx - MIN_STACK_DEPTH..self.stack_top_idx]
    }

    /// Returns a mutable reference to the top 16 elements of the stack.
    pub fn stack_top_mut(&mut self) -> &mut [Felt] {
        &mut self.stack[self.stack_top_idx - MIN_STACK_DEPTH..self.stack_top_idx]
    }

    /// Returns the element on the stack at index `idx`.
    ///
    /// This method is only meant to be used to access the stack top by operation handlers, and
    /// system event handlers.
    ///
    /// # Preconditions
    /// - `idx` must be less than or equal to 15.
    #[inline(always)]
    pub fn stack_get(&self, idx: usize) -> Felt {
        self.stack[self.stack_top_idx - idx - 1]
    }

    /// Same as [`Self::stack_get()`], but returns [`ZERO`] if `idx` falls below index 0 in the
    /// stack buffer.
    ///
    /// Use this instead of `stack_get()` when `idx` may exceed 15.
    #[inline(always)]
    pub fn stack_get_safe(&self, idx: usize) -> Felt {
        if idx < self.stack_top_idx {
            self.stack[self.stack_top_idx - idx - 1]
        } else {
            ZERO
        }
    }

    /// Mutable variant of `stack_get()`.
    ///
    /// This method is only meant to be used to access the stack top by operation handlers, and
    /// system event handlers.
    ///
    /// # Preconditions
    /// - `idx` must be less than or equal to 15.
    #[inline(always)]
    pub fn stack_get_mut(&mut self, idx: usize) -> &mut Felt {
        &mut self.stack[self.stack_top_idx - idx - 1]
    }

    /// Returns the word on the stack starting at index `start_idx` in "stack order".
    ///
    /// For `start_idx=0` the top element of the stack will be at position 0 in the word.
    ///
    /// For example, if the stack looks like this:
    ///
    /// top                                                       bottom
    /// v                                                           v
    /// a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p
    ///
    /// Then
    /// - `stack_get_word(0)` returns `[a, b, c, d]`,
    /// - `stack_get_word(1)` returns `[b, c, d, e]`,
    /// - etc.
    ///
    /// This method is only meant to be used to access the stack top by operation handlers, and
    /// system event handlers.
    ///
    /// # Preconditions
    /// - `start_idx` must be less than or equal to 12.
    #[inline(always)]
    pub fn stack_get_word(&self, start_idx: usize) -> Word {
        // Ensure we have enough elements to form a complete word
        debug_assert!(
            start_idx + WORD_SIZE <= self.stack_depth() as usize,
            "Not enough elements on stack to read word starting at index {start_idx}"
        );

        let word_start_idx = self.stack_top_idx - start_idx - WORD_SIZE;
        let mut result: [Felt; WORD_SIZE] =
            self.stack[range(word_start_idx, WORD_SIZE)].try_into().unwrap();
        // Reverse so top of stack (idx 0) goes to word[0]
        result.reverse();
        result.into()
    }

    /// Same as [`Self::stack_get_word()`], but returns [`ZERO`] for any element that falls below
    /// index 0 in the stack buffer.
    ///
    /// Use this instead of `stack_get_word()` when `start_idx + WORD_SIZE` may exceed
    /// `stack_top_idx`.
    #[inline(always)]
    pub fn stack_get_word_safe(&self, start_idx: usize) -> Word {
        let buf_end = self.stack_top_idx.saturating_sub(start_idx);
        let buf_start = self.stack_top_idx.saturating_sub(start_idx.saturating_add(WORD_SIZE));
        let num_elements_to_read_from_buf = buf_end - buf_start;

        let mut result = [ZERO; WORD_SIZE];
        if num_elements_to_read_from_buf == WORD_SIZE {
            result.copy_from_slice(&self.stack[range(buf_start, WORD_SIZE)]);
        } else if num_elements_to_read_from_buf > 0 {
            let offset = WORD_SIZE - num_elements_to_read_from_buf;
            result[offset..]
                .copy_from_slice(&self.stack[range(buf_start, num_elements_to_read_from_buf)]);
        }
        result.reverse();

        result.into()
    }

    /// Returns the number of elements on the stack in the current context.
    #[inline(always)]
    pub fn stack_depth(&self) -> u32 {
        (self.stack_top_idx - self.stack_bot_idx) as u32
    }

    /// Returns a reference to the processor's memory.
    pub fn memory(&self) -> &Memory {
        &self.memory
    }

    /// Consumes the processor and returns the advice provider, memory, and precompile
    /// transcript.
    pub fn into_parts(self) -> (AdviceProvider, Memory, PrecompileTranscript) {
        (self.advice, self.memory, self.pc_transcript)
    }

    /// Returns a reference to the execution options.
    pub fn execution_options(&self) -> &ExecutionOptions {
        &self.options
    }

    /// Returns a narrowed interface for reading and updating the processor state.
    #[inline(always)]
    pub fn state(&self) -> ProcessorState<'_> {
        ProcessorState { processor: self }
    }

    // MUTATORS
    // -------------------------------------------------------------------------------------------

    /// Writes an element to the stack at the given index.
    #[inline(always)]
    pub fn stack_write(&mut self, idx: usize, element: Felt) {
        self.stack[self.stack_top_idx - idx - 1] = element
    }

    /// Writes a word to the stack starting at the given index.
    ///
    /// `word[0]` goes to stack position start_idx (top), `word[1]` to start_idx+1, etc.
    #[inline(always)]
    pub fn stack_write_word(&mut self, start_idx: usize, word: &Word) {
        debug_assert!(start_idx <= MIN_STACK_DEPTH - WORD_SIZE);

        let word_start_idx = self.stack_top_idx - start_idx - 4;
        let mut source: [Felt; WORD_SIZE] = (*word).into();
        // Reverse so word[0] ends up at the top of stack (highest internal index)
        source.reverse();
        self.stack[range(word_start_idx, WORD_SIZE)].copy_from_slice(&source)
    }

    /// Swaps the elements at the given indices on the stack.
    #[inline(always)]
    pub fn stack_swap(&mut self, idx1: usize, idx2: usize) {
        let a = self.stack_get(idx1);
        let b = self.stack_get(idx2);
        self.stack_write(idx1, b);
        self.stack_write(idx2, a);
    }

    // DECORATOR EXECUTORS
    // --------------------------------------------------------------------------------------------

    /// Executes the decorators that should be executed before entering a node.
    fn execute_before_enter_decorators(
        &self,
        node_id: MastNodeId,
        current_forest: &MastForest,
        host: &mut impl BaseHost,
    ) -> ControlFlow<BreakReason> {
        if !self.should_execute_decorators() {
            return ControlFlow::Continue(());
        }

        #[cfg(test)]
        self.record_decorator_retrieval();

        let node = current_forest
            .get_node_by_id(node_id)
            .expect("internal error: node id {node_id} not found in current forest");

        for &decorator_id in node.before_enter(current_forest) {
            self.execute_decorator(&current_forest[decorator_id], host)?;
        }

        ControlFlow::Continue(())
    }

    /// Executes the decorators that should be executed after exiting a node.
    fn execute_after_exit_decorators(
        &self,
        node_id: MastNodeId,
        current_forest: &MastForest,
        host: &mut impl BaseHost,
    ) -> ControlFlow<BreakReason> {
        if !self.should_execute_decorators() {
            return ControlFlow::Continue(());
        }

        #[cfg(test)]
        self.record_decorator_retrieval();

        let node = current_forest
            .get_node_by_id(node_id)
            .expect("internal error: node id {node_id} not found in current forest");

        for &decorator_id in node.after_exit(current_forest) {
            self.execute_decorator(&current_forest[decorator_id], host)?;
        }

        ControlFlow::Continue(())
    }

    /// Executes the specified decorator
    fn execute_decorator(
        &self,
        decorator: &Decorator,
        host: &mut impl BaseHost,
    ) -> ControlFlow<BreakReason> {
        match decorator {
            Decorator::Debug(options) => {
                if self.in_debug_mode() {
                    let processor_state = self.state();
                    if let Err(err) = host.on_debug(&processor_state, options) {
                        return ControlFlow::Break(BreakReason::Err(
                            crate::errors::HostError::DebugHandlerError { err }.into(),
                        ));
                    }
                }
            },
            Decorator::Trace(id) => {
                if self.options.enable_tracing() {
                    let processor_state = self.state();
                    if let Err(err) = host.on_trace(&processor_state, *id) {
                        return ControlFlow::Break(BreakReason::Err(
                            crate::errors::HostError::TraceHandlerError { trace_id: *id, err }
                                .into(),
                        ));
                    }
                }
            },
        };
        ControlFlow::Continue(())
    }

    /// Increments the stack top pointer by 1.
    ///
    /// The bottom of the stack is never affected by this operation.
    #[inline(always)]
    fn increment_stack_size(&mut self) {
        self.stack_top_idx += 1;
    }

    /// Ensures the internal stack storage can accommodate one additional logical stack element.
    ///
    /// The operand stack depth limit is the semantic resource bound; the buffer is only an
    /// implementation detail. We therefore check the logical depth before allocating so a program
    /// cannot force memory growth beyond `ExecutionOptions::max_stack_depth()`. When storage does
    /// need to grow, it grows geometrically and remains heap-allocated as a boxed slice. A
    /// `SmallVec` would put a useful inline buffer inside `FastProcessor`, and preallocating the
    /// full limit would penalize ordinary programs. This policy is performance-sensitive and should
    /// be benchmarked against the fixed-buffer baseline.
    #[inline(always)]
    fn ensure_stack_capacity_for_push(&mut self) -> Result<(), ExecutionError> {
        let depth = self.stack_size() + 1;
        let max = self.options.max_stack_depth();
        if depth > max {
            return Err(ExecutionError::StackDepthLimitExceeded { depth, max });
        }

        if self.stack_top_idx >= self.stack.len() - 1 {
            self.grow_stack_buffer(self.stack_top_idx + 2);
        }

        Ok(())
    }

    fn ensure_stack_capacity_for_top_idx(&mut self, top_idx: usize) {
        if top_idx >= self.stack.len() {
            self.grow_stack_buffer(top_idx + 1);
        }
    }

    fn grow_stack_buffer(&mut self, requested_min_len: usize) {
        // The maximum allocation is tied to the logical operand stack depth, not to the current
        // buffer position. Using `stack_bot_idx` here would make the allocation ceiling drift when
        // the live stack has moved away from the initial base.
        let max_len = STACK_BUFFER_BASE_IDX
            .saturating_add(self.options.max_stack_depth())
            .saturating_add(1);
        let live_len = self.stack_size();

        // Growth also recenters the live stack at the normal base. This keeps future push/drop
        // behavior close to the fixed-buffer layout and avoids carrying unused prefix cells into
        // the new allocation. The extra slot is for the next checked push that triggered growth.
        let recentered_min_len = STACK_BUFFER_BASE_IDX.saturating_add(live_len).saturating_add(2);
        debug_assert!(recentered_min_len <= max_len);

        // Allocation growth is based on the stack's post-recentered live range, not the previous
        // buffer length. The `requested_min_len` may be beyond the allocation cap when a shallow
        // context is still positioned near the end of the old buffer; recentering the live stack is
        // what makes that valid. The VM-visible requirements are that the live stack is restored at
        // `STACK_BUFFER_BASE_IDX`, the post-recentered push slot is available, and allocation stays
        // capped by the configured stack depth. The allocation size can differ from the previous
        // doubling policy: normal push growth may allocate a couple of extra cells because of the
        // spare push slot, while restoring a deep caller from a shallow callee may allocate only
        // the requested restored range instead of doubling the old buffer. That smaller
        // restore allocation is intentional, but it means future pushes can grow again
        // sooner and should stay covered by benchmarks.
        let new_len = recentered_min_len.saturating_mul(2).max(requested_min_len).min(max_len);
        debug_assert!(new_len <= max_len);

        let mut new_stack = vec![ZERO; new_len].into_boxed_slice();
        let new_stack_bot_idx = STACK_BUFFER_BASE_IDX;
        let new_stack_top_idx = new_stack_bot_idx + live_len;

        // Only the active stack range carries VM state. Prefix/suffix cells are scratch storage and
        // stay zeroed, which keeps growth proportional to the live depth instead of the old buffer
        // length.
        new_stack[new_stack_bot_idx..new_stack_top_idx]
            .copy_from_slice(&self.stack[self.stack_bot_idx..self.stack_top_idx]);

        self.stack = new_stack;
        self.stack_bot_idx = new_stack_bot_idx;
        self.stack_top_idx = new_stack_top_idx;
    }

    /// Decrements the stack top pointer by 1.
    ///
    /// The bottom of the stack is only decremented in cases where the stack depth would become less
    /// than 16.
    #[inline(always)]
    fn decrement_stack_size(&mut self) {
        if self.stack_top_idx == MIN_STACK_DEPTH {
            // We no longer have any room in the stack buffer to decrement the stack size (which
            // would cause the `stack_bot_idx` to go below 0). We therefore reset the stack to its
            // original position.
            self.reset_stack_in_buffer(INITIAL_STACK_TOP_IDX);
        }

        self.stack_top_idx -= 1;
        self.stack_bot_idx = min(self.stack_bot_idx, self.stack_top_idx - MIN_STACK_DEPTH);
    }

    /// Resets the stack in the buffer to a new position, preserving the top 16 elements of the
    /// stack.
    ///
    /// # Preconditions
    /// - The stack is expected to have exactly 16 elements.
    #[inline(always)]
    fn reset_stack_in_buffer(&mut self, new_stack_top_idx: usize) {
        debug_assert_eq!(self.stack_depth(), MIN_STACK_DEPTH as u32);

        let new_stack_bot_idx = new_stack_top_idx - MIN_STACK_DEPTH;

        // Copy stack to its new position
        self.stack
            .copy_within(self.stack_bot_idx..self.stack_top_idx, new_stack_bot_idx);

        // Zero out stack below the new new_stack_bot_idx, since this is where overflow values
        // come from, and are guaranteed to be ZERO. We don't need to zero out above
        // `stack_top_idx`, since values there are never read before being written.
        self.stack[0..new_stack_bot_idx].fill(ZERO);

        // Update indices.
        self.stack_bot_idx = new_stack_bot_idx;
        self.stack_top_idx = new_stack_top_idx;
    }
}

// EXECUTION OUTPUT
// ===============================================================================================

/// The output of a program execution, containing the state of the stack, advice provider,
/// memory, and final precompile transcript at the end of execution.
#[derive(Debug)]
pub struct ExecutionOutput {
    pub stack: StackOutputs,
    pub advice: AdviceProvider,
    pub memory: Memory,
    pub final_precompile_transcript: PrecompileTranscript,
}

// EXECUTION CONTEXT INFO
// ===============================================================================================

/// Information about the execution context.
///
/// This struct is used to keep track of the information needed to return to the previous context
/// upon return from a `call`, `syscall` or `dyncall`.
#[derive(Debug)]
struct ExecutionContextInfo {
    /// This stores all the elements on the stack at the call site, excluding the top 16 elements.
    /// This corresponds to the overflow table in [crate::Process].
    overflow_stack: Vec<Felt>,
    ctx: ContextId,
    fn_hash: Word,
}

// NOOP TRACER
// ================================================================================================

/// A [Tracer] that does nothing.
pub struct NoopTracer;

impl Tracer for NoopTracer {
    type Processor = FastProcessor;

    #[inline(always)]
    fn start_clock_cycle(
        &mut self,
        _processor: &FastProcessor,
        _continuation: Continuation,
        _continuation_stack: &ContinuationStack,
        _current_forest: &Arc<MastForest>,
    ) {
        // do nothing
    }

    #[inline(always)]
    fn finalize_clock_cycle(
        &mut self,
        _processor: &FastProcessor,
        _op_helper_registers: OperationHelperRegisters,
        _current_forest: &Arc<MastForest>,
    ) {
        // do nothing
    }
}