Repository navigation
Move to aes 0.9 for hardware AES on ARM and add a keyed Ctr256 - #4
Merged
Merged
Conversation
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.
Rust crates that depend on warpcrypto get software AES on ARM today:
--cfg aes_armv8in.cargo/config.tomlonly applies when building inside this repo (so the Python wheels have it), not to dependents. aes 0.9 turns on the ARMv8 backend by default (aes 0.9.0 changelog: "Enable ARMv8 backend by default", picked at runtime viacpufeatures), so this moves to it. While there, it adds a keyed CTR type.Changes
aes 0.9 (
aes = ">=0.9.2, <0.9.3")tests/rust.rspass untouched.ctr256at 512 KiB on x86 dropped from 5920 to 2287 MB/s with this crate's CTR loop (0.9.3 turned VAES on by default; I haven't confirmed that is the cause) (run 42071050073). So the cap can go when rust-version moves past 1.89, but that CTR slowdown needs a look first..cargo/config.tomlis removed: aes 0.9 has noaes_armv8flag. README updated to match.Ctr256: AES-256-CTR with the key expanded once, for a stream processed piece by piece.Ctr256::new(&key, &iv),apply_keystream(&mut data),iv(),state(). It gives the same bytes as callingctr256on each piece; a test checks this across 18 chunk sizes from 0 to 4096 bytes, most of them not multiples of 16, and three starting counters, including counter carry and wraparound.ctr256itself is unchanged. It holds anAes256Enc(encryption key schedule only) and does not implementClone, so a keystream can't be duplicated by accident.Debugprints nothing secret.Key schedule wiping without aes's
zeroizefeatureIn aes 0.9 the
zeroizefeature wipes the whole cipher on every drop withzeroize_flat_type, one volatile byte store at a time (aes 0.9.2src/lib.rs:434-442; zeroize 1.9src/lib.rs:748-764).Aes256is 960 bytes (the union is sized for the software fallback's key schedule), so every call that sets up a key pays for 960 single-byte stores. That added about 120 ns per call on x86 and about 330 ns on ARM, which made short inputs slower than on master (numbers below, column A).This PR drops that feature and wraps every cipher the crate creates (the per-call functions,
pack_message/unpack_message, the Python bindings andCtr256) in a small privateWiped<C>(ManuallyDrop<C>). ItsDropzeroes the same 960 bytes with volatile machine-word stores, then acompiler_fence(SeqCst). The helper functions take&Wiped<_>, so a cipher that isn't wiped doesn't compile. For comparison, aes 0.8.4 withzeroizewiped only the active variant: 30 × 16-byte volatile stores on AES-NI (ni.rs:191-196,:265-270) or 120 × u64 on the software path (soft.rs:118-119). This wipes every byte the 0.9 feature wiped.The unsafe block, with its reasoning:
Miri runs
tests/rust.rsclean with this in place (with--cfg aes_backend="soft", since Miri can't run the AES intrinsics; the wiped struct is the same 960 bytes): miri job in run 42082454811.Alternative without unsafe (option A): drop the last commit and keep
features = ["zeroize"]. Everything else stays the same; the cost is the byte-wise wipe on short inputs. In the run below, x86 IGE at 16 B goes from 122 MB/s on master to 79,ctr256at 1 KiB from 3122 to 2472. On ARM against the--cfg aes_armv8build that the wheels use, IGE at 1 KiB goes from 1043 to 780 andctr256at 1 KiB from 2534 to 1417. At 128 KiB and up A and B are the same. That branch is at z7vian/WarpCrypto@aes-0.9. If you'd rather not carry unsafe code, take that one.Not changed: no
#[pyfunction]signature or Python behaviour changes (src/python.rsonly swaps how the cipher is built), no public Rust API removed or changed, and no version bump.Ctr256is a new type, so this fits a minor release (2.2.0).Benchmarks
Run 42072506583: GitHub
ubuntu-latest(AMD EPYC 7763) andubuntu-24.04-arm(CPU part 0xd49,aes pmullflags), Rust 1.99.1, 5 interleaved passes, median MB/s. Each variant is built as a separate crate that depends on warpcrypto by path, so this repo's.cargo/config.tomldoesn't apply. That is how other crates see it; the "+--cfg aes_armv8" column is how the Python wheels were built. A = aes 0.9 with aes'szeroize; B = this PR. (The bench built B at the commit before a comment-only change to the SAFETY note.)ARM
--cfg aes_armv8x86
One regression: on ARM,
ctr256at 128 KiB and up is about 7% slower than the--cfg aes_armv8build (5222 → 4858 MB/s at 128 KiB). It shows up in A as well, so it comes from aes 0.9.2's ARM backend, not from the wipe. It affects large CTR buffers on ARM in the Python wheels, i.e. file and CDN downloads in Python callers on ARM machines. IGE and everything else on ARM is the same or faster; Rust dependents on ARM go from software AES to 4–27× faster.Ctr256vs callingctr256per chunk (same stream fed in fixed-size chunks, B, MB/s):ctr256Ctr256ctr256Ctr256CI
The repo's workflow run on my fork (renamed copy,
fail-fast: false, plus the Miri job): run 42082454811. The Rust jobs (1.85 and stable on Linux x86, Linux ARM, macOS and Windows), clippy,cargo publish --dry-run, Miri and the Python matrix pass, exceptbuild (ubuntu-latest, 3.9): it stops at the "Set up Python 3.9" step withThe version '3.9' with architecture 'x64' was not found for Ubuntu 26.04., before any build step runs.build (macos-latest, 3.12)was cancelled after 15 minutes without getting a runner and passed on re-run (attempt 2).