Skip to content

keccak: avoid materializing round constants array - #128

Closed
shuklaayush wants to merge 1 commit into
RustCrypto:masterfrom
shuklaayush:master
Closed

keccak: avoid materializing round constants array#128
shuklaayush wants to merge 1 commit into
RustCrypto:masterfrom
shuklaayush:master

Conversation

@shuklaayush

@shuklaayush shuklaayush commented Aug 18, 2026

Copy link
Copy Markdown
  • Switch from eager array .map() to lazy iterator mapping for round constants
  • Avoid materializing a temporary [L; ROUNDS], reducing stack usage

@newpavlov

Copy link
Copy Markdown
Member

With enabled optimization this code gets optimized into a pre-computed const table, so this change is not necessary.

@newpavlov newpavlov closed this Aug 19, 2026
@shuklaayush

Copy link
Copy Markdown
Author

@newpavlov thanks for the reply! the conversions are constant-folded into a table, but the owned array is still materialized in the optimized aarch64 codegen

before:

sub sp, sp, #0x190

ldp q0, q1, [x8, #0x80]
stp q0, q1, [sp, #0xf0]
ldp q0, q1, [x8, #0xa0]
stp q0, q1, [sp, #0x110]
ldp q0, q1, [x8, #0x40]
stp q0, q1, [sp, #0xb0]
ldp q0, q1, [x8, #0x60]
stp q0, q1, [sp, #0xd0]
ldp q0, q1, [x8]
stp q0, q1, [sp, #0x70]
ldp q0, q1, [x8, #0x20]
stp q0, q1, [sp, #0x90]

after:

sub sp, sp, #0xc0

adrp x9, ...
add  x9, x9, ...
ldr  x9, [x9, x8]

on my macbook air m3, running

RUSTFLAGS='--cfg keccak_backend="soft"' \
cargo +nightly bench -p keccak --bench mod -- keccak_f1600 --exact

goes from 172.63 ns before to 145.93 ns after

@newpavlov

Copy link
Copy Markdown
Member

Interesting. I opened #129 to resolve it in a different and hopefully less fragile fashion.

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