Skip to content

Commit

Permalink
chor(deps): Upgrade dependency rand to v0.9
Browse files Browse the repository at this point in the history
Prepare for rust edition 2024, where `gen` will be a language keyword.
  • Loading branch information
jan-ferdinand committed Feb 10, 2025
1 parent 9906d5e commit 5873c1e
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 119 deletions.
3 changes: 1 addition & 2 deletions twenty-first/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ lazy_static = "1.5.0"
num-bigint = { version = "0.4", features = ["serde"] }
num-traits = "0.2"
phf = { version = "0.11", features = ["macros"] }
rand = { version = "0.8", features = ["min_const_gen"] }
rand_distr = "0.4"
rand = "0.9"
rayon = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde-big-array = "0"
Expand Down
2 changes: 1 addition & 1 deletion twenty-first/benches/merkle_tree_auth_structure_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn auth_structure_len(c: &mut Criterion<AuthStructureEncodingLength>) {
let mut total_len = AuthStructureEncodingLength(0.0);
for _ in 0..iters {
let opened_indices = (0..num_opened_indices)
.map(|_| rng.gen_range(0..num_leafs))
.map(|_| rng.random_range(0..num_leafs))
.collect_vec();
let auth_structure = mt.authentication_structure(&opened_indices).unwrap();
let this_len = auth_structure.encode().len();
Expand Down
2 changes: 1 addition & 1 deletion twenty-first/benches/merkle_tree_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl MerkleTreeSampler {

fn indices_to_open(&mut self) -> Vec<usize> {
(0..self.num_opened_indices)
.map(|_| self.rng.gen_range(0..self.num_leafs()))
.map(|_| self.rng.random_range(0..self.num_leafs()))
.collect()
}

Expand Down
17 changes: 8 additions & 9 deletions twenty-first/src/amount/u32s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use num_bigint::BigUint;
use num_traits::ConstZero;
use num_traits::One;
use num_traits::Zero;
use rand::distr::Distribution;
use rand::distr::StandardUniform;
use rand::Rng;
use rand_distr::Distribution;
use rand_distr::Standard;
use serde_big_array;
use serde_big_array::BigArray;
use serde_derive::Deserialize;
Expand Down Expand Up @@ -304,9 +304,9 @@ impl<const N: usize> Sum for U32s<N> {
}
}

impl<const N: usize> Distribution<U32s<N>> for Standard {
impl<const N: usize> Distribution<U32s<N>> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> U32s<N> {
let values = rng.sample::<[u32; N], Standard>(Standard);
let values = rng.random();
U32s { values }
}
}
Expand Down Expand Up @@ -386,7 +386,6 @@ impl<const N: usize> BFieldCodec for U32s<N> {
#[cfg(test)]
mod u32s_tests {
use rand::random;
use rand::thread_rng;
use rand::Rng;
use rand::RngCore;

Expand Down Expand Up @@ -418,7 +417,7 @@ mod u32s_tests {

#[test]
fn u128_conversion_test() {
let mut rng = thread_rng();
let mut rng = rand::rng();
for _ in 0..100 {
let a_as_u128: u128 = ((rng.next_u64() as u128) << 64) | rng.next_u64() as u128;
let a: U32s<4> = a_as_u128.try_into().unwrap();
Expand Down Expand Up @@ -756,12 +755,12 @@ mod u32s_tests {
fn get_bit_set_bit_pbt() {
let outer_count = 100;
let inner_count = 20;
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let vals: Vec<U32s<4>> = random_elements(outer_count);
for mut val in vals {
let bit_value: bool = rng.gen();
let bit_value: bool = rng.random();
for _ in 0..inner_count {
let bit_index = rng.gen_range(0..4 * 32);
let bit_index = rng.random_range(0..4 * 32);
val.set_bit(bit_index, bit_value);
assert_eq!(bit_value, val.get_bit(bit_index));
}
Expand Down
21 changes: 10 additions & 11 deletions twenty-first/src/math/b_field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use num_traits::ConstZero;
use num_traits::One;
use num_traits::Zero;
use phf::phf_map;
use rand::distr::Distribution;
use rand::distr::StandardUniform;
use rand::Rng;
use rand_distr::Distribution;
use rand_distr::Standard;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
Expand Down Expand Up @@ -643,9 +643,9 @@ impl CyclicGroupGenerator for BFieldElement {
}
}

impl Distribution<BFieldElement> for Standard {
impl Distribution<BFieldElement> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BFieldElement {
BFieldElement::new(rng.gen_range(0..=BFieldElement::MAX))
BFieldElement::new(rng.random_range(0..=BFieldElement::MAX))
}
}

Expand Down Expand Up @@ -802,7 +802,6 @@ mod b_prime_field_element_test {
use proptest::prelude::*;
use proptest_arbitrary_interop::arb;
use rand::random;
use rand::thread_rng;
use test_strategy::proptest;

use crate::math::b_field_element::*;
Expand Down Expand Up @@ -1314,8 +1313,8 @@ mod b_prime_field_element_test {
let one = BFieldElement::ONE;
assert_eq!(zero, zero.inverse_or_zero());

let mut rng = rand::thread_rng();
let elem: BFieldElement = rng.gen();
let mut rng = rand::rng();
let elem: BFieldElement = rng.random();
if elem.is_zero() {
assert_eq!(zero, elem.inverse_or_zero())
} else {
Expand All @@ -1325,10 +1324,10 @@ mod b_prime_field_element_test {

#[test]
fn test_random_squares() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let p = BFieldElement::P;
for _ in 0..100 {
let a = rng.gen_range(0..p);
let a = rng.random_range(0..p);
let asq = (((a as u128) * (a as u128)) % (p as u128)) as u64;
let b = BFieldElement::new(a);
let bsq = BFieldElement::new(asq);
Expand All @@ -1353,9 +1352,9 @@ mod b_prime_field_element_test {

#[test]
fn test_random_raw() {
let mut rng = thread_rng();
let mut rng = rand::rng();
for _ in 0..100 {
let e: BFieldElement = rng.gen();
let e: BFieldElement = rng.random();
let bytes = e.raw_bytes();
let c = BFieldElement::from_raw_bytes(&bytes);
assert_eq!(e, c);
Expand Down
15 changes: 4 additions & 11 deletions twenty-first/src/math/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use itertools::Itertools;
use num_bigint::BigUint;
use num_traits::ConstZero;
use num_traits::Zero;
use rand::distr::Distribution;
use rand::distr::StandardUniform;
use rand::Rng;
use rand_distr::Distribution;
use rand_distr::Standard;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
Expand Down Expand Up @@ -105,16 +105,9 @@ impl fmt::UpperHex for Digest {
}
}

impl Distribution<Digest> for Standard {
impl Distribution<Digest> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Digest {
// FIXME: impl Fill for [BFieldElement] to rng.fill() a [BFieldElement; Digest::LEN].
let elements = rng
.sample_iter(Standard)
.take(Digest::LEN)
.collect_vec()
.try_into()
.unwrap();
Digest::new(elements)
Digest::new(rng.random())
}
}

Expand Down
11 changes: 5 additions & 6 deletions twenty-first/src/math/lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,6 @@ mod lattice_test {
use num_traits::ConstOne;
use num_traits::Zero;
use rand::random;
use rand::thread_rng;
use rand::RngCore;
use sha3::Digest as Sha3Digest;
use sha3::Sha3_256;
Expand Down Expand Up @@ -875,7 +874,7 @@ mod lattice_test {

#[test]
fn test_embedding() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let msg: [u8; 32] = (0..32)
.map(|_| (rng.next_u32() % 256) as u8)
.collect_vec()
Expand All @@ -889,7 +888,7 @@ mod lattice_test {

#[test]
fn test_module_distributivity() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let randomness = (0..(2 * 3 + 2 * 3 + 3) * 64 * 9)
.map(|_| (rng.next_u32() % 256) as u8)
.collect_vec();
Expand All @@ -912,7 +911,7 @@ mod lattice_test {

#[test]
fn test_module_multiply() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let randomness = (0..(2 * 3 + 2 * 3 + 3) * 64 * 9)
.map(|_| (rng.next_u32() % 256) as u8)
.collect_vec();
Expand All @@ -931,7 +930,7 @@ mod lattice_test {

#[test]
fn test_kem() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let mut key_randomness: [u8; 32] = [0u8; 32];
rng.fill_bytes(&mut key_randomness);
let mut ctxt_randomness: [u8; 32] = [0u8; 32];
Expand Down Expand Up @@ -978,7 +977,7 @@ mod lattice_test {
fn serialization_deserialization_test() {
// This is tested here since the serialization for these objects is a bit more complicated
// than the standard serde stuff. So to be sure that it works, we just run this test here.
let mut rng = thread_rng();
let mut rng = rand::rng();
let mut key_randomness: [u8; 32] = [0u8; 32];
rng.fill_bytes(&mut key_randomness);
let mut ctxt_randomness: [u8; 32] = [0u8; 32];
Expand Down
7 changes: 3 additions & 4 deletions twenty-first/src/math/mds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,13 @@ pub fn generated_function(input: &[u64]) -> [u64; 16] {
#[cfg(test)]
mod tests {
use itertools::Itertools;
use rand::thread_rng;
use rand::RngCore;

use super::*;

#[test]
fn test_karatsuba() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let n = 8;
let a = (0..n)
.map(|_| (rng.next_u32() % (1 << 20)) as u64)
Expand All @@ -525,7 +524,7 @@ mod tests {

#[test]
fn test_negacyclic_mul() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let n = 8;
let a = (0..n)
.map(|_| (rng.next_u32() % (1 << 20)) as u64)
Expand All @@ -548,7 +547,7 @@ mod tests {

#[test]
fn test_recursive_cyclic_mul() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let n = 16;
let a = (0..n).map(|_| rng.next_u32() as u64).collect_vec();
let b = (0..n)
Expand Down
10 changes: 5 additions & 5 deletions twenty-first/src/math/other.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rand::distributions::Distribution;
use rand::distributions::Standard;
use rand::distr::Distribution;
use rand::distr::StandardUniform;
use rand::Rng;

/// Generate `n` random elements using [`rand::thread_rng()`].
/// Generate `n` random elements using [`rand::rng()`].
///
/// For example implementations of the [`Distribution`] trait for [`Standard`], see
/// [`BFieldElement`][bfe] or [`XFieldElement`][xfe].
Expand All @@ -11,7 +11,7 @@ use rand::Rng;
/// [xfe]: crate::prelude::XFieldElement
pub fn random_elements<T>(n: usize) -> Vec<T>
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
rand::thread_rng().sample_iter(Standard).take(n).collect()
rand::rng().sample_iter(StandardUniform).take(n).collect()
}
11 changes: 5 additions & 6 deletions twenty-first/src/math/tip5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ pub(crate) mod tip5_tests {
use prop::sample::size_range;
use proptest::prelude::*;
use proptest_arbitrary_interop::arb;
use rand::thread_rng;
use rand::Rng;
use rand::RngCore;
use rayon::prelude::IntoParallelIterator;
Expand All @@ -720,8 +719,8 @@ pub(crate) mod tip5_tests {
impl Tip5 {
pub(crate) fn randomly_seeded() -> Self {
let mut sponge = Self::init();
let mut rng = thread_rng();
sponge.absorb(rng.gen());
let mut rng = rand::rng();
sponge.absorb(rng.random());
sponge
}
}
Expand Down Expand Up @@ -1061,7 +1060,7 @@ pub(crate) mod tip5_tests {

#[test]
fn test_complex_product() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let mut random_small_i64 = || (rng.next_u32() % (1 << 16)) as i64;
for _ in 0..1000 {
let f = (random_small_i64(), random_small_i64());
Expand All @@ -1088,9 +1087,9 @@ pub(crate) mod tip5_tests {

#[test]
fn test_mds_agree() {
let mut rng = thread_rng();
let mut rng = rand::rng();
let initial_state: [BFieldElement; STATE_SIZE] = (0..STATE_SIZE)
.map(|_| BFieldElement::new(rng.gen_range(0..10)))
.map(|_| BFieldElement::new(rng.random_range(0..10)))
.collect_vec()
.try_into()
.unwrap();
Expand Down
19 changes: 7 additions & 12 deletions twenty-first/src/math/x_field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use num_traits::ConstOne;
use num_traits::ConstZero;
use num_traits::One;
use num_traits::Zero;
use rand::distr::Distribution;
use rand::distr::StandardUniform;
use rand::Rng;
use rand_distr::Distribution;
use rand_distr::Standard;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -321,14 +321,9 @@ impl PrimitiveRootOfUnity for XFieldElement {
}
}

impl Distribution<XFieldElement> for Standard {
impl Distribution<XFieldElement> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> XFieldElement {
let coefficients = [
rng.gen::<BFieldElement>(),
rng.gen::<BFieldElement>(),
rng.gen::<BFieldElement>(),
];
XFieldElement { coefficients }
XFieldElement::new(rng.random())
}
}

Expand Down Expand Up @@ -834,10 +829,10 @@ mod tests {

#[test]
fn x_field_overloaded_arithmetic_test() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
for _ in 0..100 {
let xfe = rng.gen::<XFieldElement>();
let bfe = rng.gen::<BFieldElement>();
let xfe = rng.random::<XFieldElement>();
let bfe = rng.random::<BFieldElement>();

// 1. xfe + bfe.lift() = bfe.lift() + xfe
// 2. xfe + bfe = xfe + bfe.lift()
Expand Down
Loading

0 comments on commit 5873c1e

Please sign in to comment.