-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto-bigint: add
rand
feature (#508)
Adds initial support for random number generation, for both `UInt` and `Limb`. Also adds a `UInt::random_mod` function which generates a random number within the range of a provided modulus using rejection sampling.
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! Random number generator support | ||
|
||
use super::Limb; | ||
use rand_core::{CryptoRng, RngCore}; | ||
|
||
impl Limb { | ||
/// Generate a random limb | ||
#[cfg(target_pointer_width = "32")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] | ||
pub fn random(mut rng: impl CryptoRng + RngCore) -> Self { | ||
Self(rng.next_u32()) | ||
} | ||
|
||
/// Generate a random limb | ||
#[cfg(target_pointer_width = "64")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] | ||
pub fn random(mut rng: impl CryptoRng + RngCore) -> Self { | ||
Self(rng.next_u64()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Random number generator support | ||
|
||
use super::UInt; | ||
use crate::Limb; | ||
use rand_core::{CryptoRng, RngCore}; | ||
use subtle::ConstantTimeLess; | ||
|
||
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))] | ||
impl<const LIMBS: usize> UInt<LIMBS> { | ||
/// Generate a cryptographically secure random [`UInt`]. | ||
pub fn random(mut rng: impl CryptoRng + RngCore) -> Self { | ||
let mut limbs = [Limb::default(); LIMBS]; | ||
|
||
for limb in &mut limbs { | ||
*limb = Limb::random(&mut rng) | ||
} | ||
|
||
limbs.into() | ||
} | ||
|
||
/// Generate a cryptographically secure random [`UInt`] which is less than | ||
/// a given `modulus`. | ||
/// | ||
/// This function uses rejection sampling, a method which produces an | ||
/// unbiased distribution of in-range values provided the underlying | ||
/// [`CryptoRng`] is unbiased, but runs in variable-time. | ||
/// | ||
/// The variable-time nature of the algorithm should not pose a security | ||
/// issue so long as the underlying random number generator is truly a | ||
/// [`CryptoRng`], where previous outputs are unrelated to subsequent | ||
/// outputs and do not reveal information about the RNG's internal state. | ||
pub fn random_mod(mut rng: impl CryptoRng + RngCore, modulus: &Self) -> Self { | ||
loop { | ||
let n = Self::random(&mut rng); | ||
|
||
if n.ct_lt(&modulus).into() { | ||
return n; | ||
} | ||
} | ||
} | ||
} |