Repository navigation
Decrypt ZipCrypto with the CRC-32 instructions on 64-bit ARM - #9
Merged
Merged
Conversation
Each byte of ZipCrypto updates its keys with two steps of ZIP's CRC-32, and the next byte's keystream depends on them, so decryption is one long chain. ARMv8's CRC32B is exactly that step, and takes about three cycles where the table lookup takes a load and three ALU operations. Where the processor has it (checked at run time), the payload now decrypts through it; elsewhere the portable loop runs as before (x86's CRC32 instruction is CRC-32C, a different polynomial). A new test holds the two to the same plain bytes and final keys. 8 MiB ZipCrypto entry: M4 162 -> 232 MB/s; i7-8700K unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
What
Each byte of ZipCrypto updates its three keys with two steps of ZIP's CRC-32 (on
key0with the plain byte, onkey2with the top byte ofkey1). The next byte's keystream comes fromkey2, so decryption is one long chain: about 27 cycles a byte on an M4, roughly 160 MB/s.ARMv8's
CRC32Bis one byte of the reflected CRC-32 that ZIP uses, without the inversions at either end, which is exactly ZipCrypto's key update. It takes about three cycles. The table lookup LLVM builds from the bitwise loop takes an XOR, an AND, a load and a shifted XOR. Two of those come off the chain on every byte.accel::zipcrypto_decryptdecrypts the payload withCRC32Bwhen the processor has it (is_aarch64_feature_detected!("crc"), true on every Apple core). It appends to the output and leaves the keys where the data ends.CRC32instruction computes CRC-32C, a different polynomial.Measured (fastest round,
examples/bench.rs)On the M4 that's about 19 cycles a byte, down from 27. What's left of the chain is the keystream multiply, the
key1multiply-add and the twoCRC32Bs.Correctness
zipcrypto_decryption_agrees_with_and_without_crc_instructionsholds the instruction path to the portablecrypto_steploop. It covers random keys and 0–4099 bytes, plus the final keys and appending after existing output.key1 >> 16instead ofkey1 >> 24to the second CRC on purpose fails both tests.cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo testpass on aarch64-apple-darwin and x86_64-linux.🤖 Generated with Claude Code