Skip to content

AArch64 R² mod m for a public m: word steps for any top word - #4948

Merged
alex merged 7 commits into
mainfrom
claude/aarch64-public-precompute-normalized
Nov 11, 2026
Merged

alex merged 7 commits into
mainfrom
claude/aarch64-public-precompute-normalized

Conversation

@alex

@alex alex commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Builds on #4957 and #4963 (merged), whose Proof/Bignum/WordStepNorm.lean it extends. Its branch has main (8564b91b) merged in: WordStepNorm.lean keeps #4963's 3-by-2 lemmas alongside this PR's norm_unique and norm_inv_step, and the AArch64 crate is regenerated by the full emitter.

For a public m whose top bit is clear, R² mod m used the doublings and Montgomery squares (r2StepsOld). This affected vg_rsa_public_precompute, and the FFDH and CRT setups that share r2StepsPub. Moduli whose top bit is set already used word steps of long division (fastPub).

Now every m of three or more words uses word steps: R2Div.fastN, with Knuth's normalization for the quotient estimate.

  • normN: computes P = 2^z, the shift that makes d' = ⌊m P / 2^(64 (w - 1))⌋ ≥ 2^63, using 63 csel-masked doublings.
  • recipN: computes v for d'.
  • quotN: estimates q̂ from the top words of x P (by mul/umulh by P) with Möller–Granlund division by v.
  • t = x 2^64 - q̂ m is computed from x and m themselves.
  • x starts at 2^(64 (w - 1)) (setTop) and takes w + 1 steps.

The top-bit-set path is unchanged. m of two words keeps the doublings (choiceW, a branch on the public w), because quotN reads three of x's words.

Proofs

  • Proof/Bignum/AArch64/R2dNorm.lean (new):
    • normN_ok, recipN_ok, r2dQuotN_ok, r2dStepN_ok (by WordStep.qhat_norm and wordStepQ_val), the steps loop, setTop_ok, r2dFastN_ok and r2dChoiceW_ok.
    • PubR2.r2Pub_ok takes fastN when the top bit is clear.
  • Proof/Bignum/AArch64/R2dNormCT.lean (new): constant time.
    • The relations fix m, x and the shift z.
    • z is a function of m by the new WordStep.norm_unique.
    • addBackPub's branch is bound the way R2dPubCT binds it.
    • r2Pub_ct moves here from R2dPubCT.lean, since it now depends on fastN's CT; CrtCTNSetup and PcCT import it from here.
  • WordStepNorm.lean: gains norm_unique, and norm_inv_step moves there from PPC64LE/PreR2Words.lean.

The emitter changes the AArch64 rsa, ffdh and internal vg_rsa_private_crt files, since all three use r2StepsPub.

No TCB, Spec or Rust changes.

Measurements

qemu-aarch64, µs per vg_rsa_public_precompute, two interleaved runs of main against this branch; the R² values are identical in every case.

modulus main branch
2048, top 7f 42–43 13–14
2048, top 01 46 14–15
2040, top c5 46 16–17
2016, top 45 56–87 15–16
3072, top 45 89–94 28–34
4096, top 45 155–156 49–50
2048 / 4096, top bit set 21–29 / 46–48 20–22 / 46–75 (noise)

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, base c38d155d = #4946's head, before #4957 merged; the AArch64 code it measures is unchanged since, with OpenSSL and aws-lc-rs; default configuration shown, the sha3 and none configurations 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).

Benchmark Base Head Change
ffdh_cold_first_key/256 714.56 µs 713.94 µs 0.1% faster
ffdh_cold_first_key/384 790.99 µs 790.09 µs 0.1% faster
ffdh_cold_first_key/512 1.58 ms 1.58 ms 0.1% faster
ffdh_exchange/256 351.39 µs 351.68 µs 0.1% slower
ffdh_exchange/384 819.11 µs 803.69 µs 1.9% faster
ffdh_exchange/512 1.64 ms 1.64 ms 0.0% slower
ffdh_parameters_from/256 572.27 µs 572.25 µs 0.0% faster
ffdh_parameters_from/384 494.01 µs 493.65 µs 0.1% faster
ffdh_parameters_from/512 994.48 µs 993.98 µs 0.1% faster
rsa_from_components/256 2.83 ms 2.83 ms 0.1% faster
rsa_from_components/384 6.03 ms 6.03 ms 0.1% slower
rsa_from_components/512 10.41 ms 10.41 ms 0.0% slower
rsa_from_crt/256 419.23 µs 419.72 µs 0.1% slower
rsa_from_crt/384 895.92 µs 895.52 µs 0.0% faster
rsa_from_crt/512 1.55 ms 1.55 ms 0.2% slower
rsa_from_primes/256 820.51 µs 821.24 µs 0.1% slower
rsa_from_primes/384 1.77 ms 1.77 ms 0.2% slower
rsa_from_primes/512 3.08 ms 3.08 ms 0.1% slower
rsa_pkcs1_sign/256 679.67 µs 680.17 µs 0.1% slower
rsa_pkcs1_sign/384 2.09 ms 2.09 ms 0.1% slower
rsa_pkcs1_sign/512 4.71 ms 4.72 ms 0.0% slower
rsa_pkcs1_verify_once/256 19.34 µs 19.48 µs 0.7% slower
rsa_pkcs1_verify_once/384 41.10 µs 41.81 µs 1.7% slower
rsa_pkcs1_verify_once/512 72.49 µs 71.59 µs 1.3% faster
rsa_public_key/256 27 ns 27 ns 0.3% faster
rsa_public_key/384 29 ns 29 ns 0.4% faster
rsa_public_key/512 31 ns 30 ns 0.2% faster

Checks

On the branch merged with main (8564b91b): every Artifacts.* registration for AArch64, PPC64LE and RISC-V (all import WordStepNorm) builds, the full Emit.lean --check exits 0, the static checks pass, and under qemu-aarch64 cargo test rsa (46 unit, 1 CAVP, 18 Wycheproof) and cargo test ffdh pass. Before the merge:

  • Full lake build, lake env lean --run Emit.lean --check.
  • Every step of the Static checks job, and ci/check_lean_public_imports.py on the edited modules.
  • Under qemu-aarch64 with Wycheproof: cargo test --release filtered on rsa, ffdh and dh.

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh


Generated by Claude Code

…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
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 11, 2026
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
alex changed the base branch from claude/ppc64le-public-precompute-words to claude/riscv64-public-precompute-words November 11, 2026 15:06
@github-actions github-actions Bot added untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. and removed untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. labels Nov 11, 2026
Base automatically changed from claude/riscv64-public-precompute-words to main November 11, 2026 16:54
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with main, found after 8564b91 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

…-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
alex marked this pull request as ready for review November 11, 2026 17:33
@alex
alex enabled auto-merge November 11, 2026 17:34
@alex
alex added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit e3ee0fd Nov 11, 2026
25 checks passed
@alex
alex deleted the claude/aarch64-public-precompute-normalized branch November 11, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants