Skip to content

x86-64 vg_rsa_check_key: word-level remainders for balanced keys - #4971

Merged
alex merged 11 commits into
mainfrom
claude/x86-key-import-mont
Nov 11, 2026
Merged

alex merged 11 commits into
mainfrom
claude/x86-key-import-mont

Conversation

@alex

@alex alex commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

What

vg_rsa_check_key on x86-64 spent most of its time (about 87%, and 56% of PrivateKey::from_primes) in bit-serial restoring division: e·d mod (p−1), e·d mod (q−1) and q·qInv mod p. vg_rsa_crt_values already has a proven, reciprocal-normalized word-level long division (WordDivide) for keys of the usual shape. This PR reuses it in check_key under the same public guard: w even, n_len = 8w = 2·p_len, p_len = q_len, and n's top bit set.

Under the guard, the checks become:

  • dX < X − 1, then d mod (X − 1) = dX by WordDivide, then e·dX ≡ 1 (mod X − 1). The e·dX check keeps its one-word bit-serial remainder, which is cheap. Given dX < X − 1 and e·dX ≡ 1, d mod (X − 1) = dX holds exactly when e·d ≡ 1 (mod_swap, keyValid_word).
  • qInv < p, then q·qInv mod p = 1, with the product's low w words divided by WordDivide.

WordDivide normalizes by setting the divisor's top bit. A factor of such an n already has that bit set whenever p·q = n (factors_guard); when p·q ≠ n, the mask is clear anyway.

Keys of any other shape keep the bit-serial path (bitChecks, unchanged). Both branches are proven to return keyValid, and both are constant time.

Constant time: the public data (KPub) is only the workspace, pointers and lengths, and n; d, p − 1, q − 1, qInv and every product and remainder are secret on both paths. The branch reads only w, n_len, p_len, q_len and n's first byte (guard_ct).

Tests: tests/wycheproof/rsa_keys.rs loads every two-prime key of Wycheproof's rsa_*_test.json through from_primes and from_crt, with the ±1 corruptions of d, dP, dQ and qInv. The 2048-, 3072- and 4096-bit keys take the word path; the two-prime keys of rsa_oaep_misc_test.json with 388- and 504-byte moduli (w = 49 and 63, odd) take the bit-serial one.

Other changes:

  • The setup now stores the stride 8(w+2) in the header (strideInit), which WordDivide reads.
  • sDQ moves from sFn 12 to sFn 15, because sFn 12 is the stride slot WordDivide uses. Nothing else used sFn 15.

Trust

No change to TCB/ or Spec/. The new Lean modules are Proof/Rsa/X86_64/KeyWord.lean (correctness) and KeyWordCT.lean (constant time). For constant time, KPub now carries n_len, n's pointer and n's bytes, all public, which the guard reads. The rest is edits to the check_key proofs (KeyMain, KeyCTDefs, KeyCTSetup, KeyCTCode, KeyCTMain).

Measurements

Instructions per from_primes call (callgrind, Wycheproof keys, VG_CPU_FEATURES=none; the default features give the same numbers to within 1%). "Before" is main before #4964, and "after" is this branch merged with main.

bits check_key before check_key after from_primes before from_primes after
2048 1.13M 0.154M (7.3×) 1.77M 0.79M (2.2×)
3072 2.40M 0.276M (8.7×) 3.62M 1.47M (2.5×)
4096 4.16M 0.430M (9.7×) 6.14M 2.37M (2.6×)

After this, vg_rsa_crt_values is the largest part, mostly its divsteps inverse.

Benchmarks

Run 43094222077 (x86_64, module rsa, groups rsa_from_.*, base ade85059 = main, with OpenSSL and aws-lc-rs). All seven VG_CPU_FEATURES configurations ran, and none is slower. 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 1.78 ms 1.64 ms 8.2% faster 1.55 ms 1.1× slower – –
rsa_from_components/384 3.86 ms 3.74 ms 2.9% faster 4.48 ms 1.2× faster – –
rsa_from_components/512 6.84 ms 6.60 ms 3.4% faster 9.44 ms 1.4× faster – –
rsa_from_crt/256 68.70 µs 13.91 µs 79.8% faster 1.49 µs 9.4× slower 97.33 µs 7.0× faster
rsa_from_crt/384 136.60 µs 25.83 µs 81.1% faster 2.11 µs 12.3× slower 194.21 µs 7.5× faster
rsa_from_crt/512 235.86 µs 40.60 µs 82.8% faster 2.65 µs 15.3× slower 320.87 µs 7.9× faster
rsa_from_primes/256 90.54 µs 33.41 µs 63.1% faster 48.58 µs 1.5× faster – –
rsa_from_primes/384 173.79 µs 62.62 µs 64.0% faster 84.97 µs 1.4× faster – –
rsa_from_primes/512 307.59 µs 103.15 µs 66.5% faster 137.29 µs 1.3× faster – –

With VG_CPU_FEATURES=none, from_primes is 52–58% faster and from_crt 70–76% faster. from_primes is now 1.3–1.5× faster than OpenSSL's. OpenSSL's from_crt (1.5–2.7 µs) does not check the key as vg_rsa_check_key does, so that row is not like for like; aws-lc-rs, which does, is 7–8× slower. from_components spends its time recovering the primes, which this PR does not change.

Checks

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

… Arm and AArch64 key checks)

Byte-identical to Proof/Rsa/MontCheckMath.lean on claude/ppc64le-rsa-key-checks-mul (a05ab4a).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Under a public guard (crt_values' WordDivide guard: w even, n_len = 8w =
2 p_len, p_len = q_len, n's top bit set) check d mod (X - 1) = dX and
q qInv mod p = 1 with the proven reciprocal long division instead of the
bit-serial reductions; other shapes keep the bit-serial path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
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 11, 2026
@alex
alex marked this pull request as ready for review November 11, 2026 20:36
@alex
alex added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 201ecdc Nov 11, 2026
43 of 44 checks passed
@alex
alex deleted the claude/x86-key-import-mont branch November 11, 2026 22:59
alex pushed a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHgwmxoKvRUNy64bKd
alex pushed a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHgwmxoKvRUNy64bKd
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