Skip to content

AArch64 vg_rsa_crt_values: the inverse's tail by one word division - #4981

Merged
alex merged 15 commits into
mainfrom
claude/aarch64-inv-tail
Nov 12, 2026
Merged

alex merged 15 commits into
mainfrom
claude/aarch64-inv-tail

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Follows #4974 (AArch64 key import, merged), whose wordGuard and wdiv this 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% of crt_values at 2048 bits and 38% at 4096.

Under the balanced-key wordGuard (#4974), the inverse now leaves them out (inverseAtS), and qTail computes x₂ · 2^K mod p, K = 15n + 10 = 64Q + T (1 ≤ T ≤ 64):

  • T in-place doublings of x₂ over n + 1 words (tailDbl);
  • the result placed Q words up in the zeroed [aC] (place);
  • wdiv of its L = n + Q + 1 words by p, straight into [aX₂]. [aC] is two arrays above [aX₂], so the address computation is crt_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), so wdiv's quotient estimates are those of a normalized divisor. When p·q ≠ n, the mask already fails and the outputs are zero, and wdiv still runs in the same time and in bounds.

Proofs

  • finishS_ok, inverseBS_ok, inverseAtS_ok, and a new InvLaw field: m ∣ x₂ · 2^(5B) · a − gcd, x₂ < m.
  • Proof/Rsa/AArch64/CvTail.lean: tailK_ok, an in-place dblIPStep_ok, tailPass_ok, tailDbl_ok, tailLen_ok, tailXb_ok, placeBlk_ok and qTail_ok.
  • cvInv_ok keeps its post, with the new hypothesis that p is n words with its top bit set under the guard (from guard_factors), via cvGuarded_ok, cvInvG_ok and cvInvA_ok.
  • Constant time: inverseAtS_ct, guardG_ct, guarded_ct, qTail_ct (tailK_ct, tailDbl_ct, zeroAP_ct, place_ct, placeCopy_ct, tailWdiv_ct), skip_ct, invGcd_ct and invPart_ct.

Trust

No change to TCB/ or Spec/.

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):

bits crt_values
2048 325,470 → 227,391 (−30.1%)
3072 649,039 → 428,614 (−34.0%)
4096 1,081,783 → 690,852 (−36.1%)

Keys that fail the guard: +36 instructions, from the second evaluation of the guard. check_key is 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, groups rsa_from_.*, base 69c6f82 = #4974's head, so it measures this PR alone; with OpenSSL and aws-lc-rs). from_primes runs crt_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) and from_components (prime recovery) don't use this inverse and are within 0.4%. The runner's default configuration:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.05 ms 2.04 ms 0.4% faster 2.49 ms 1.2× faster – –
rsa_from_components/384 4.33 ms 4.32 ms 0.3% faster 7.28 ms 1.7× faster – –
rsa_from_components/512 7.45 ms 7.44 ms 0.2% faster 16.01 ms 2.2× faster – –
rsa_from_crt/256 17.35 µs 17.33 µs 0.1% faster 2.18 µs 8.0× slower 130.83 µs 7.5× faster
rsa_from_crt/384 32.76 µs 32.72 µs 0.1% faster 2.95 µs 11.1× slower 265.29 µs 8.1× faster
rsa_from_crt/512 53.16 µs 53.04 µs 0.2% faster 3.75 µs 14.2× slower 452.63 µs 8.5× faster
rsa_from_primes/256 42.30 µs 36.60 µs 13.5% faster 101.29 µs 2.8× faster – –
rsa_from_primes/384 81.19 µs 67.92 µs 16.3% faster 172.95 µs 2.5× faster – –
rsa_from_primes/512 131.85 µs 108.70 µs 17.6% faster 270.78 µs 2.5× faster – –

The VG_CPU_FEATURES=none and sha3 jobs agree: from_primes 13.4–17.6% faster, everything else within 0.5%.

Checks

On f6b695f, a plain merge of main 82dcd68 (after #4974's squash, the five conflicted files, the AArch64 rsa.rs/rsa.s, CvCTCode.lean, CvCTMain.lean and CvMain.lean, keep this branch's side, after checking that main's equal #4974's head 69c6f82): on main's CI cache, the 8 AArch64 RSA/DSA/FFDH registrations, CvVerified and CkVerified build; the full Emit.lean changes nothing and Emit.lean --check exits 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.lean changed only crates/verified-garbage-asm-aarch64/{src/rsa.rs, assembly/rsa.s}, Emit.lean --check exits 0, check_lean_public_imports.py on the edited modules, and the same qemu tests.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

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
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
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 12, 2026
Base automatically changed from claude/aarch64-key-import-words to main November 12, 2026 01:35
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with main, found after 0b6d045 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.

# 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
alex marked this pull request as ready for review November 12, 2026 02:03
@alex
alex enabled auto-merge November 12, 2026 02:08
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 47ced24 Nov 12, 2026
25 checks passed
@alex
alex deleted the claude/aarch64-inv-tail branch November 12, 2026 02:19
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