use alloc::vec::Vec;
use miden_core::Felt;
#[cfg(any(test, feature = "testing"))]
use miden_core::Word;
use miden_core::utils::bytes_to_packed_u32_elements;
use miden_protocol::utils::{HexParseError, hex_to_bytes};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Keccak256Output([u8; 32]);
impl Keccak256Output {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn from_hex(hex_str: &str) -> Result<Self, HexParseError> {
let bytes: [u8; 32] = hex_to_bytes(hex_str)?;
Ok(Self(bytes))
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn to_elements(&self) -> Vec<Felt> {
bytes_to_packed_u32_elements(&self.0)
}
#[cfg(any(test, feature = "testing"))]
pub fn to_words(&self) -> [Word; 2] {
let elements = self.to_elements();
let lo: [Felt; 4] = elements[0..4].try_into().expect("to_elements returns 8 felts");
let hi: [Felt; 4] = elements[4..8].try_into().expect("to_elements returns 8 felts");
[Word::new(lo), Word::new(hi)]
}
}
impl From<[u8; 32]> for Keccak256Output {
fn from(bytes: [u8; 32]) -> Self {
Self::new(bytes)
}
}