Skip to content

keccak: extract Keccak1600 struct with CPU feature detection#107

Open
tarcieri wants to merge 3 commits intomasterfrom
keccak/keccak1600-struct
Open

keccak: extract Keccak1600 struct with CPU feature detection#107
tarcieri wants to merge 3 commits intomasterfrom
keccak/keccak1600-struct

Conversation

@tarcieri
Copy link
Member

Extracts a struct to hold the state for Keccak-p1600/Keccak-f1600, along with a CPU feature detection InitToken which is queried at the time the state is initialized.

The previous p1600/f1600 functions with CPU feature detection support have been factored onto this struct, leaving the software implementation available unconditionally as part of the public API, and avoiding performing CPU feature detection on each invocation of the permutation.

It looks like it should be possible to slot a struct like this into something like Sha3HasherCore in place of its current state array.

@tarcieri tarcieri requested a review from newpavlov February 14, 2026 00:18
Extracts a struct to hold the state for Keccak-p1600/Keccak-f1600, along
with a CPU feature detection `InitToken` which is queried at the time
the state is initialized.

The previous `p1600`/`f1600` functions with CPU feature detection
support have been factored onto this struct, leaving the software
implementation available unconditionally as part of the public API, and
avoiding performing CPU feature detection on each invocation of the
permutation.

It looks like it should be possible to slot a struct like this into
something like `Sha3HasherCore` in place of its current `state` array.
@tarcieri tarcieri force-pushed the keccak/keccak1600-struct branch from 2c5b8f6 to 55ba965 Compare February 14, 2026 00:19
@tarcieri
Copy link
Member Author

Though this is ARMv8-only for now (but still works even in a soft-only config), there are assembly implementations of other backends we could potentially support from XKCP where the current ASM is sourced from, like one for AVX2: https://github.com/XKCP/XKCP/blob/716f007dd73ef28d357b8162173646be574ad1b7/lib/low/KeccakP-1600/AVX2/KeccakP-1600-AVX2.s

Though yes, ideally we would have intrinsics (and if we did, could potentially get rid of the cfg gating and have it Just Work)

@tarcieri tarcieri force-pushed the keccak/keccak1600-struct branch from c68f815 to f44516c Compare February 14, 2026 21:54
@tarcieri tarcieri force-pushed the keccak/keccak1600-struct branch from 8866aec to 83643f0 Compare February 14, 2026 22:33
Copy link
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this struct you still branch on each processed block, which prevents potential optimizations like using permuted data in registers to perform XOR without round-tripping through stack. As I wrote in the previous issue we probably need a callback-based API similar to the backend traits in cipher (but I think we could simplify it significantly).

But I guess this PR is a fine starting point.


/// Extract the state array.
#[must_use]
pub fn into_inner(self) -> [u64; PLEN] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure it's worth to duplicate the From impl with this method.

/// ```toml
/// # In .cargo/config.toml
/// [build]
/// rustflags = ['--cfg', 'keccak_backend="armv8_asm"']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to put this to the crate docs/README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants