Skip to content

The future of ThreadRng #1828

Description

@dhardy

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions