Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common traits #16

Merged
merged 5 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use {Rng, Error};
// TODO: replace many of the panics below with Result error handling

/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
/// the operating system.
///
/// Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
Expand All @@ -37,6 +39,12 @@ use {Rng, Error};
/// in-depth discussion.
pub struct OsRng(imp::OsRng);

impl fmt::Debug for OsRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl OsRng {
/// Create a new `OsRng`.
pub fn new() -> Result<OsRng, Error> {
Expand Down Expand Up @@ -69,12 +77,6 @@ impl Rng for OsRng {
}
}

impl fmt::Debug for OsRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "OsRng {{}}")
}
}

// Specialisation of `ReadRng` for our purposes
#[derive(Debug)]
struct ReadRng<R> (R);
Expand Down
27 changes: 18 additions & 9 deletions src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! The ChaCha random number generator.

use core::num::Wrapping as w;
use core::fmt;
use {Rng, CryptoRng, SeedFromRng, SeedableRng, Error};

#[allow(bad_style)]
Expand All @@ -29,19 +30,19 @@ const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of
///
/// [1]: D. J. Bernstein, [*ChaCha, a variant of
/// Salsa20*](http://cr.yp.to/chacha.html)
#[derive(Copy, Clone, Debug)]
#[derive(Clone)]
pub struct ChaChaRng {
buffer: [w32; STATE_WORDS], // Internal buffer of output
state: [w32; STATE_WORDS], // Initial state
index: usize, // Index into state
}

static EMPTY: ChaChaRng = ChaChaRng {
buffer: [w(0); STATE_WORDS],
state: [w(0); STATE_WORDS],
index: STATE_WORDS
};

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for ChaChaRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChaChaRng {{}}")
}
}

macro_rules! quarter_round{
($a: expr, $b: expr, $c: expr, $d: expr) => {{
Expand Down Expand Up @@ -102,7 +103,11 @@ impl ChaChaRng {
/// - 2917185654
/// - 2419978656
pub fn new_unseeded() -> ChaChaRng {
let mut rng = EMPTY;
let mut rng = ChaChaRng {
buffer: [w(0); STATE_WORDS],
state: [w(0); STATE_WORDS],
index: STATE_WORDS
};
rng.init(&[0; KEY_WORDS]);
rng
}
Expand Down Expand Up @@ -265,7 +270,11 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
/// Only up to 8 words are used; if less than 8
/// words are used, the remaining are set to zero.
fn from_seed(seed: &'a [u32]) -> ChaChaRng {
let mut rng = EMPTY;
let mut rng = ChaChaRng {
buffer: [w(0); STATE_WORDS],
state: [w(0); STATE_WORDS],
index: STATE_WORDS
};
rng.init(&[0u32; KEY_WORDS]);
// set key in place
{
Expand Down
1 change: 1 addition & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl Clone for IsaacRng {
}
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for IsaacRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IsaacRng {{}}")
Expand Down
1 change: 1 addition & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Clone for Isaac64Rng {
}
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for Isaac64Rng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Isaac64Rng {{}}")
Expand Down
11 changes: 9 additions & 2 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! Xorshift generators

use core::num::Wrapping as w;
use core::fmt;
use {Rng, SeedFromRng, SeedableRng, Error};

/// An Xorshift[1] random number
Expand All @@ -23,15 +24,21 @@ use {Rng, SeedFromRng, SeedableRng, Error};
/// [1]: Marsaglia, George (July 2003). ["Xorshift
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
/// Statistical Software*. Vol. 8 (Issue 14).
#[allow(missing_copy_implementations)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct XorShiftRng {
x: w<u32>,
y: w<u32>,
z: w<u32>,
w: w<u32>,
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for XorShiftRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "XorShiftRng {{}}")
}
}

impl XorShiftRng {
/// Creates a new XorShiftRng instance which is not seeded.
///
Expand Down
8 changes: 4 additions & 4 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

//! A wrapper around any Read to treat it as an RNG.

use std::fmt::Debug;
use std::io;
use std::io::Read;

Expand All @@ -35,11 +34,12 @@ use {Rng, Error, ErrorKind};
/// println!("{:x}", distributions::uniform::<u32, _>(&mut rng));
/// ```
#[derive(Debug)]
pub struct ReadRng<R: Debug> {
// Do not derive Clone, because it could share the underlying reader
pub struct ReadRng<R> {
reader: R
}

impl<R: Read + Debug> ReadRng<R> {
impl<R: Read> ReadRng<R> {
/// Create a new `ReadRng` from a `Read`.
pub fn new(r: R) -> ReadRng<R> {
ReadRng {
Expand All @@ -48,7 +48,7 @@ impl<R: Read + Debug> ReadRng<R> {
}
}

impl<R: Read + Debug> Rng for ReadRng<R> {
impl<R: Read> Rng for ReadRng<R> {
fn next_u32(&mut self) -> u32 {
::rand_core::impls::next_u32_via_fill(self)
}
Expand Down
3 changes: 1 addition & 2 deletions src/thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 32_768;
type ReseedingStdRng = ReseedingRng<StdRng, ReseedWithNew>;

/// The thread-local RNG.
#[derive(Clone)]
#[allow(missing_debug_implementations)]
#[derive(Clone, Debug)]
pub struct ThreadRng {
rng: Rc<RefCell<ReseedingStdRng>>,
}
Expand Down