Skip to content

perf(aes): encrypt AES-CTR counter blocks in batches - #1203

Merged
Pr0methean merged 1 commit into
zip-rs:masterfrom
Slurp9187:aes-ctr-batch
Oct 27, 2026
Merged

Pr0methean merged 1 commit into
zip-rs:masterfrom
Slurp9187:aes-ctr-batch

Conversation

@Slurp9187

@Slurp9187 Slurp9187 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

AesCtrZipKeyStream::crypt_in_place encrypts one counter block per encrypt_block call and then XORs it in. The counter blocks are independent, so the aes backend 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_blocks call, XORs them in, and zeroizes the batch. Partial blocks at either end still go through buffer as 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:

before after
encrypt 8 MiB 631 MiB/s 1.14 GiB/s
decrypt 8 MiB 699 MiB/s 1.40 GiB/s
encrypt 64 KiB 121 MiB/s 133 MiB/s
decrypt 64 KiB 125 MiB/s 137 MiB/s

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:

before after
encrypt 8 MiB 64 MiB/s 224 MiB/s
decrypt 8 MiB 62 MiB/s 231 MiB/s
encrypt 64 KiB 45 MiB/s 90 MiB/s
decrypt 64 KiB 45 MiB/s 92 MiB/s

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:

  • New test aes_ctr::tests::batched_keystream_matches_block_by_block compares 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.
  • Archives written by 9.1.2 read back with this change, and the other way round (AES-128/192/256, 0 B to 1 MiB, uneven write and read sizes).
  • The full CI matrix and the fuzz jobs pass on my fork.

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 through encrypt_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-features reports field_reassign_with_default at src/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.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

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
Pr0methean enabled auto-merge October 27, 2026 18:12
Comment thread benches/aes.rs Dismissed
@Pr0methean
Pr0methean added this pull request to the merge queue Oct 27, 2026
@Pr0methean Pr0methean added this to the 9.1.3 milestone Oct 27, 2026
Merged via the queue into zip-rs:master with commit 20e809a Oct 27, 2026
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants