Repository navigation
perf(aes): encrypt AES-CTR counter blocks in batches - #1203
Merged
Merged
Conversation
AesCtrZipKeyStream::crypt_in_place encrypted one counter block per encrypt_block call. For whole blocks it now fills up to 32 counter blocks and encrypts them in one encrypt_blocks call, which lets the aes backend process several blocks at once (8 with AES-NI or ARMv8, 4 with the 64-bit software backend). The keystream is unchanged. The batch is zeroized after use. Adds a test comparing the batched keystream with a block-by-block reference across call splits, and a benchmark for AES-256 entries.
Pr0methean
approved these changes
Oct 27, 2026
Pr0methean
enabled auto-merge
October 27, 2026 18:12
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AesCtrZipKeyStream::crypt_in_placeencrypts one counter block perencrypt_blockcall and then XORs it in. The counter blocks are independent, so theaesbackend could process several at once (8 with AES-NI or ARMv8), but one-block calls don't let it.This change handles whole blocks in batches of up to 32: it fills the counter blocks, encrypts them with one
encrypt_blockscall, XORs them in, and zeroizes the batch. Partial blocks at either end still go throughbufferas before. The keystream is unchanged.Benchmark (new
benches/aes.rs,cargo bench --bench aes --features aes-crypto), one Stored AES-256 entry, before and after this change on 2431276. Measured on an AMD Ryzen 9 9950X3D under Windows, so absolute numbers will be lower on most machines, and the ratio depends on the CPU's AES-NI throughput:At 64 KiB most of the time is the PBKDF2 key derivation, which takes about 0.40 ms per entry on this machine (timed on its own).
With the software backend (
RUSTFLAGS='--cfg aes_backend="soft"', what machines without AES instructions use), same machine:The software backend is bitsliced and encrypts 4 blocks per call on 64-bit targets (2 on 32-bit) for about the cost of one, so one-block calls wasted most of its work.
Testing:
aes_ctr::tests::batched_keystream_matches_block_by_blockcompares the output with a block-at-a-time reference for AES-128/192/256 over 1,573 bytes, split six ways across calls. Removing the counter increment in the batch makes it fail, along with the existing 40-byte vectors.Notes:
The batch array is 512 bytes on the stack.
With the opt-in VAES-512 backend (
--cfg aes_backend="avx512", 64 blocks at a time), a 32-block batch goes throughencrypt_tail_blocks, which does the same work as before: no gain there, and no loss. The default x86 backend is AES-NI.cargo +nightly clippy --all-targets --no-default-featuresreportsfield_reassign_with_defaultatsrc/extra_fields/extra_field.rs:369. That is on master as well and isn't touched here.The PR title must conform to Conventional Commits and start with one of the types specified by the Angular convention.
Need help on this PR? Tag
@codesmith-botwith what you need.