Skip to content

Commit 2abc78a

Browse files
authored
Merge pull request #765 from dhardy/entropy
Use getrandom, simplify OsRng, deprecate EntropyRng
2 parents 3a5b711 + f84dccb commit 2abc78a

33 files changed

+275
-1959
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ appveyor = { repository = "rust-random/rand" }
2222
[features]
2323
default = ["std"] # without "std" rand uses libcore
2424
nightly = ["simd_support"] # enables all features requiring nightly rust
25-
std = ["rand_core/std", "alloc", "rand_os", "rand_jitter/std"]
25+
std = ["rand_core/std", "alloc", "getrandom"]
2626
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
2727
i128_support = [] # enables i128 and u128 support
2828
simd_support = ["packed_simd"] # enables SIMD support
2929
serde1 = ["rand_core/serde1", "rand_isaac/serde1", "rand_xorshift/serde1"] # enables serialization for PRNGs
3030
# re-export optional WASM dependencies to avoid breakage:
31-
wasm-bindgen = ["rand_os/wasm-bindgen"]
32-
stdweb = ["rand_os/stdweb"]
31+
wasm-bindgen = ["getrandom/wasm-bindgen"]
32+
stdweb = ["getrandom/stdweb"]
3333

3434
[workspace]
3535
members = [
@@ -49,9 +49,8 @@ members = [
4949
[dependencies]
5050
rand_core = { path = "rand_core", version = "0.4" }
5151
rand_pcg = { path = "rand_pcg", version = "0.1" }
52-
rand_jitter = { path = "rand_jitter", version = "0.1" }
53-
rand_os = { path = "rand_os", version = "0.1", optional = true }
5452
rand_hc = { path = "rand_hc", version = "0.1" }
53+
getrandom = { version = "0.1.1", optional = true }
5554
log = { version = "0.4", optional = true }
5655

5756
[dependencies.packed_simd]

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ pinned version of Rustc if you require compatibility with a specific version.
7575

7676
## Crate Features
7777

78-
Rand is built with the `std` and `rand_os` features enabled by default:
78+
Rand is built with the `std` and `getrandom` features enabled by default:
7979

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

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

103103
- `thread_rng()`, and `random()` are not available, as they require thread-local
104104
storage and an entropy source.
105-
- `OsRng` and `EntropyRng` are unavailable.
106-
- `JitterRng` code is still present, but a nanosecond timer must be provided via
107-
`JitterRng::new_with_timer`
108105
- Since no external entropy is available, it is not possible to create
109106
generators with fresh seeds using the `FromEntropy` trait (user must provide
110107
a seed).

benches/generators.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use test::{black_box, Bencher};
2525

2626
use rand::prelude::*;
2727
use rand::rngs::adapter::ReseedingRng;
28-
use rand::rngs::{OsRng, JitterRng, EntropyRng};
28+
use rand::rngs::OsRng;
2929
use rand_isaac::{IsaacRng, Isaac64Rng};
3030
use rand_chacha::ChaChaRng;
3131
use rand_hc::{Hc128Rng, Hc128Core};
@@ -129,17 +129,6 @@ gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
129129
gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
130130
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());
131131

132-
// Do not test JitterRng like the others by running it RAND_BENCH_N times per,
133-
// measurement, because it is way too slow. Only run it once.
134-
#[bench]
135-
fn gen_u64_jitter(b: &mut Bencher) {
136-
let mut rng = JitterRng::new().unwrap();
137-
b.iter(|| {
138-
rng.gen::<u64>()
139-
});
140-
b.bytes = size_of::<u64>() as u64;
141-
}
142-
143132
macro_rules! init_gen {
144133
($fnn:ident, $gen:ident) => {
145134
#[bench]
@@ -170,13 +159,6 @@ init_gen!(init_isaac, IsaacRng);
170159
init_gen!(init_isaac64, Isaac64Rng);
171160
init_gen!(init_chacha, ChaChaRng);
172161

173-
#[bench]
174-
fn init_jitter(b: &mut Bencher) {
175-
b.iter(|| {
176-
JitterRng::new().unwrap()
177-
});
178-
}
179-
180162

181163
const RESEEDING_THRESHOLD: u64 = 1024*1024*1024; // something high enough to get
182164
// deterministic measurements
@@ -185,7 +167,7 @@ const RESEEDING_THRESHOLD: u64 = 1024*1024*1024; // something high enough to get
185167
fn reseeding_hc128_bytes(b: &mut Bencher) {
186168
let mut rng = ReseedingRng::new(Hc128Core::from_entropy(),
187169
RESEEDING_THRESHOLD,
188-
EntropyRng::new());
170+
OsRng);
189171
let mut buf = [0u8; BYTES_LEN];
190172
b.iter(|| {
191173
for _ in 0..RAND_BENCH_N {
@@ -202,7 +184,7 @@ macro_rules! reseeding_uint {
202184
fn $fnn(b: &mut Bencher) {
203185
let mut rng = ReseedingRng::new(Hc128Core::from_entropy(),
204186
RESEEDING_THRESHOLD,
205-
EntropyRng::new());
187+
OsRng);
206188
b.iter(|| {
207189
let mut accum: $ty = 0;
208190
for _ in 0..RAND_BENCH_N {

rand_jitter/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# rand_jitter
22
[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand)
33
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand)
4-
[![Latest version](https://img.shields.io/crates/v/rand_os.svg)](https://crates.io/crates/rand_jitter)
4+
[![Latest version](https://img.shields.io/crates/v/rand_jitter.svg)](https://crates.io/crates/rand_jitter)
55
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
66
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_jitter)
7-
[![API](https://docs.rs/rand_os/badge.svg)](https://docs.rs/rand_jitter)
7+
[![API](https://docs.rs/rand_jitter/badge.svg)](https://docs.rs/rand_jitter)
88
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.32+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements)
99

1010
Non-physical true random number generator based on timing jitter.

rand_os/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [0.2.0] - 2019-04-08
9+
Replaced implementation with a backwards-compatible shim around
10+
[getrandom](https://crates.io/crates/getrandom).
11+
812
## [0.1.3] - 2019-03-05
913
### Changes
1014
- Fix support for Illumos (#730)

rand_os/Cargo.toml

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand_os"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
authors = ["The Rand Project Developers"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"
@@ -9,30 +9,18 @@ documentation = "https://docs.rs/rand_os"
99
homepage = "https://crates.io/crates/rand_os"
1010
description = "OS backed Random Number Generator"
1111
keywords = ["random", "rng", "os"]
12+
edition = "2018"
1213

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

18+
[features]
19+
log = ["getrandom/log"]
20+
# re-export optional WASM dependencies to avoid breakage:
21+
wasm-bindgen = ["getrandom/wasm-bindgen"]
22+
stdweb = ["getrandom/stdweb"]
23+
1724
[dependencies]
1825
rand_core = { path = "../rand_core", version = "0.4", features = ["std"] }
19-
log = { version = "0.4", optional = true }
20-
21-
[target.'cfg(unix)'.dependencies]
22-
libc = "0.2"
23-
24-
[target.'cfg(windows)'.dependencies]
25-
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] }
26-
27-
[target.'cfg(target_os = "cloudabi")'.dependencies]
28-
cloudabi = "0.0.3"
29-
30-
[target.'cfg(target_os = "fuchsia")'.dependencies]
31-
fuchsia-cprng = "0.1.0"
32-
33-
[target.wasm32-unknown-unknown.dependencies]
34-
wasm-bindgen = { version = "0.2.12", optional = true }
35-
stdweb = { version = "0.4", optional = true }
36-
37-
[target.'cfg(target_env = "sgx")'.dependencies]
38-
rdrand = "0.5.0"
26+
getrandom = "0.1.1"

rand_os/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
A random number generator that retrieves randomness straight from the
1212
operating system.
1313

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

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

2121
Links:
2222

rand_os/src/cloudabi.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

rand_os/src/dragonfly_haiku_emscripten.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

rand_os/src/dummy_log.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)