Skip to content

DSA parameter search over a refillable tape on i686 - #4295

Merged
reaperhulk merged 3 commits into
mainfrom
claude/unify-random-bytes-crypto-xqmg0z-dsa-x86
Nov 6, 2026
Merged

reaperhulk merged 3 commits into
mainfrom
claude/unify-random-bytes-crypto-xqmg0z-dsa-x86

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Moves i686's DSA parameter generation to the refill protocol (Spec/Dsa/ParameterSearch.lean, Spec/Entropy.lean), as #4234 did for x86-64 and #4293 for PPC64LE. i686's old three-tape function is deleted. This is an implementation PR: nothing in TCB/ or Spec/ changes.

Lean

The generation code and proofs under Proof/Dsa/X86/Generation/ are changed in place, following PPC64LE's design.

Impl/Dsa/X86/Generation.lean

  • vg_dsa_parameter_search(out, out_len, used, rand, rand_len, scratch, scratch_len, state) takes cdecl arguments and keeps the 216-byte stack. It dispatches on state[1], the width. There are four variants: baseline, _sha1_shani, _sha256_shani and _sha1_shani_sha256_shani, registered in Generic/Sha1/Sha256/MillerRabin/X86/Dsa.lean.
  • Each seed is read at a cursor in rand, with its witnesses after it. The generator search reads a window of at most 128·p_len octets. If the window is short and no g is found, the search asks for more entropy, and the generator loop stops when fewer than p_len octets are left.
  • Status follows x86-64's request formula. With no seeds left, it reports exhausted (3). When the tape is short, it reports exhausted if the octets left reach the budget 32769·L; otherwise it asks for min(2·max(avail, 256·L), 32769·L) octets.
  • Pointers the code writes through (state, used) are each loaded in their own block, as the constant-time proof needs.
  • vg_dsa_parameter_search_init(state, p_len) writes [4096, L, 32769·L, 512·L] and returns 1, or writes zeros and returns 0. It is registered in Artifacts/Dsa/X86.lean.

Proofs

  • Env now has the single tape rand. Each seed and each h window is a slice of it at an offset, so the per-seed q, p and g proofs and their constant-time proofs carry over with small edits.
  • The seed result uses the shared refillAttempt. The controller (Search, SearchCT) uses the shared search_* lemmas, and the result (Finish, FinishCT) uses requested_requests. Entry, ABI, ABIEnv, Correct, TopCT and Satisfiable were rewritten.
  • Init.lean reuses the i686 FFDH init helpers from Proof/Ffdh/X86/ParamInit.lean.
  • The old controller and its spec plumbing (seedList, sCont/sNext, the outcome based on seedResult) are removed.

Rust

  • src/dsa/parameters/search.rs now covers i686 too: a cdecl Search type and an exhaustive match over the SHA-1/SHA-256 backends.
  • search/tests.rs checks the i686 dispatch table against the emitted feature sets, and runs each selectable variant.
  • The i686 select, its three-tape test and its imports are removed from parameters.rs.

Size

added removed
Lean (33 files) 3,193 984
Generated crate 7,008 4,705
Rust 134 125

Checks

  • A full lake build passes. Emit.lean --check passes with the standard axiom audit, and no file uses sorry, native_decide, bv_decide or maxHeartbeats.
  • All the Static checks steps pass, and so does check_lean_public_imports --base origin/main.
  • cargo fmt and clippy -D warnings pass on x86-64 and on i686-unknown-linux-gnu.
  • The DSA tests pass on i686 with the detected features and with VG_CPU_FEATURES=none, and on x86-64.

Benchmark

Run: https://github.com/pyca/verified-garbage/actions/runs/42124762826 (arches: x86, modules: dsa, base main).

Benchmark Base Head Change Base (none) Head (none) Change
dsa_generate_parameters/1024 547.10 ms 539.74 ms 1.3% faster 548.31 ms 541.21 ms 1.3% faster
dsa_generate_parameters/2048 710.56 ms 690.01 ms 2.9% faster 711.53 ms 687.98 ms 3.3% faster
dsa_generate_parameters/3072 6664.91 ms 7051.89 ms 5.8% slower 6682.89 ms 7058.07 ms 5.6% slower

These differences are not noise. The benchmark feeds a fixed tape (fixed_tape in bench/benches/primitives/main.rs). The base splits it into three tapes: seeds, h values, then witnesses. The refill search instead reads seeds, witnesses and h windows from one tape in order, so the same stream gives different seeds and witnesses, and the search does a different amount of work. The same shift appears on every architecture (#4293, #4296, #4297): about 1% less at 1024 bits, 2–3% less at 2048 and 4–6% more at 3072. It measures which candidates this tape yields, not how fast the code runs.

The other DSA benchmarks run code this PR does not change. With detected features they move by noise in both directions; with none they agree within 5%.

This conflicts with #4293 only in the cfgs of src/dsa/parameters*.rs. Whichever merges second gets rebased.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxFgLnhRDJS1FKU7Ub

Convert i686 DSA parameter generation in place from the three-tape
`vg_dsa_generate_parameters` to the refillable-tape search of
`Spec/Dsa/ParameterSearch.lean`: `vg_dsa_parameter_search` (one per SHA-1,
SHA-256 and Miller-Rabin backend, against `parameterSearchContract`) and
`vg_dsa_parameter_search_init` (against `parameterInitContract`).

The seeds, their witnesses and the generator's `h` candidates are all read
from `rand`: each seed at the cursor `fC`, its window of at most 128
candidates after p's witnesses (a short window without a g ran out); the
controller counts rejected seeds from `state[0]` and stops on parameters,
no seeds left, or a tape that ran out, and the result writes `out`, `used`
and `state` (the next tape's length `min(2 max(avail, 256 L), 32769 L)`, or
exhausted once the octets left reach the budget). The environment's seeds and
`h` candidates become views of `rand` (`Env.atSeed`, `Env.window`), so the
per-seed proofs (q, p, g and their constant time) are kept, and the old
controller, result, entry and ABI proofs are replaced. The seed proofs use
the shared `refillAttempt` and `search_*` lemmas
(`Proof/Dsa/Generation/RefillSeed.lean`, `RefillSearch.lean`).

No TCB or Spec change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxFgLnhRDJS1FKU7Ub
i686 joins x86-64 on the shared entropy::serve path, matching the SHA-1
and SHA-256 backends exhaustively over the four emitted variants. The
three-tape path and its i686 select are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxFgLnhRDJS1FKU7Ub
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxFgLnhRDJS1FKU7Ub
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 4136ba7 Nov 6, 2026
34 checks passed
@reaperhulk
reaperhulk deleted the claude/unify-random-bytes-crypto-xqmg0z-dsa-x86 branch November 6, 2026 04:35
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