1use alloc::string::String;
2use core::fmt::Write;
3
4use thiserror::Error;
5
6pub fn bytes_to_hex_string<const N: usize>(data: [u8; N]) -> String {
8 let mut s = String::with_capacity(N + 2);
9
10 s.push_str("0x");
11 for byte in data.iter() {
12 write!(s, "{byte:02x}").expect("formatting hex failed");
13 }
14
15 s
16}
17
18#[derive(Debug, Error)]
20pub enum HexParseError {
21 #[error("expected hex data to have length {expected}, including the 0x prefix, found {actual}")]
22 InvalidLength { expected: usize, actual: usize },
23 #[error("hex encoded data must start with 0x prefix")]
24 MissingPrefix,
25 #[error("hex encoded data must contain only characters [0-9a-fA-F]")]
26 InvalidChar,
27 #[error("hex encoded values of a Digest must be inside the field modulus")]
28 OutOfRange,
29}
30
31pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseError> {
33 let expected: usize = (N * 2) + 2;
34 if value.len() != expected {
35 return Err(HexParseError::InvalidLength { expected, actual: value.len() });
36 }
37
38 if !value.starts_with("0x") {
39 return Err(HexParseError::MissingPrefix);
40 }
41
42 let mut data = value.bytes().skip(2).map(|v| match v {
43 b'0'..=b'9' => Ok(v - b'0'),
44 b'a'..=b'f' => Ok(v - b'a' + 10),
45 b'A'..=b'F' => Ok(v - b'A' + 10),
46 _ => Err(HexParseError::InvalidChar),
47 });
48
49 let mut decoded = [0u8; N];
50 for byte in decoded.iter_mut() {
51 let high: u8 = data.next().unwrap()?;
53 let low: u8 = data.next().unwrap()?;
54 *byte = (high << 4) + low;
55 }
56
57 Ok(decoded)
58}