Repository navigation
x86-64 vg_rsa_check_key: word-level remainders for balanced keys - #4971
Merged
Merged
Conversation
… Arm and AArch64 key checks) Byte-identical to Proof/Rsa/MontCheckMath.lean on claude/ppc64le-rsa-key-checks-mul (a05ab4a). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Under a public guard (crt_values' WordDivide guard: w even, n_len = 8w = 2 p_len, p_len = q_len, n's top bit set) check d mod (X - 1) = dX and q qInv mod p = 1 with the proven reciprocal long division instead of the bit-serial reductions; other shapes keep the bit-serial path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
alex
marked this pull request as ready for review
November 11, 2026 20:36
alex
pushed a commit
that referenced
this pull request
Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHHgwmxoKvRUNy64bKd
alex
pushed a commit
that referenced
this pull request
Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHHgwmxoKvRUNy64bKd
This was referenced Nov 11, 2026
Merged
Merged
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
vg_rsa_check_keyon x86-64 spent most of its time (about 87%, and 56% ofPrivateKey::from_primes) in bit-serial restoring division:e·d mod (p−1),e·d mod (q−1)andq·qInv mod p.vg_rsa_crt_valuesalready has a proven, reciprocal-normalized word-level long division (WordDivide) for keys of the usual shape. This PR reuses it incheck_keyunder the same public guard:weven,n_len = 8w = 2·p_len,p_len = q_len, andn's top bit set.Under the guard, the checks become:
dX < X − 1, thend mod (X − 1) = dXbyWordDivide, thene·dX ≡ 1 (mod X − 1). Thee·dXcheck keeps its one-word bit-serial remainder, which is cheap. GivendX < X − 1ande·dX ≡ 1,d mod (X − 1) = dXholds exactly whene·d ≡ 1(mod_swap,keyValid_word).qInv < p, thenq·qInv mod p = 1, with the product's lowwwords divided byWordDivide.WordDividenormalizes by setting the divisor's top bit. A factor of such annalready has that bit set wheneverp·q = n(factors_guard); whenp·q ≠ n, the mask is clear anyway.Keys of any other shape keep the bit-serial path (
bitChecks, unchanged). Both branches are proven to returnkeyValid, and both are constant time.Constant time: the public data (
KPub) is only the workspace, pointers and lengths, andn;d,p − 1,q − 1,qInvand every product and remainder are secret on both paths. The branch reads onlyw,n_len,p_len,q_lenandn's first byte (guard_ct).Tests:
tests/wycheproof/rsa_keys.rsloads every two-prime key of Wycheproof'srsa_*_test.jsonthroughfrom_primesandfrom_crt, with the ±1 corruptions ofd,dP,dQandqInv. The 2048-, 3072- and 4096-bit keys take the word path; the two-prime keys ofrsa_oaep_misc_test.jsonwith 388- and 504-byte moduli (w= 49 and 63, odd) take the bit-serial one.Other changes:
8(w+2)in the header (strideInit), whichWordDividereads.sDQmoves fromsFn 12tosFn 15, becausesFn 12is the stride slotWordDivideuses. Nothing else usedsFn 15.Trust
No change to
TCB/orSpec/. The new Lean modules areProof/Rsa/X86_64/KeyWord.lean(correctness) andKeyWordCT.lean(constant time). For constant time,KPubnow carriesn_len,n's pointer andn's bytes, all public, which the guard reads. The rest is edits to the check_key proofs (KeyMain,KeyCTDefs,KeyCTSetup,KeyCTCode,KeyCTMain).Measurements
Instructions per
from_primescall (callgrind, Wycheproof keys,VG_CPU_FEATURES=none; the default features give the same numbers to within 1%). "Before" is main before #4964, and "after" is this branch merged with main.After this,
vg_rsa_crt_valuesis the largest part, mostly its divsteps inverse.Benchmarks
Run 43094222077 (x86_64, module
rsa, groupsrsa_from_.*, baseade85059= main, with OpenSSL and aws-lc-rs). All sevenVG_CPU_FEATURESconfigurations ran, and none is slower. The runner's default configuration:rsa_from_components/256rsa_from_components/384rsa_from_components/512rsa_from_crt/256rsa_from_crt/384rsa_from_crt/512rsa_from_primes/256rsa_from_primes/384rsa_from_primes/512With
VG_CPU_FEATURES=none,from_primesis 52–58% faster andfrom_crt70–76% faster.from_primesis now 1.3–1.5× faster than OpenSSL's. OpenSSL'sfrom_crt(1.5–2.7 µs) does not check the key asvg_rsa_check_keydoes, so that row is not like for like; aws-lc-rs, which does, is 7–8× slower.from_componentsspends its time recovering the primes, which this PR does not change.Checks
WordDividenow needsw ≥ 6, which check_key always has (w ≥ 8).Emit.leanwrite finished with exit status 0, andEmit.lean --checkthen exits 0; onlycrates/verified-garbage-asm-x86_64rsachanged.cargo testpasses in full on x86-64 with default features, and the RSA tests pass withVG_CPU_FEATURES=none.check_lean_public_imports.pyon the edited modules pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh