Skip to content

Commit ff121cf

Browse files
committed
Use Hc128 on Emscripten
This is considered an ugly hack, but easier than making the new ChaChaRng compatible with Emscripten. Potentially support for Emscripten will be dropped in the future, if it doesn't get support for u128.
1 parent 33bd404 commit ff121cf

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ members = [
5555

5656
[dependencies]
5757
rand_core = { path = "rand_core", version = "0.4" }
58-
rand_chacha = { path = "rand_chacha", version = "0.2" }
5958
rand_pcg = { path = "rand_pcg", version = "0.1", optional = true }
6059
# Do not depend on 'getrandom_package' directly; use the 'getrandom' feature!
6160
getrandom_package = { version = "0.1.1", package = "getrandom", optional = true }
@@ -72,6 +71,13 @@ features = ["into_bits"]
7271
# Used for fork protection (reseeding.rs)
7372
libc = { version = "0.2.22", default-features = false }
7473

74+
# Emscripten does not support 128-bit integers, which are used by ChaCha code.
75+
# We work around this by using a different RNG.
76+
[target.'cfg(not(target_os = "emscripten"))'.dependencies]
77+
rand_chacha = { path = "rand_chacha", version = "0.2" }
78+
[target.'cfg(target_os = "emscripten")'.dependencies]
79+
rand_hc = { path = "rand_hc", version = "0.1" }
80+
7581
[dev-dependencies]
7682
rand_pcg = { path = "rand_pcg", version = "0.1" }
7783
# Only for benches:

src/distributions/integer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use {Rng};
1212
use distributions::{Distribution, Standard};
1313
#[cfg(rustc_1_28)]
14-
use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
14+
use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroUsize};
15+
#[cfg(not(target_os = "emscripten"))] use core::num::NonZeroU128;
1516
#[cfg(feature="simd_support")]
1617
use packed_simd::*;
1718
#[cfg(all(target_arch = "x86", feature="nightly"))]

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
extern crate getrandom_package as getrandom;
6363

6464
extern crate rand_core;
65-
extern crate rand_chacha;
65+
#[cfg(not(target_os = "emscripten"))] extern crate rand_chacha;
66+
#[cfg(target_os = "emscripten")] extern crate rand_hc;
6667
#[cfg(feature="small_rng")] extern crate rand_pcg;
6768

6869
#[cfg(feature = "log")] #[macro_use] extern crate log;

src/rngs/adapter/reseeding.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,14 @@ mod fork {
324324
#[cfg(test)]
325325
mod test {
326326
use {Rng, SeedableRng};
327-
use rand_chacha::ChaCha8Core;
327+
use rngs::std::Core;
328328
use rngs::mock::StepRng;
329329
use super::ReseedingRng;
330330

331331
#[test]
332332
fn test_reseeding() {
333333
let mut zero = StepRng::new(0, 0);
334-
let rng = ChaCha8Core::from_rng(&mut zero).unwrap();
334+
let rng = Core::from_rng(&mut zero).unwrap();
335335
let thresh = 1; // reseed every time the buffer is exhausted
336336
let mut reseeding = ReseedingRng::new(rng, thresh, zero);
337337

@@ -351,7 +351,7 @@ mod test {
351351
#[test]
352352
fn test_clone_reseeding() {
353353
let mut zero = StepRng::new(0, 0);
354-
let rng = ChaCha8Core::from_rng(&mut zero).unwrap();
354+
let rng = Core::from_rng(&mut zero).unwrap();
355355
let mut rng1 = ReseedingRng::new(rng, 32*4, zero);
356356

357357
let first: u32 = rng1.gen();

src/rngs/std.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
//! The standard RNG
1010
1111
use {RngCore, CryptoRng, Error, SeedableRng};
12-
use rand_chacha::ChaCha20Rng;
12+
13+
#[cfg(target_os = "emscripten")] pub(crate) use rand_hc::Hc128Core as Core;
14+
#[cfg(not(target_os = "emscripten"))] pub(crate) use rand_chacha::ChaCha20Core as Core;
15+
#[cfg(target_os = "emscripten")] use rand_hc::Hc128Rng as Rng;
16+
#[cfg(not(target_os = "emscripten"))] use rand_chacha::ChaCha20Rng as Rng;
1317

1418
/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
1519
/// on the current platform, to be statistically strong and unpredictable
@@ -27,7 +31,7 @@ use rand_chacha::ChaCha20Rng;
2731
///
2832
/// [rand_chacha]: https://crates.io/crates/rand_chacha
2933
#[derive(Clone, Debug)]
30-
pub struct StdRng(ChaCha20Rng);
34+
pub struct StdRng(Rng);
3135

3236
impl RngCore for StdRng {
3337
#[inline(always)]
@@ -50,14 +54,14 @@ impl RngCore for StdRng {
5054
}
5155

5256
impl SeedableRng for StdRng {
53-
type Seed = <ChaCha20Rng as SeedableRng>::Seed;
57+
type Seed = <Rng as SeedableRng>::Seed;
5458

5559
fn from_seed(seed: Self::Seed) -> Self {
56-
StdRng(ChaCha20Rng::from_seed(seed))
60+
StdRng(Rng::from_seed(seed))
5761
}
5862

5963
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
60-
ChaCha20Rng::from_rng(rng).map(StdRng)
64+
Rng::from_rng(rng).map(StdRng)
6165
}
6266
}
6367

src/rngs/thread.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::cell::UnsafeCell;
1313
use {RngCore, CryptoRng, SeedableRng, Error};
1414
use rngs::adapter::ReseedingRng;
1515
use rngs::OsRng;
16-
use rand_chacha::ChaCha20Core;
16+
use super::std::Core;
1717

1818
// Rationale for using `UnsafeCell` in `ThreadRng`:
1919
//
@@ -58,12 +58,12 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 1024 * 64;
5858
#[derive(Copy, Clone, Debug)]
5959
pub struct ThreadRng {
6060
// use of raw pointer implies type is neither Send nor Sync
61-
rng: *mut ReseedingRng<ChaCha20Core, OsRng>,
61+
rng: *mut ReseedingRng<Core, OsRng>,
6262
}
6363

6464
thread_local!(
65-
static THREAD_RNG_KEY: UnsafeCell<ReseedingRng<ChaCha20Core, OsRng>> = {
66-
let r = ChaCha20Core::from_rng(OsRng).unwrap_or_else(|err|
65+
static THREAD_RNG_KEY: UnsafeCell<ReseedingRng<Core, OsRng>> = {
66+
let r = Core::from_rng(OsRng).unwrap_or_else(|err|
6767
panic!("could not initialize thread_rng: {}", err));
6868
let rng = ReseedingRng::new(r,
6969
THREAD_RNG_RESEED_THRESHOLD,

0 commit comments

Comments
 (0)