Skip to content

Keep the fixed-tape hooks only where benchmarks need them - #4271

Merged
reaperhulk merged 6 commits into
mainfrom
claude/upbeat-ritchie-k1qwi2
Nov 5, 2026
Merged

reaperhulk merged 6 commits into
mainfrom
claude/upbeat-ritchie-k1qwi2

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

The *_with_rng functions (and rsa::keygen::generate_from) took their random bytes from the caller so the benchmarks could run on fixed tapes. They were pub and only #[doc(hidden)], so any user could call them, and some unit tests relied on them.

What changes

Hooks removed (6 functions + ML-KEM's benchmark use). RSA-PSS signing, OAEP and PKCS #1 v1.5 encryption, DSA signing and key generation, and FFDH private-key generation take the same time whatever their random bytes. The RSA ones are proven constant-time in them; the others only redraw rarely, which criterion averages out. Their *_with_rng functions are gone and their benchmarks call the public API. The ML-KEM benchmarks also call encapsulate now, not encapsulate_internal, which stays pub and hidden for the FIPS 203 known-answer tests in tests/.

Hooks kept, behind a new internal-bench feature (3 functions). The prime searches take as long as the draws make them: how many candidates get rejected depends on the random bytes. Only these keep a generate_with_rng, and only the bench crate enables the feature:

  • rsa::keygen::generate_with_rng
  • dsa::Parameters::generate_with_rng
  • ffdh::Parameters::generate_with_rng

Nothing but the benchmarks uses them: the library, its unit tests and the integration tests build and pass without the feature.

RSA key generation. generate_from read a slice through its own search loop (prime_from), which the unit tests used. It is replaced by generate_with_rng, which goes through the same Random refills as generate. prime_from, Search::budget and Error::NotEnoughRandomness (only that path could return it) are removed. The unit tests run the search over a given stream instead. The benchmark records the tape each seed's generation draws and replays the median one.

Tests. Where unit tests give chosen bytes (DSA, OAEP, PKCS #1 v1.5 encryption), they call private *_from functions. The tests that a fixed tape gives the same result are dropped (RSA-PSS's Wycheproof check, FFDH's two): they only checked that the caller's bytes are the ones used, and FFDH's generation paths stay covered by tests/cavp/ffdh.rs.

API change: the *_with_rng functions and rsa::keygen::Error::NotEnoughRandomness are removed (all were undocumented).

No changes to TCB/, Spec/ or any Lean file.

Benchmarks

No Benchmarks workflow run: the library's code paths are unchanged. The benchmarks themselves change:

  • The six moved to the public API now include one OS random-number call per operation, so their numbers may shift slightly against earlier runs.
  • The RSA key-generation benchmark now times the same refill path as generate.

All changed benchmarks pass cargo bench -- --test.

Checks run

  • cargo fmt --check.
  • cargo clippy --all-targets -D warnings with no features, internal-bench, cpu-features-env and std, and on the bench crate.
  • The DSA, RSA and FFDH unit tests.
  • Every step of the Static checks job.
  • Not run locally: the Wycheproof suite and the coverage merge.

Generated by Claude Code

The `*_with_rng` functions and `rsa::keygen::generate_from` take their
random bytes from the caller so that the benchmarks can run on identical
work from fixed tapes. They were `pub` and only `#[doc(hidden)]`, so any
user could call them. Now they exist only with the new `internal-bench`
feature, which only the benchmarks enable.

Each public function and its gated twin call a private `*_from` function
that the unit tests use directly. `rsa::keygen::generate_from` and what
only it uses are also built for the unit tests.

Drop the Wycheproof RSA-PSS check that a fixed salt gives the same
signature: it only checked that the closure's bytes become the salt, and
signing is constant-time in the salt anyway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
Like the RSA-PSS one, they only checked that the caller's bytes are the
ones used. tests/cavp/ffdh.rs still runs both generation paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
`rsa::keygen::generate_from` read a slice through its own prime search
loop (`prime_from`), which the unit tests used, so the tests needed the
code that only the benchmark should. Replace it with a gated
`generate_with_rng`, as the other primitives have, which goes through
the same `Random` refills as `generate`.

`prime_from`, `Search::budget` and `Error::NotEnoughRandomness`, which
only that path used, are gone. The unit tests run the search through
`Random` over a given stream instead. The benchmark records each seed's
tape through `generate_with_rng` and replays the median one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
RSA-PSS signing, OAEP and PKCS #1 v1.5 encryption, and DSA and FFDH key
generation and signing take the same time whatever their random bytes, but
for a rare redraw that the benchmarks average out, so they need no fixed
tape. Their benchmarks call the public functions, and their
`*_with_rng` functions are gone. Only the prime searches, RSA key
generation and DSA's and FFDH's parameter generation, keep a
`generate_with_rng` under `internal-bench`: how long they take depends on
how many candidates the draws reject.

RSA-PSS's `sign` draws its salt itself again, as nothing else used
`sign_from`; the others keep their private functions, which the unit
tests give chosen bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
Encapsulation takes the same time whatever `m`, and decapsulation
whatever the ciphertext, so the benchmarks need no fixed `m`: they call
`encapsulate`, and leave `encapsulate_internal` to the known-answer
tests it exists for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
`main` made RSA's sign and encrypt functions methods of the keys
(`sign_pss_with_rng`, `encrypt_oaep_with_rng`,
`encrypt_pkcs1v15_with_rng`): those hooks are gone too, `sign_pss` draws
its salt itself, the encryptions keep private `*_from` methods for their
unit tests, and the benchmarks call `sign_pss`, `encrypt_oaep` and
`encrypt_pkcs1v15`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwGpXLG9ZS1C7YjUJm
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit a050a9b Nov 5, 2026
34 checks passed
@reaperhulk
reaperhulk deleted the claude/upbeat-ritchie-k1qwi2 branch November 5, 2026 16:49
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