miden_core/utils/
mod.rs

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