miden_crypto/utils/
mod.rs

1//! Utilities used in this crate which can also be generally useful downstream.
2
3use alloc::{string::String, vec::Vec};
4use core::fmt::{self, Write};
5
6use thiserror::Error;
7#[cfg(feature = "std")]
8pub use winter_utils::ReadAdapter;
9pub use winter_utils::{
10    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SliceReader,
11    uninit_vector,
12};
13
14use crate::{Felt, FieldElement, StarkField, Word};
15
16// CONSTANTS
17// ================================================================================================
18
19/// The number of byte chunks that can be safely embedded in a field element
20const BINARY_CHUNK_SIZE: usize = 7;
21
22// UTILITY FUNCTIONS
23// ================================================================================================
24
25/// Converts a [Word] into hex.
26pub fn word_to_hex(w: &Word) -> Result<String, fmt::Error> {
27    let mut s = String::new();
28
29    for byte in w.iter().flat_map(|e| e.to_bytes()) {
30        write!(s, "{byte:02x}")?;
31    }
32
33    Ok(s)
34}
35
36/// Renders an array of bytes as hex into a String.
37pub fn bytes_to_hex_string<const N: usize>(data: [u8; N]) -> String {
38    let mut s = String::with_capacity(N + 2);
39
40    s.push_str("0x");
41    for byte in data.iter() {
42        write!(s, "{byte:02x}").expect("formatting hex failed");
43    }
44
45    s
46}
47
48/// Defines errors which can occur during parsing of hexadecimal strings.
49#[derive(Debug, Error)]
50pub enum HexParseError {
51    #[error("expected hex data to have length {expected}, including the 0x prefix, found {actual}")]
52    InvalidLength { expected: usize, actual: usize },
53    #[error("hex encoded data must start with 0x prefix")]
54    MissingPrefix,
55    #[error("hex encoded data must contain only characters [0-9a-fA-F]")]
56    InvalidChar,
57    #[error("hex encoded values of a Digest must be inside the field modulus")]
58    OutOfRange,
59}
60
61/// Parses a hex string into an array of bytes of known size.
62pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseError> {
63    let expected: usize = (N * 2) + 2;
64    if value.len() != expected {
65        return Err(HexParseError::InvalidLength { expected, actual: value.len() });
66    }
67
68    if !value.starts_with("0x") {
69        return Err(HexParseError::MissingPrefix);
70    }
71
72    let mut data = value.bytes().skip(2).map(|v| match v {
73        b'0'..=b'9' => Ok(v - b'0'),
74        b'a'..=b'f' => Ok(v - b'a' + 10),
75        b'A'..=b'F' => Ok(v - b'A' + 10),
76        _ => Err(HexParseError::InvalidChar),
77    });
78
79    let mut decoded = [0u8; N];
80    for byte in decoded.iter_mut() {
81        // These `unwrap` calls are okay because the length was checked above
82        let high: u8 = data.next().unwrap()?;
83        let low: u8 = data.next().unwrap()?;
84        *byte = (high << 4) + low;
85    }
86
87    Ok(decoded)
88}
89
90/// Converts a sequence of bytes into vector field elements with padding. This guarantees that no
91/// two sequences or bytes map to the same sequence of field elements.
92///
93/// Packs bytes into chunks of `BINARY_CHUNK_SIZE` and adds padding to the final chunk using a `1`
94/// bit followed by zeros. This ensures the original bytes can be recovered during decoding without
95/// any ambiguity.
96///
97/// Note that by the endianness of the conversion as well as the fact that we are packing at most
98/// `56 = 7 * 8` bits in each field element, the padding above with `1` should never overflow the
99/// field size.
100///
101/// # Arguments
102/// * `bytes` - Byte slice to encode
103///
104/// # Returns
105/// Vector of `Felt` elements with the last element containing padding
106pub fn bytes_to_elements_with_padding(bytes: &[u8]) -> Vec<Felt> {
107    if bytes.is_empty() {
108        return vec![];
109    }
110
111    // determine the number of field elements needed to encode `bytes` when each field element
112    // represents at most 7 bytes.
113    let num_field_elem = bytes.len().div_ceil(BINARY_CHUNK_SIZE);
114
115    // initialize a buffer to receive the little-endian elements.
116    let mut buf = [0_u8; 8];
117
118    // iterate the chunks of bytes, creating a field element from each chunk
119    let last_chunk_idx = num_field_elem - 1;
120
121    bytes
122        .chunks(BINARY_CHUNK_SIZE)
123        .enumerate()
124        .map(|(current_chunk_idx, chunk)| {
125            // copy the chunk into the buffer
126            if current_chunk_idx != last_chunk_idx {
127                buf[..BINARY_CHUNK_SIZE].copy_from_slice(chunk);
128            } else {
129                // on the last iteration, we pad `buf` with a 1 followed by as many 0's as are
130                // needed to fill it
131                buf.fill(0);
132                buf[..chunk.len()].copy_from_slice(chunk);
133                buf[chunk.len()] = 1;
134            }
135
136            Felt::new(u64::from_le_bytes(buf))
137        })
138        .collect()
139}
140
141/// Converts a sequence of padded field elements back to the original bytes.
142///
143/// Reconstructs the original byte sequence by removing the padding added by `bytes_to_felts`.
144/// The padding consists of a `1` bit followed by zeros in the final field element.
145///
146/// Note that by the endianness of the conversion as well as the fact that we are packing at most
147/// `56 = 7 * 8` bits in each field element, the padding above with `1` should never overflow the
148/// field size.
149///
150/// # Arguments
151/// * `felts` - Slice of field elements with padding in the last element
152///
153/// # Returns
154/// * `Some(Vec<u8>)` - The original byte sequence with padding removed
155/// * `None` - If no padding marker (`1` bit) is found
156pub fn padded_elements_to_bytes(felts: &[Felt]) -> Option<Vec<u8>> {
157    let number_felts = felts.len();
158    if number_felts == 0 {
159        return Some(vec![]);
160    }
161
162    let mut result = Vec::with_capacity(number_felts * BINARY_CHUNK_SIZE);
163    for felt in felts.iter().take(number_felts - 1) {
164        let felt_bytes = felt.as_int().to_le_bytes();
165        result.extend_from_slice(&felt_bytes[..BINARY_CHUNK_SIZE]);
166    }
167
168    // handle the last field element
169    let felt_bytes = felts[number_felts - 1].as_int().to_le_bytes();
170    let pos = felt_bytes.iter().rposition(|entry| *entry == 1_u8)?;
171
172    result.extend_from_slice(&felt_bytes[..pos]);
173    Some(result)
174}
175
176/// Converts field elements to raw byte representation.
177///
178/// Each `Felt` is converted to its full `ELEMENT_BYTES` representation, in little-endian form
179/// and canonical form, without any padding removal or validation. This is the inverse
180/// of `bytes_to_elements_exact`.
181///
182/// # Arguments
183/// * `felts` - Slice of field elements to convert
184///
185/// # Returns
186/// Vector containing the raw bytes from all field elements
187pub fn elements_to_bytes(felts: &[Felt]) -> Vec<u8> {
188    let number_felts = felts.len();
189    let mut result = Vec::with_capacity(number_felts * Felt::ELEMENT_BYTES);
190    for felt in felts.iter().take(number_felts) {
191        let felt_bytes = felt.as_int().to_le_bytes();
192        result.extend_from_slice(&felt_bytes);
193    }
194
195    result
196}
197
198/// Converts bytes to field elements with validation.
199///
200/// This function validates that:
201/// - The input bytes length is divisible by `Felt::ELEMENT_BYTES`
202/// - All `Felt::ELEMENT_BYTES`-byte sequences represent valid field elements
203///
204/// # Arguments
205/// * `bytes` - Byte slice that must be a multiple of `Felt::ELEMENT_BYTES` in length
206///
207/// # Returns
208/// `Option<Vec<Felt>>` - Vector of `Felt` elements if all validations pass, or None otherwise
209pub fn bytes_to_elements_exact(bytes: &[u8]) -> Option<Vec<Felt>> {
210    // Check that the length is divisible by ELEMENT_BYTES
211    if !bytes.len().is_multiple_of(Felt::ELEMENT_BYTES) {
212        return None;
213    }
214
215    let mut result = Vec::with_capacity(bytes.len() / Felt::ELEMENT_BYTES);
216
217    for chunk in bytes.chunks_exact(Felt::ELEMENT_BYTES) {
218        let chunk_array: [u8; Felt::ELEMENT_BYTES] =
219            chunk.try_into().expect("should succeed given the length check above");
220
221        let value = u64::from_le_bytes(chunk_array);
222
223        // Validate that the value represents a valid field element
224        if value >= Felt::MODULUS {
225            return None;
226        }
227
228        result.push(Felt::new(value));
229    }
230
231    Some(result)
232}