Skip to main content

miden_core/program/stack/
mod.rs

1use crate::{Felt, field::PrimeField64};
2
3mod inputs;
4pub use inputs::{InputError, StackInputs};
5
6mod outputs;
7pub use outputs::{OutputError, StackOutputs};
8
9#[cfg(test)]
10mod tests;
11
12// CONSTANTS
13// ================================================================================================
14
15/// Represents:
16/// - Number of elements that can be initialized at the start of execution and remain populated at
17///   the end of execution.
18/// - Number of elements that can be accessed directly via instructions.
19/// - Number of elements that remain visible to the callee when the context is switched via `call`
20///   or `syscall` instructions.
21/// - Number of elements below which the depth of the stack never drops.
22pub const MIN_STACK_DEPTH: usize = 16;
23
24// HELPER FUNCTIONS
25// ================================================================================================
26
27/// Get the number of non-zero stack elements.
28fn get_num_stack_values(values: &[Felt; MIN_STACK_DEPTH]) -> u8 {
29    let mut num_trailing_zeros = 0;
30    for v in values.iter().rev() {
31        if v.as_canonical_u64() == 0 {
32            num_trailing_zeros += 1;
33        } else {
34            break;
35        }
36    }
37    (MIN_STACK_DEPTH - num_trailing_zeros) as u8
38}