1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the snarkVM library.
// The snarkVM library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The snarkVM library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the snarkVM library. If not, see <https://www.gnu.org/licenses/>.
use rand::{
distributions::{Distribution, Standard},
rngs::StdRng,
Rng,
SeedableRng,
};
use rand_xorshift::XorShiftRng;
/// A trait for a uniform random number generator.
pub trait Uniform: Sized {
/// Samples a random value from a uniform distribution.
fn rand<R: Rng + ?Sized>(rng: &mut R) -> Self;
}
impl<T> Uniform for T
where
Standard: Distribution<T>,
{
#[inline]
fn rand<R: Rng + ?Sized>(rng: &mut R) -> Self {
rng.sample(Standard)
}
}
/// A fast RNG used **solely** for testing and benchmarking, **not** for any real world purposes.
pub fn test_rng() -> XorShiftRng {
// Obtain the initial seed using entropy provided by the OS.
let seed = StdRng::from_entropy().gen();
// Print the seed, so it's displayed if any of the tests using `test_rng` fails.
println!("\nInitializing 'test_rng' with seed '{seed}'\n");
// Use the seed to initialize a fast, non-cryptographic Rng.
XorShiftRng::seed_from_u64(seed)
}
/// A function that can be used in order to reproduce any failing test cases that use `test_rng`.
pub fn test_rng_fixed(seed: u64) -> XorShiftRng {
// Use the seed to initialize a fast, non-cryptographic Rng.
XorShiftRng::seed_from_u64(seed)
}
/// A CryptoRNG used **solely** for testing and benchmarking, **not** for any real world purposes.
pub fn test_crypto_rng() -> StdRng {
StdRng::from_entropy()
}
/// A fixed CryptoRNG used **solely** for **debugging** tests, **not** for any real world purposes.
pub fn test_crypto_rng_fixed() -> StdRng {
let seed = 1245897092u64;
StdRng::seed_from_u64(seed)
}