forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the ACP rust-lang/libs-team#393.
- Loading branch information
Showing
42 changed files
with
754 additions
and
480 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Random value generation. | ||
//! | ||
//! The [`Random`] trait allows generating a random value for a type using a | ||
//! given [`RandomSource`]. | ||
/// A source of randomness. | ||
#[unstable(feature = "random", issue = "none")] | ||
pub trait RandomSource { | ||
/// Fills `bytes` with random bytes. | ||
fn fill_bytes(&mut self, bytes: &mut [u8]); | ||
} | ||
|
||
/// A trait for getting a random value for a type. | ||
/// | ||
/// **Warning:** Be careful when manipulating random values! The | ||
/// [`random`](Random::random) method on integers samples them with a uniform | ||
/// distribution, so a value of 1 is just as likely as [`i32::MAX`]. By using | ||
/// modulo operations, some of the resulting values can become more likely than | ||
/// others. Use audited crates when in doubt. | ||
#[unstable(feature = "random", issue = "none")] | ||
pub trait Random: Sized { | ||
/// Generates a random value. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self; | ||
} | ||
|
||
impl Random for bool { | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
u8::random(source) & 1 == 1 | ||
} | ||
} | ||
|
||
impl Random for u8 { | ||
/// Generates a random value. | ||
/// | ||
/// **Warning:** Be careful when manipulating the resulting value! This | ||
/// method samples according to a uniform distribution, so a value of 1 is | ||
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some | ||
/// values can become more likely than others. Use audited crates when in | ||
/// doubt. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
let mut byte = [0]; | ||
source.fill_bytes(&mut byte); | ||
byte[0] | ||
} | ||
} | ||
|
||
impl Random for i8 { | ||
/// Generates a random value. | ||
/// | ||
/// **Warning:** Be careful when manipulating the resulting value! This | ||
/// method samples according to a uniform distribution, so a value of 1 is | ||
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some | ||
/// values can become more likely than others. Use audited crates when in | ||
/// doubt. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
u8::random(source) as i8 | ||
} | ||
} | ||
|
||
macro_rules! impl_primitive { | ||
($t:ty) => { | ||
impl Random for $t { | ||
/// Generates a random value. | ||
/// | ||
/// **Warning:** Be careful when manipulating the resulting value! This | ||
/// method samples according to a uniform distribution, so a value of 1 is | ||
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some | ||
/// values can become more likely than others. Use audited crates when in | ||
/// doubt. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
let mut bytes = (0 as Self).to_ne_bytes(); | ||
source.fill_bytes(&mut bytes); | ||
Self::from_ne_bytes(bytes) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_primitive!(u16); | ||
impl_primitive!(i16); | ||
impl_primitive!(u32); | ||
impl_primitive!(i32); | ||
impl_primitive!(u64); | ||
impl_primitive!(i64); | ||
impl_primitive!(u128); | ||
impl_primitive!(i128); | ||
impl_primitive!(usize); | ||
impl_primitive!(isize); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! Random value generation. | ||
//! | ||
//! The [`Random`] trait allows generating a random value for a type using a | ||
//! given [`RandomSource`]. | ||
#[unstable(feature = "random", issue = "none")] | ||
pub use core::random::*; | ||
|
||
use crate::sys::random as sys; | ||
|
||
/// The default random source. | ||
/// | ||
/// This asks the system for the best random data it can provide, meaning the | ||
/// resulting bytes *should* be usable for cryptographic purposes. Check your | ||
/// platform's documentation for the specific guarantees it provides. The high | ||
/// quality of randomness provided by this source means it is quite slow. If | ||
/// you need a larger quantity of random numbers, consider using another random | ||
/// number generator (potentially seeded from this one). | ||
/// | ||
/// # Examples | ||
/// | ||
/// Generating a [version 4/variant 1 UUID] represented as text: | ||
/// ``` | ||
/// #![feature(random)] | ||
/// | ||
/// use std::random::{DefaultRandomSource, Random}; | ||
/// | ||
/// let bits = u128::random(&mut DefaultRandomSource); | ||
/// let g1 = (bits >> 96) as u32; | ||
/// let g2 = (bits >> 80) as u16; | ||
/// let g3 = (0x4000 | (bits >> 64) & 0x0fff) as u16; | ||
/// let g4 = (0x8000 | (bits >> 48) & 0x3fff) as u16; | ||
/// let g5 = (bits & 0xffffffffffff) as u64; | ||
/// let uuid = format!("{g1:08x}-{g2:04x}-{g3:04x}-{g4:04x}-{g5:012x}"); | ||
/// println!("{uuid}"); | ||
/// ``` | ||
/// | ||
/// [version 4/variant 1 UUID]: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random) | ||
/// | ||
/// # Underlying sources | ||
/// | ||
/// Platform | Source | ||
/// -----------------------|--------------------------------------------------------------- | ||
/// Linux | [`getrandom`] or [`/dev/urandom`] after polling `/dev/random` | ||
/// Windows | [`ProcessPrng`] | ||
/// macOS and other UNIXes | [`getentropy`] | ||
/// other Apple platforms | `CCRandomGenerateBytes` | ||
/// Fuchsia | [`cprng_draw`] | ||
/// Hermit | `read_entropy` | ||
/// Horizon | `getrandom` shim | ||
/// Hurd, L4Re, QNX | `/dev/urandom` | ||
/// NetBSD before 10.0 | [`kern.arandom`] | ||
/// Redox | `/scheme/rand` | ||
/// SGX | [`rdrand`] | ||
/// SOLID | `SOLID_RNG_SampleRandomBytes` | ||
/// TEEOS | `TEE_GenerateRandom` | ||
/// UEFI | [`EFI_RNG_PROTOCOL`] | ||
/// VxWorks | `randABytes` after waiting for `randSecure` to become ready | ||
/// WASI | `random_get` | ||
/// ZKVM | `sys_rand` | ||
/// | ||
/// **Disclaimer:** The sources used might change over time. | ||
/// | ||
/// [`getrandom`]: https://www.man7.org/linux/man-pages/man2/getrandom.2.html | ||
/// [`/dev/urandom`]: https://www.man7.org/linux/man-pages/man4/random.4.html | ||
/// [`ProcessPrng`]: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng | ||
/// [`getentropy`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html | ||
/// [`cprng_draw`]: https://fuchsia.dev/reference/syscalls/cprng_draw | ||
/// [`kern.arandom`]: https://man.netbsd.org/rnd.4 | ||
/// [`rdrand`]: https://en.wikipedia.org/wiki/RDRAND | ||
/// [`EFI_RNG_PROTOCOL`]: https://uefi.org/specs/UEFI/2.10/37_Secure_Technologies.html#random-number-generator-protocol | ||
#[derive(Default, Debug, Clone, Copy)] | ||
#[unstable(feature = "random", issue = "none")] | ||
pub struct DefaultRandomSource; | ||
|
||
#[unstable(feature = "random", issue = "none")] | ||
impl RandomSource for DefaultRandomSource { | ||
fn fill_bytes(&mut self, bytes: &mut [u8]) { | ||
sys::fill_bytes(bytes, false) | ||
} | ||
} | ||
|
||
/// A best-effort random source used for creating hash-map seeds. | ||
#[unstable(feature = "random", issue = "none")] | ||
pub(crate) struct BestEffortRandomSource; | ||
|
||
impl RandomSource for BestEffortRandomSource { | ||
fn fill_bytes(&mut self, bytes: &mut [u8]) { | ||
sys::fill_bytes(bytes, true) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.