Repository navigation
FFDH parameter generation on x86-64: stop the sieve at the first divisor, cheaper screen - #4507
Merged
Merged
Conversation
…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
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
This was referenced Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #4141, following #4401.
Counting the work again
On current
main, I counted every stage ofSpec.Ffdh.sievedParameterSearchexactly, 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: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 change (x86-64
Impl+Proofonly; no spec or TCB change)reduce255. 255 divides 2^32 − 1, 2^16 − 1 and 2^8 − 1, so four foldsx 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.(count − r13) & ~flag.firstHit_anyproves this decides exactly as the whole table does.sieve_ctproves 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.outerScreen).screen, and a rejected candidate goes straight to status 2.outerScreen_okproves the flag.outerScreen_ctproves the timing: the generator branch is public, and each branch (byteScreen2_ct,byteScreen5_ct) is taint-checked.CandidateTest.lean's import ofTestQwas split across two lines (publicthenimport). That made it public and hid the rest of the header fromci/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.
ffdh_parameters_g2/64ffdh_parameters_g5/64ffdh_parameters_g2/64(none)ffdh_parameters_g5/64(none)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:
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:
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), andEmit.lean: only the x86-64ffdhfiles change.Static checks, andcheck_lean_public_imports.pyon the edited modules.cargo test --release ffdh, also withVG_CPU_FEATURES=none --features cpu-features-env.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHGjpPevDhRbNs9mJKT