rust-lang / rust-lang/rust

Tracking Issue for secure random data generation in `std`

Open
#130,703 196 comments 89 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

View all comments

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 Rng {
    fn fill_bytes(&mut self, bytes: &mut [u8]);
}

impl<'a, R: Rng + ?Sized> Rng for &'a mut R { ... }

pub trait Distribution<T> {
    fn sample(&self, source: &mut (impl Rng + ?Sized)) -> T;
}

impl Distribution<bool> for RangeFull { ... }
impl Distribution</* all integer types */> for RangeFull { ... }
impl Distribution</* all integer types */> for core::range::RangeInclusive<_> { ... }

// std::random (additionally)

pub struct SystemRng;

impl Rng for SystemRng { ... }

pub fn random<T>(dist: impl Distribution<T>) -> T;
Steps / History
Unresolved Questions

Contributor guide

Open the contributing guide

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 reading the core::random and std::random API sections, then inspect the remaining implementation item #160205 and stabilization PR #157168. The issue's done state is completion of the listed implementation, resolution of the naming and other unresolved concerns, and the FCP and stabilization work.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems, security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.