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

Add RngCore::bytes_per_round #396

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ pub trait RngCore {
///
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;

/// Number of bytes generated per round of this RNG.
///
/// Some algorithms would benefit from knowing some basic properties about
/// the RNG. In terms of performance an algorithm may want to know whether
/// an RNG is best at generating `u32`s, or could provide `u64`s or more at
/// little to no extra cost.
///
/// For many RNGs a simple definition is: the smallest number of bytes this
/// RNG can generate without throwing away part of the generated value.
///
/// `bytes_per_round` has a default implementation that returns `4` (bytes).
fn bytes_per_round(&self) -> usize { 4 }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an associated constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried something like that in a not really thought through attempt: #377 (comment). The problem is that we then can't make RngCore into a trait object.

Copy link
Member

@dhardy dhardy Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird; doesn't sound like associated constants should prevent a trait from becoming object-safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☹️

error[E0038]: the trait `rand_core::RngCore` cannot be made into an object
    --> src\lib.rs:1189:21
     |
1189 |         let mut r = Box::new(rng) as Box<RngCore>;
     |                     ^^^^^^^^^^^^^ the trait `rand_core::RngCore` cannot be made into an object
     |
     = note: the trait cannot contain associated consts like `BYTES_PER_ROUND`
     = note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::boxed::Box<rand_core::RngCore>>` for `std::boxed::Box<test::TestRng<StdRng>>`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really give a default implementation here?

Did you forget to implement for Jitter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure. A disadvantage with a default implementation is that you can easily forget a wrapper.

Did you forget to implement for Jitter?

JitterRng::next_u32() is about twice as fast as next_u64(), so 4 bytes would be the best fit there.

}

/// A trait for RNGs which do not generate random numbers individually, but in
Expand Down Expand Up @@ -403,6 +416,10 @@ impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
(**self).try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
(**self).bytes_per_round()
}
}

#[cfg(feature="alloc")]
Expand All @@ -424,4 +441,8 @@ impl<R: RngCore + ?Sized> RngCore for Box<R> {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
(**self).try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
(**self).bytes_per_round()
}
}
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,10 @@ impl RngCore for StdRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
self.0.bytes_per_round()
}
}

impl SeedableRng for StdRng {
Expand Down Expand Up @@ -936,6 +940,10 @@ impl RngCore for SmallRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
self.0.bytes_per_round()
}
}

impl SeedableRng for SmallRng {
Expand Down Expand Up @@ -1017,6 +1025,9 @@ mod test {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.inner.try_fill_bytes(dest)
}
fn bytes_per_round(&self) -> usize {
self.inner.bytes_per_round()
}
}

pub fn rng(seed: u64) -> TestRng<StdRng> {
Expand Down
2 changes: 2 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ impl RngCore for StepRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}

fn bytes_per_round(&self) -> usize { 8 }
}
8 changes: 8 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ impl RngCore for OsRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
// The overhead of doing a syscall is large compared to the time
// it takes to generate the values. Requesting many values at a time is
// often faster than only one at a time.
// 256 is the limit some operating systems have per system call.
256
}
}

#[cfg(all(unix,
Expand Down
4 changes: 4 additions & 0 deletions src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ where R: BlockRngCore<Item = u32> + SeedableRng,
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn bytes_per_round(&self) -> usize {
self.0.bytes_per_round()
}
}

impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr>
Expand Down