Skip to content

Commit 410a418

Browse files
authored
Merge pull request #98 from rust-random/remove-rand_chacha
Prefer RNGs from `rand` over RNGs from other crates
2 parents 80f5d28 + 0460bbe commit 410a418

7 files changed

Lines changed: 60 additions & 73 deletions

File tree

src/contrib-doc.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,6 @@ The [rust-random/getrandom](https://github.com/rust-random/getrandom)
4646
repository contains only a single crate, hence a simple `cargo doc` will
4747
suffice.
4848

49-
### Cross-crate links
50-
51-
When referring to another crate, we prefer linking to the crate page on
52-
crates.io since (a) this includes the README documenting the purpose of the
53-
crate and (b) this links directly to both the repository and the API
54-
documentation. Example:
55-
56-
```rust,noplayground
57-
// Link to the crate page:
58-
//! [`rand_chacha`]: https://crates.io/crates/rand_chacha
59-
```
60-
61-
When referring to an item from within another crate,
62-
63-
1. if that item is accessible via a crate dependency (even if not via the
64-
public API), use the Rust item path
65-
2. otherwise, use an absolute link to docs.rs
66-
67-
Examples:
68-
69-
```rust,noplayground
70-
//! We depend on rand_core, therefore can use the Rust path:
71-
//! [`BlockRngCore`]: rand_core::block::BlockRngCore
72-
73-
//! rand_chacha is not a dependency, but is within the same repository:
74-
//! [`ChaCha20Rng`]: ../../rand_chacha/struct.ChaCha20Rng.html
75-
76-
//! Link directly to docs.rs, with major & minor but no patch version:
77-
//! [`getrandom`]: https://docs.rs/getrandom/0.1/getrandom/fn.getrandom.html
78-
```
79-
8049
## Auxiliary documentation
8150

8251
### README files

src/contrib-scope.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ following reasons:
5858
- to provide a few high-quality alternative generators
5959
- historical usage
6060

61-
These are implemented within "family" crates, e.g. `rand_chacha`, `rand_pcg`,
61+
These are implemented within "family" crates, e.g. `chacha20`, `rand_pcg`,
6262
`rand_xoshiro`.
6363

6464
We have received several requests to adopt new algorithms into the library; when

src/crate-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Release versions of `Cargo.toml` can be viewed on `docs.rs`:
99
- <https://docs.rs/crate/rand/latest/source/Cargo.toml.orig>
1010
- <https://docs.rs/crate/rand_core/latest/source/Cargo.toml.orig>
1111
- <https://docs.rs/crate/rand_distr/latest/source/Cargo.toml.orig>
12-
- <https://docs.rs/crate/rand_chacha/latest/source/Cargo.toml.orig>
12+
- <https://docs.rs/crate/chacha20/latest/source/Cargo.toml.orig>
1313
- <https://docs.rs/crate/rand_xoshiro/latest/source/Cargo.toml.orig>
1414
- <https://docs.rs/crate/rand_pcg/latest/source/Cargo.toml.orig>
1515

src/crates.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# The crate family
22

33
<pre><code class="language-plain"> ┌ <a href="https://docs.rs/statrs/">statrs</a>
4-
<a href="https://docs.rs/getrandom/">getrandom</a> ┐ ├ <a href="https://docs.rs/rand_distr/">rand_distr</a>
5-
└ <a href="https://docs.rs/rand_core/">rand_core</a> ┬─────────────┬ <a href="https://docs.rs/rand/">rand</a> ┘
6-
├ <a href="https://docs.rs/rand_chacha/">rand_chacha</a> ┘
7-
├ <a href="https://docs.rs/rand_pcg/">rand_pcg</a>
8-
└ [other RNG crates]
4+
├ <a href="https://docs.rs/rand_distr/">rand_distr</a>
5+
<a href="https://docs.rs/rand_core/">rand_core</a> ┬───────────────┬ <a href="https://docs.rs/rand/">rand</a> ┘
6+
├ <a href="https://docs.rs/chacha20/">chacha20</a> ─────┤
7+
├ <a href="https://docs.rs/getrandom/">getrandom</a> ────┘
8+
├ <a href="https://docs.rs/rand_chacha/">rand_chacha</a>
9+
├ <a href="https://docs.rs/rand_hc/">rand_hc</a>
10+
├ <a href="https://docs.rs/rand_isaac/">rand_isaac</a>
11+
├ <a href="https://docs.rs/rand_jitter/">rand_jitter</a>
12+
├ <a href="https://docs.rs/rand_pcg/">rand_pcg</a>
13+
├ <a href="https://docs.rs/rand_sfc/">rand_sfc</a>
14+
├ <a href="https://docs.rs/rand_seeder/">rand_seeder</a>
15+
├ <a href="https://docs.rs/rand_xorshift/">rand_xorshift</a>
16+
└ <a href="https://docs.rs/rand_xoshiro/">rand_xoshiro</a>
917
</code></pre>
1018

1119
## Interfaces
@@ -20,15 +28,23 @@ random-number sources.
2028
The following crates implement pseudo-random number generators
2129
(see [Our RNGs](guide-rngs.md)):
2230

23-
- [`rand_chacha`] provides generators using the ChaCha cipher
31+
- [`chacha20`] provides generators using the ChaCha cipher
32+
(used internally by [`rand`] via the `chacha` feature)
33+
- [`rand_chacha`] also provides ChaCha-cipher generators
34+
(predecessor of `chacha20` for this purpose; kept for compatibility)
2435
- [`rand_hc`] implements a generator using the HC-128 cipher
2536
- [`rand_isaac`] implements the ISAAC generators
2637
- [`rand_pcg`] implements a small selection of PCG generators
27-
- [`rand_xoshiro`] implements the SplitMix and Xoshiro generators
38+
- [`rand_sfc`] implements the SFC (Small Fast Counter) generators
2839
- [`rand_xorshift`] implements the basic Xorshift generator
40+
- [`rand_xoshiro`] implements the SplitMix and Xoshiro generators
2941

3042
Exceptionally, [`SmallRng`] is implemented directly in [`rand`].
3143

44+
In addition, [`rand_jitter`] provides a jitter-based entropy source, and
45+
[`rand_seeder`] derives seeds from arbitrary hashable data (used as a building
46+
block in [Seeding RNGs](guide-seeding.md)).
47+
3248
## rand (main crate)
3349

3450
The [`rand`] crate is designed for easy usage of common random-number
@@ -59,13 +75,17 @@ number distributions: uniform and weighted sampling. For everything else,
5975
[`rand_distr`]: https://docs.rs/rand_distr/
6076
[`statrs`]: https://docs.rs/statrs/
6177
[`getrandom`]: https://docs.rs/getrandom/
62-
[`rand_chacha`]: https://docs.rs/rand_chacha/
78+
[`chacha20`]: https://docs.rs/chacha20/
6379
[`rand_pcg`]: https://docs.rs/rand_pcg/
6480
[`rand_xoshiro`]: https://docs.rs/rand_xoshiro/
6581
[`log`]: https://docs.rs/log/
6682
[`serde`]: https://serde.rs/
83+
[`rand_chacha`]: https://docs.rs/rand_chacha/
6784
[`rand_hc`]: https://docs.rs/rand_hc/
6885
[`rand_isaac`]: https://docs.rs/rand_isaac/
86+
[`rand_jitter`]: https://docs.rs/rand_jitter/
87+
[`rand_sfc`]: https://docs.rs/rand_sfc/
88+
[`rand_seeder`]: https://docs.rs/rand_seeder/
6989
[`rand_xorshift`]: https://docs.rs/rand_xorshift/
7090

7191
[`RngCore`]: https://docs.rs/rand_core/latest/rand_core/trait.RngCore.html

src/guide-parallel.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ a unique stream on each.
2525

2626
Which RNG families support multiple streams?
2727

28-
- [ChaCha](https://docs.rs/rand_chacha/latest/rand_chacha/): the ChaCha RNGs
28+
- [ChaCha](https://docs.rs/rand/latest/rand/rngs/struct.ChaCha20Rng.html): the ChaCha RNGs
2929
support 256-bit seed, 64-bit stream and 64-bit counter (per 16-word block),
3030
thus supporting 2<sup>64</sup> streams of 2<sup>68</sup> words each.
3131
- [Hc128](https://docs.rs/rand_hc/latest/rand_hc/) is a cryptographic RNG
@@ -145,4 +145,4 @@ fn main() {
145145

146146
[`rng()`]: https://docs.rs/rand/latest/rand/fn.rng.html
147147
[`map_init`]: https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.map_init
148-
[`ChaCha8Rng::set_stream`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html#method.set_stream
148+
[`ChaCha8Rng::set_stream`]: https://docs.rs/rand/latest/rand/rngs/struct.ChaCha8Rng.html#method.set_stream

src/guide-rngs.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ You may wish to refer to the [pcg-random] and [xoshiro] websites.
3939

4040
| name | full name | performance | memory | quality | period | features |
4141
|------|-----------|-------------|--------|---------|--------|----------|
42-
| [`SmallRng`] | (unspecified) | 7 GB/s | 16 bytes | ★★★☆☆ |`u32` * 2<sup>64</sup> | not portable |
43-
| [`Pcg32`] | PCG XSH RR 64/32 (LCG) | 3 GB/s | 16 bytes | ★★★☆☆ | `u32` * 2<sup>64</sup> ||
44-
| [`Pcg64`] | PCG XSL 128/64 (LCG) | 4 GB/s | 32 bytes | ★★★☆☆ | `u64` * 2<sup>128</sup> ||
45-
| [`Pcg64Mcg`] | PCG XSL 128/64 (MCG) | 7 GB/s | 16 bytes | ★★★☆☆ | `u64` * 2<sup>126</sup> ||
46-
| [`XorShiftRng`] | Xorshift 32/128 | 5 GB/s | 16 bytes | ★☆☆☆☆ | `u32` * 2<sup>128</sup> - 1 ||
47-
| [`Xoshiro256PlusPlus`] | Xoshiro256++ | 7 GB/s | 32 bytes | ★★★☆☆ | `u64` * 2<sup>256</sup> - 1 | jump-ahead |
48-
| [`Xoshiro256Plus`] | Xoshiro256+ | 8 GB/s | 32 bytes | ★★☆☆☆ | `u64` * 2<sup>256</sup> - 1 | jump-ahead |
49-
| [`SplitMix64`] | splitmix64 | 8 GB/s | 8 bytes | ★☆☆☆☆ | `u64` * 2<sup>64</sup> ||
50-
| [`StepRng`] | counter | 51 GB/s | 16 bytes | ☆☆☆☆☆ | `u64` * 2<sup>64</sup> ||
51-
52-
Here, performance is measured roughly for `u64` outputs on a 3.4GHz Haswell CPU
53-
(note that this will vary significantly by application; in general cryptographic
54-
RNGs do better with byte sequence output). Quality ratings are
42+
| [`SmallRng`] | (unspecified) | 11 GB/s | 16 bytes | ★★★☆☆ |`u32` * 2<sup>64</sup> | not portable |
43+
| [`Pcg32`] | PCG XSH RR 64/32 (LCG) | 5 GB/s | 16 bytes | ★★★☆☆ | `u32` * 2<sup>64</sup> ||
44+
| [`Pcg64`] | PCG XSL 128/64 (LCG) | 7 GB/s | 32 bytes | ★★★☆☆ | `u64` * 2<sup>128</sup> ||
45+
| [`Pcg64Mcg`] | PCG XSL 128/64 (MCG) | 8 GB/s | 16 bytes | ★★★☆☆ | `u64` * 2<sup>126</sup> ||
46+
| [`XorShiftRng`] | Xorshift 32/128 | 7 GB/s | 16 bytes | ★☆☆☆☆ | `u32` * 2<sup>128</sup> - 1 ||
47+
| [`Xoshiro256PlusPlus`] | Xoshiro256++ | 11 GB/s | 32 bytes | ★★★☆☆ | `u64` * 2<sup>256</sup> - 1 | jump-ahead |
48+
| [`Xoshiro256Plus`] | Xoshiro256+ | 13 GB/s | 32 bytes | ★★☆☆☆ | `u64` * 2<sup>256</sup> - 1 | jump-ahead |
49+
| [`SplitMix64`] | splitmix64 | 13 GB/s | 8 bytes | ★☆☆☆☆ | `u64` * 2<sup>64</sup> ||
50+
| `StepRng` | counter | 35 GB/s | 16 bytes | ☆☆☆☆☆ | `u64` * 2<sup>64</sup> ||
51+
52+
Here, performance is measured roughly for `u64` outputs on an
53+
AMD Ryzen 9 9950X3D (note that this will vary significantly by application; in
54+
general cryptographic RNGs do better with byte sequence output). Quality ratings are
5555
based on theory and observable defects, roughly as follows:
5656

5757
- ★☆☆☆☆ = suitable for simple applications but with significant flaws
@@ -79,12 +79,12 @@ table since CSPRNGs may not have observable defects.
7979

8080
| name | full name | performance | initialization | memory | security (predictability) | forward secrecy |
8181
|------|-----------|--------------|--------------|----------|----------------|-------------------------|
82-
| [`StdRng`] | (unspecified) | 1.5 GB/s | fast | 136 bytes | widely trusted | no |
83-
| [`ChaCha20Rng`] | ChaCha20 | 1.8 GB/s | fast | 136 bytes | [rigorously analysed](https://tools.ietf.org/html/rfc7539#section-1) | no |
84-
| [`ChaCha8Rng`] | ChaCha8 | 2.2 GB/s | fast | 136 bytes | small security margin | no |
85-
| [`Hc128Rng`] | HC-128 | 2.1 GB/s | slow | 4176 bytes | [recommended by eSTREAM](http://www.ecrypt.eu.org/stream/) | no |
86-
| [`IsaacRng`] | ISAAC | 1.1 GB/s | slow | 2072 bytes | [unknown](https://burtleburtle.net/bob/rand/isaacafa.html) | unknown |
87-
| [`Isaac64Rng`] | ISAAC-64 | 2.2 GB/s | slow | 4136 bytes| unknown | unknown |
82+
| [`StdRng`] | (unspecified) | 4.1 GB/s | fast | 136 bytes | widely trusted | no |
83+
| [`ChaCha20Rng`] | ChaCha20 | 2.6 GB/s | fast | 136 bytes | [rigorously analysed](https://tools.ietf.org/html/rfc7539#section-1) | no |
84+
| [`ChaCha8Rng`] | ChaCha8 | 5.8 GB/s | fast | 136 bytes | small security margin | no |
85+
| [`Hc128Rng`] | HC-128 | 4.6 GB/s | slow | 4176 bytes | [recommended by eSTREAM](http://www.ecrypt.eu.org/stream/) | no |
86+
| [`IsaacRng`] | ISAAC | 2.1 GB/s | slow | 2072 bytes | [unknown](https://burtleburtle.net/bob/rand/isaacafa.html) | unknown |
87+
| [`Isaac64Rng`] | ISAAC-64 | 3.7 GB/s | slow | 4136 bytes| unknown | unknown |
8888

8989
It should be noted that the ISAAC generators are only included for
9090
historical reasons: they have been with the Rust language since the very
@@ -131,13 +131,13 @@ Mersenne Twister MT19937 algorithm requires 2.5 kB of state.
131131
CSPRNGs typically require more memory; since the seed size is recommended
132132
to be at least 192 bits and some more may be required for the algorithm,
133133
256 bits would be approximately the minimum secure size. In practice,
134-
CSPRNGs tend to use quite a bit more, [`ChaChaRng`] is relatively small with
134+
CSPRNGs tend to use quite a bit more, [`ChaCha20Rng`] is relatively small with
135135
136 bytes of state.
136136

137137
### Initialization time
138138

139139
The time required to initialize new generators varies significantly. Many
140-
simple PRNGs and even some cryptographic ones (including [`ChaChaRng`])
140+
simple PRNGs and even some cryptographic ones (including [`ChaCha20Rng`])
141141
only need to copy the seed value and some constants into their state, and
142142
thus can be constructed very quickly. In contrast, CSPRNGs with large state
143143
require an expensive key-expansion.
@@ -311,7 +311,6 @@ by P. Hellekalek.
311311
[`rngs` module]: https://docs.rs/rand/latest/rand/rngs/
312312
[`SmallRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SmallRng.html
313313
[`StdRng`]: https://docs.rs/rand/latest/rand/rngs/struct.StdRng.html
314-
[`StepRng`]: https://docs.rs/rand/latest/rand/rngs/mock/struct.StepRng.html
315314
[`rand::rng`]: https://docs.rs/rand/latest/rand/fn.rng.html
316315
[basic PRNGs]: #basic-pseudo-random-number-generators-prngs
317316
[CSPRNGs]: #cryptographically-secure-pseudo-random-number-generators-csprngs
@@ -322,9 +321,8 @@ by P. Hellekalek.
322321
[`Xoshiro256PlusPlus`]: https://docs.rs/rand_xoshiro/latest/rand_xoshiro/struct.Xoshiro256PlusPlus.html
323322
[`Xoshiro256Plus`]: https://docs.rs/rand_xoshiro/latest/rand_xoshiro/struct.Xoshiro256Plus.html
324323
[`SplitMix64`]: https://docs.rs/rand_xoshiro/latest/rand_xoshiro/struct.SplitMix64.html
325-
[`ChaChaRng`]: https://docs.rs/rand_chacha/latest/rand_chacha/type.ChaChaRng.html
326-
[`ChaCha20Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha20Rng.html
327-
[`ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
324+
[`ChaCha20Rng`]: https://docs.rs/chacha20/latest/chacha20/struct.ChaCha20Rng.html
325+
[`ChaCha8Rng`]: https://docs.rs/chacha20/latest/chacha20/struct.ChaCha8Rng.html
328326
[`Hc128Rng`]: https://docs.rs/rand_hc/latest/rand_hc/struct.Hc128Rng.html
329327
[`IsaacRng`]: https://docs.rs/rand_isaac/latest/rand_isaac/isaac/struct.IsaacRng.html
330328
[`Isaac64Rng`]: https://docs.rs/rand_isaac/latest/rand_isaac/isaac64/struct.Isaac64Rng.html

src/guide-seeding.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ to a hash result, then using that result to seed a generator. The
110110

111111
```rust,noplayground
112112
use rand::prelude::*;
113+
use rand::rngs::Xoshiro256PlusPlus;
113114
use rand_seeder::{Seeder, SipHasher};
114-
use rand_pcg::Pcg64;
115115
116116
fn main() {
117117
// In one line:
118-
let mut rng: Pcg64 = Seeder::from("stripy zebra").into_rng();
118+
let mut rng: Xoshiro256PlusPlus = Seeder::from("stripy zebra").into_rng();
119119
println!("{}", rng.random::<char>());
120120
121121
// If we want to be more explicit, first we create a SipRng:
@@ -124,11 +124,11 @@ fn main() {
124124
// (Note: hasher_rng is a full RNG and can be used directly.)
125125
126126
// Now, we use hasher_rng to create a seed:
127-
let mut seed: <Pcg64 as SeedableRng>::Seed = Default::default();
127+
let mut seed: <Xoshiro256PlusPlus as SeedableRng>::Seed = Default::default();
128128
hasher_rng.fill(&mut seed);
129129
130130
// And create our RNG from that seed:
131-
let mut rng = Pcg64::from_seed(seed);
131+
let mut rng = Xoshiro256PlusPlus::from_seed(seed);
132132
println!("{}", rng.random::<char>());
133133
}
134134
```
@@ -144,6 +144,6 @@ function such as Argon2 must be used.
144144
[`SeedableRng::from_rng`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.from_rng
145145
[`SeedableRng::seed_from_u64`]: https://docs.rs/rand_core/latest/rand_core/trait.SeedableRng.html#method.seed_from_u64
146146
[`XorShiftRng`]: https://docs.rs/rand_xorshift/latest/rand_xorshift/struct.XorShiftRng.html
147-
[`ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
147+
[`ChaCha8Rng`]: https://docs.rs/rand/latest/rand/rngs/struct.ChaCha8Rng.html
148148
[`rand_seeder`]: https://github.com/rust-random/seeder/
149149
[`rand::make_rng()`]: https://docs.rs/rand/latest/rand/fn.make_rng.html

0 commit comments

Comments
 (0)