-
Notifications
You must be signed in to change notification settings - Fork 57
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
Require RngCore
instead of CryptoRngCore
for various random methods
#710
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many doc comments that reference cryptographically-secure generation, and are no longer accurate with the relaxed trait bound.
@@ -233,7 +233,7 @@ where | |||
T: Random + Zero, | |||
{ | |||
/// Generate a random `NonZero<T>`. | |||
fn random(mut rng: &mut impl CryptoRngCore) -> Self { | |||
fn random(mut rng: &mut impl RngCore) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be useful to move aspects of the internal comment to the doc comment now that a CSPRNG is no longer guaranteed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this doc comment and allowed the Random::random()
one take over.
@@ -283,7 +283,7 @@ pub trait Constants: ConstZero { | |||
#[cfg(feature = "rand_core")] | |||
pub trait Random: Sized { | |||
/// Generate a cryptographically secure random value. | |||
fn random(rng: &mut impl CryptoRngCore) -> Self; | |||
fn random(rng: &mut impl RngCore) -> Self; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment is no longer accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a note
@@ -346,25 +346,22 @@ pub trait RandomBits: Sized { | |||
/// Generate a cryptographically secure random value in range `[0, 2^bit_length)`. | |||
/// | |||
/// A wrapper for [`RandomBits::try_random_bits`] that panics on error. | |||
fn random_bits(rng: &mut impl CryptoRngCore, bit_length: u32) -> Self { | |||
fn random_bits(rng: &mut impl RngCore, bit_length: u32) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment is no longer accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added notes for the methods in this trait
rng: &mut impl CryptoRngCore, | ||
bit_length: u32, | ||
) -> Result<Self, RandomBitsError>; | ||
fn try_random_bits(rng: &mut impl RngCore, bit_length: u32) -> Result<Self, RandomBitsError>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment is no longer accurate.
|
||
/// Generate a cryptographically secure random value in range `[0, 2^bit_length)`, | ||
/// returning an integer with the closest available size to `bits_precision` | ||
/// (if the implementing type supports runtime sizing). | ||
/// | ||
/// A wrapper for [`RandomBits::try_random_bits_with_precision`] that panics on error. | ||
fn random_bits_with_precision( | ||
rng: &mut impl CryptoRngCore, | ||
rng: &mut impl RngCore, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment is no longer accurate.
@@ -398,7 +395,7 @@ pub trait RandomMod: Sized + Zero { | |||
/// issue so long as the underlying random number generator is truly a | |||
/// CSRNG, where previous outputs are unrelated to subsequent | |||
/// outputs and do not reveal information about the RNG's internal state. | |||
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self; | |||
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be very careful about this doc comment, as the user may not be aware of what constitutes a CSPRNG.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At any rate, the top-line comment is no longer accurate.
@@ -43,7 +40,7 @@ impl RandomMod for BoxedUint { | |||
/// The variable-time nature of the algorithm should not pose a security issue so long as the | |||
/// underlying random number generator is truly a CSRNG, where previous outputs are unrelated to | |||
/// subsequent outputs and do not reveal information about the RNG's internal state. | |||
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self { | |||
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be very careful about this doc comment, as the user may not be aware of what constitutes a CSPRNG.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At any rate, the top-line comment is no longer accurate.
use subtle::ConstantTimeLess; | ||
|
||
impl<const LIMBS: usize> Random for Uint<LIMBS> { | ||
/// Generate a cryptographically secure random [`Uint`]. | ||
fn random(mut rng: &mut impl CryptoRngCore) -> Self { | ||
fn random(mut rng: &mut impl RngCore) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comment is no longer accurate.
@@ -93,7 +90,7 @@ impl<const LIMBS: usize> RandomMod for Uint<LIMBS> { | |||
/// issue so long as the underlying random number generator is truly a | |||
/// CSRNG, where previous outputs are unrelated to subsequent | |||
/// outputs and do not reveal information about the RNG's internal state. | |||
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self { | |||
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See earlier comments.
Thanks for checking, that's what happens when you make a PR late at night |
I was in the process of adding a little more stuff to the docs... sorry, should have switched it to the draft form |
Basically just the additional lines
Maybe it would be too much hand-holding anyway. |
That seems okay if you want to submit a followup |
Does this change mean that random number generation functions using rejection sampling should be explicitly tagged with a |
Ugh, I mean it was always "vartime". I agree that now there are more footguns. |
Made some documentation updates in #711 toward this. |
Relaxes
CryptoRngCore
requirement toRngCore
. Fixes #137