miden_crypto/utils/
mod.rs

1//! Utilities used in this crate which can also be generally useful downstream.
2
3use alloc::string::String;
4use core::fmt::{self, Write};
5
6use thiserror::Error;
7
8use super::Word;
9
10mod kv_map;
11
12// RE-EXPORTS
13// ================================================================================================
14
15pub use winter_utils::{
16    uninit_vector, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
17    SliceReader,
18};
19
20pub mod collections {
21    pub use super::kv_map::*;
22}
23
24// UTILITY FUNCTIONS
25// ================================================================================================
26
27/// Converts a [Word] into hex.
28pub fn word_to_hex(w: &Word) -> Result<String, fmt::Error> {
29    let mut s = String::new();
30
31    for byte in w.iter().flat_map(|e| e.to_bytes()) {
32        write!(s, "{byte:02x}")?;
33    }
34
35    Ok(s)
36}
37
38/// Renders an array of bytes as hex into a String.
39pub fn bytes_to_hex_string<const N: usize>(data: [u8; N]) -> String {
40    let mut s = String::with_capacity(N + 2);
41
42    s.push_str("0x");
43    for byte in data.iter() {
44        write!(s, "{byte:02x}").expect("formatting hex failed");
45    }
46
47    s
48}
49
50/// Defines errors which can occur during parsing of hexadecimal strings.
51#[derive(Debug, Error)]
52pub enum HexParseError {
53    #[error(
54        "expected hex data to have length {expected}, including the 0x prefix, found {actual}"
55    )]
56    InvalidLength { expected: usize, actual: usize },
57    #[error("hex encoded data must start with 0x prefix")]
58    MissingPrefix,
59    #[error("hex encoded data must contain only characters [a-zA-Z0-9]")]
60    InvalidChar,
61    #[error("hex encoded values of a Digest must be inside the field modulus")]
62    OutOfRange,
63}
64
65/// Parses a hex string into an array of bytes of known size.
66pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseError> {
67    let expected: usize = (N * 2) + 2;
68    if value.len() != expected {
69        return Err(HexParseError::InvalidLength { expected, actual: value.len() });
70    }
71
72    if !value.starts_with("0x") {
73        return Err(HexParseError::MissingPrefix);
74    }
75
76    let mut data = value.bytes().skip(2).map(|v| match v {
77        b'0'..=b'9' => Ok(v - b'0'),
78        b'a'..=b'f' => Ok(v - b'a' + 10),
79        b'A'..=b'F' => Ok(v - b'A' + 10),
80        _ => Err(HexParseError::InvalidChar),
81    });
82
83    let mut decoded = [0u8; N];
84    for byte in decoded.iter_mut() {
85        // These `unwrap` calls are okay because the length was checked above
86        let high: u8 = data.next().unwrap()?;
87        let low: u8 = data.next().unwrap()?;
88        *byte = (high << 4) + low;
89    }
90
91    Ok(decoded)
92}