miden_processor/
utils.rs

1use miden_core::Felt;
2// RE-EXPORTS
3// ================================================================================================
4pub use miden_core::utils::*;
5
6// HELPER FUNCTIONS
7// ================================================================================================
8
9/// Splits an element into two field elements containing 32-bit integer values
10#[inline(always)]
11pub(crate) fn split_element(value: Felt) -> (Felt, Felt) {
12    let value = value.as_int();
13    let lo = (value as u32) as u64;
14    let hi = value >> 32;
15    (Felt::new(hi), Felt::new(lo))
16}
17
18/// Splits an element into two 16 bit integer limbs. It assumes that the field element contains a
19/// valid 32-bit integer value.
20pub(crate) fn split_element_u32_into_u16(value: Felt) -> (Felt, Felt) {
21    let (hi, lo) = split_u32_into_u16(value.as_int());
22    (Felt::new(hi as u64), Felt::new(lo as u64))
23}
24
25/// Splits a u64 integer assumed to contain a 32-bit value into two u16 integers.
26///
27/// # Errors
28/// Fails in debug mode if the provided value is not a 32-bit value.
29pub(crate) fn split_u32_into_u16(value: u64) -> (u16, u16) {
30    const U32MAX: u64 = u32::MAX as u64;
31    debug_assert!(value <= U32MAX, "not a 32-bit value");
32
33    let lo = value as u16;
34    let hi = (value >> 16) as u16;
35
36    (hi, lo)
37}