Skip to content

Commit

Permalink
Merge pull request #765 from dhardy/entropy
Browse files Browse the repository at this point in the history
Use getrandom, simplify OsRng, deprecate EntropyRng
  • Loading branch information
dhardy committed Apr 6, 2019
2 parents 3a5b711 + f84dccb commit 2abc78a
Show file tree
Hide file tree
Showing 33 changed files with 275 additions and 1,959 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Expand Up @@ -22,14 +22,14 @@ appveyor = { repository = "rust-random/rand" }
[features]
default = ["std"] # without "std" rand uses libcore
nightly = ["simd_support"] # enables all features requiring nightly rust
std = ["rand_core/std", "alloc", "rand_os", "rand_jitter/std"]
std = ["rand_core/std", "alloc", "getrandom"]
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
i128_support = [] # enables i128 and u128 support
simd_support = ["packed_simd"] # enables SIMD support
serde1 = ["rand_core/serde1", "rand_isaac/serde1", "rand_xorshift/serde1"] # enables serialization for PRNGs
# re-export optional WASM dependencies to avoid breakage:
wasm-bindgen = ["rand_os/wasm-bindgen"]
stdweb = ["rand_os/stdweb"]
wasm-bindgen = ["getrandom/wasm-bindgen"]
stdweb = ["getrandom/stdweb"]

[workspace]
members = [
Expand All @@ -49,9 +49,8 @@ members = [
[dependencies]
rand_core = { path = "rand_core", version = "0.4" }
rand_pcg = { path = "rand_pcg", version = "0.1" }
rand_jitter = { path = "rand_jitter", version = "0.1" }
rand_os = { path = "rand_os", version = "0.1", optional = true }
rand_hc = { path = "rand_hc", version = "0.1" }
getrandom = { version = "0.1.1", optional = true }
log = { version = "0.4", optional = true }

[dependencies.packed_simd]
Expand Down
9 changes: 3 additions & 6 deletions README.md
Expand Up @@ -75,11 +75,11 @@ pinned version of Rustc if you require compatibility with a specific version.

## Crate Features

Rand is built with the `std` and `rand_os` features enabled by default:
Rand is built with the `std` and `getrandom` features enabled by default:

- `std` enables functionality dependent on the `std` lib and implies `alloc`
and `rand_os`
- `rand_os` enables the `rand_os` crate, `rngs::OsRng` and enables its usage;
and `getrandom`
- `getrandom` is an optional crate, providing the code behind `rngs::OsRng`;
the continued existance of this feature is not guaranteed so users are
encouraged to specify `std` instead

Expand All @@ -102,9 +102,6 @@ functionality depending on `std`:

- `thread_rng()`, and `random()` are not available, as they require thread-local
storage and an entropy source.
- `OsRng` and `EntropyRng` are unavailable.
- `JitterRng` code is still present, but a nanosecond timer must be provided via
`JitterRng::new_with_timer`
- Since no external entropy is available, it is not possible to create
generators with fresh seeds using the `FromEntropy` trait (user must provide
a seed).
Expand Down
24 changes: 3 additions & 21 deletions benches/generators.rs
Expand Up @@ -25,7 +25,7 @@ use test::{black_box, Bencher};

use rand::prelude::*;
use rand::rngs::adapter::ReseedingRng;
use rand::rngs::{OsRng, JitterRng, EntropyRng};
use rand::rngs::OsRng;
use rand_isaac::{IsaacRng, Isaac64Rng};
use rand_chacha::ChaChaRng;
use rand_hc::{Hc128Rng, Hc128Core};
Expand Down Expand Up @@ -129,17 +129,6 @@ gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());

// Do not test JitterRng like the others by running it RAND_BENCH_N times per,
// measurement, because it is way too slow. Only run it once.
#[bench]
fn gen_u64_jitter(b: &mut Bencher) {
let mut rng = JitterRng::new().unwrap();
b.iter(|| {
rng.gen::<u64>()
});
b.bytes = size_of::<u64>() as u64;
}

macro_rules! init_gen {
($fnn:ident, $gen:ident) => {
#[bench]
Expand Down Expand Up @@ -170,13 +159,6 @@ init_gen!(init_isaac, IsaacRng);
init_gen!(init_isaac64, Isaac64Rng);
init_gen!(init_chacha, ChaChaRng);

#[bench]
fn init_jitter(b: &mut Bencher) {
b.iter(|| {
JitterRng::new().unwrap()
});
}


const RESEEDING_THRESHOLD: u64 = 1024*1024*1024; // something high enough to get
// deterministic measurements
Expand All @@ -185,7 +167,7 @@ const RESEEDING_THRESHOLD: u64 = 1024*1024*1024; // something high enough to get
fn reseeding_hc128_bytes(b: &mut Bencher) {
let mut rng = ReseedingRng::new(Hc128Core::from_entropy(),
RESEEDING_THRESHOLD,
EntropyRng::new());
OsRng);
let mut buf = [0u8; BYTES_LEN];
b.iter(|| {
for _ in 0..RAND_BENCH_N {
Expand All @@ -202,7 +184,7 @@ macro_rules! reseeding_uint {
fn $fnn(b: &mut Bencher) {
let mut rng = ReseedingRng::new(Hc128Core::from_entropy(),
RESEEDING_THRESHOLD,
EntropyRng::new());
OsRng);
b.iter(|| {
let mut accum: $ty = 0;
for _ in 0..RAND_BENCH_N {
Expand Down
4 changes: 2 additions & 2 deletions rand_jitter/README.md
@@ -1,10 +1,10 @@
# rand_jitter
[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand)
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
[![Latest version](https://img.shields.io/crates/v/rand_os.svg)](https://crates.io/crates/rand_jitter)
[![Latest version](https://img.shields.io/crates/v/rand_jitter.svg)](https://crates.io/crates/rand_jitter)
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_jitter)
[![API](https://docs.rs/rand_os/badge.svg)](https://docs.rs/rand_jitter)
[![API](https://docs.rs/rand_jitter/badge.svg)](https://docs.rs/rand_jitter)
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.32+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)

Non-physical true random number generator based on timing jitter.
Expand Down
4 changes: 4 additions & 0 deletions rand_os/CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.2.0] - 2019-04-08
Replaced implementation with a backwards-compatible shim around
[getrandom](https://crates.io/crates/getrandom).

## [0.1.3] - 2019-03-05
### Changes
- Fix support for Illumos (#730)
Expand Down
30 changes: 9 additions & 21 deletions rand_os/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand_os"
version = "0.1.3"
version = "0.2.0"
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -9,30 +9,18 @@ documentation = "https://docs.rs/rand_os"
homepage = "https://crates.io/crates/rand_os"
description = "OS backed Random Number Generator"
keywords = ["random", "rng", "os"]
edition = "2018"

[badges]
travis-ci = { repository = "rust-random/rand" }
appveyor = { repository = "rust-random/rand" }

[features]
log = ["getrandom/log"]
# re-export optional WASM dependencies to avoid breakage:
wasm-bindgen = ["getrandom/wasm-bindgen"]
stdweb = ["getrandom/stdweb"]

[dependencies]
rand_core = { path = "../rand_core", version = "0.4", features = ["std"] }
log = { version = "0.4", optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] }

[target.'cfg(target_os = "cloudabi")'.dependencies]
cloudabi = "0.0.3"

[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-cprng = "0.1.0"

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = { version = "0.2.12", optional = true }
stdweb = { version = "0.4", optional = true }

[target.'cfg(target_env = "sgx")'.dependencies]
rdrand = "0.5.0"
getrandom = "0.1.1"
10 changes: 5 additions & 5 deletions rand_os/README.md
Expand Up @@ -11,12 +11,12 @@
A random number generator that retrieves randomness straight from the
operating system.

This crate depends on [rand_core](https://crates.io/crates/rand_core) and is
part of the [Rand project](https://github.com/rust-random/rand).
This crate provides `OsRng` as a shim around
[getrandom](https://crates.io/crates/getrandom)
implementing `RngCore` from [rand_core](https://crates.io/crates/rand_core).

This crate aims to support all of Rust's `std` platforms with a system-provided
entropy source. Unlike other Rand crates, this crate does not support `no_std`
(handling this gracefully is a current discussion topic).
Note: the `rand` crate provides an equivalent `OsRng`; the two implementations
are equivalent, though distinct types.

Links:

Expand Down
39 changes: 0 additions & 39 deletions rand_os/src/cloudabi.rs

This file was deleted.

39 changes: 0 additions & 39 deletions rand_os/src/dragonfly_haiku_emscripten.rs

This file was deleted.

10 changes: 0 additions & 10 deletions rand_os/src/dummy_log.rs

This file was deleted.

45 changes: 0 additions & 45 deletions rand_os/src/freebsd.rs

This file was deleted.

28 changes: 0 additions & 28 deletions rand_os/src/fuchsia.rs

This file was deleted.

0 comments on commit 2abc78a

Please sign in to comment.