1use zeroize::{Zeroize, ZeroizeOnDrop};
6
7use crate::duplex_sponge::{DuplexSponge, Permutation};
8
9pub type Keccak = DuplexSponge<AlignedKeccakF1600>;
12
13fn transmute_state(st: &mut AlignedKeccakF1600) -> &mut [u64; 25] {
14 unsafe { &mut *std::ptr::from_mut::<AlignedKeccakF1600>(st).cast::<[u64; 25]>() }
15}
16
17#[derive(Clone, Zeroize, ZeroizeOnDrop)]
21#[repr(align(8))]
22pub struct AlignedKeccakF1600([u8; 200]);
23
24impl Permutation for AlignedKeccakF1600 {
25 type U = u8;
26 const N: usize = 136 + 64;
27 const R: usize = 136;
28
29 fn new(tag: [u8; 32]) -> Self {
30 let mut state = Self::default();
31 state.0[Self::R..Self::R + 32].copy_from_slice(&tag);
32 state
33 }
34
35 fn permute(&mut self) {
36 keccak::f1600(transmute_state(self));
37 }
38}
39
40impl Default for AlignedKeccakF1600 {
41 fn default() -> Self {
42 Self([0u8; Self::N])
43 }
44}
45
46impl AsRef<[u8]> for AlignedKeccakF1600 {
47 fn as_ref(&self) -> &[u8] {
48 &self.0
49 }
50}
51
52impl AsMut<[u8]> for AlignedKeccakF1600 {
53 fn as_mut(&mut self) -> &mut [u8] {
54 &mut self.0
55 }
56}