Skip to content

Commit a99efe2

Browse files
baloopinkforest
andauthored
curve,ed,x: Bump rand_core to 0.9 (#777)
--------- Co-authored-by: pinkforest <[email protected]>
1 parent a9aa947 commit a99efe2

File tree

20 files changed

+216
-172
lines changed

20 files changed

+216
-172
lines changed

.github/workflows/workspace.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
- name: no_std / no feat ${{ matrix.crate }}
7979
run: cargo build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --no-default-features
8080
- name: no_std / cargo hack ${{ matrix.crate }}
81-
run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,getrandom
81+
run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,os_rng
8282

8383
clippy:
8484
name: Check that clippy is happy

curve25519-dalek/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ sha2 = { version = "0.11.0-rc.0", default-features = false }
3434
bincode = "1"
3535
criterion = { version = "0.5", features = ["html_reports"] }
3636
hex = "0.4.2"
37-
rand = "0.8"
38-
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
37+
rand = "0.9"
38+
rand_core = { version = "0.9", default-features = false, features = ["os_rng"] }
3939

4040
[build-dependencies]
4141
rustc_version = "0.4.0"
@@ -47,9 +47,9 @@ 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 }
52-
rand_core = { version = "0.6.4", 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 }
52+
rand_core = { version = "0.9", default-features = false, optional = true }
5353
digest = { version = "0.11.0-rc.0", default-features = false, optional = true, features = ["block-api"] }
5454
subtle = { version = "2.6.0", default-features = false, features = ["const-generics"] }
5555
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }

curve25519-dalek/benches/dalek_benchmarks.rs

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

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

55
use criterion::{
66
BatchSize, BenchmarkGroup, BenchmarkId, Criterion, criterion_main, measurement::Measurement,
@@ -31,7 +31,7 @@ mod edwards_benches {
3131
BenchmarkId::new("Batch EdwardsPoint compression", batch_size),
3232
&batch_size,
3333
|b, &size| {
34-
let mut rng = OsRng;
34+
let mut rng = OsRng.unwrap_err();
3535
let points: Vec<EdwardsPoint> =
3636
(0..size).map(|_| EdwardsPoint::random(&mut rng)).collect();
3737
b.iter(|| EdwardsPoint::compress_batch(&points));
@@ -64,7 +64,7 @@ mod edwards_benches {
6464

6565
fn vartime_double_base_scalar_mul<M: Measurement>(c: &mut BenchmarkGroup<M>) {
6666
c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| {
67-
let mut rng = thread_rng();
67+
let mut rng = rng();
6868
let A = EdwardsPoint::mul_base(&Scalar::random(&mut rng));
6969
bench.iter_batched(
7070
|| (Scalar::random(&mut rng), Scalar::random(&mut rng)),
@@ -76,7 +76,7 @@ mod edwards_benches {
7676

7777
#[cfg(feature = "digest")]
7878
fn hash_to_curve<M: Measurement>(c: &mut BenchmarkGroup<M>) {
79-
let mut rng = thread_rng();
79+
let mut rng = rng();
8080

8181
let mut msg = [0u8; 32];
8282
let mut domain_sep = [0u8; 32];
@@ -114,12 +114,12 @@ mod multiscalar_benches {
114114
use curve25519_dalek::traits::VartimePrecomputedMultiscalarMul;
115115

116116
fn construct_scalars(n: usize) -> Vec<Scalar> {
117-
let mut rng = thread_rng();
117+
let mut rng = rng();
118118
(0..n).map(|_| Scalar::random(&mut rng)).collect()
119119
}
120120

121121
fn construct_points(n: usize) -> Vec<EdwardsPoint> {
122-
let mut rng = thread_rng();
122+
let mut rng = rng();
123123
(0..n)
124124
.map(|_| EdwardsPoint::mul_base(&Scalar::random(&mut rng)))
125125
.collect()
@@ -287,7 +287,7 @@ mod ristretto_benches {
287287
|b, &&size| {
288288
let mut rng = OsRng;
289289
let points: Vec<RistrettoPoint> = (0..size)
290-
.map(|_| RistrettoPoint::random(&mut rng))
290+
.map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap())
291291
.collect();
292292
b.iter(|| RistrettoPoint::double_and_compress_batch(&points));
293293
},
@@ -337,7 +337,7 @@ mod scalar_benches {
337337
use super::*;
338338

339339
fn scalar_arith<M: Measurement>(c: &mut BenchmarkGroup<M>) {
340-
let mut rng = thread_rng();
340+
let mut rng = rng();
341341

342342
c.bench_function("Scalar inversion", |b| {
343343
let s = Scalar::from(897987897u64).invert();
@@ -372,7 +372,7 @@ mod scalar_benches {
372372
BenchmarkId::new("Batch scalar inversion", *batch_size),
373373
&batch_size,
374374
|b, &&size| {
375-
let mut rng = OsRng;
375+
let mut rng = OsRng.unwrap_err();
376376
let scalars: Vec<Scalar> =
377377
(0..size).map(|_| Scalar::random(&mut rng)).collect();
378378
b.iter(|| {

curve25519-dalek/src/edwards.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ use digest::{
113113
#[cfg(feature = "group")]
114114
use {
115115
group::{GroupEncoding, cofactor::CofactorGroup, prime::PrimeGroup},
116+
rand_core::TryRngCore,
116117
subtle::CtOption,
117118
};
118119

@@ -722,7 +723,7 @@ impl EdwardsPoint {
722723
/// Uses rejection sampling, generating a random `CompressedEdwardsY` and then attempting point
723724
/// decompression, rejecting invalid points.
724725
#[cfg(any(test, feature = "rand_core"))]
725-
pub fn random(mut rng: impl RngCore) -> Self {
726+
pub fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
726727
let mut repr = CompressedEdwardsY([0u8; 32]);
727728
loop {
728729
rng.fill_bytes(&mut repr.0);
@@ -1419,9 +1420,16 @@ impl Debug for EdwardsPoint {
14191420
impl group::Group for EdwardsPoint {
14201421
type Scalar = Scalar;
14211422

1422-
fn random(rng: impl RngCore) -> Self {
1423-
// Call the inherent `pub fn random` defined above
1424-
Self::random(rng)
1423+
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
1424+
let mut repr = CompressedEdwardsY([0u8; 32]);
1425+
loop {
1426+
rng.try_fill_bytes(&mut repr.0)?;
1427+
if let Some(p) = repr.decompress() {
1428+
if !IsIdentity::is_identity(&p) {
1429+
break Ok(p);
1430+
}
1431+
}
1432+
}
14251433
}
14261434

14271435
fn identity() -> Self {
@@ -1664,20 +1672,20 @@ impl Zeroize for SubgroupPoint {
16641672
impl group::Group for SubgroupPoint {
16651673
type Scalar = Scalar;
16661674

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

16701678
// This will almost never loop, but `Group::random` is documented as returning a
16711679
// non-identity element.
16721680
let s = loop {
1673-
let s: Scalar = Field::random(&mut rng);
1681+
let s: Scalar = Field::try_from_rng(rng)?;
16741682
if !s.is_zero_vartime() {
16751683
break s;
16761684
}
16771685
};
16781686

16791687
// This gives an element of the prime-order subgroup.
1680-
Self::generator() * s
1688+
Ok(Self::generator() * s)
16811689
}
16821690

16831691
fn identity() -> Self {
@@ -1743,9 +1751,7 @@ impl CofactorGroup for EdwardsPoint {
17431751
mod test {
17441752
use super::*;
17451753

1746-
// If `group` is set, then this is already imported in super
1747-
#[cfg(not(feature = "group"))]
1748-
use rand_core::RngCore;
1754+
use rand_core::TryRngCore;
17491755

17501756
#[cfg(feature = "alloc")]
17511757
use alloc::vec::Vec;
@@ -2040,7 +2046,7 @@ mod test {
20402046
#[cfg(feature = "precomputed-tables")]
20412047
let random_point = {
20422048
let mut b = [0u8; 32];
2043-
csprng.fill_bytes(&mut b);
2049+
csprng.try_fill_bytes(&mut b).unwrap();
20442050
EdwardsPoint::mul_base_clamped(b) + constants::EIGHT_TORSION[1]
20452051
};
20462052
// Make a basepoint table from the random point. We'll use this with mul_base_clamped
@@ -2066,7 +2072,7 @@ mod test {
20662072
for _ in 0..100 {
20672073
// This will be reduced mod l with probability l / 2^256 ≈ 6.25%
20682074
let mut a_bytes = [0u8; 32];
2069-
csprng.fill_bytes(&mut a_bytes);
2075+
csprng.try_fill_bytes(&mut a_bytes).unwrap();
20702076

20712077
assert_eq!(
20722078
EdwardsPoint::mul_base_clamped(a_bytes),
@@ -2151,7 +2157,7 @@ mod test {
21512157
#[cfg(feature = "alloc")]
21522158
#[test]
21532159
fn compress_batch() {
2154-
let mut rng = rand::thread_rng();
2160+
let mut rng = rand::rng();
21552161

21562162
// TODO(tarcieri): proptests?
21572163
// Make some points deterministically then randomly
@@ -2207,7 +2213,7 @@ mod test {
22072213
// A single iteration of a consistency check for MSM.
22082214
#[cfg(feature = "alloc")]
22092215
fn multiscalar_consistency_iter(n: usize) {
2210-
let mut rng = rand::thread_rng();
2216+
let mut rng = rand::rng();
22112217

22122218
// Construct random coefficients x0, ..., x_{n-1},
22132219
// followed by some extra hardcoded ones.
@@ -2270,7 +2276,7 @@ mod test {
22702276
#[test]
22712277
#[cfg(feature = "alloc")]
22722278
fn batch_to_montgomery() {
2273-
let mut rng = rand::thread_rng();
2279+
let mut rng = rand::rng();
22742280

22752281
let scalars = (0..128)
22762282
.map(|_| Scalar::random(&mut rng))
@@ -2295,7 +2301,7 @@ mod test {
22952301
#[test]
22962302
#[cfg(feature = "alloc")]
22972303
fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
2298-
let mut rng = rand::thread_rng();
2304+
let mut rng = rand::rng();
22992305

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

curve25519-dalek/src/montgomery.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ mod test {
437437
#[cfg(feature = "alloc")]
438438
use alloc::vec::Vec;
439439

440-
use rand_core::{CryptoRng, RngCore};
440+
use rand_core::{CryptoRng, RngCore, TryRngCore};
441441

442442
#[test]
443443
fn identity_in_different_coordinates() {
@@ -521,8 +521,8 @@ mod test {
521521
}
522522

523523
/// Returns a random point on the prime-order subgroup
524-
fn rand_prime_order_point(mut rng: impl RngCore + CryptoRng) -> EdwardsPoint {
525-
let s: Scalar = Scalar::random(&mut rng);
524+
fn rand_prime_order_point<R: CryptoRng + ?Sized>(rng: &mut R) -> EdwardsPoint {
525+
let s: Scalar = Scalar::random(rng);
526526
EdwardsPoint::mul_base(&s)
527527
}
528528

@@ -540,10 +540,10 @@ mod test {
540540

541541
#[test]
542542
fn montgomery_ladder_matches_edwards_scalarmult() {
543-
let mut csprng = rand_core::OsRng;
543+
let mut csprng = rand_core::OsRng.unwrap_err();
544544

545545
for _ in 0..100 {
546-
let p_edwards = rand_prime_order_point(csprng);
546+
let p_edwards = rand_prime_order_point(&mut csprng);
547547
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();
548548

549549
let s: Scalar = Scalar::random(&mut csprng);
@@ -558,11 +558,11 @@ mod test {
558558
// multiplying by the Scalar representation of the same bits
559559
#[test]
560560
fn montgomery_mul_bits_be() {
561-
let mut csprng = rand_core::OsRng;
561+
let mut csprng = rand_core::OsRng.unwrap_err();
562562

563563
for _ in 0..100 {
564564
// Make a random prime-order point P
565-
let p_edwards = rand_prime_order_point(csprng);
565+
let p_edwards = rand_prime_order_point(&mut csprng);
566566
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();
567567

568568
// Make a random integer b
@@ -583,7 +583,7 @@ mod test {
583583
// integers b₁, b₂ and random (curve or twist) point P.
584584
#[test]
585585
fn montgomery_mul_bits_be_twist() {
586-
let mut csprng = rand_core::OsRng;
586+
let mut csprng = rand_core::OsRng.unwrap_err();
587587

588588
for _ in 0..100 {
589589
// Make a random point P on the curve or its twist
@@ -629,7 +629,7 @@ mod test {
629629
for _ in 0..100 {
630630
// This will be reduced mod l with probability l / 2^256 ≈ 6.25%
631631
let mut a_bytes = [0u8; 32];
632-
csprng.fill_bytes(&mut a_bytes);
632+
csprng.try_fill_bytes(&mut a_bytes).unwrap();
633633

634634
assert_eq!(
635635
MontgomeryPoint::mul_base_clamped(a_bytes),

0 commit comments

Comments
 (0)