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    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable, SliceReader,
17    uninit_vector,
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("expected hex data to have length {expected}, including the 0x prefix, found {actual}")]
54    InvalidLength { expected: usize, actual: usize },
55    #[error("hex encoded data must start with 0x prefix")]
56    MissingPrefix,
57    #[error("hex encoded data must contain only characters [a-zA-Z0-9]")]
58    InvalidChar,
59    #[error("hex encoded values of a Digest must be inside the field modulus")]
60    OutOfRange,
61}
62
63/// Parses a hex string into an array of bytes of known size.
64pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseError> {
65    let expected: usize = (N * 2) + 2;
66    if value.len() != expected {
67        return Err(HexParseError::InvalidLength { expected, actual: value.len() });
68    }
69
70    if !value.starts_with("0x") {
71        return Err(HexParseError::MissingPrefix);
72    }
73
74    let mut data = value.bytes().skip(2).map(|v| match v {
75        b'0'..=b'9' => Ok(v - b'0'),
76        b'a'..=b'f' => Ok(v - b'a' + 10),
77        b'A'..=b'F' => Ok(v - b'A' + 10),
78        _ => Err(HexParseError::InvalidChar),
79    });
80
81    let mut decoded = [0u8; N];
82    for byte in decoded.iter_mut() {
83        // These `unwrap` calls are okay because the length was checked above
84        let high: u8 = data.next().unwrap()?;
85        let low: u8 = data.next().unwrap()?;
86        *byte = (high << 4) + low;
87    }
88
89    Ok(decoded)
90}