Tracking Issue for secure random data generation in std (original) (raw)
Feature gate: #![feature(random)]
This is a tracking issue for secure random data generation support in std.
Central to this feature is the RandomSource trait inside core::random, which generates random bytes. std also exposes the platform's secure random number generator via the DefaultRandomSource type. There is a Distribution<T> trait for distributions that can sample random values of a specific type, and a random function for convenience to allow things like random(1..=6).
Public API
// core::random
pub trait RandomSource { fn fill_bytes(&mut self, bytes: &mut [u8]); }
pub trait Distribution { fn sample(&self, source: &mut (impl RandomSource + ?Sized)) -> T; }
impl Distribution for RangeFull { ... } impl Distribution</* all integer types /> for / all range types */ { ... }
// std::random (additionally)
pub struct DefaultRandomSource;
impl RandomSource for DefaultRandomSource { ... }
pub fn random(dist: &(impl Distribution + ?Sized)) -> T;
Steps / History
- ACP: Simple secure random number generation libs-team#393
- Implementation
- std: implement the random feature (alternative version) #129201
- API rework for
Distribution<T>and newrandom
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Naming: the ACP used
gen_bytesandDefaultRng, the implementation PR usesfill_bytesandDefaultRandomSource(see arguments pro gen_bytes and pro fill_bytes) - Concerns listed at Simple secure random number generation libs-team#393 (comment) should be addressed