Remove mocks (#460)

Gated random() construtors on cfg(test)
This commit is contained in:
Michael Rosenberg 2022-12-08 15:36:22 -05:00 committed by GitHub
parent 1013560fe4
commit 42e93d7faf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 58 deletions

View file

@ -1506,7 +1506,7 @@ mod test {
// Construct random coefficients x0, ..., x_{n-1},
// followed by some extra hardcoded ones.
let xs = (0..n)
.map(|_| crate::mocks::MockScalar::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
// The largest scalar allowed by the type system, 2^255-1
.chain(iter::once(Scalar::from_bits([0xff; 32])))
.collect::<Vec<_>>();
@ -1576,11 +1576,11 @@ mod test {
let B = &crate::constants::ED25519_BASEPOINT_TABLE;
let static_scalars = (0..128)
.map(|_| crate::mocks::MockScalar::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<_>>();
let dynamic_scalars = (0..128)
.map(|_| crate::mocks::MockScalar::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<_>>();
let check_scalar: Scalar = static_scalars

View file

@ -81,6 +81,3 @@ pub(crate) mod backend;
// Generic code for window lookups
pub(crate) mod window;
#[cfg(test)]
pub(crate) mod mocks;

View file

@ -1,42 +0,0 @@
//! This is used mocking / proxying the below for tests:
//! - random_test() to Scalar::random() depending on feature `rand_core`
use crate::ristretto::RistrettoPoint;
use crate::scalar::Scalar;
use rand_core::{CryptoRng, RngCore};
pub struct MockScalar;
impl MockScalar {
#[cfg(feature = "rand_core")]
/// Proxy Scalar::random() for random_test
pub fn random<R: RngCore + CryptoRng + ?Sized>(rng: &mut R) -> Scalar {
Scalar::random(rng)
}
#[cfg(not(feature = "rand_core"))]
/// Mock Scalar::random() for random_test
pub fn random<R: RngCore + CryptoRng + ?Sized>(rng: &mut R) -> Scalar {
let mut scalar_bytes = [0u8; 64];
rng.fill_bytes(&mut scalar_bytes);
Scalar::from_bytes_mod_order_wide(&scalar_bytes)
}
}
pub struct MockRistrettoPoint;
impl MockRistrettoPoint {
#[cfg(feature = "rand_core")]
/// Proxy RistrettoPoint::random() for random_test
pub fn random<R: RngCore + CryptoRng + ?Sized>(rng: &mut R) -> RistrettoPoint {
RistrettoPoint::random(rng)
}
#[cfg(not(feature = "rand_core"))]
/// Mock RistrettoPoint::random() for random_test
pub fn random<R: RngCore + CryptoRng + ?Sized>(rng: &mut R) -> RistrettoPoint {
let mut uniform_bytes = [0u8; 64];
rng.fill_bytes(&mut uniform_bytes);
RistrettoPoint::from_uniform_bytes(&uniform_bytes)
}
}

View file

@ -168,7 +168,7 @@ use core::ops::{Add, Neg, Sub};
use core::ops::{AddAssign, SubAssign};
use core::ops::{Mul, MulAssign};
#[cfg(feature = "rand_core")]
#[cfg(any(test, feature = "rand_core"))]
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "digest")]
@ -666,7 +666,7 @@ impl RistrettoPoint {
)
}
#[cfg(feature = "rand_core")]
#[cfg(any(test, feature = "rand_core"))]
/// Return a `RistrettoPoint` chosen uniformly at random using a user-provided RNG.
///
/// # Inputs
@ -1347,7 +1347,7 @@ mod test {
fn four_torsion_random() {
let mut rng = OsRng;
let B = &constants::RISTRETTO_BASEPOINT_TABLE;
let P = B * &crate::mocks::MockScalar::random(&mut rng);
let P = B * &Scalar::random(&mut rng);
let P_coset = P.coset4();
for point in P_coset {
assert_eq!(P, RistrettoPoint(point));
@ -1674,7 +1674,7 @@ mod test {
let mut rng = OsRng;
let B = &constants::RISTRETTO_BASEPOINT_TABLE;
for _ in 0..100 {
let P = B * &crate::mocks::MockScalar::random(&mut rng);
let P = B * &Scalar::random(&mut rng);
let compressed_P = P.compress();
let Q = compressed_P.decompress().unwrap();
assert_eq!(P, Q);
@ -1687,7 +1687,7 @@ mod test {
let mut rng = OsRng;
let mut points: Vec<RistrettoPoint> = (0..1024)
.map(|_| crate::mocks::MockRistrettoPoint::random(&mut rng))
.map(|_| RistrettoPoint::random(&mut rng))
.collect();
points[500] = RistrettoPoint::identity();
@ -1706,11 +1706,11 @@ mod test {
let B = &crate::constants::RISTRETTO_BASEPOINT_TABLE;
let static_scalars = (0..128)
.map(|_| crate::mocks::MockScalar::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<_>>();
let dynamic_scalars = (0..128)
.map(|_| crate::mocks::MockScalar::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<_>>();
let check_scalar: Scalar = static_scalars

View file

@ -152,7 +152,7 @@ use core::ops::{Sub, SubAssign};
use cfg_if::cfg_if;
#[cfg(feature = "rand_core")]
#[cfg(any(test, feature = "rand_core"))]
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "digest")]
@ -569,7 +569,7 @@ impl Zeroize for Scalar {
}
impl Scalar {
#[cfg(feature = "rand_core")]
#[cfg(any(test, feature = "rand_core"))]
/// Return a `Scalar` chosen uniformly at random using a user-provided RNG.
///
/// # Inputs
@ -1413,7 +1413,7 @@ mod test {
fn non_adjacent_form_random() {
let mut rng = rand::thread_rng();
for _ in 0..1_000 {
let x = crate::mocks::MockScalar::random(&mut rng);
let x = Scalar::random(&mut rng);
for w in &[5, 6, 7, 8] {
non_adjacent_form_iter(*w, &x);
}