Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: no_std / no feat ${{ matrix.crate }}
run: cargo build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --no-default-features
- name: no_std / cargo hack ${{ matrix.crate }}
run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,getrandom
run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,os_rng

clippy:
name: Check that clippy is happy
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ resolver = "2"

[profile.dev]
opt-level = 2

16 changes: 8 additions & 8 deletions curve25519-dalek/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ rustdoc-args = [
features = ["serde", "rand_core", "digest", "legacy_compatibility", "group-bits"]

[dev-dependencies]
sha2 = { version = "0.10", default-features = false }
sha2 = { version = "0.11.0-rc.0", default-features = false }
bincode = "1"
criterion = { version = "0.5", features = ["html_reports"] }
hex = "0.4.2"
rand = "0.8"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
rand = "0.9"
rand_core = { version = "0.9", default-features = false, features = ["os_rng"] }

[build-dependencies]
rustc_version = "0.4.0"
Expand All @@ -47,11 +47,11 @@ required-features = ["alloc", "rand_core"]

[dependencies]
cfg-if = "1"
ff = { version = "0.13", default-features = false, optional = true }
group = { version = "0.13", default-features = false, optional = true }
rand_core = { version = "0.6.4", default-features = false, optional = true }
digest = { version = "0.10", default-features = false, optional = true }
subtle = { version = "2.6.0", default-features = false, features = ["const-generics"]}
ff = { version = "=0.14.0-pre.0", default-features = false, optional = true }
group = { version = "=0.14.0-pre.0", default-features = false, optional = true }
rand_core = { version = "0.9", default-features = false, optional = true }
digest = { version = "0.11.0-rc.0", default-features = false, optional = true }
subtle = { version = "2.6.0", default-features = false, features = ["const-generics"] }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
zeroize = { version = "1", default-features = false, optional = true }

Expand Down
16 changes: 8 additions & 8 deletions curve25519-dalek/benches/dalek_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_snake_case)]

use rand::{rngs::OsRng, thread_rng};
use rand::{rng, rngs::OsRng, TryRngCore};

use criterion::{
criterion_main, measurement::Measurement, BatchSize, BenchmarkGroup, BenchmarkId, Criterion,
Expand Down Expand Up @@ -29,7 +29,7 @@ mod edwards_benches {
BenchmarkId::new("Batch EdwardsPoint compression", batch_size),
&batch_size,
|b, &size| {
let mut rng = OsRng;
let mut rng = OsRng.unwrap_err();
let points: Vec<EdwardsPoint> =
(0..size).map(|_| EdwardsPoint::random(&mut rng)).collect();
b.iter(|| EdwardsPoint::compress_batch(&points));
Expand Down Expand Up @@ -62,7 +62,7 @@ mod edwards_benches {

fn vartime_double_base_scalar_mul<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| {
let mut rng = thread_rng();
let mut rng = rng();
let A = EdwardsPoint::mul_base(&Scalar::random(&mut rng));
bench.iter_batched(
|| (Scalar::random(&mut rng), Scalar::random(&mut rng)),
Expand Down Expand Up @@ -96,12 +96,12 @@ mod multiscalar_benches {
use curve25519_dalek::traits::VartimePrecomputedMultiscalarMul;

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

fn construct_points(n: usize) -> Vec<EdwardsPoint> {
let mut rng = thread_rng();
let mut rng = rng();
(0..n)
.map(|_| EdwardsPoint::mul_base(&Scalar::random(&mut rng)))
.collect()
Expand Down Expand Up @@ -269,7 +269,7 @@ mod ristretto_benches {
|b, &&size| {
let mut rng = OsRng;
let points: Vec<RistrettoPoint> = (0..size)
.map(|_| RistrettoPoint::random(&mut rng))
.map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap())
.collect();
b.iter(|| RistrettoPoint::double_and_compress_batch(&points));
},
Expand Down Expand Up @@ -319,7 +319,7 @@ mod scalar_benches {
use super::*;

fn scalar_arith<M: Measurement>(c: &mut BenchmarkGroup<M>) {
let mut rng = thread_rng();
let mut rng = rng();

c.bench_function("Scalar inversion", |b| {
let s = Scalar::from(897987897u64).invert();
Expand Down Expand Up @@ -354,7 +354,7 @@ mod scalar_benches {
BenchmarkId::new("Batch scalar inversion", *batch_size),
&batch_size,
|b, &&size| {
let mut rng = OsRng;
let mut rng = OsRng.unwrap_err();
let scalars: Vec<Scalar> =
(0..size).map(|_| Scalar::random(&mut rng)).collect();
b.iter(|| {
Expand Down
40 changes: 23 additions & 17 deletions curve25519-dalek/src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ use core::ops::{AddAssign, SubAssign};
use core::ops::{Mul, MulAssign};

#[cfg(feature = "digest")]
use digest::{generic_array::typenum::U64, Digest};
use digest::{array::typenum::U64, Digest};

#[cfg(feature = "group")]
use {
group::{cofactor::CofactorGroup, prime::PrimeGroup, GroupEncoding},
rand_core::TryRngCore,
subtle::CtOption,
};

Expand Down Expand Up @@ -667,7 +668,7 @@ impl EdwardsPoint {
/// Uses rejection sampling, generating a random `CompressedEdwardsY` and then attempting point
/// decompression, rejecting invalid points.
#[cfg(any(test, feature = "rand_core"))]
pub fn random(mut rng: impl RngCore) -> Self {
pub fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
let mut repr = CompressedEdwardsY([0u8; 32]);
loop {
rng.fill_bytes(&mut repr.0);
Expand Down Expand Up @@ -1364,9 +1365,16 @@ impl Debug for EdwardsPoint {
impl group::Group for EdwardsPoint {
type Scalar = Scalar;

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

fn identity() -> Self {
Expand Down Expand Up @@ -1609,20 +1617,20 @@ impl Zeroize for SubgroupPoint {
impl group::Group for SubgroupPoint {
type Scalar = Scalar;

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

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

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

fn identity() -> Self {
Expand Down Expand Up @@ -1688,9 +1696,7 @@ impl CofactorGroup for EdwardsPoint {
mod test {
use super::*;

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

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
Expand Down Expand Up @@ -1985,7 +1991,7 @@ mod test {
#[cfg(feature = "precomputed-tables")]
let random_point = {
let mut b = [0u8; 32];
csprng.fill_bytes(&mut b);
csprng.try_fill_bytes(&mut b).unwrap();
EdwardsPoint::mul_base_clamped(b) + constants::EIGHT_TORSION[1]
};
// Make a basepoint table from the random point. We'll use this with mul_base_clamped
Expand All @@ -2011,7 +2017,7 @@ mod test {
for _ in 0..100 {
// This will be reduced mod l with probability l / 2^256 ≈ 6.25%
let mut a_bytes = [0u8; 32];
csprng.fill_bytes(&mut a_bytes);
csprng.try_fill_bytes(&mut a_bytes).unwrap();

assert_eq!(
EdwardsPoint::mul_base_clamped(a_bytes),
Expand Down Expand Up @@ -2096,7 +2102,7 @@ mod test {
#[cfg(feature = "alloc")]
#[test]
fn compress_batch() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

// TODO(tarcieri): proptests?
// Make some points deterministically then randomly
Expand Down Expand Up @@ -2152,7 +2158,7 @@ mod test {
// A single iteration of a consistency check for MSM.
#[cfg(feature = "alloc")]
fn multiscalar_consistency_iter(n: usize) {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

// Construct random coefficients x0, ..., x_{n-1},
// followed by some extra hardcoded ones.
Expand Down Expand Up @@ -2215,7 +2221,7 @@ mod test {
#[test]
#[cfg(feature = "alloc")]
fn batch_to_montgomery() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

let scalars = (0..128)
.map(|_| Scalar::random(&mut rng))
Expand All @@ -2240,7 +2246,7 @@ mod test {
#[test]
#[cfg(feature = "alloc")]
fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

let static_scalars = (0..128)
.map(|_| Scalar::random(&mut rng))
Expand Down
20 changes: 10 additions & 10 deletions curve25519-dalek/src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use core::{
use crate::constants::{APLUS2_OVER_FOUR, MONTGOMERY_A, MONTGOMERY_A_NEG};
use crate::edwards::{CompressedEdwardsY, EdwardsPoint};
use crate::field::FieldElement;
use crate::scalar::{clamp_integer, Scalar};
use crate::scalar::{Scalar, clamp_integer};

use crate::traits::Identity;

Expand Down Expand Up @@ -437,7 +437,7 @@ mod test {
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use rand_core::{CryptoRng, RngCore};
use rand_core::{CryptoRng, RngCore, TryRngCore};

#[test]
fn identity_in_different_coordinates() {
Expand Down Expand Up @@ -521,8 +521,8 @@ mod test {
}

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

Expand All @@ -540,10 +540,10 @@ mod test {

#[test]
fn montgomery_ladder_matches_edwards_scalarmult() {
let mut csprng = rand_core::OsRng;
let mut csprng = rand_core::OsRng.unwrap_err();

for _ in 0..100 {
let p_edwards = rand_prime_order_point(csprng);
let p_edwards = rand_prime_order_point(&mut csprng);
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();

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

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

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

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

assert_eq!(
MontgomeryPoint::mul_base_clamped(a_bytes),
Expand Down
Loading