Skip to content

Commit 514f255

Browse files
committed
fixup rand 0.9 migration
1 parent c1e3ffa commit 514f255

File tree

17 files changed

+123
-121
lines changed

17 files changed

+123
-121
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ resolver = "2"
1111
opt-level = 2
1212

1313
[patch.crates-io]
14+
digest = { git = "https://github.com/RustCrypto/traits.git", branch = "digest/newtype" }
15+
signature = { git = "https://github.com/RustCrypto/traits.git" }
16+
1417
ed25519 = { git = "https://github.com/RustCrypto/signatures.git" }
18+
19+
blake2 = { git = "https://github.com/RustCrypto/hashes.git", branch = "newtype" }
20+
sha2 = { git = "https://github.com/RustCrypto/hashes.git", branch = "newtype" }
21+
sha3 = { git = "https://github.com/RustCrypto/hashes.git", branch = "newtype" }
22+
23+
merlin = { git = "https://github.com/nresare/merlin.git", branch = "bump_to_rand_0.9" }

curve25519-dalek/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ rustdoc-args = [
3030
features = ["serde", "rand_core", "digest", "legacy_compatibility", "group-bits"]
3131

3232
[dev-dependencies]
33-
sha2 = { version = "=0.11.0-pre.4", default-features = false }
33+
sha2 = { version = "=0.11.0-pre.5", default-features = false }
3434
bincode = "1"
3535
criterion = { version = "0.5", features = ["html_reports"] }
3636
hex = "0.4.2"
@@ -47,10 +47,10 @@ required-features = ["alloc", "rand_core"]
4747

4848
[dependencies]
4949
cfg-if = "1"
50-
ff = { version = "0.13", default-features = false, optional = true }
51-
group = { version = "0.13", default-features = false, optional = true }
50+
ff = { version = "=0.14.0-pre.0", default-features = false, optional = true }
51+
group = { version = "=0.14.0-pre.0", default-features = false, optional = true }
5252
rand_core = { version = "0.9", default-features = false, optional = true }
53-
digest = { version = "=0.11.0-pre.9", default-features = false, optional = true }
53+
digest = { version = "=0.11.0-pre.10", default-features = false, optional = true }
5454
subtle = { version = "2.6.0", default-features = false }
5555
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
5656
zeroize = { version = "1", default-features = false, optional = true }

curve25519-dalek/benches/dalek_benchmarks.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(non_snake_case)]
22

3-
use rand::{rngs::OsRng, thread_rng};
3+
use rand::{TryRngCore, rng, rngs::OsRng};
44

55
use criterion::{
66
criterion_main, measurement::Measurement, BatchSize, BenchmarkGroup, BenchmarkId, Criterion,
@@ -46,7 +46,7 @@ mod edwards_benches {
4646

4747
fn vartime_double_base_scalar_mul<M: Measurement>(c: &mut BenchmarkGroup<M>) {
4848
c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| {
49-
let mut rng = thread_rng();
49+
let mut rng = rng();
5050
let A = EdwardsPoint::mul_base(&Scalar::random(&mut rng));
5151
bench.iter_batched(
5252
|| (Scalar::random(&mut rng), Scalar::random(&mut rng)),
@@ -78,12 +78,12 @@ mod multiscalar_benches {
7878
use curve25519_dalek::traits::VartimePrecomputedMultiscalarMul;
7979

8080
fn construct_scalars(n: usize) -> Vec<Scalar> {
81-
let mut rng = thread_rng();
81+
let mut rng = rng();
8282
(0..n).map(|_| Scalar::random(&mut rng)).collect()
8383
}
8484

8585
fn construct_points(n: usize) -> Vec<EdwardsPoint> {
86-
let mut rng = thread_rng();
86+
let mut rng = rng();
8787
(0..n)
8888
.map(|_| EdwardsPoint::mul_base(&Scalar::random(&mut rng)))
8989
.collect()
@@ -251,7 +251,7 @@ mod ristretto_benches {
251251
|b, &&size| {
252252
let mut rng = OsRng;
253253
let points: Vec<RistrettoPoint> = (0..size)
254-
.map(|_| RistrettoPoint::random(&mut rng))
254+
.map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap())
255255
.collect();
256256
b.iter(|| RistrettoPoint::double_and_compress_batch(&points));
257257
},
@@ -301,7 +301,7 @@ mod scalar_benches {
301301
use super::*;
302302

303303
fn scalar_arith<M: Measurement>(c: &mut BenchmarkGroup<M>) {
304-
let mut rng = thread_rng();
304+
let mut rng = rng();
305305

306306
c.bench_function("Scalar inversion", |b| {
307307
let s = Scalar::from(897987897u64).invert();
@@ -336,7 +336,7 @@ mod scalar_benches {
336336
BenchmarkId::new("Batch scalar inversion", *batch_size),
337337
&batch_size,
338338
|b, &&size| {
339-
let mut rng = OsRng;
339+
let mut rng = OsRng.unwrap_err();
340340
let scalars: Vec<Scalar> =
341341
(0..size).map(|_| Scalar::random(&mut rng)).collect();
342342
b.iter(|| {

curve25519-dalek/src/edwards.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ use {
113113
};
114114

115115
#[cfg(feature = "group")]
116-
use rand_core::RngCore;
116+
use rand_core::TryRngCore;
117117

118118
use subtle::Choice;
119119
use subtle::ConditionallyNegatable;
@@ -1283,13 +1283,13 @@ impl Debug for EdwardsPoint {
12831283
impl group::Group for EdwardsPoint {
12841284
type Scalar = Scalar;
12851285

1286-
fn random(mut rng: impl RngCore) -> Self {
1286+
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
12871287
let mut repr = CompressedEdwardsY([0u8; 32]);
12881288
loop {
1289-
rng.fill_bytes(&mut repr.0);
1289+
rng.try_fill_bytes(&mut repr.0)?;
12901290
if let Some(p) = repr.decompress() {
12911291
if !IsIdentity::is_identity(&p) {
1292-
break p;
1292+
break Ok(p);
12931293
}
12941294
}
12951295
}
@@ -1514,20 +1514,20 @@ define_mul_assign_variants!(LHS = SubgroupPoint, RHS = Scalar);
15141514
impl group::Group for SubgroupPoint {
15151515
type Scalar = Scalar;
15161516

1517-
fn random(mut rng: impl RngCore) -> Self {
1517+
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
15181518
use group::ff::Field;
15191519

15201520
// This will almost never loop, but `Group::random` is documented as returning a
15211521
// non-identity element.
15221522
let s = loop {
1523-
let s: Scalar = Field::random(&mut rng);
1523+
let s: Scalar = Field::try_from_rng(rng)?;
15241524
if !s.is_zero_vartime() {
15251525
break s;
15261526
}
15271527
};
15281528

15291529
// This gives an element of the prime-order subgroup.
1530-
Self::generator() * s
1530+
Ok(Self::generator() * s)
15311531
}
15321532

15331533
fn identity() -> Self {
@@ -1593,9 +1593,7 @@ impl CofactorGroup for EdwardsPoint {
15931593
mod test {
15941594
use super::*;
15951595

1596-
// If `group` is set, then this is already imported in super
1597-
#[cfg(not(feature = "group"))]
1598-
use rand_core::RngCore;
1596+
use rand_core::TryRngCore;
15991597

16001598
#[cfg(feature = "alloc")]
16011599
use alloc::vec::Vec;
@@ -1890,7 +1888,7 @@ mod test {
18901888
#[cfg(feature = "precomputed-tables")]
18911889
let random_point = {
18921890
let mut b = [0u8; 32];
1893-
csprng.fill_bytes(&mut b);
1891+
csprng.try_fill_bytes(&mut b).unwrap();
18941892
EdwardsPoint::mul_base_clamped(b) + constants::EIGHT_TORSION[1]
18951893
};
18961894
// Make a basepoint table from the random point. We'll use this with mul_base_clamped
@@ -1916,7 +1914,7 @@ mod test {
19161914
for _ in 0..100 {
19171915
// This will be reduced mod l with probability l / 2^256 ≈ 6.25%
19181916
let mut a_bytes = [0u8; 32];
1919-
csprng.fill_bytes(&mut a_bytes);
1917+
csprng.try_fill_bytes(&mut a_bytes).unwrap();
19201918

19211919
assert_eq!(
19221920
EdwardsPoint::mul_base_clamped(a_bytes),
@@ -2032,7 +2030,7 @@ mod test {
20322030
// A single iteration of a consistency check for MSM.
20332031
#[cfg(feature = "alloc")]
20342032
fn multiscalar_consistency_iter(n: usize) {
2035-
let mut rng = rand::thread_rng();
2033+
let mut rng = rand::rng();
20362034

20372035
// Construct random coefficients x0, ..., x_{n-1},
20382036
// followed by some extra hardcoded ones.
@@ -2095,7 +2093,7 @@ mod test {
20952093
#[test]
20962094
#[cfg(feature = "alloc")]
20972095
fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
2098-
let mut rng = rand::thread_rng();
2096+
let mut rng = rand::rng();
20992097

21002098
let static_scalars = (0..128)
21012099
.map(|_| Scalar::random(&mut rng))

curve25519-dalek/src/montgomery.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ mod test {
431431
#[cfg(feature = "alloc")]
432432
use alloc::vec::Vec;
433433

434-
use rand_core::{CryptoRng, RngCore};
434+
use rand_core::{CryptoRng, RngCore, TryRngCore};
435435

436436
#[test]
437437
fn identity_in_different_coordinates() {
@@ -515,8 +515,8 @@ mod test {
515515
}
516516

517517
/// Returns a random point on the prime-order subgroup
518-
fn rand_prime_order_point(mut rng: impl RngCore + CryptoRng) -> EdwardsPoint {
519-
let s: Scalar = Scalar::random(&mut rng);
518+
fn rand_prime_order_point<R: CryptoRng + ?Sized>(rng: &mut R) -> EdwardsPoint {
519+
let s: Scalar = Scalar::random(rng);
520520
EdwardsPoint::mul_base(&s)
521521
}
522522

@@ -534,7 +534,7 @@ mod test {
534534

535535
#[test]
536536
fn montgomery_ladder_matches_edwards_scalarmult() {
537-
let mut csprng = rand_core::OsRng;
537+
let mut csprng = rand_core::OsRng.unwrap_err();
538538

539539
for _ in 0..100 {
540540
let p_edwards = rand_prime_order_point(&mut csprng);
@@ -552,7 +552,7 @@ mod test {
552552
// multiplying by the Scalar representation of the same bits
553553
#[test]
554554
fn montgomery_mul_bits_be() {
555-
let mut csprng = rand_core::OsRng;
555+
let mut csprng = rand_core::OsRng.unwrap_err();
556556

557557
for _ in 0..100 {
558558
// Make a random prime-order point P
@@ -577,7 +577,7 @@ mod test {
577577
// integers b₁, b₂ and random (curve or twist) point P.
578578
#[test]
579579
fn montgomery_mul_bits_be_twist() {
580-
let mut csprng = rand_core::OsRng;
580+
let mut csprng = rand_core::OsRng.unwrap_err();
581581

582582
for _ in 0..100 {
583583
// Make a random point P on the curve or its twist
@@ -623,7 +623,7 @@ mod test {
623623
for _ in 0..100 {
624624
// This will be reduced mod l with probability l / 2^256 ≈ 6.25%
625625
let mut a_bytes = [0u8; 32];
626-
csprng.fill_bytes(&mut a_bytes);
626+
csprng.try_fill_bytes(&mut a_bytes).unwrap();
627627

628628
assert_eq!(
629629
MontgomeryPoint::mul_base_clamped(a_bytes),

curve25519-dalek/src/ristretto.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ use core::ops::{Add, Neg, Sub};
169169
use core::ops::{AddAssign, SubAssign};
170170
use core::ops::{Mul, MulAssign};
171171

172-
#[cfg(any(test, feature = "rand_core"))]
173-
use rand_core::CryptoRngCore;
172+
#[cfg(feature = "rand_core")]
173+
use rand_core::TryCryptoRng;
174174

175175
#[cfg(feature = "digest")]
176176
use digest::array::typenum::U64;
@@ -183,7 +183,7 @@ use crate::field::FieldElement;
183183
#[cfg(feature = "group")]
184184
use {
185185
group::{cofactor::CofactorGroup, prime::PrimeGroup, GroupEncoding},
186-
rand_core::RngCore,
186+
rand_core::TryRngCore,
187187
subtle::CtOption,
188188
};
189189

@@ -696,8 +696,7 @@ impl RistrettoPoint {
696696
///
697697
/// # Inputs
698698
///
699-
/// * `rng`: any RNG which implements `CryptoRngCore`
700-
/// (i.e. `CryptoRng` + `RngCore`) interface.
699+
/// * `rng`: any RNG which implements `CryptoRng` interface.
701700
///
702701
/// # Returns
703702
///
@@ -709,11 +708,11 @@ impl RistrettoPoint {
709708
/// discrete log of the output point with respect to any other
710709
/// point should be unknown. The map is applied twice and the
711710
/// results are added, to ensure a uniform distribution.
712-
pub fn random<R: CryptoRngCore + ?Sized>(rng: &mut R) -> Self {
711+
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
713712
let mut uniform_bytes = [0u8; 64];
714-
rng.fill_bytes(&mut uniform_bytes);
713+
rng.try_fill_bytes(&mut uniform_bytes)?;
715714

716-
RistrettoPoint::from_uniform_bytes(&uniform_bytes)
715+
Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes))
717716
}
718717

719718
#[cfg(feature = "digest")]
@@ -1181,11 +1180,11 @@ impl Debug for RistrettoPoint {
11811180
impl group::Group for RistrettoPoint {
11821181
type Scalar = Scalar;
11831182

1184-
fn random(mut rng: impl RngCore) -> Self {
1183+
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
11851184
// NOTE: this is duplicated due to different `rng` bounds
11861185
let mut uniform_bytes = [0u8; 64];
1187-
rng.fill_bytes(&mut uniform_bytes);
1188-
RistrettoPoint::from_uniform_bytes(&uniform_bytes)
1186+
rng.try_fill_bytes(&mut uniform_bytes)?;
1187+
Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes))
11891188
}
11901189

11911190
fn identity() -> Self {
@@ -1278,7 +1277,7 @@ mod test {
12781277
use super::*;
12791278
use crate::edwards::CompressedEdwardsY;
12801279

1281-
use rand_core::OsRng;
1280+
use rand_core::{OsRng, TryRngCore};
12821281

12831282
#[test]
12841283
#[cfg(feature = "serde")]
@@ -1471,7 +1470,7 @@ mod test {
14711470

14721471
#[test]
14731472
fn four_torsion_random() {
1474-
let mut rng = OsRng;
1473+
let mut rng = OsRng.unwrap_err();
14751474
let P = RistrettoPoint::mul_base(&Scalar::random(&mut rng));
14761475
let P_coset = P.coset4();
14771476
for point in P_coset {
@@ -1796,7 +1795,7 @@ mod test {
17961795

17971796
#[test]
17981797
fn random_roundtrip() {
1799-
let mut rng = OsRng;
1798+
let mut rng = OsRng.unwrap_err();
18001799
for _ in 0..100 {
18011800
let P = RistrettoPoint::mul_base(&Scalar::random(&mut rng));
18021801
let compressed_P = P.compress();
@@ -1806,14 +1805,15 @@ mod test {
18061805
}
18071806

18081807
#[test]
1809-
#[cfg(all(feature = "alloc", feature = "rand_core"))]
1808+
#[cfg(all(feature = "alloc", feature = "rand_core", feature = "group"))]
18101809
fn double_and_compress_1024_random_points() {
1810+
use group::Group;
18111811
let mut rng = OsRng;
18121812

18131813
let mut points: Vec<RistrettoPoint> = (0..1024)
1814-
.map(|_| RistrettoPoint::random(&mut rng))
1814+
.map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap())
18151815
.collect();
1816-
points[500] = RistrettoPoint::identity();
1816+
points[500] = <RistrettoPoint as Group>::identity();
18171817

18181818
let compressed = RistrettoPoint::double_and_compress_batch(&points);
18191819

@@ -1825,7 +1825,7 @@ mod test {
18251825
#[test]
18261826
#[cfg(feature = "alloc")]
18271827
fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
1828-
let mut rng = rand::thread_rng();
1828+
let mut rng = rand::rng();
18291829

18301830
let static_scalars = (0..128)
18311831
.map(|_| Scalar::random(&mut rng))

0 commit comments

Comments
 (0)