use alloc::vec::Vec;
use core::ops::{Bound, Range};
use crate::{Felt, Word, crypto::hash::Blake3_256, field::PrimeCharacteristicRing};
mod col_matrix;
pub use col_matrix::ColMatrix;
#[cfg(feature = "std")]
pub use miden_crypto::utils::ReadAdapter;
pub use miden_crypto::{
stark::matrix::{Matrix, RowMajorMatrix},
utils::{
assume_init_vec, flatten_slice_elements, flatten_vector_elements, group_slice_elements,
uninit_vector,
},
};
pub use miden_formatting::hex::{DisplayHex, ToHex, to_hex};
pub use miden_utils_indexing::{
CsrMatrix, CsrValidationError, DenseIdMap, Idx, IndexVec, IndexedVecError, LookupByIdx,
newtype_id,
};
pub trait ToElements {
fn to_elements(&self) -> Vec<Felt>;
}
impl<const N: usize> ToElements for [u64; N] {
fn to_elements(&self) -> Vec<Felt> {
self.iter().map(|&v| Felt::from_u64(v)).collect()
}
}
impl ToElements for Vec<u64> {
fn to_elements(&self) -> Vec<Felt> {
self.iter().map(|&v| Felt::from_u64(v)).collect()
}
}
pub fn hash_string_to_word<'a>(value: impl Into<&'a str>) -> Word {
let digest_bytes: [u8; 32] = Blake3_256::hash(value.into().as_bytes()).into();
[
Felt::new(u64::from_le_bytes(digest_bytes[0..8].try_into().unwrap())),
Felt::new(u64::from_le_bytes(digest_bytes[8..16].try_into().unwrap())),
Felt::new(u64::from_le_bytes(digest_bytes[16..24].try_into().unwrap())),
Felt::new(u64::from_le_bytes(digest_bytes[24..32].try_into().unwrap())),
]
.into()
}
pub trait IntoBytes<const N: usize> {
fn into_bytes(self) -> [u8; N];
}
impl IntoBytes<32> for [Felt; 4] {
fn into_bytes(self) -> [u8; 32] {
let mut result = [0; 32];
result[..8].copy_from_slice(&self[0].as_canonical_u64().to_le_bytes());
result[8..16].copy_from_slice(&self[1].as_canonical_u64().to_le_bytes());
result[16..24].copy_from_slice(&self[2].as_canonical_u64().to_le_bytes());
result[24..].copy_from_slice(&self[3].as_canonical_u64().to_le_bytes());
result
}
}
pub const fn range(start: usize, len: usize) -> Range<usize> {
Range { start, end: start + len }
}
pub fn bound_into_included_u64<I>(bound: Bound<&I>, is_start: bool) -> u64
where
I: Clone + Into<u64>,
{
match bound {
Bound::Excluded(i) => i.clone().into().saturating_sub(1),
Bound::Included(i) => i.clone().into(),
Bound::Unbounded => {
if is_start {
0
} else {
u64::MAX
}
},
}
}
const BYTES_PER_U32: usize = core::mem::size_of::<u32>();
pub fn bytes_to_packed_u32_elements(bytes: &[u8]) -> Vec<Felt> {
bytes
.chunks(BYTES_PER_U32)
.map(|chunk| {
let mut packed = [0u8; BYTES_PER_U32];
packed[..chunk.len()].copy_from_slice(chunk);
Felt::from_u32(u32::from_le_bytes(packed))
})
.collect()
}
pub fn packed_u32_elements_to_bytes(elements: &[Felt]) -> Vec<u8> {
elements
.iter()
.flat_map(|felt| {
let value = felt.as_canonical_u64() as u32;
value.to_le_bytes()
})
.collect()
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
proptest! {
#[test]
fn proptest_packed_u32_elements_roundtrip(values in prop::collection::vec(any::<u32>(), 0..100)) {
let felts: Vec<Felt> = values.iter().map(|&v| Felt::from_u32(v)).collect();
let bytes = packed_u32_elements_to_bytes(&felts);
let roundtrip_felts = bytes_to_packed_u32_elements(&bytes);
prop_assert_eq!(felts, roundtrip_felts);
}
}
#[test]
#[should_panic]
fn debug_assert_is_checked() {
debug_assert!(false);
}
}