permutation_xoodoo/
state.rs

1//! Xoodoo permutation state struct.
2
3use crypto_permutation::PermutationState;
4
5const LEN: usize = 12;
6type StateRepresentation = [u32; LEN];
7
8/// State for the Xoodoo permutation. 48 bytes, internally represented by 12
9/// `u32`s in little endian encoding.
10#[derive(Clone)]
11#[cfg_attr(feature = "debug", derive(Debug, PartialEq))]
12pub struct XoodooState {
13    state: StateRepresentation,
14}
15
16/// Writer into the keccak permutation state.
17///
18/// Does nothing fancy except for little-endian to native-endian conversion.
19type CopyWriter<'a> = crypto_permutation::io::le_uint_slice_writer::LeU32SliceWriter<'a>;
20/// Writer that xors into the keccak permutation state.
21///
22/// Does nothing fancy except for little-endian to native-endian conversion.
23type XorWriter<'a> = crypto_permutation::io::le_uint_slice_writer::LeU32SliceXorWriter<'a>;
24/// Reader that reads from the keccak permutation state and outputs it's bytes
25/// in little endian order.
26type StateReader<'a> = crypto_permutation::io::le_uint_slice_reader::LeU32SliceReader<'a>;
27
28impl Default for XoodooState {
29    fn default() -> Self {
30        Self { state: [0; LEN] }
31    }
32}
33
34impl core::ops::BitXorAssign<&Self> for XoodooState {
35    fn bitxor_assign(&mut self, rhs: &Self) {
36        for (self_chunk, other_chunk) in self.get_state_mut().iter_mut().zip(rhs.get_state().iter())
37        {
38            *self_chunk ^= *other_chunk;
39        }
40    }
41}
42
43impl PermutationState for XoodooState {
44    type CopyWriter<'a> = CopyWriter<'a>;
45    type Representation = StateRepresentation;
46    type StateReader<'a> = StateReader<'a>;
47    type XorWriter<'a> = XorWriter<'a>;
48
49    const SIZE: usize = 48;
50
51    fn from_state(state: Self::Representation) -> Self {
52        Self { state }
53    }
54
55    fn get_state(&self) -> &Self::Representation {
56        &self.state
57    }
58
59    fn get_state_mut(&mut self) -> &mut Self::Representation {
60        &mut self.state
61    }
62
63    fn reader<'a>(&'a self) -> Self::StateReader<'a> {
64        StateReader::new(self.get_state())
65    }
66
67    fn copy_writer<'a>(&'a mut self) -> Self::CopyWriter<'a> {
68        CopyWriter::new(self.get_state_mut())
69    }
70
71    fn xor_writer<'a>(&'a mut self) -> Self::XorWriter<'a> {
72        XorWriter::new(self.get_state_mut())
73    }
74}