Skip to content

FFDH parameter generation on x86-64: stop the sieve at the first divisor, cheaper screen - #4507

Merged
alex merged 6 commits into
mainfrom
claude/perf-4141-ffdh-paramgen
Nov 7, 2026
Merged

alex merged 6 commits into
mainfrom
claude/perf-4141-ffdh-paramgen

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #4141, following #4401.

Counting the work again

On current main, I counted every stage of Spec.Ffdh.sievedParameterSearch exactly, by simulating it on the same tapes the library searches. I timed each kind of candidate on its own, using tapes of K copies of one candidate followed by a known safe prime. Results at 512 bits, g = 2, on an Intel Xeon @ 2.10 GHz:

a candidate that… share of candidates before after the sieve change
the screen (congruence, 3, 5, 17) rejects 95.6% 1.34 µs 0.76 µs
p's trial division rejects 3.2% 18.7 µs 1.3 µs
q's trial division rejects 0.9% 34.3 µs 14.9 µs
fails p's first Miller–Rabin round 0.3% 87 µs 82 µs

These times include the harness filling the tape byte by byte. With a precomputed tape, after change 4, a screen-rejected candidate costs the library about 0.10 µs.

  • The fixed bench tape is lucky. It needs 49k candidates for g = 2; an average tape needs about 190k. So I also compare on seven tapes.
  • The screen was mostly a 64-step bit-by-bit reduction of the folded word modulo 255.
  • The sieve divided every candidate the screen passed by all 511 primes, whatever it found.

The change (x86-64 Impl + Proof only; no spec or TCB change)

  1. reduce255. 255 divides 2^32 − 1, 2^16 − 1 and 2^8 − 1, so four folds x mod 2^k + x / 2^k (k = 32, 16, 8, 8) and one conditional subtraction give x mod 255. That is 23 instructions instead of about 830 serial ones.
  2. The sieve stops at the first divisor.
    • The candidate comes from the tape, which the contract leaks, so its loop may exit after the first word of RSA's table (four primes) that holds a divisor.
    • The exit test is branch-free: (count − r13) & ~flag.
    • firstHit_any proves this decides exactly as the whole table does.
  3. Timing of the sieve. sieve_ct proves that both runs of the relational timing check share the candidate (SieveIn/SieveAt) and so stop after the same word (two_loop). The table setup, each word and the final flag read are taint-checked as before.
  4. Screen before the workspace (outerScreen).
    • The generator's screen now runs on p's octets in the output buffer, before p's phase workspace is set up and p is loaded into it.
    • The octets' sum is p modulo 255, since 256 ≡ 1, and the last octet gives p modulo 8. So the result is exactly screen, and a rejected candidate goes straight to status 2.
    • A rejected candidate takes about 1240 instructions instead of 1770.
    • outerScreen_ok proves the flag. outerScreen_ct proves the timing: the generator branch is public, and each branch (byteScreen2_ct, byteScreen5_ct) is taint-checked.
    • CandidateTest.lean's import of TestQ was split across two lines (public then import). That made it public and hid the rest of the header from ci/check_lean_single_importer.py; it is now a private import.

Measurements

Benchmarks workflow, x86-64, with OpenSSL, for the whole PR (changes 1–4): https://github.com/pyca/verified-garbage/actions/runs/42399663177. Its runner was an AMD EPYC 9V45; the run for changes 1–3 (https://github.com/pyca/verified-garbage/actions/runs/42398242546) ran on a slower runner, so the two runs' absolute times are not comparable.

base head change OpenSSL head vs OpenSSL
ffdh_parameters_g2/64 62.74 ms 32.60 ms 48.0% faster 36.51 ms 1.1× faster
ffdh_parameters_g5/64 104.94 ms 50.64 ms 51.8% faster 42.49 ms 1.2× slower
ffdh_parameters_g2/64 (none) 102.05 ms 59.40 ms 41.8% faster
ffdh_parameters_g5/64 (none) 163.66 ms 86.92 ms 46.9% faster

The OpenSSL numbers are medians of a search with high variance; its mean on that runner was 49.2 ms (g = 2) and 57.7 ms (g = 5).

Local runs on seven tapes (the bench tape and seeds 1000–1005), sums of the best of 3, in ms:

main changes 1–3 speedup
g = 2 3052 1415 2.2×
g = 5 2364 1245 1.9×

I measured change 4 with the tape pre-generated, alternating old and new builds. It is about 10% faster for g = 2 and about 2% for g = 5, within this machine's noise.

What remains

The per-candidate floor is no longer what remains: a screen-rejected candidate now costs about 0.1 µs. For g = 2, about 0.55 µs per candidate on average:

where the time goes share cost per candidate concerned
candidates whose p fails its first Miller–Rabin round 45% ~77 µs: both full sieves plus ~45 µs of round setup in the shared Miller–Rabin code
candidates whose q is rejected after p's full sieve 27% ~14 µs
the screen itself 25% ~0.1 µs

The word-wise load would only touch the ~4% of candidates that pass the screen, so it is not worth doing. Two steps within the spec remain:

Checks

  • lake build (all), and Emit.lean: only the x86-64 ffdh files change.
  • Every step of Static checks, and check_lean_public_imports.py on the edited modules.
  • cargo test --release ffdh, also with VG_CPU_FEATURES=none --features cpu-features-env.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT

…he sieve

Each fresh candidate p that passed the generator's congruence went straight
to the full trial division of p by RSA's 511 primes, whose cost does not
depend on where it finds a divisor, and only then was q = (p - 1)/2 checked.
With g = 5 the congruence (p = 3 or 7 mod 10) lets 2/5 of all candidates
through, among them every p whose q is even or divisible by 3, so most
candidates paid for a sieve that q's checks then rejected.

The congruence check now reduces p modulo 840 (g = 2) or 420 (g = 5), in
the same single pass as before, and accepts only the remainders for which
neither 2, 3, 5 nor 7 rules out p or q: a remainder in a fixed list, tested
without a branch. Everything it rejects the trial division of p or q would
reject (`congruent_sieved`), so the search, its result and its tape
consumption are unchanged; only fewer candidates reach the sieve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT
…ng it

The congruence check reduced each fresh full-width candidate bit by bit,
64 conditional subtractions per word: about 1.4 us of each candidate at 512
bits, most of what remained of the search after the sieve was screened.

Since 2^64 = 1 modulo 2^64 - 1, the candidate is congruent to the sum of its
words with end-around carries (`fold`: an add and an adc per word), and 255
= 3 * 5 * 17 divides 2^64 - 1: one word reduced bit by bit (`reduce255`)
gives p mod 3, 5 and 17, and the low word gives p mod 8. The check accepts p
mod 255 in a fixed list (q's and p's 3, 5 and 17) and p = 3 mod 8 (g = 2)
or p = 3 mod 4 (g = 5); 7 is no longer screened, 17 is. As before, it
rejects only what the trial division of p or q would (`congruent_sieved`),
so the search's result and tape consumption are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT
…-paramgen

# Conflicts:
#	crates/verified-garbage-asm-x86_64/assembly/ffdh.s
#	crates/verified-garbage-asm-x86_64/src/ffdh.rs
… bit loop

The congruence check reduced the candidate's word folded modulo 2^64 - 1
modulo 255 bit by bit: 64 dependent conditional subtractions, about 830
instructions and most of the half of each rejected candidate's time that
the screen takes.

255 divides 2^32 - 1, 2^16 - 1 and 2^8 - 1, so x mod 2^k + x / 2^k keeps x
modulo 255 for k = 32, 16, 8; four such folds (32, 16, 8, 8) leave a value
below 264, and one conditional subtraction gives x mod 255: 23
instructions (`reduce255`). The screen's result is unchanged.

A candidate the screen rejects now takes 0.76 us instead of 1.34 us at 512
bits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT
Each candidate that passed the screen was divided by all 511 primes of
RSA's table (`obviouslyComposite`), and its q again if p had none: the sieve
took about 18 us at 512 bits whatever it found, most of a candidate's cost
after the screen.

The candidate is the tape's, which the contract leaks, so the sieve may
stop at its first divisor. Its loop now exits after the first word of the
table (four primes) with a divisor of the candidate, or at the end of the
table (`sieveNext`, a branch-free exit test, `(count - r13) & ~flag`); a
random candidate that passes the screen is rejected after about a third of
the table. The result is unchanged: the words divided by (`firstHit`)
decide as the whole table does (`firstHit_any`).

The sieve's timing is now that of its public candidate: `sieve_ct` proves
that both runs of the relational timing check, which share the candidate,
stop after the same word, with the table set up and the flag read as
before by the taint analysis (`inner_sievePrefix_ct`, `inner_sieveWord_ct`,
`inner_sieveTail_ct`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT
@alex
alex enabled auto-merge November 7, 2026 17:58
…kspace

Every candidate set up the p phase's workspace and loaded p into it byte by
byte (`phaseSetup`, `loadOutput`) before the generator's screen, which
rejects about 96% of them.

The screen now runs first, on p's octets in the output buffer
(`outerScreen`): their sum is p modulo 255 (256 = 1 mod 255) and the last
octet gives p modulo 8, so it decides exactly `screen`, as the check on the
loaded words does, and a rejected candidate goes straight to status 2. A
rejected candidate takes about 1240 instructions instead of 1770.

`outerScreen_ok` proves the screen's flag; its timing follows from the
generator (public) and a taint check of each branch (`outerScreen_ct`).
CandidateTest's import of TestQ was written across two lines
(`public` then `import`), which made it public and hid the rest of the
header from ci/check_lean_single_importer.py; it is a private import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 1b6fb1d Nov 7, 2026
37 checks passed
@alex
alex deleted the claude/perf-4141-ffdh-paramgen branch November 7, 2026 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants