Repository navigation
Reduce ARMv7 SM4 S-box inversions with complemented intermediates - #3035
Merged
reaperhulk merged 1 commit intoOct 21, 2026
Merged
Conversation
reaperhulk
force-pushed
the
codex/arm-sm4-sbox-polarity
branch
from
October 21, 2026 17:52
67984fc to
3cb6e19
Compare
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 21, 2026
reaperhulk
force-pushed
the
codex/arm-sm4-sbox-polarity
branch
from
October 21, 2026 19:03
3cb6e19 to
b645b90
Compare
reaperhulk
changed the base branch from
main
to
codex/arm-sm4-batched-cbc-decrypt
October 21, 2026 19:03
This was referenced Oct 21, 2026
reaperhulk
added this pull request to stack #3045
October 21, 2026 19:44
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.
ARMv7's bitsliced SM4 S-box materializes 13 inversions while evaluating its Boolean circuit. Carry selected intermediates complemented and use OR gates through De Morgan's law, leaving nine explicit inversions. Reorder and allocate the equivalent circuit to reduce the generated S-box from 205 to 199 ARM instructions. The core is shared by ECB and every mode, including serial CBC/CFB encryption and OFB.
The existing exhaustive kernel proof checks the generated instructions against all 256 S-box inputs. All functional, constant-time, ABI and memory-frame proofs remain intact. A small SM4 allocator extension replaces selected AND gates with OR gates; its output is explicitly written out and checked against the compiler, and the instructions themselves are verified. No specification or TCB changes, lookup tables, or new ISA requirements.
Validation: full Lean build (22450 jobs), scoped emitter generation/audits and generated-code checks for all five SM4 registrations, public-import checks, static checks, all 19 SM4 integration tests, ARM/Thumb assembly compilation, Rustfmt and ARM Clippy. Native ARM/Thumb benchmark passed, paired against
766b22d169ef8dbeed285e031117efa29eeb2d1don Neoverse N2. Every measured mode and size improved by 1.3–1.7%.16 KiB timings in microseconds (lower is better):
The serial modes remain substantially slower than OpenSSL. The first comparison predates #3033/#3036/#3037. After those merged, this PR was rebased and regenerated for their inlined cipher cores; the new comparison below covers those changed implementations. This PR is now stacked on #3034, which batches CBC decryption; the generated code includes both CBC improvements.
Full CI passed, including native ARM/Thumb tests, Lean, emitter audits and static checks.
Post-rebase CFB/OFB benchmark, paired against main
7755b984f60deb7a4f27e6e72458b400bb580ef0. Only CFB/OFB ran: they gained newly generated inlined S-box code after their parent implementations merged. ECB/CTR code and its earlier measurements are unchanged.16 KiB timings in microseconds:
The new inlined implementations improve by 1.7–1.8% at 16 KiB; the serial gap remains about 6.3 times OpenSSL. Full post-rebase Lean build (22428 jobs), scoped emitter audits/check, ARM/Thumb assembly compilation, Rustfmt and ARM Clippy pass. Updated full CI passed.
Final integration: regenerated both CBC directions after #3040 merged and #3034 was restacked on it. Full Lean build (22450 jobs), all five SM4 emitter/audit checks, ARM/Thumb compilation, all 19 SM4 tests, public-import checks, Rustfmt and ARM Clippy pass. CBC-only comparison against the exact #3034 parent
634f60aaebaf7d9d45a452f7980653692b5e3e08passed on both ARM and Thumb. Updated full CI passed, including Lean, emitter audits/checks, ARM/Thumb tests, static checks and coverage. Earlier ECB/CTR/CFB/OFB code and measurements are unchanged.Final combined CBC timings at 16 KiB, in microseconds:
All measured CBC sizes improved by 1.4–1.9%. CBC decryption also beats OpenSSL at 1 KiB (ARM 7.74 vs 8.47 µs; Thumb 7.79 vs 8.60 µs). At 64 bytes it remains slower (2.02 vs 1.42/1.44 µs); serial CBC encryption remains about 6.3 times slower at 16 KiB.