Repository navigation
AArch64 R² mod m for a public m: word steps for any top word - #4948
Merged
Merged
Conversation
…sion R² mod n was 2^(64 (w - 1)), 64 + w modular doublings, -n⁻¹ mod 2³² and six Montgomery squares. It is now 2^(64 (w - 1)) and w + 1 word steps x := x 2^64 mod n (Knuth's Algorithm D): q̂ is estimated from the top words of x P and n P for P = 2^z normalizing n's top word, by 64 bits of branch-free restoring division, then t = x 2^64 - q̂ n with n added back twice by masks. This holds for any top word of n, so no modulus takes a slow path; every count depends only on w. The generic arithmetic (the normalized estimate, q ≤ q̂ ≤ q + 2, and the step for any such q̂) is in Proof/Bignum/WordStepNorm.lean, for the other targets' word steps. Constant time is the existing taint check of core. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
For a public m whose top bit is clear, R² mod m (vg_rsa_public_precompute, and the FFDH and CRT setups that share r2StepsPub) took the doublings and squarings. It now takes word steps of long division too (fastN): q̂ is estimated from the top words of x P and m P for P = 2^z normalizing m's top word (normN, recipN for d', quotN), x starts at 2^(64 (w - 1)) (setTop) and takes w + 1 steps. Moduli whose top bit is set keep fastPub unchanged; m of two words keeps the doublings (choiceW), as quotN reads three of x's words. The generic arithmetic is PPC64LE's (WordStepNorm), with norm_inv_step moved there and norm_unique (the shift is a function of m, which the constant-time proof needs) added. r2Pub_ct moves to R2dNormCT.lean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
R² mod n was 2^(64 (w - 1) + 63 - z), w + 1 + z modular doublings, -n⁻¹ mod 2⁶⁴ and six Montgomery squares. It is now 2^(64 (w - 1)) and w + 1 word steps x := x 2^64 mod n (Knuth's Algorithm D), as PPC64LE's: q̂ is estimated from the top words of x P and n P for P = 2^z normalizing n's top word (z from lz, as before), so any top word works and every count depends only on w. The estimate is Möller and Granlund's division by the reciprocal v = ⌊(2^128 - 1) / d'⌋ - 2^64 (as AArch64's), v computed once by 64 bits of branch-free restoring division, its corrections by sltu masks. t = x 2^64 - q̂ n is computed in place in x, one fused pass of the row q̂ n and the subtraction, then n added back twice under the mask of t's sign. The proofs (Proof/Bignum/RISCV64/PreR2Words.lean) reuse the target-independent WordStepNorm.lean (qhat_norm, wordStepQ_val, top_shift, shl_add_lt, qhat_fix) and WordStep.lean (mg_quot, divBit_inv, recip_eq). Constant time is the existing taint check of core. The old path's r2TopBit, r2SqS, r2SquaresS and r2Rest, used by nothing else, are removed. Instructions per call under qemu-riscv64 (top byte 0xc5): 2048 bits 164,337 -> 52,521; 3072 bits 350,917 -> 111,801; 4096 bits 607,385 -> 193,609. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
The tree is claude/riscv64-public-precompute-words-v2 (e89930c): this branch's commit rebased onto main after #4957 merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
The tree is claude/aarch64-public-precompute-normalized-v2 (2cfc4d4); full lake build, Emit --check (exit 0) and the static checks pass on it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
alex
changed the base branch from
claude/ppc64le-public-precompute-words
to
claude/riscv64-public-precompute-words
November 11, 2026 15:06
Base automatically changed from
claude/riscv64-public-precompute-words
to
main
November 11, 2026 16:54
|
This pull request has conflicts with Posted by |
…-precompute-normalized # Conflicts: # crates/verified-garbage-asm-riscv64/assembly/rsa.s # crates/verified-garbage-asm-riscv64/src/rsa.rs # lean/VerifiedGarbage/Proof/Bignum/WordStepNorm.lean
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
alex
marked this pull request as ready for review
November 11, 2026 17:33
alex
enabled auto-merge
November 11, 2026 17:34
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.
Builds on #4957 and #4963 (merged), whose
Proof/Bignum/WordStepNorm.leanit extends. Its branch hasmain(8564b91b) merged in:WordStepNorm.leankeeps #4963's 3-by-2 lemmas alongside this PR'snorm_uniqueandnorm_inv_step, and the AArch64 crate is regenerated by the full emitter.For a public
mwhose top bit is clear,R² mod mused the doublings and Montgomery squares (r2StepsOld). This affectedvg_rsa_public_precompute, and the FFDH and CRT setups that sharer2StepsPub. Moduli whose top bit is set already used word steps of long division (fastPub).Now every
mof three or more words uses word steps:R2Div.fastN, with Knuth's normalization for the quotient estimate.normN: computesP = 2^z, the shift that makesd' = ⌊m P / 2^(64 (w - 1))⌋ ≥ 2^63, using 63csel-masked doublings.recipN: computesvford'.quotN: estimatesq̂from the top words ofx P(bymul/umulhbyP) with Möller–Granlund division byv.t = x 2^64 - q̂ mis computed fromxandmthemselves.xstarts at2^(64 (w - 1))(setTop) and takesw + 1steps.The top-bit-set path is unchanged.
mof two words keeps the doublings (choiceW, a branch on the publicw), becausequotNreads three ofx's words.Proofs
Proof/Bignum/AArch64/R2dNorm.lean(new):normN_ok,recipN_ok,r2dQuotN_ok,r2dStepN_ok(byWordStep.qhat_normandwordStepQ_val), the steps loop,setTop_ok,r2dFastN_okandr2dChoiceW_ok.PubR2.r2Pub_oktakesfastNwhen the top bit is clear.Proof/Bignum/AArch64/R2dNormCT.lean(new): constant time.m,xand the shiftz.zis a function ofmby the newWordStep.norm_unique.addBackPub's branch is bound the wayR2dPubCTbinds it.r2Pub_ctmoves here fromR2dPubCT.lean, since it now depends onfastN's CT;CrtCTNSetupandPcCTimport it from here.WordStepNorm.lean: gainsnorm_unique, andnorm_inv_stepmoves there fromPPC64LE/PreR2Words.lean.The emitter changes the AArch64
rsa,ffdhand internalvg_rsa_private_crtfiles, since all three user2StepsPub.No TCB, Spec or Rust changes.
Measurements
qemu-aarch64, µs per
vg_rsa_public_precompute, two interleaved runs ofmainagainst this branch; the R² values are identical in every case.The top-bit-set path runs byte-identical code, so those rows are qemu noise. Every top-bit-clear shape is about 3× faster, and no modulus of three or more words takes the doubling path any more.
Benchmarks
Run 42970845302 (aarch64, modules
rsa ffdh, basec38d155d= #4946's head, before #4957 merged; the AArch64 code it measures is unchanged since, with OpenSSL and aws-lc-rs; default configuration shown, thesha3andnoneconfigurations agree). The benchmark keys and the FFDH groups all have their top bit set, so they run byte-identical code: every row is within ±2%, i.e. no regression. The gain is for moduli whose bit length is not a multiple of 64 (the qemu table above).ffdh_cold_first_key/256ffdh_cold_first_key/384ffdh_cold_first_key/512ffdh_exchange/256ffdh_exchange/384ffdh_exchange/512ffdh_parameters_from/256ffdh_parameters_from/384ffdh_parameters_from/512rsa_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/512rsa_pkcs1_sign/256rsa_pkcs1_sign/384rsa_pkcs1_sign/512rsa_pkcs1_verify_once/256rsa_pkcs1_verify_once/384rsa_pkcs1_verify_once/512rsa_public_key/256rsa_public_key/384rsa_public_key/512Checks
On the branch merged with
main(8564b91b): everyArtifacts.*registration for AArch64, PPC64LE and RISC-V (all importWordStepNorm) builds, the fullEmit.lean --checkexits 0, the static checks pass, and under qemu-aarch64cargo test rsa(46 unit, 1 CAVP, 18 Wycheproof) andcargo test ffdhpass. Before the merge:lake build,lake env lean --run Emit.lean --check.ci/check_lean_public_imports.pyon the edited modules.cargo test --releasefiltered onrsa,ffdhanddh.https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
Generated by Claude Code