Repository navigation
AArch64 vg_rsa_crt_values: the inverse's tail by one word division - #4981
Merged
Merged
Conversation
Each word step x := x 2^64 mod m of R2Div (step, stepPub and stepN, so vg_rsa_public_precompute, vg_rsa_private_precompute and every other user of r2Steps): * corrects the 2-by-1 estimate q̂₀ with the next words of x and m (x P and m P in stepN; Knuth's step D3, a 3-by-2 estimate) to q̂ = min(⌊U / D⌋, 2^64 - 1), at most one above the quotient (quot3, quot3N), so m is added back once instead of twice; normN also stores d'ₗ, the next word of m P; * computes t = x 2^64 - q̂ m in place of x in one pass (mulSub), as the sum ~t = ~(x 2^64) + q̂ m with mac's two carries, instead of q̂ m into the accumulator, a subtraction pass and a copy back. For w = 2 (and w = 3 in stepN) the word below x is taken as 0 by a csel on w, so every count, branch and address still depends only on w. The target-independent arithmetic is WordStepNorm's (qhat3_norm, quot3_val, wordStepQ1_val), as on PPC64LE and RISC-V. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
WordStepNorm: qhat3_div and wordStepQ1_div, the 3-by-2 estimate and single add-back for any dividend y < 2^64 m (qhat3_norm and wordStepQ1_val, unchanged, are now their y = x 2^64 cases), and shift_u. WordDiv: the fold of X mod m over its words, for any word width W. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
…ey guard vg_rsa_crt_values and vg_rsa_check_key reduce by p - 1, q - 1 and p a word at a time (wdiv, R2Div.stepU) when wordGuard passes (16 | k, p_len = q_len = k / 2, n's top bit set), and fall back to the bit-serial reductions otherwise. Proofs: functional (wdiv_ok, ckWModOne_ok, cvWordChoice_ok) and constant time (wdiv_ct, wmodOne_ckct, wordChoice_ct). Work in progress: the regenerated AArch64 crate follows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
CvCTMain no longer imports CvMain, so its Mathlib import stays out of the DSA proofs (check_lean_speed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
#4958's vg_rsa_private_crt_precomputed embeds the R2Div step this branch changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
…h64-key-import-words
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
Under wordGuard, the batched inverse leaves out its 15n+10 modular doublings (inverseAtS) and qTail computes x₂ 2^(15n+10) mod p with wdiv; the fallback is unchanged. Regenerates the AArch64 crate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
…port-words # Conflicts: # lean/VerifiedGarbage/Impl/Bignum/AArch64/R2Div.lean # lean/VerifiedGarbage/Proof/Bignum/AArch64/R2dSub.lean
|
This pull request has conflicts with Posted by |
# Conflicts: # crates/verified-garbage-asm-aarch64/assembly/rsa.s # crates/verified-garbage-asm-aarch64/src/rsa.rs # lean/VerifiedGarbage/Proof/Rsa/AArch64/CvCTCode.lean # lean/VerifiedGarbage/Proof/Rsa/AArch64/CvCTMain.lean # lean/VerifiedGarbage/Proof/Rsa/AArch64/CvMain.lean
alex
marked this pull request as ready for review
November 12, 2026 02:03
alex
enabled auto-merge
November 12, 2026 02:08
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.
Follows #4974 (AArch64 key import, merged), whose
wordGuardandwdivthis uses. The AArch64 counterpart of #4980 (x86-64, merged).crt_values' batched inverse ends by multiplying x₂ by 2^(5B), B = 3n + 2, with 15n + 10 modular doublings. Those were 34% ofcrt_valuesat 2048 bits and 38% at 4096.Under the balanced-key
wordGuard(#4974), the inverse now leaves them out (inverseAtS), andqTailcomputes x₂ · 2^K mod p, K = 15n + 10 = 64Q + T (1 ≤ T ≤ 64):tailDbl);[aC](place);wdivof its L = n + Q + 1 words by p, straight into[aX₂].[aC]is two arrays above[aX₂], so the address computation iscrt_values' existing one.Keys that fail the guard run the old code unchanged. The shape follows #4980 (
guarded,tailK,tailDbl,place,qTail), except that the dividend is L words rather than w (5 quotient steps instead of 17 at 2048).Overflow. After
csubP, x₂ < p < 2^(64n), and T ≤ 64, so x₂ · 2^T < 2^(64(n+1)): the doublings never carry out of word n, and the n + 1 copied words hold the result exactly. Hence x₂ · 2^K < 2^(64L), with L = n + Q + 1 ≤ w + 2.Exactness. Under the guard and p·q = n, p is n = k/16 words with its top bit set (
guard_factors), sowdiv's quotient estimates are those of a normalized divisor. When p·q ≠ n, the mask already fails and the outputs are zero, andwdivstill runs in the same time and in bounds.Proofs
finishS_ok,inverseBS_ok,inverseAtS_ok, and a newInvLawfield: m ∣ x₂ · 2^(5B) · a − gcd, x₂ < m.Proof/Rsa/AArch64/CvTail.lean:tailK_ok, an in-placedblIPStep_ok,tailPass_ok,tailDbl_ok,tailLen_ok,tailXb_ok,placeBlk_okandqTail_ok.cvInv_okkeeps its post, with the new hypothesis that p is n words with its top bit set under the guard (fromguard_factors), viacvGuarded_ok,cvInvG_okandcvInvA_ok.inverseAtS_ct,guardG_ct,guarded_ct,qTail_ct(tailK_ct,tailDbl_ct,zeroAP_ct,place_ct,placeCopy_ct,tailWdiv_ct),skip_ct,invGcd_ctandinvPart_ct.Trust
No change to
TCB/orSpec/.Measurements
qemu-aarch64 instructions for
crt_values, #4974 → this branch (identical results to the old code on 32 keys: balanced, unbalanced, padded, p·q ≠ n, wrong d/dP/dQ/qInv):crt_valuesKeys that fail the guard: +36 instructions, from the second evaluation of the guard.
check_keyis unchanged.Possible next step: about 6.5k of the new tail at 2048 is the T one-bit doublings. A one-pass shift by T would remove most of that (about 3%), but the AArch64 model has only immediate shifts. Register-amount shifts (
lslv/lsrv) would be a TCB addition in their own PR, worth it if other code would use variable shifts too.Benchmarks
Run 43133337472 (aarch64, module
rsa, groupsrsa_from_.*, base 69c6f82 = #4974's head, so it measures this PR alone; with OpenSSL and aws-lc-rs).from_primesrunscrt_values, so it is 13.5–17.6% faster, now 2.5–2.8× faster than OpenSSL's;from_crt(which checks the given CRT values) andfrom_components(prime recovery) don't use this inverse and are within 0.4%. 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/512The
VG_CPU_FEATURES=noneandsha3jobs agree:from_primes13.4–17.6% faster, everything else within 0.5%.Checks
On f6b695f, a plain merge of
main82dcd68 (after #4974's squash, the five conflicted files, the AArch64rsa.rs/rsa.s,CvCTCode.lean,CvCTMain.leanandCvMain.lean, keep this branch's side, after checking thatmain's equal #4974's head 69c6f82): on main's CI cache, the 8 AArch64 RSA/DSA/FFDH registrations,CvVerifiedandCkVerifiedbuild; the fullEmit.leanchanges nothing andEmit.lean --checkexits 0; every Static-checks step passes; under qemu the rsa (65), dsa (75) and ffdh (24) tests pass.Before the merge, on b85f671: the same builds, the full
Emit.leanchanged onlycrates/verified-garbage-asm-aarch64/{src/rsa.rs, assembly/rsa.s},Emit.lean --checkexits 0,check_lean_public_imports.pyon the edited modules, and the same qemu tests.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh