/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sha3-0.10.8/src/state.rs
Line | Count | Source |
1 | | use core::convert::TryInto; |
2 | | |
3 | | const PLEN: usize = 25; |
4 | | const DEFAULT_ROUND_COUNT: usize = 24; |
5 | | |
6 | | #[derive(Clone)] |
7 | | pub(crate) struct Sha3State { |
8 | | pub state: [u64; PLEN], |
9 | | round_count: usize, |
10 | | } |
11 | | |
12 | | impl Default for Sha3State { |
13 | 103k | fn default() -> Self { |
14 | 103k | Self { |
15 | 103k | state: [0u64; PLEN], |
16 | 103k | round_count: DEFAULT_ROUND_COUNT, |
17 | 103k | } |
18 | 103k | } |
19 | | } |
20 | | |
21 | | impl Sha3State { |
22 | 0 | pub(crate) fn new(round_count: usize) -> Self { |
23 | 0 | Self { |
24 | 0 | state: [0u64; PLEN], |
25 | 0 | round_count, |
26 | 0 | } |
27 | 0 | } |
28 | | |
29 | | #[inline(always)] |
30 | 167k | pub(crate) fn absorb_block(&mut self, block: &[u8]) { |
31 | 167k | debug_assert_eq!(block.len() % 8, 0); |
32 | | |
33 | 3.01M | for (b, s) in block.chunks_exact(8).zip(self.state.iter_mut()) { |
34 | 3.01M | *s ^= u64::from_le_bytes(b.try_into().unwrap()); |
35 | 3.01M | } |
36 | | |
37 | 167k | keccak::p1600(&mut self.state, self.round_count); |
38 | 167k | } |
39 | | |
40 | | #[inline(always)] |
41 | 218k | pub(crate) fn as_bytes(&self, out: &mut [u8]) { |
42 | 4.23M | for (o, s) in out.chunks_mut(8).zip(self.state.iter()) { |
43 | 4.23M | o.copy_from_slice(&s.to_le_bytes()[..o.len()]); |
44 | 4.23M | } |
45 | 218k | } |
46 | | |
47 | | #[inline(always)] |
48 | 206k | pub(crate) fn permute(&mut self) { |
49 | 206k | keccak::p1600(&mut self.state, self.round_count); |
50 | 206k | } |
51 | | } |