Skip to content

FFDH parameter generation on x86-64: sieve p's first 64 primes before q's sieve - #4525

Merged
alex merged 1 commit into
mainfrom
claude/perf-4141-ffdh-early-sieve
Nov 7, 2026
Merged

alex merged 1 commit into
mainfrom
claude/perf-4141-ffdh-early-sieve

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #4141, following #4507.

What was slow

After #4507, for g = 2 about 27% of the search's time went to candidates whose q has a small factor. Each of those still paid for p's whole sieve first (~14 µs per such candidate), because p was sieved in full before q.

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

The trial checks now run in this order:

  1. p's first words (earlyCheck, sieveFirst): oddness, then the first 16 words of RSA's table (64 primes). It stops at the first divisor, as the whole sieve does. Most candidates that pass the screen and are rejected by trial division have a factor among these 64 primes.
  2. q's whole sieve (sieveQ), as before.
  3. The rest of p's sieve (sieveP): p's whole sieve, once q has passed, before the Miller–Rabin witnesses.

The result is unchanged:

  • earlyOk_of_sieved proves that the early check rejects only candidates that p's whole sieve also rejects.
  • So candidate_result still equals Spec.Ffdh.sievedParameterCandidate; only the order of the trial checks changes.

Correctness:

  • sieveFirst_ok and earlyCheck_ok / earlyCheck_state_ok prove what the new checks compute.
  • sieveP_ok proves the reordered p sieve.

Timing:

  • The sieve's relational proof (SieveAt, sieveWords, word_at) now takes the number of table words as a parameter.
  • sieveFirst_ct reuses it for 16 words, with the setup taint-checked (inner_sievePrefixN_ct).
  • earlyCheck_ct and sieveP_ct prove the timing of the new early check and the reordered p sieve.

Measurements

Benchmarks workflow, x86-64, against main at ae39a25, with OpenSSL: https://github.com/pyca/verified-garbage/actions/runs/42406449406 (medians).

base this PR change OpenSSL this PR vs OpenSSL
ffdh_parameters_g2/64 32.64 ms 28.95 ms 11.3% faster 31.27 ms 1.08× faster
ffdh_parameters_g5/64 51.38 ms 42.26 ms 17.7% faster 42.97 ms 1.02× faster

OpenSSL's search has high variance; its means on that runner were 47.4 ms (g = 2) and 53.7 ms (g = 5). An earlier run, https://github.com/pyca/verified-garbage/actions/runs/42405025925, started before this PR existed and so had no base to compare with.

Local runs on seven tapes (the bench tape and seeds 1000–1005), sums of the best of 3. I alternated the two builds, two runs each (Intel Xeon). OpenSSL's figure is 7 × its mean over 300 runs on the same machine:

#4507's head this PR change OpenSSL
g = 2 1137 / 1233 ms 993 / 1000 ms 13–19% faster 508 ms
g = 5 1014 / 1010 ms 897 / 801 ms 12–21% faster 508 ms

What remains

I profiled candidates whose p fails its first Miller–Rabin round: about 90 µs each natively.

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

… sieve

p is now divided by only the first 16 words of RSA's table (64 primes) before
q's whole sieve, and by the rest of the table after q passes. Most candidates
that pass the screen have a small factor among those 64 primes, and a q with a
small factor is now rejected before p's whole sieve.

- `sieveFirst`/`earlyCheck`: oddness, then the sieve with a fixed count of
  `firstWords` words, stopping at the first divisor as the whole sieve does.
- `sieveP`: p's whole sieve, after q's (`sieveQ`), before the witnesses.
- `earlyOk_of_sieved`: the early check rejects only what p's whole sieve
  rejects, so the order of the checks does not change the result
  (`candidate_result`).
- Timing: `sieveFirst_ct`/`earlyCheck_ct` (the sieve's relational proof, now
  over any count of words) and `sieveP_ct`.

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