miden_crypto/utils/mod.rs
1//! Utilities used in this crate which can also be generally useful downstream.
2
3use alloc::{boxed::Box, string::String, vec::Vec};
4use core::{
5 fmt::{self, Write},
6 mem::{ManuallyDrop, MaybeUninit},
7};
8
9// Re-export serialization traits from miden-serde-utils
10#[cfg(feature = "std")]
11pub use miden_serde_utils::ReadAdapter;
12pub use miden_serde_utils::{
13 BudgetedReader, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
14 SliceReader,
15};
16use p3_maybe_rayon::prelude::*;
17
18use crate::{Felt, Word, field::QuotientMap};
19
20// CONSTANTS
21// ================================================================================================
22
23/// The number of byte chunks that can be safely embedded in a field element
24const BINARY_CHUNK_SIZE: usize = 7;
25
26// RE-EXPORTS
27// ================================================================================================
28
29pub use k256::elliptic_curve::zeroize;
30
31// UTILITY FUNCTIONS
32// ================================================================================================
33
34/// Converts a [Word] into hex.
35pub fn word_to_hex(w: &Word) -> Result<String, fmt::Error> {
36 let mut s = String::new();
37
38 for byte in w.iter().flat_map(|&e| e.to_bytes()) {
39 write!(s, "{byte:02x}")?;
40 }
41
42 Ok(s)
43}
44
45/// Writes `bytes` into the formatter as a `0x`-prefixed, lowercase hexadecimal string.
46///
47/// Useful for `Display` implementations that want compact, canonical hex output (e.g. in `tracing`
48/// logs) without allocating an intermediate `String`. This mirrors the format produced by
49/// [`bytes_to_hex_string`], which only accepts fixed-size arrays.
50pub fn write_hex(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
51 write!(f, "0x")?;
52 for byte in bytes {
53 write!(f, "{byte:02x}")?;
54 }
55 Ok(())
56}
57
58pub use miden_field::utils::{HexParseError, bytes_to_hex_string, hex_to_bytes};
59
60// CONVERSIONS BETWEEN BYTES AND ELEMENTS
61// ================================================================================================
62
63/// Converts a sequence of bytes into vector field elements with padding. This guarantees that no
64/// two sequences or bytes map to the same sequence of field elements.
65///
66/// Packs bytes into chunks of `BINARY_CHUNK_SIZE` and adds padding to the final chunk using a `1`
67/// bit followed by zeros. This ensures the original bytes can be recovered during decoding without
68/// any ambiguity.
69///
70/// Note that by the endianness of the conversion as well as the fact that we are packing at most
71/// `56 = 7 * 8` bits in each field element, the padding above with `1` should never overflow the
72/// field size.
73///
74/// # Arguments
75/// * `bytes` - Byte slice to encode
76///
77/// # Returns
78/// Vector of `Felt` elements with the last element containing padding
79pub fn bytes_to_elements_with_padding(bytes: &[u8]) -> Vec<Felt> {
80 if bytes.is_empty() {
81 return vec![];
82 }
83
84 // determine the number of field elements needed to encode `bytes` when each field element
85 // represents at most 7 bytes.
86 let num_field_elem = bytes.len().div_ceil(BINARY_CHUNK_SIZE);
87
88 // initialize a buffer to receive the little-endian elements.
89 let mut buf = [0_u8; 8];
90
91 // iterate the chunks of bytes, creating a field element from each chunk
92 let last_chunk_idx = num_field_elem - 1;
93
94 bytes
95 .chunks(BINARY_CHUNK_SIZE)
96 .enumerate()
97 .map(|(current_chunk_idx, chunk)| {
98 // copy the chunk into the buffer
99 if current_chunk_idx != last_chunk_idx {
100 buf[..BINARY_CHUNK_SIZE].copy_from_slice(chunk);
101 } else {
102 // on the last iteration, we pad `buf` with a 1 followed by as many 0's as are
103 // needed to fill it
104 buf.fill(0);
105 buf[..chunk.len()].copy_from_slice(chunk);
106 buf[chunk.len()] = 1;
107 }
108
109 Felt::new_unchecked(u64::from_le_bytes(buf))
110 })
111 .collect()
112}
113
114/// Converts a sequence of padded field elements back to the original bytes.
115///
116/// Reconstructs the original byte sequence by removing the padding added by
117/// `bytes_to_elements_with_padding`.
118/// The padding consists of a `1` bit followed by zeros in the final field element.
119/// Any bytes after the last `1` marker in the final field element are ignored and are not
120/// validated to be zero.
121///
122/// Note that by the endianness of the conversion as well as the fact that we are packing at most
123/// `56 = 7 * 8` bits in each field element, the padding above with `1` should never overflow the
124/// field size.
125///
126/// # Arguments
127/// * `felts` - Slice of field elements with padding in the last element
128///
129/// # Returns
130/// * `Some(Vec<u8>)` - The original byte sequence with padding removed
131/// * `None` - If no padding marker (`1` bit) is found
132pub fn padded_elements_to_bytes(felts: &[Felt]) -> Option<Vec<u8>> {
133 let number_felts = felts.len();
134 if number_felts == 0 {
135 return Some(vec![]);
136 }
137
138 let mut result = Vec::with_capacity(number_felts * BINARY_CHUNK_SIZE);
139 for felt in felts.iter().take(number_felts - 1) {
140 let felt_bytes = felt.as_canonical_u64().to_le_bytes();
141 result.extend_from_slice(&felt_bytes[..BINARY_CHUNK_SIZE]);
142 }
143
144 // handle the last field element
145 let felt_bytes = felts[number_felts - 1].as_canonical_u64().to_le_bytes();
146 let pos = felt_bytes.iter().rposition(|entry| *entry == 1_u8)?;
147
148 result.extend_from_slice(&felt_bytes[..pos]);
149 Some(result)
150}
151
152/// Converts field elements to raw byte representation.
153///
154/// Each `Felt` is converted to its full `NUM_BYTES` representation, in little-endian form
155/// and canonical form, without any padding removal or validation. This is the inverse
156/// of `bytes_to_elements_exact`.
157///
158/// # Arguments
159/// * `felts` - Slice of field elements to convert
160///
161/// # Returns
162/// Vector containing the raw bytes from all field elements
163pub fn elements_to_bytes(felts: &[Felt]) -> Vec<u8> {
164 let number_felts = felts.len();
165 let mut result = Vec::with_capacity(number_felts * Felt::NUM_BYTES);
166 for felt in felts.iter().take(number_felts) {
167 let felt_bytes = felt.as_canonical_u64().to_le_bytes();
168 result.extend_from_slice(&felt_bytes);
169 }
170
171 result
172}
173
174/// Converts bytes to field elements with validation.
175///
176/// This function validates that:
177/// - The input bytes length is divisible by `Felt::NUM_BYTES`
178/// - All `Felt::NUM_BYTES`-byte sequences represent valid field elements
179///
180/// # Arguments
181/// * `bytes` - Byte slice that must be a multiple of `Felt::NUM_BYTES` in length
182///
183/// # Returns
184/// `Option<Vec<Felt>>` - Vector of `Felt` elements if all validations pass, or None otherwise
185pub fn bytes_to_elements_exact(bytes: &[u8]) -> Option<Vec<Felt>> {
186 // Check that the length is divisible by NUM_BYTES
187 if !bytes.len().is_multiple_of(Felt::NUM_BYTES) {
188 return None;
189 }
190
191 let mut result = Vec::with_capacity(bytes.len() / Felt::NUM_BYTES);
192
193 for chunk in bytes.chunks_exact(Felt::NUM_BYTES) {
194 let chunk_array: [u8; Felt::NUM_BYTES] =
195 chunk.try_into().expect("should succeed given the length check above");
196
197 let value = u64::from_le_bytes(chunk_array);
198
199 // Validate that the value represents a valid field element
200 let felt = Felt::from_canonical_checked(value)?;
201 result.push(felt);
202 }
203
204 Some(result)
205}
206
207/// Converts bytes to field elements using u32 packing in little-endian format.
208///
209/// Each field element contains a u32 value representing up to 4 bytes. If the byte length
210/// is not a multiple of 4, the final field element is zero-padded.
211///
212/// # Arguments
213/// - `bytes`: The byte slice to convert
214///
215/// # Returns
216/// A vector of field elements, each containing 4 bytes packed in little-endian order.
217///
218/// # Examples
219/// ```rust
220/// # use miden_crypto::{Felt, utils::bytes_to_packed_u32_elements};
221///
222/// let bytes = vec![0x01, 0x02, 0x03, 0x04, 0x05];
223/// let felts = bytes_to_packed_u32_elements(&bytes);
224/// assert_eq!(felts, vec![Felt::new_unchecked(0x04030201), Felt::new_unchecked(0x00000005)]);
225/// ```
226pub fn bytes_to_packed_u32_elements(bytes: &[u8]) -> Vec<Felt> {
227 const BYTES_PER_U32: usize = size_of::<u32>();
228
229 bytes
230 .chunks(BYTES_PER_U32)
231 .map(|chunk| {
232 // Pack up to 4 bytes into a u32 in little-endian format
233 let mut packed = [0u8; BYTES_PER_U32];
234 packed[..chunk.len()].copy_from_slice(chunk);
235 Felt::from_u32(u32::from_le_bytes(packed))
236 })
237 .collect()
238}
239
240// VECTOR FUNCTIONS (ported from Winterfell's winter-utils)
241// ================================================================================================
242
243/// Returns a vector of the specified length with un-initialized memory.
244///
245/// This is usually faster than requesting a vector with initialized memory and is useful when we
246/// overwrite all contents of the vector immediately after memory allocation.
247pub fn uninit_vector<T>(length: usize) -> Vec<MaybeUninit<T>> {
248 Vec::from(Box::new_uninit_slice(length))
249}
250
251/// Converts a fully-initialized `Vec<MaybeUninit<T>>` into `Vec<T>`.
252///
253/// # Safety
254/// All elements must be initialized before calling this function.
255pub unsafe fn assume_init_vec<T>(v: Vec<MaybeUninit<T>>) -> Vec<T> {
256 let mut v = ManuallyDrop::new(v);
257 let ptr = v.as_mut_ptr();
258 let len = v.len();
259 let cap = v.capacity();
260 // SAFETY: caller guarantees all elements are initialized.
261 unsafe { Vec::from_raw_parts(ptr.cast::<T>(), len, cap) }
262}
263
264// GROUPING / UN-GROUPING FUNCTIONS (ported from Winterfell's winter-utils)
265// ================================================================================================
266
267/// Transmutes a slice of `n` elements into a slice of `n` / `N` elements, each of which is
268/// an array of `N` elements.
269///
270/// This function just re-interprets the underlying memory and is thus zero-copy.
271/// # Panics
272/// Panics if `n` is not divisible by `N`.
273pub fn group_slice_elements<T, const N: usize>(source: &[T]) -> &[[T; N]] {
274 let (chunks, remainder) = source.as_chunks::<N>();
275 assert!(remainder.is_empty(), "source length must be divisible by {N}");
276 chunks
277}
278
279/// Transmutes a slice of `n` arrays each of length `N`, into a slice of `N` * `n` elements.
280///
281/// This function just re-interprets the underlying memory and is thus zero-copy.
282pub fn flatten_slice_elements<T, const N: usize>(source: &[[T; N]]) -> &[T] {
283 // SAFETY: [T; N] has the same alignment and memory layout as an array of T.
284 // p3-util's as_base_slice handles the conversion safely.
285 unsafe { p3_util::as_base_slice(source) }
286}
287
288/// Transmutes a vector of `n` arrays each of length `N`, into a vector of `N` * `n` elements.
289///
290/// This function just re-interprets the underlying memory and is thus zero-copy.
291pub fn flatten_vector_elements<T, const N: usize>(source: Vec<[T; N]>) -> Vec<T> {
292 // SAFETY: [T; N] has the same alignment and memory layout as an array of T.
293 // p3-util's flatten_to_base handles the conversion without reallocations.
294 unsafe { p3_util::flatten_to_base(source) }
295}
296
297// TRANSPOSING (ported from Winterfell's winter-utils)
298// ================================================================================================
299
300/// Transposes a slice of `n` elements into a matrix with `N` columns and `n`/`N` rows.
301///
302/// When `concurrent` feature is enabled, the slice will be transposed using multiple threads.
303/// Uses uninit_vector for ~31% speedup at 1024x1024 (benches/transpose.rs).
304///
305/// # Panics
306/// Panics if `n` is not divisible by `N`.
307pub fn transpose_slice<T: Copy + Send + Sync, const N: usize>(source: &[T]) -> Vec<[T; N]> {
308 let row_count = source.len() / N;
309 assert_eq!(
310 row_count * N,
311 source.len(),
312 "source length must be divisible by {}, but was {}",
313 N,
314 source.len()
315 );
316
317 let mut result = uninit_vector::<[T; N]>(row_count);
318 result.par_iter_mut().enumerate().for_each(|(i, slot)| {
319 let row = core::array::from_fn(|j| source[i + j * row_count]);
320 slot.write(row);
321 });
322 // SAFETY: all rows are written above.
323 unsafe { assume_init_vec(result) }
324}