1use alloc::vec::Vec;
2
3use miden_core::Felt;
4#[cfg(any(test, feature = "testing"))]
5use miden_core::Word;
6use miden_core::utils::bytes_to_packed_u32_elements;
7use miden_protocol::utils::{HexParseError, hex_to_bytes};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Keccak256Output([u8; 32]);
15
16impl Keccak256Output {
17 pub fn new(bytes: [u8; 32]) -> Self {
19 Self(bytes)
20 }
21
22 pub fn from_hex(hex_str: &str) -> Result<Self, HexParseError> {
26 let bytes: [u8; 32] = hex_to_bytes(hex_str)?;
27 Ok(Self(bytes))
28 }
29
30 pub fn as_bytes(&self) -> &[u8; 32] {
32 &self.0
33 }
34
35 pub fn to_elements(&self) -> Vec<Felt> {
38 bytes_to_packed_u32_elements(&self.0)
39 }
40
41 #[cfg(any(test, feature = "testing"))]
46 pub fn to_words(&self) -> [Word; 2] {
47 let elements = self.to_elements();
48 let lo: [Felt; 4] = elements[0..4].try_into().expect("to_elements returns 8 felts");
49 let hi: [Felt; 4] = elements[4..8].try_into().expect("to_elements returns 8 felts");
50 [Word::new(lo), Word::new(hi)]
51 }
52}
53
54impl From<[u8; 32]> for Keccak256Output {
55 fn from(bytes: [u8; 32]) -> Self {
56 Self::new(bytes)
57 }
58}