miden_crypto/utils/
mod.rs1use alloc::string::String;
4use core::fmt::{self, Write};
5
6use thiserror::Error;
7
8use super::Word;
9
10mod kv_map;
11
12pub 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
24pub 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
38pub 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#[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
63pub 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 let high: u8 = data.next().unwrap()?;
85 let low: u8 = data.next().unwrap()?;
86 *byte = (high << 4) + low;
87 }
88
89 Ok(decoded)
90}