rust-random / rust-random/rand

The future of `ThreadRng`

Open
#1,828 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
512
Avg merge
3d 9h
Merged PRs (30d)
7

Description

Can we have one generator to do it all (excepting reproducibility)? ThreadRng attempts to achieve this:

  • It's fairly fast
  • It only requires 320 bytes + overhead (Rc and thread_local) per thread
  • It's unpredictable
  • It has periodic reseeding

Put another way, there are reasons that this is sub-optimal:

  • SmallRng is significantly faster (very roughly double the performance) and much smaller (32 bytes) while being good enough for many uses
  • ThreadRng does not currently have forward secrecy. We could tack this on for approx 10-25% performance overhead.

Plan

ThreadRng has quite a few users, so I do not believe we should just abandon it. Instead, I propose:

  • Re-add ReseedingRng not as a generic wrapper but as a concrete struct over ChaCha12 with periodic reseeding and fast erasure.
  • Add StrongRng (name?) as a handle to a Mutex<ReseedingRng> in static memory (feature-gated, default off).
  • Add SmallThreadRng as a thread-local SmallRng and maybe rand::fast_rng() to access it (feature-gated, default off).
  • If possible, #[deprecate] the existing ThreadRng and methods after adding replacements before the next breaking release.

Note: the fast-erasure (forward secrecy) addition requires breaking changes to rand_core. The rest should be possible to add in a patch release.

Note: this is very much a tentative plan. Feedback welcome!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing ThreadRng and rand_core interfaces, then compare the proposed ReseedingRng over ChaCha12, StrongRng, and SmallThreadRng entry points. Done means the tentative replacement and deprecation plan is resolved and implemented, with the required forward-secrecy changes to rand_core accounted for.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cryptography
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.