Skip to content

Commit d74cfe5

Browse files
committed
fix: clippy
1 parent 7cadd9d commit d74cfe5

File tree

24 files changed

+162
-176
lines changed

24 files changed

+162
-176
lines changed

examples/aes_chained_cbc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! attacker can detect which original message was used in the ciphertext which is shown here.
77
#![allow(incomplete_features)]
88
#![feature(generic_const_exprs)]
9-
use rand::{thread_rng, Rng};
9+
use rand::{rng, Rng};
1010
use ronkathon::encryption::symmetric::{
1111
aes::{Block, Key, AES},
1212
modes::cbc::CBC,
@@ -52,18 +52,18 @@ fn attacker<'a>(key: &Key<128>, iv: &Block, ciphertext: Vec<u8>) -> &'a [u8] {
5252
/// We simulate Chained CBC and show that attacker can know whether initial plaintext was message 1
5353
/// or 2.
5454
fn main() {
55-
let mut rng = thread_rng();
55+
let mut rng = rng();
5656

5757
// generate a random key and publicly known IV, and initiate CBC with AES cipher
58-
let key = Key::<128>::new(rng.gen());
59-
let iv = Block(rng.gen());
58+
let key = Key::<128>::new(rng.random());
59+
let iv = Block(rng.random());
6060
let cbc = CBC::<AES<128>>::new(iv);
6161

6262
// Chose 2 random messages, {m_0, m_1}
6363
let messages = attacker_chosen_message();
6464

6565
// select a uniform bit b, and chose message m_b for encryption
66-
let bit = rng.gen_range(0..=1);
66+
let bit = rng.random_range(0..=1);
6767
let encrypted = cbc.encrypt(&key, messages[bit]);
6868

6969
let predicted_message = attacker(&key, &iv, encrypted);

src/algebra/field/binary_towers/extension.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use rand::{
9-
distributions::{Distribution, Standard},
9+
distr::{Distribution, StandardUniform},
1010
Rng,
1111
};
1212

@@ -262,12 +262,12 @@ where
262262
}
263263
}
264264

265-
impl<const K: usize> Distribution<BinaryTowers<K>> for Standard
265+
impl<const K: usize> Distribution<BinaryTowers<K>> for StandardUniform
266266
where [(); 1 << K]:
267267
{
268268
#[inline]
269269
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BinaryTowers<K> {
270-
let num = rng.gen_range(1..1 << (1 << K));
270+
let num = rng.random_range(1..1 << (1 << K));
271271
let coefficients = to_bool_vec(num, 1 << K).try_into().unwrap_or_else(|v: Vec<BinaryField>| {
272272
panic!("Expected a Vec of length {} but it was {}", 1 << K, v.len())
273273
});

src/algebra/field/binary_towers/tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rand::{thread_rng, Rng};
1+
use rand::{rng, Rng};
22
use rstest::rstest;
33

44
use super::*;
@@ -54,9 +54,9 @@ fn num_digit(#[case] num: u64, #[case] digits: usize) {
5454

5555
#[test]
5656
fn add_sub_neg() {
57-
let mut rng = thread_rng();
58-
let a = rng.gen::<BinaryTowers<3>>();
59-
let b = rng.gen::<BinaryTowers<3>>();
57+
let mut rng = rng();
58+
let a = rng.random::<BinaryTowers<3>>();
59+
let b = rng.random::<BinaryTowers<3>>();
6060

6161
assert_eq!(a + a, BinaryTowers::<3>::ZERO);
6262
assert_eq!(a + a, b + b);
@@ -87,11 +87,11 @@ fn mul_div(#[case] a: BinaryTowers<3>, #[case] b: BinaryTowers<3>, #[case] res:
8787

8888
#[test]
8989
fn small_by_large_mul() {
90-
let mut rng = thread_rng();
90+
let mut rng = rng();
9191
for _ in 0..100 {
92-
let a = rng.gen::<BinaryTowers<5>>();
92+
let a = rng.random::<BinaryTowers<5>>();
9393

94-
let val = rng.gen_range(0..1 << (1 << 3));
94+
let val = rng.random_range(0..1 << (1 << 3));
9595

9696
let b = BinaryTowers::<3>::from(val);
9797
let d = BinaryTowers::<5>::from(val);
@@ -111,8 +111,8 @@ fn small_by_large_mul() {
111111

112112
#[test]
113113
fn efficient_embedding() {
114-
let mut rng = thread_rng();
115-
let a = rng.gen::<BinaryTowers<4>>();
114+
let mut rng = rng();
115+
let a = rng.random::<BinaryTowers<4>>();
116116

117117
let (a1, a2) = a.into();
118118

src/algebra/field/extension/gf_101_2.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! verified by finding out embedding degree of the curve, i.e. smallest k such that r|q^k-1.
99
1010
use super::*;
11-
use crate::{Distribution, Monomial, Polynomial, Rng, Standard};
11+
use crate::{Distribution, Monomial, Polynomial, Rng, StandardUniform};
1212

1313
impl ExtensionField<2, 101> for PlutoBaseFieldExtension {
1414
/// irreducible polynomial used to reduce field polynomials to second degree:
@@ -127,10 +127,11 @@ impl FiniteField for PlutoBaseFieldExtension {
127127
const PRIMITIVE_ELEMENT: Self = Self::new([PlutoBaseField::new(14), PlutoBaseField::new(9)]);
128128
}
129129

130-
impl<const N: usize, const P: usize> Distribution<GaloisField<N, P>> for Standard {
130+
impl<const N: usize, const P: usize> Distribution<GaloisField<N, P>> for StandardUniform {
131131
#[inline]
132132
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> GaloisField<N, P> {
133-
let coeffs = (0..N).map(|_| rng.gen::<PrimeField<P>>()).collect::<Vec<_>>().try_into().unwrap();
133+
let coeffs =
134+
(0..N).map(|_| rng.random::<PrimeField<P>>()).collect::<Vec<_>>().try_into().unwrap();
134135
GaloisField::<N, P>::new(coeffs)
135136
}
136137
}
@@ -147,7 +148,7 @@ impl Mul for PlutoBaseFieldExtension {
147148
Polynomial::<Monomial, PlutoBaseField, 3>::from(Self::IRREDUCIBLE_POLYNOMIAL_COEFFICIENTS);
148149
let product = (poly_self * poly_rhs) % poly_irred;
149150
let res: [PlutoBaseField; 2] =
150-
array::from_fn(|i| product.coefficients.get(i).cloned().unwrap_or(PlutoBaseField::ZERO));
151+
array::from_fn(|i| product.coefficients.get(i).copied().unwrap_or(PlutoBaseField::ZERO));
151152

152153
Self::new(res)
153154
}
@@ -251,10 +252,10 @@ mod tests {
251252

252253
#[test]
253254
fn add_sub_neg_mul() {
254-
let mut rng = rand::thread_rng();
255-
let x = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
256-
let y = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
257-
let z = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
255+
let mut rng = rand::rng();
256+
let x = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
257+
let y = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
258+
let z = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
258259
assert_eq!(x + (-x), <PlutoBaseFieldExtension>::ZERO);
259260
assert_eq!(-x, <PlutoBaseFieldExtension>::ZERO - x);
260261
assert_eq!(
@@ -268,13 +269,13 @@ mod tests {
268269
assert_eq!(x - (y + z), (x - y) - z);
269270
assert_eq!((x + y) - z, x + (y - z));
270271
assert_eq!(x * (y + z), x * y + x * z);
271-
assert_eq!(x + y + z + x + y + z, [x, x, y, y, z, z].iter().cloned().sum());
272+
assert_eq!(x + y + z + x + y + z, [x, x, y, y, z, z].iter().copied().sum());
272273
}
273274

274275
#[test]
275276
fn pow() {
276-
let mut rng = rand::thread_rng();
277-
let x = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
277+
let mut rng = rand::rng();
278+
let x = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
278279

279280
assert_eq!(x, x.pow(1));
280281

@@ -284,24 +285,24 @@ mod tests {
284285

285286
#[test]
286287
fn inv_div() {
287-
let mut rng = rand::thread_rng();
288+
let mut rng = rand::rng();
288289
// Loop rng's until we get something with inverse.
289290
let mut x = <PlutoBaseFieldExtension>::ZERO;
290291
let mut x_inv = None;
291292
while x_inv.is_none() {
292-
x = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
293+
x = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
293294
x_inv = x.inverse();
294295
}
295296
let mut y = <PlutoBaseFieldExtension>::ZERO;
296297
let mut y_inv = None;
297298
while y_inv.is_none() {
298-
y = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
299+
y = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
299300
y_inv = y.inverse();
300301
}
301302
let mut z = <PlutoBaseFieldExtension>::ZERO;
302303
let mut z_inv = None;
303304
while z_inv.is_none() {
304-
z = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
305+
z = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
305306
z_inv = z.inverse();
306307
}
307308
assert_eq!(x * x.inverse().unwrap(), <PlutoBaseFieldExtension>::ONE);
@@ -329,12 +330,12 @@ mod tests {
329330

330331
#[test]
331332
fn add_sub_mul_subfield() {
332-
let mut rng = rand::thread_rng();
333-
let x = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
333+
let mut rng = rand::rng();
334+
let x = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
334335
let mut y = <PlutoBaseFieldExtension>::ZERO;
335336
let mut y_inv = None;
336337
while y_inv.is_none() {
337-
y = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
338+
y = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
338339
y_inv = y.inverse();
339340
}
340341

@@ -362,17 +363,17 @@ mod tests {
362363

363364
#[test]
364365
fn sqrt() {
365-
let mut rng = rand::thread_rng();
366-
let x = <PlutoBaseFieldExtension>::from(rng.gen::<PlutoBaseField>());
366+
let mut rng = rand::rng();
367+
let x = <PlutoBaseFieldExtension>::from(rng.random::<PlutoBaseField>());
367368
let x_sq = x.pow(2);
368369

369370
let res = x_sq.sqrt();
370371
assert!(res.is_some());
371372

372373
assert_eq!(res.unwrap().0 * res.unwrap().0, x * x);
373374

374-
let x_0 = rng.gen::<PlutoBaseField>();
375-
let x_1 = rng.gen::<PlutoBaseField>();
375+
let x_0 = rng.random::<PlutoBaseField>();
376+
let x_1 = rng.random::<PlutoBaseField>();
376377
let x = <PlutoBaseFieldExtension>::new([x_0, x_1]);
377378

378379
let x_sq = x.pow(2);

src/algebra/field/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use super::Finite;
1313

1414
/// A field is a set of elements on which addition, subtraction, multiplication, and division are
1515
/// defined.
16-
1716
#[const_trait]
1817
pub trait Field:
1918
std::fmt::Debug

src/algebra/field/prime/arithmetic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ mod tests {
134134
}
135135

136136
fn combined_arithmetic_check<const P: usize>() {
137-
let mut rng = rand::thread_rng();
138-
let x = rng.gen::<PrimeField<P>>();
139-
let y = rng.gen::<PrimeField<P>>();
140-
let z = rng.gen::<PrimeField<P>>();
137+
let mut rng = rand::rng();
138+
let x = rng.random::<PrimeField<P>>();
139+
let y = rng.random::<PrimeField<P>>();
140+
let z = rng.random::<PrimeField<P>>();
141141
assert_eq!(x + (-x), <PrimeField<P>>::ZERO);
142142
assert_eq!(-x, <PrimeField<P>>::ZERO - x);
143143
assert_eq!(x + x, x * <PrimeField<P>>::new(2));
@@ -148,7 +148,7 @@ mod tests {
148148
assert_eq!(x - (y + z), (x - y) - z);
149149
assert_eq!((x + y) - z, x + (y - z));
150150
assert_eq!(x * (y + z), x * y + x * z);
151-
assert_eq!(x + y + z + x + y + z, [x, x, y, y, z, z].iter().cloned().sum());
151+
assert_eq!(x + y + z + x + y + z, [x, x, y, y, z, z].iter().copied().sum());
152152
}
153153

154154
#[rstest]

src/algebra/field/prime/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{fmt, str::FromStr};
77

8-
use rand::{distributions::Standard, prelude::Distribution, Rng};
8+
use rand::{distr::StandardUniform, prelude::Distribution, Rng};
99

1010
use super::*;
1111
use crate::algebra::Finite;
@@ -210,7 +210,7 @@ impl<const P: usize> fmt::Display for PrimeField<P> {
210210
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.value) }
211211
}
212212

213-
impl<const P: usize> Distribution<PrimeField<P>> for Standard {
213+
impl<const P: usize> Distribution<PrimeField<P>> for StandardUniform {
214214
#[inline]
215215
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PrimeField<P> {
216216
loop {

src/curve/pairing.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn tangent_line<C: EllipticCurve>(a: AffinePoint<C>, input: AffinePoint<C>)
197197
line_function::<C>(a, a, input)
198198
}
199199

200-
impl Distribution<AffinePoint<PlutoBaseCurve>> for Standard {
200+
impl Distribution<AffinePoint<PlutoBaseCurve>> for StandardUniform {
201201
#[inline]
202202
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> AffinePoint<PlutoBaseCurve> {
203203
loop {
@@ -214,12 +214,14 @@ impl Distribution<AffinePoint<PlutoBaseCurve>> for Standard {
214214
}
215215
}
216216

217-
impl Distribution<AffinePoint<PlutoExtendedCurve>> for Standard {
217+
impl Distribution<AffinePoint<PlutoExtendedCurve>> for StandardUniform {
218218
#[inline]
219219
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> AffinePoint<PlutoExtendedCurve> {
220220
loop {
221-
let x =
222-
PlutoBaseFieldExtension::new([rng.gen::<PlutoBaseField>(), rng.gen::<PlutoBaseField>()]);
221+
let x = PlutoBaseFieldExtension::new([
222+
rng.random::<PlutoBaseField>(),
223+
rng.random::<PlutoBaseField>(),
224+
]);
223225
let rhs: PlutoBaseFieldExtension =
224226
x.pow(3) + x * PlutoExtendedCurve::EQUATION_A + PlutoExtendedCurve::EQUATION_B;
225227
if rhs.euler_criterion() {
@@ -257,10 +259,10 @@ mod tests {
257259

258260
// to keep the support disjoint, a random element `S` on extended curve is used, which shouldn't
259261
// be equal to P, -Q, P-Q
260-
let mut rng = rand::thread_rng();
261-
let mut s = rng.gen::<AffinePoint<PlutoExtendedCurve>>();
262+
let mut rng = rand::rng();
263+
let mut s = rng.random::<AffinePoint<PlutoExtendedCurve>>();
262264
while s == p || s == -q || s == p - q {
263-
s = rng.gen::<AffinePoint<PlutoExtendedCurve>>();
265+
s = rng.random::<AffinePoint<PlutoExtendedCurve>>();
264266
}
265267

266268
// (D_Q) ~ (Q+S) - (S) (equivalent divisors)
@@ -278,11 +280,11 @@ mod tests {
278280

279281
#[test]
280282
fn random_point() {
281-
let mut rng = rand::thread_rng();
282-
let point = rng.gen::<AffinePoint<PlutoBaseCurve>>();
283+
let mut rng = rand::rng();
284+
let point = rng.random::<AffinePoint<PlutoBaseCurve>>();
283285
println!("Random point: {point:?}");
284286

285-
let ext_point = rng.gen::<AffinePoint<PlutoExtendedCurve>>();
287+
let ext_point = rng.random::<AffinePoint<PlutoExtendedCurve>>();
286288
println!("Random extended point: {ext_point:?}");
287289
}
288290

src/diffie_hellman/ecdh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ mod tests {
2525

2626
#[test]
2727
fn test_compute_shared_secret() {
28-
let mut rng = rand::rngs::OsRng;
28+
let mut rng = rand::rng();
2929

30-
let d_a = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..=PlutoScalarField::ORDER));
31-
let d_b = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..=PlutoScalarField::ORDER));
30+
let d_a = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..=PlutoScalarField::ORDER));
31+
let d_b = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..=PlutoScalarField::ORDER));
3232

3333
let q_a = AffinePoint::<PlutoBaseCurve>::GENERATOR * d_a;
3434
let q_b = AffinePoint::<PlutoBaseCurve>::GENERATOR * d_b;

src/diffie_hellman/tp_ecdh.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ pub fn compute_shared_secret(
6969

7070
let pairing = pairing::<_, { PlutoBaseCurve::ORDER }>(p_b, q_c);
7171

72-
let shared_secret = pairing.pow(d_a.value);
73-
74-
shared_secret
72+
pairing.pow(d_a.value)
7573
}
7674

7775
#[cfg(test)]
7876
mod tests {
77+
use rand::rng;
78+
7979
use super::*;
8080
use crate::{
8181
algebra::{field::prime::PlutoScalarField, group::FiniteCyclicGroup, Finite},
@@ -84,9 +84,9 @@ mod tests {
8484

8585
#[test]
8686
fn test_compute_local_pair() {
87-
let mut rng = rand::rngs::OsRng;
87+
let mut rng = rng();
8888

89-
let d_a = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..=PlutoScalarField::ORDER));
89+
let d_a = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..=PlutoScalarField::ORDER));
9090

9191
let (p_a, q_a) = compute_local_pair(d_a);
9292

@@ -96,14 +96,14 @@ mod tests {
9696

9797
#[test]
9898
fn test_compute_tripartite_shared_secret() {
99-
let mut rng = rand::rngs::OsRng;
99+
let mut rng = rng();
100100

101101
let p = AffinePoint::<PlutoBaseCurve>::GENERATOR;
102102
let q = AffinePoint::<PlutoExtendedCurve>::GENERATOR;
103103

104-
let d_a = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..PlutoScalarField::ORDER));
105-
let d_b = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..PlutoScalarField::ORDER));
106-
let d_c = PlutoScalarField::new(rand::Rng::gen_range(&mut rng, 1..PlutoScalarField::ORDER));
104+
let d_a = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..PlutoScalarField::ORDER));
105+
let d_b = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..PlutoScalarField::ORDER));
106+
let d_c = PlutoScalarField::new(rand::Rng::random_range(&mut rng, 1..PlutoScalarField::ORDER));
107107

108108
let p_a = p * d_a;
109109
let p_b = p * d_b;

0 commit comments

Comments
 (0)