miden_core/utils/
mod.rs

1pub mod sync;
2
3use alloc::vec::Vec;
4use core::{
5    fmt::Debug,
6    ops::{Bound, Range},
7};
8
9// RE-EXPORTS
10// ================================================================================================
11pub use miden_crypto::utils::{
12    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SliceReader,
13    collections, uninit_vector,
14};
15#[cfg(feature = "std")]
16pub use winter_utils::ReadAdapter;
17pub use winter_utils::group_slice_elements;
18
19use crate::Felt;
20
21pub mod math {
22    pub use math::batch_inversion;
23}
24
25// TO ELEMENTS
26// ================================================================================================
27
28pub trait ToElements {
29    fn to_elements(&self) -> Vec<Felt>;
30}
31
32impl<const N: usize> ToElements for [u64; N] {
33    fn to_elements(&self) -> Vec<Felt> {
34        self.iter().map(|&v| Felt::new(v)).collect()
35    }
36}
37
38impl ToElements for Vec<u64> {
39    fn to_elements(&self) -> Vec<Felt> {
40        self.iter().map(|&v| Felt::new(v)).collect()
41    }
42}
43
44// INTO BYTES
45// ================================================================================================
46
47pub trait IntoBytes<const N: usize> {
48    fn into_bytes(self) -> [u8; N];
49}
50
51impl IntoBytes<32> for [Felt; 4] {
52    fn into_bytes(self) -> [u8; 32] {
53        let mut result = [0; 32];
54
55        result[..8].copy_from_slice(&self[0].as_int().to_le_bytes());
56        result[8..16].copy_from_slice(&self[1].as_int().to_le_bytes());
57        result[16..24].copy_from_slice(&self[2].as_int().to_le_bytes());
58        result[24..].copy_from_slice(&self[3].as_int().to_le_bytes());
59
60        result
61    }
62}
63
64// PUSH MANY
65// ================================================================================================
66
67pub trait PushMany<T> {
68    fn push_many(&mut self, value: T, n: usize);
69}
70
71impl<T: Copy> PushMany<T> for Vec<T> {
72    fn push_many(&mut self, value: T, n: usize) {
73        let new_len = self.len() + n;
74        self.resize(new_len, value);
75    }
76}
77
78// RANGE
79// ================================================================================================
80
81/// Returns a [Range] initialized with the specified `start` and with `end` set to `start` + `len`.
82pub const fn range(start: usize, len: usize) -> Range<usize> {
83    Range { start, end: start + len }
84}
85
86/// Converts and parses a [Bound] into an included u64 value.
87pub fn bound_into_included_u64<I>(bound: Bound<&I>, is_start: bool) -> u64
88where
89    I: Clone + Into<u64>,
90{
91    match bound {
92        Bound::Excluded(i) => i.clone().into().saturating_sub(1),
93        Bound::Included(i) => i.clone().into(),
94        Bound::Unbounded => {
95            if is_start {
96                0
97            } else {
98                u64::MAX
99            }
100        },
101    }
102}
103
104// ARRAY CONSTRUCTORS
105// ================================================================================================
106
107/// Returns an array of N vectors initialized with the specified capacity.
108pub fn new_array_vec<T: Debug, const N: usize>(capacity: usize) -> [Vec<T>; N] {
109    (0..N)
110        .map(|_| Vec::with_capacity(capacity))
111        .collect::<Vec<_>>()
112        .try_into()
113        .expect("failed to convert vector to array")
114}
115
116#[test]
117#[should_panic]
118fn debug_assert_is_checked() {
119    // enforce the release checks to always have `RUSTFLAGS="-C debug-assertions".
120    //
121    // some upstream tests are performed with `debug_assert`, and we want to assert its correctness
122    // downstream.
123    //
124    // for reference, check
125    // https://github.com/0xMiden/miden-vm/issues/433
126    debug_assert!(false);
127}
128
129// FORMATTING
130// ================================================================================================
131
132pub use miden_formatting::hex::{DisplayHex, ToHex, to_hex};