summaryrefslogtreecommitdiffstats
path: root/third_party/rust/sha3/src/state.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/sha3/src/state.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/sha3/src/state.rs')
-rw-r--r--third_party/rust/sha3/src/state.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/third_party/rust/sha3/src/state.rs b/third_party/rust/sha3/src/state.rs
new file mode 100644
index 0000000000..1ba9f11bd1
--- /dev/null
+++ b/third_party/rust/sha3/src/state.rs
@@ -0,0 +1,51 @@
+use core::convert::TryInto;
+
+const PLEN: usize = 25;
+const DEFAULT_ROUND_COUNT: usize = 24;
+
+#[derive(Clone)]
+pub(crate) struct Sha3State {
+ pub state: [u64; PLEN],
+ round_count: usize,
+}
+
+impl Default for Sha3State {
+ fn default() -> Self {
+ Self {
+ state: [0u64; PLEN],
+ round_count: DEFAULT_ROUND_COUNT,
+ }
+ }
+}
+
+impl Sha3State {
+ pub(crate) fn new(round_count: usize) -> Self {
+ Self {
+ state: [0u64; PLEN],
+ round_count,
+ }
+ }
+
+ #[inline(always)]
+ pub(crate) fn absorb_block(&mut self, block: &[u8]) {
+ debug_assert_eq!(block.len() % 8, 0);
+
+ for (b, s) in block.chunks_exact(8).zip(self.state.iter_mut()) {
+ *s ^= u64::from_le_bytes(b.try_into().unwrap());
+ }
+
+ keccak::p1600(&mut self.state, self.round_count);
+ }
+
+ #[inline(always)]
+ pub(crate) fn as_bytes(&self, out: &mut [u8]) {
+ for (o, s) in out.chunks_mut(8).zip(self.state.iter()) {
+ o.copy_from_slice(&s.to_le_bytes()[..o.len()]);
+ }
+ }
+
+ #[inline(always)]
+ pub(crate) fn permute(&mut self) {
+ keccak::p1600(&mut self.state, self.round_count);
+ }
+}