Skip to content

Run WinZip AES-CTR on the processor's AES instructions - #6

Merged
xbmc4lyfe merged 1 commit into
mainfrom
claude/aes-hw
Nov 6, 2026
Merged

xbmc4lyfe merged 1 commit into
mainfrom
claude/aes-hw

Conversation

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator

What

zipview no longer forbids unsafe. Its inflate backend (zlib-rs) already relies on unsafe, and the processor's AES instructions can't be used without it.

  • New src/accel.rs. It runs the WinZip AES-CTR keystream on the CPU's AES instructions:
    • 64-bit ARM: AESE + AESMC, and AESE + XOR for the last round.
    • x86-64: AES-NI, AESENC and AESENCLAST.
    • Eight counter blocks go through the rounds together, so the AES unit is never waiting on one block.
  • Runtime fallback. The instructions are checked for at run time with is_aarch64_feature_detected!("aes") / is_x86_feature_detected!("aes"). Without them, the existing T-table code runs.
  • Where unsafe is allowed. It is confined to that module: the call into the #[target_feature] function after the check, plus 16-byte loads and stores inside arrays and chunks of known size. Each has a SAFETY comment.
  • Lints. #![forbid(unsafe_code)] becomes #![deny(unsafe_op_in_unsafe_fn)] plus clippy::undocumented_unsafe_blocks.
  • rust-version. 1.85 → 1.87: from 1.87, intrinsics are safe to call inside a #[target_feature] function, so only the pointer loads and stores need unsafe blocks.
  • Docs. The README's Design section and the crate docs say where the unsafe is and why.

Measured (fastest round, examples/bench.rs)

case M4 Air M5 Air (load ~180) i7-8700K (load ~25)
aes128-ctr-8M 921 → 13,984 MB/s 415 → 6,386 MB/s 241–270 → 2,259–2,449 MB/s
aes256-ctr-8M 677 → 12,991 MB/s 304 → 5,695 MB/s 179–195 → 2,086–2,227 MB/s
aes256-entry-1M (derive + decrypt) 2.03 → 0.58 ms 4.71 → 1.68 ms 4.57–6.57 → 1.17–1.30 ms

What remains of a whole entry's time is PBKDF2, which the next change moves onto the SHA-1 instructions.

Correctness

  • ctr_matches_the_bytewise_reference checks decrypt_winzip_ctr against the byte-wise reference for all three key sizes. That covers a 1 MiB + 4133-byte buffer, every length 0–199, and lengths around 4 KiB and 64 KiB, so it exercises the hardware path wherever the CPU has AES (Apple M-series, the i7-8700K, GitHub's runners). It now also checks the portable T-table path directly.
  • Breaking the last round on purpose fails the test on both aarch64 and x86-64.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass on aarch64-apple-darwin and x86_64-linux.

🤖 Generated with Claude Code

zipview no longer forbids unsafe: its inflate backend already relies on it,
and the processor's AES instructions need it. src/accel.rs runs the CTR
keystream on ARMv8 AES (AESE/AESMC) or x86 AES-NI (AESENC/AESENCLAST), eight
counter blocks at a time so the AES unit never waits on a single block,
after checking for the instructions at run time; without them the T-table
code runs as before. The unsafe is limited to that module's intrinsic calls
and 16-byte loads and stores, each with a SAFETY comment, which
clippy::undocumented_unsafe_blocks now requires. rust-version goes to 1.87,
where intrinsics are safe to call inside a target_feature function. The
reference test now checks both the public function and the portable path.
AES-128-CTR: M4 921 -> 13984 MB/s, M5 415 -> 6386, i7-8700K 241 -> 2449.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T19:51:54.004593Z 43d589c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@xbmc4lyfe
xbmc4lyfe merged commit d4611d8 into main Nov 6, 2026
3 checks passed
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.

1 participant