miden_processor/
utils.rs

1use alloc::vec::Vec;
2
3// RE-EXPORTS
4// ================================================================================================
5pub use vm_core::utils::*;
6
7use super::Felt;
8
9// HELPER FUNCTIONS
10// ================================================================================================
11
12/// Returns the number of rows in the provided execution trace assumed to be in column-major form
13/// and contain at least one column.
14pub(crate) fn get_trace_len(trace: &[Vec<Felt>]) -> usize {
15    trace[0].len()
16}
17
18/// Splits an element into two field elements containing 32-bit integer values
19#[inline(always)]
20pub(crate) fn split_element(value: Felt) -> (Felt, Felt) {
21    let value = value.as_int();
22    let lo = (value as u32) as u64;
23    let hi = value >> 32;
24    (Felt::new(hi), Felt::new(lo))
25}
26
27/// Splits an element into two 16 bit integer limbs. It assumes that the field element contains a
28/// valid 32-bit integer value.
29pub(crate) fn split_element_u32_into_u16(value: Felt) -> (Felt, Felt) {
30    let (hi, lo) = split_u32_into_u16(value.as_int());
31    (Felt::new(hi as u64), Felt::new(lo as u64))
32}
33
34/// Splits a u64 integer assumed to contain a 32-bit value into two u16 integers.
35///
36/// # Errors
37/// Fails in debug mode if the provided value is not a 32-bit value.
38pub(crate) fn split_u32_into_u16(value: u64) -> (u16, u16) {
39    const U32MAX: u64 = u32::MAX as u64;
40    debug_assert!(value <= U32MAX, "not a 32-bit value");
41
42    let lo = value as u16;
43    let hi = (value >> 16) as u16;
44
45    (hi, lo)
46}