Skip to content

x86-64 RSA inverse: each batch's updates fused into one pass per pair - #4995

Merged
alex merged 1 commit into
mainfrom
claude/x64-inv-fused
Nov 12, 2026
Merged

alex merged 1 commit into
mainfrom
claude/x64-inv-fused

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

The x86-64 counterpart of #4989 (i686), #4991 (RISC-V) and #4992 (AArch64). Before this PR, each batch of the batched inverse (Inv.batch) made separate passes over the words:

  • lin, the matrix rows;
  • shr, the shift of (f, g) by 59;
  • mred, the Montgomery reduction of (a, b);
  • copyW, the copies between them.

It now makes one in-place pass for each pair:

  • fgRow: (u f + v g) / 2^59 and (q f + r g) / 2^59 from one read of each word, with two-word accumulators. The division by 2^59 is folded into the store of word j − 1.
  • abRow: u a + v b + k m for both rows, with three-word accumulators, stored a word down. k comes from column 0 (the kd/ke slots), and the last column adds the sign words.

Every caller of Inv.batch uses it: crt_values (inverseN/inverseNS), recover_primes_det (inverse) and keygen (inverseK). lin, shr, mred, copyW and absM are deleted, along with their proofs.

Proofs

  • New module Proof/Rsa/X86_64/InvB/Rows.lean, with fgRow_ok and abRow_ok. Only Batch imports it.
  • batch_ok now composes the two rows. BS gains one fact: word n of aP is 0.
  • The inverse's contract (InvLaw, inverse_ok) is unchanged, so no caller's proof changes beyond the batch.
  • Constant time: both rows keep their end pointer in r15 and recompute n from it, so every branch and address is a function of registers that taint tracks as public. A variant kept entirely in registers, which held n and the end pointer in memory slots, was 1% faster (238k instead of 240k at 2048). It fails taint_decide, because the analysis makes values loaded from memory secret, so this PR keeps the version that passes.

Trust

No change to TCB/ or Spec/. In crates/verified-garbage-asm-x86_64, only rsa and internal/vg_rsa_keygen_key change.

Measurements

Callgrind, one call through the emitted crate, main (3170b02) → this branch:

function 2048 3072 4096
crt_values 277,880 → 240,480 (−13.5%) 521,798 → 446,318 (−14.5%) 838,676 → 712,060 (−15.1%)
recover_primes_det 26.33M → 26.19M (−0.5%) 56.41M → 56.13M (−0.5%) 97.80M → 97.32M (−0.5%)

recover_primes_det is dominated by work other than the inverse. Keygen's time is the random prime search: its saving of about 100k instructions per call is far below run-to-run variance.

Benchmarks

Run 43187425116: x86_64, module rsa, groups rsa_from_.*, base 3170b02 (main), with OpenSSL and aws-lc-rs. Each configuration is one interleaved run on its own runner. The inverse runs the same code in every configuration, so the rows are six samples of one change.

rsa_from_primes is mostly one crt_values call.

configuration (CPU) 2048 3072 4096
default (EPYC 9V45) 5.2% faster 6.0% faster 6.9% slower
avx,avx2,bmi2,adx (EPYC 9V74) 3.7% faster 5.0% faster 13.2% faster
none (EPYC 9V74) 3.5% faster 2.2% faster 4.8% faster
avx,avx2,bmi1,bmi2,adx (EPYC 7763) 3.9% faster 5.1% faster 6.0% faster
avx,avx2,bmi1,bmi2 (Xeon Platinum 8573C) 4.5% faster 5.6% faster 5.6% faster
sha,ssse3 (EPYC 7763) 3.0% faster 4.0% faster 4.3% faster

The default job's 4096-bit row (82.9 → 88.7 µs median) disagrees with its own 2048- and 3072-bit rows, with the other five runs (on three other CPU models), and with callgrind's −15.1%. Its head samples are also unusually spread: a standard deviation of 24.4 µs, against 1.7 µs for base. So it reads as disturbance during that one round rather than a slowdown. It has not been rerun.

rsa_from_components (recover_primes_det) moves by 0.1–1.5% either way, as expected for a 0.5% change in instructions. rsa_from_crt does not run the inverse.

Default configuration:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 1.63 ms 1.65 ms 1.2% slower 1.57 ms 1.1× slower – –
rsa_from_components/384 3.79 ms 3.83 ms 1.0% slower 4.50 ms 1.2× faster – –
rsa_from_components/512 6.55 ms 6.45 ms 1.5% faster 9.55 ms 1.5× faster – –
rsa_from_crt/256 14.07 µs 14.01 µs 0.4% faster 1.48 µs 9.5× slower 99.72 µs 7.1× faster
rsa_from_crt/384 25.04 µs 24.91 µs 0.5% faster 2.17 µs 11.5× slower 196.39 µs 7.9× faster
rsa_from_crt/512 40.58 µs 39.25 µs 3.3% faster 2.71 µs 14.5× slower 324.06 µs 8.3× faster
rsa_from_primes/256 29.01 µs 27.49 µs 5.2% faster 52.65 µs 1.9× faster – –
rsa_from_primes/384 52.53 µs 49.35 µs 6.0% faster 85.15 µs 1.7× faster – –
rsa_from_primes/512 82.94 µs 88.65 µs 6.9% slower 126.06 µs 1.4× faster – –

The avx512ifma configuration was not measured, because its runner (EPYC 7763) lacks those features.

Checks

On ca02889, one commit on main (3170b02):

  • The full lake build passes, and so do the full Emit.lean and Emit.lean --check (exit 0).
  • Every step of the Static checks passes, and so do check_mcdt.py and check_lean_public_imports.py on the edited modules.
  • cargo test --release with Wycheproof passes with default CPU features, and with VG_CPU_FEATURES=none (cpu-features-env).

This PR does not depend on #4989, #4990 or #4993.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

The batch of 59 divsteps now updates f, g and a, b in place, in one pass
each over their words: (f, g) with two-word accumulators, the shift by
2^59 folded into the store of the word before, and (a, b) with three-word
accumulators of u·a + v·b + k·m, the multiple k of m taken from column 0
and the sign words added in the last column. This replaces lin, shr, mred
and copyW, and their proofs, for every caller (crt_values, recover_primes_det
and key generation's inverse). Counts stay in registers, so the batch is
still constant time (taint_decide).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
@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
@alex
alex marked this pull request as ready for review November 12, 2026 09:11
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 22e53fe Nov 12, 2026
44 checks passed
@alex
alex deleted the claude/x64-inv-fused branch November 12, 2026 11:57
alex pushed a commit that referenced this pull request Nov 12, 2026
Merges origin/main (c7f68f6), which contains #4990 (squash-merged as
f36e35a; this branch was stacked on it) and #4995 (x86-64 fused
inverse).

Squash double-apply: for each file below, main's version is
byte-identical to #4990's head (6c4ad41), so the branch's version is
taken:
- lean/VerifiedGarbage/Proof/Rsa/X86/KcWord.lean (conflicted)
- Impl/Rsa/{X86_64,AArch64}/CheckKeyCore.lean,
  Proof/Rsa/AArch64/{CkCT,CkPieces,CkSteps}.lean,
  Proof/Rsa/X86_64/{KeyCTPieces,KeyChecks,KeySteps}.lean (auto-merged
  without conflict, but with #4990's definitions duplicated, e.g. mulPQ)

Impl/Rsa/X86_64/Keys.lean auto-merged, keeping both this branch's
changes and #4995's.

The conflicting generated files (rsa.s and rsa.rs of the x86, x86_64
and powerpc64le crates) and every other crate were regenerated with the
full emitter (Emit.lean); Emit.lean --check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGGAywWt7REP7CHZ6u
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