Repository navigation
Keep the fixed-tape hooks only where benchmarks need them - #4271
Merged
Merged
Conversation
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
This was referenced Nov 5, 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.
The
*_with_rngfunctions (andrsa::keygen::generate_from) took their random bytes from the caller so the benchmarks could run on fixed tapes. They werepuband 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_rngfunctions are gone and their benchmarks call the public API. The ML-KEM benchmarks also callencapsulatenow, notencapsulate_internal, which stayspuband hidden for the FIPS 203 known-answer tests intests/.Hooks kept, behind a new
internal-benchfeature (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 agenerate_with_rng, and only the bench crate enables the feature:rsa::keygen::generate_with_rngdsa::Parameters::generate_with_rngffdh::Parameters::generate_with_rngNothing but the benchmarks uses them: the library, its unit tests and the integration tests build and pass without the feature.
RSA key generation.
generate_fromread a slice through its own search loop (prime_from), which the unit tests used. It is replaced bygenerate_with_rng, which goes through the sameRandomrefills asgenerate.prime_from,Search::budgetandError::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
*_fromfunctions. 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 bytests/cavp/ffdh.rs.API change: the
*_with_rngfunctions andrsa::keygen::Error::NotEnoughRandomnessare 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:
generate.All changed benchmarks pass
cargo bench -- --test.Checks run
cargo fmt --check.cargo clippy --all-targets -D warningswith no features,internal-bench,cpu-features-envandstd, and on the bench crate.Generated by Claude Code