Skip to content

x86-64 vg_rsa_crt_values: qInv's factor 2^(5B) by one word-level reduction for balanced keys - #4980

Merged
alex merged 4 commits into
mainfrom
claude/x86-64-inv-tail
Nov 12, 2026
Merged

alex merged 4 commits into
mainfrom
claude/x86-64-inv-tail

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

vg_rsa_crt_values on x86-64 computes qInv with a batched divsteps inverse (50 batches of 59 divsteps at 2048 bits). Its result needs a final factor 2^K, K = 5B = 15n + 10, which the inverse applied as K one-bit modular doublings of n + 1 words: 250 doublings at 2048 bits, about 32% of crt_values, and 490 at 4096 bits, about 35%. This PR replaces them, for balanced keys, with one word-level reduction by the WordDivide that crt_values and check_key already use (#4971).

Shape. invPart = [guarded inverseNS inverseN] ++ gcdPart ++ [guarded qTail skip], where guarded is wordChoice's public guard (w even, n_len = 8w = 2·p_len, p_len = q_len, n's top bit set) followed by .ite.

  • Fallback: the original inverseN, code and proof unchanged.
  • inverseNS is the inverse without its K doublings; Inv.finishS keeps one conditional subtraction, so x₂ < p.
  • qTail: (K − 1) mod 64 + 1 one-bit doublings of x₂'s low n + 1 words, then x₂ placed ⌊(K − 1)/64⌋ words up in WordDivide's dividend, so the dividend is x₂·2^K; the existing WordDivide reduces it modulo p (normalize + code, w steps). d is saved in array 10 meanwhile and restored after.

Value. cvQTail_ok gives x₂·2^K mod p, given 2^(64n−1) ≤ p < 2^(64n) and x₂ < p. Those bounds come from WordDivide.factors_word, under the guard and p·q = n, which the output mask requires, so the outputs equal the spec for every input that reaches them. cvInv_ok's post-condition is unchanged: p ∣ x₂·q − gcd and x₂ < p. Since x₂ < p < 2^(64n) and the one-bit doublings are at most 64, x₂·2^T < 2^(64(n+1)): they never carry out of word n.

Constant time. The guard reads only lengths and n's top byte, already public. The tail's counts (T, Q, n + 1) depend on w alone. Each piece is checked with taint_decide (guarded_ct, invQS_ct, tailDbl_ct, placeCopy_ct, qTail_ct, invPart_ct).

Trust

No change to TCB/ or Spec/. InvLaw, the class the registrations instantiate, gains a field inverseNS_ok, proved in InvB/Main.

Measurements

callgrind, one x86-64 call per key, main (3fc218b) → this branch:

bits crt_values from_primes
2048 366,082 → 277,880 (−24%) 779k → 691k
3072 718,202 → 521,798 (−27%) 1.460M → 1.264M
4096 1,185,394 → 838,676 (−29%) 2.335M → 1.989M

Possible follow-ups, about 2% of crt_values each: a variable shift instead of the one-bit doublings, and starting WordDivide at the dividend's top nonzero word (as the AArch64 version does, which needs WordDivide's step count as a parameter).

Benchmarks

Run 43125859343 (x86_64, module rsa, groups rsa_from_.*, base 3fc218b, with OpenSSL and aws-lc-rs). from_primes runs crt_values, so it is 10.8–15.9% faster; from_crt (which checks the given CRT values instead of computing them) and from_components (prime recovery) don't use this inverse and are within ±0.4%. from_primes is now 1.7–2.1× faster than OpenSSL's.

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.47 ms 2.47 ms 0.3% slower 2.38 ms about the same – –
rsa_from_components/384 5.45 ms 5.43 ms 0.3% faster 6.82 ms 1.3× faster – –
rsa_from_components/512 9.59 ms 9.55 ms 0.4% faster 14.97 ms 1.6× faster – –
rsa_from_crt/256 23.58 µs 23.52 µs 0.3% faster 2.20 µs 10.7× slower 169.24 µs 7.2× faster
rsa_from_crt/384 42.21 µs 42.18 µs 0.1% faster 2.96 µs 14.3× slower 349.06 µs 8.3× faster
rsa_from_crt/512 66.31 µs 66.30 µs 0.0% faster 3.69 µs 18.0× slower 620.78 µs 9.4× faster
rsa_from_primes/256 55.98 µs 49.94 µs 10.8% faster 104.86 µs 2.1× faster – –
rsa_from_primes/384 104.23 µs 90.28 µs 13.4% faster 165.83 µs 1.8× faster – –
rsa_from_primes/512 169.45 µs 142.47 µs 15.9% faster 244.29 µs 1.7× faster – –

Checks (on 50fd766)

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

…Divide under the guard

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
…e public guard

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 00:40
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 57d3b56 Nov 12, 2026
38 checks passed
@alex
alex deleted the claude/x86-64-inv-tail branch November 12, 2026 00:48
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