Skip to content

Commit fd7a51f

Browse files
committed
update more dependencies
1 parent e944f9d commit fd7a51f

File tree

9 files changed

+77
-41
lines changed

9 files changed

+77
-41
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ elliptic-curve = { version = "0.13", features = [
3434
"voprf",
3535
] }
3636
generic-array = "1"
37-
rand_core = { version = "0.6", default-features = false }
37+
rand_core = { version = "0.9", default-features = false }
3838
serde = { version = "1", default-features = false, features = [
3939
"derive",
4040
], optional = true }
4141
sha2 = { version = "0.10", default-features = false, optional = true }
42-
subtle = { version = "2.3", default-features = false }
42+
subtle = { version = "2.6", default-features = false }
4343
zeroize = { version = "1.5", default-features = false }
4444

4545
[dev-dependencies]
@@ -58,7 +58,7 @@ p521 = { version = "0.13.3", default-features = false, features = [
5858
"voprf",
5959
] }
6060
proptest = "1"
61-
rand = "0.8"
61+
rand = "0.9"
6262
regex = "1"
6363
serde_json = "1"
6464
sha2 = "0.10"

src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use digest::{Digest, Output, OutputSizeUser};
1616
use generic_array::sequence::Concat;
1717
use generic_array::typenum::{IsLess, Unsigned, U2, U256, U9};
1818
use generic_array::{ArrayLength, GenericArray};
19-
use rand_core::{CryptoRng, RngCore};
19+
use rand_core::{TryCryptoRng, TryRngCore};
2020
use subtle::ConstantTimeEq;
2121

2222
#[cfg(feature = "serde")]
@@ -128,7 +128,7 @@ pub struct Proof<CS: CipherSuite> {
128128

129129
/// Can only fail with [`Error::Batch`].
130130
#[allow(clippy::many_single_char_names)]
131-
pub(crate) fn generate_proof<CS: CipherSuite, R: RngCore + CryptoRng>(
131+
pub(crate) fn generate_proof<CS: CipherSuite, R: TryRngCore + TryCryptoRng>(
132132
rng: &mut R,
133133
k: <CS::Group as Group>::Scalar,
134134
a: <CS::Group as Group>::Elem,

src/group/elliptic_curve.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// of this source tree. You may select, at your option, one of the above-listed
77
// licenses.
88

9+
use core::num::NonZeroU32;
910
use core::ops::Add;
1011

1112
use digest::core_api::BlockSizeUser;
@@ -19,7 +20,7 @@ use elliptic_curve::{
1920
};
2021
use generic_array::typenum::{IsLess, IsLessOrEqual, Sum, U256};
2122
use generic_array::{ArrayLength, GenericArray};
22-
use rand_core::{CryptoRng, RngCore};
23+
use rand_core::{TryCryptoRng, TryRngCore};
2324

2425
use super::Group;
2526
use crate::{Error, InternalError, Result};
@@ -93,8 +94,8 @@ where
9394
.map_err(|_| Error::Deserialization)
9495
}
9596

96-
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
97-
*SecretKey::<Self>::random(rng).to_nonzero_scalar()
97+
fn random_scalar<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Self::Scalar {
98+
*SecretKey::<Self>::random(&mut CompatRng(rng)).to_nonzero_scalar()
9899
}
99100

100101
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar {
@@ -123,3 +124,39 @@ where
123124
.map_err(|_| Error::Deserialization)
124125
}
125126
}
127+
128+
/// Adapter allowing `rand_core 0.9` RNGs to satisfy the `elliptic_curve` 0.13
129+
/// requirement for `rand_core 0.6` traits.
130+
struct CompatRng<'a, R>(&'a mut R);
131+
132+
impl<'a, R> elliptic_curve::rand_core::RngCore for CompatRng<'a, R>
133+
where
134+
R: TryRngCore,
135+
{
136+
fn next_u32(&mut self) -> u32 {
137+
self.0.try_next_u32().expect("RNG failure")
138+
}
139+
140+
fn next_u64(&mut self) -> u64 {
141+
self.0.try_next_u64().expect("RNG failure")
142+
}
143+
144+
fn fill_bytes(&mut self, dest: &mut [u8]) {
145+
self.0
146+
.try_fill_bytes(dest)
147+
.expect("RNG failure while filling bytes");
148+
}
149+
150+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), elliptic_curve::rand_core::Error> {
151+
self.0.try_fill_bytes(dest).map_err(|_| compat_error())?;
152+
Ok(())
153+
}
154+
}
155+
156+
impl<'a, R> elliptic_curve::rand_core::CryptoRng for CompatRng<'a, R> where R: TryCryptoRng {}
157+
158+
fn compat_error() -> elliptic_curve::rand_core::Error {
159+
let code = NonZeroU32::new(elliptic_curve::rand_core::Error::CUSTOM_START)
160+
.expect("CUSTOM_START must be non-zero");
161+
elliptic_curve::rand_core::Error::from(code)
162+
}

src/group/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use digest::core_api::BlockSizeUser;
1818
use digest::{FixedOutput, HashMarker};
1919
use generic_array::typenum::{IsLess, IsLessOrEqual, Sum, U256};
2020
use generic_array::{ArrayLength, GenericArray};
21-
use rand_core::{CryptoRng, RngCore};
21+
use rand_core::{TryCryptoRng, TryRngCore};
2222
#[cfg(feature = "ristretto255")]
2323
pub use ristretto::Ristretto255;
2424
use subtle::{Choice, ConstantTimeEq};
@@ -101,7 +101,7 @@ where
101101
fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem>;
102102

103103
/// picks a scalar at random
104-
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar;
104+
fn random_scalar<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Self::Scalar;
105105

106106
/// The multiplicative inverse of this scalar
107107
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar;

src/group/ristretto.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use digest::{FixedOutput, HashMarker};
1515
use elliptic_curve::hash2curve::{ExpandMsg, ExpandMsgXmd, Expander};
1616
use generic_array::typenum::{IsLess, IsLessOrEqual, U256, U32, U64};
1717
use generic_array::GenericArray;
18-
use rand_core::{CryptoRng, RngCore};
18+
use rand_core::{TryCryptoRng, TryRngCore};
1919
use subtle::ConstantTimeEq;
2020

2121
use super::Group;
@@ -94,10 +94,11 @@ impl Group for Ristretto255 {
9494
.ok_or(Error::Deserialization)
9595
}
9696

97-
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
97+
fn random_scalar<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Self::Scalar {
9898
loop {
9999
let mut scalar_bytes = [0u8; 32];
100-
rng.fill_bytes(&mut scalar_bytes);
100+
rng.try_fill_bytes(&mut scalar_bytes)
101+
.expect("RNG failure while filling scalar bytes");
101102

102103
if let Ok(scalar) = Self::deserialize_scalar(&scalar_bytes) {
103104
break scalar;

src/oprf.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use derive_where::derive_where;
1414
use digest::{Digest, Output};
1515
use generic_array::typenum::Unsigned;
1616
use generic_array::GenericArray;
17-
use rand_core::{CryptoRng, RngCore};
17+
use rand_core::{TryCryptoRng, TryRngCore};
1818

1919
use crate::common::{
2020
derive_key_internal, deterministic_blind_unchecked, hash_to_group, i2osp_2,
@@ -73,7 +73,7 @@ impl<CS: CipherSuite> OprfClient<CS> {
7373
///
7474
/// # Errors
7575
/// [`Error::Input`] if the `input` is empty or longer then [`u16::MAX`].
76-
pub fn blind<R: RngCore + CryptoRng>(
76+
pub fn blind<R: TryRngCore + TryCryptoRng>(
7777
input: &[u8],
7878
blinding_factor_rng: &mut R,
7979
) -> Result<OprfClientBlindResult<CS>> {
@@ -146,9 +146,10 @@ impl<CS: CipherSuite> OprfServer<CS> {
146146
///
147147
/// # Errors
148148
/// [`Error::Protocol`] if the protocol fails and can't be completed.
149-
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self> {
149+
pub fn new<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Result<Self> {
150150
let mut seed = GenericArray::<_, <CS::Group as Group>::ScalarLen>::default();
151-
rng.fill_bytes(&mut seed);
151+
rng.try_fill_bytes(&mut seed)
152+
.map_err(|_| Error::Protocol)?;
152153
Self::new_from_seed(&seed, &[])
153154
}
154155

@@ -268,6 +269,7 @@ mod tests {
268269
use core::ptr;
269270

270271
use rand::rngs::OsRng;
272+
use rand::TryRngCore;
271273

272274
use super::*;
273275
use crate::common::{Dst, STR_HASH_TO_GROUP};
@@ -304,7 +306,7 @@ mod tests {
304306
fn base_inversion_unsalted<CS: CipherSuite>() {
305307
let mut rng = OsRng;
306308
let mut input = [0u8; 64];
307-
rng.fill_bytes(&mut input);
309+
rng.try_fill_bytes(&mut input).unwrap();
308310
let client_blind_result = OprfClient::<CS>::blind(&input, &mut rng).unwrap();
309311
let client_finalize_result = client_blind_result
310312
.state

src/poprf.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use derive_where::derive_where;
1616
use digest::{Digest, Output, OutputSizeUser};
1717
use generic_array::typenum::Unsigned;
1818
use generic_array::{ArrayLength, GenericArray};
19-
use rand_core::{CryptoRng, RngCore};
19+
use rand_core::{TryCryptoRng, TryRngCore};
2020

2121
use crate::common::{
2222
derive_keypair, deterministic_blind_unchecked, generate_proof, hash_to_group, i2osp_2,
@@ -75,7 +75,7 @@ impl<CS: CipherSuite> PoprfClient<CS> {
7575
///
7676
/// # Errors
7777
/// [`Error::Input`] if the `input` is empty or longer than [`u16::MAX`].
78-
pub fn blind<R: RngCore + CryptoRng>(
78+
pub fn blind<R: TryRngCore + TryCryptoRng>(
7979
input: &[u8],
8080
blinding_factor_rng: &mut R,
8181
) -> Result<PoprfClientBlindResult<CS>> {
@@ -189,9 +189,10 @@ impl<CS: CipherSuite> PoprfServer<CS> {
189189
///
190190
/// # Errors
191191
/// [`Error::Protocol`] if the protocol fails and can't be completed.
192-
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self> {
192+
pub fn new<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Result<Self> {
193193
let mut seed = GenericArray::<_, <CS::Group as Group>::ScalarLen>::default();
194-
rng.fill_bytes(&mut seed);
194+
rng.try_fill_bytes(&mut seed)
195+
.map_err(|_| Error::Protocol)?;
195196

196197
Self::new_from_seed(&seed, &[])
197198
}
@@ -235,7 +236,7 @@ impl<CS: CipherSuite> PoprfServer<CS> {
235236
/// # Errors
236237
/// - [`Error::Info`] if the `info` is longer than `u16::MAX`.
237238
/// - [`Error::Protocol`] if the protocol fails and can't be completed.
238-
pub fn blind_evaluate<R: RngCore + CryptoRng>(
239+
pub fn blind_evaluate<R: TryRngCore + TryCryptoRng>(
239240
&self,
240241
rng: &mut R,
241242
blinded_element: &BlindedElement<CS>,
@@ -273,7 +274,7 @@ impl<CS: CipherSuite> PoprfServer<CS> {
273274
/// - [`Error::Info`] if the `info` is longer than `u16::MAX`.
274275
/// - [`Error::Protocol`] if the protocol fails and can't be completed.
275276
#[cfg(feature = "alloc")]
276-
pub fn batch_blind_evaluate<'a, R: RngCore + CryptoRng, IE>(
277+
pub fn batch_blind_evaluate<'a, R: TryRngCore + TryCryptoRng, IE>(
277278
&self,
278279
rng: &mut R,
279280
blinded_elements: &'a IE,
@@ -346,7 +347,7 @@ impl<CS: CipherSuite> PoprfServer<CS> {
346347
pub fn batch_blind_evaluate_finish<
347348
'a,
348349
'b,
349-
R: RngCore + CryptoRng,
350+
R: TryRngCore + TryCryptoRng,
350351
IB: Iterator<Item = &'a BlindedElement<CS>> + ExactSizeIterator,
351352
IE,
352353
>(

src/tests/mock_rng.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use alloc::vec::Vec;
1010
use core::cmp::min;
1111

12-
use rand_core::{CryptoRng, Error, RngCore};
12+
use rand_core::{CryptoRng, RngCore};
1313

1414
/// A simple implementation of `RngCore` for testing purposes.
1515
///
@@ -54,12 +54,6 @@ impl RngCore for CycleRng {
5454
dest[..len].copy_from_slice(&self.v[..len]);
5555
rotate_left(&mut self.v, len);
5656
}
57-
58-
#[inline]
59-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
60-
self.fill_bytes(dest);
61-
Ok(())
62-
}
6357
}
6458

6559
// This is meant for testing only

src/voprf.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use derive_where::derive_where;
1616
use digest::{Digest, Output};
1717
use generic_array::typenum::Unsigned;
1818
use generic_array::GenericArray;
19-
use rand_core::{CryptoRng, RngCore};
19+
use rand_core::{TryCryptoRng, TryRngCore};
2020

2121
use crate::common::{
2222
derive_keypair, deterministic_blind_unchecked, generate_proof, hash_to_group, i2osp_2,
@@ -75,7 +75,7 @@ impl<CS: CipherSuite> VoprfClient<CS> {
7575
///
7676
/// # Errors
7777
/// [`Error::Input`] if the `input` is empty or longer then [`u16::MAX`].
78-
pub fn blind<R: RngCore + CryptoRng>(
78+
pub fn blind<R: TryRngCore + TryCryptoRng>(
7979
input: &[u8],
8080
blinding_factor_rng: &mut R,
8181
) -> Result<VoprfClientBlindResult<CS>> {
@@ -196,9 +196,10 @@ impl<CS: CipherSuite> VoprfServer<CS> {
196196
///
197197
/// # Errors
198198
/// [`Error::Protocol`] if the protocol fails and can't be completed.
199-
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Result<Self> {
199+
pub fn new<R: TryRngCore + TryCryptoRng>(rng: &mut R) -> Result<Self> {
200200
let mut seed = GenericArray::<_, <CS::Group as Group>::ScalarLen>::default();
201-
rng.fill_bytes(&mut seed);
201+
rng.try_fill_bytes(&mut seed)
202+
.map_err(|_| Error::Protocol)?;
202203
// This can't fail as the hash output is type constrained.
203204
Self::new_from_seed(&seed, &[])
204205
}
@@ -238,7 +239,7 @@ impl<CS: CipherSuite> VoprfServer<CS> {
238239
/// Computes the second step for the multiplicative blinding version of
239240
/// DH-OPRF. This message is sent from the server (who holds the OPRF key)
240241
/// to the client.
241-
pub fn blind_evaluate<R: RngCore + CryptoRng>(
242+
pub fn blind_evaluate<R: TryRngCore + TryCryptoRng>(
242243
&self,
243244
rng: &mut R,
244245
blinded_element: &BlindedElement<CS>,
@@ -271,7 +272,7 @@ impl<CS: CipherSuite> VoprfServer<CS> {
271272
/// [`Error::Batch`] if the number of `blinded_elements` and
272273
/// `evaluation_elements` don't match or is longer then [`u16::MAX`]
273274
#[cfg(feature = "alloc")]
274-
pub fn batch_blind_evaluate<'a, R: RngCore + CryptoRng, I>(
275+
pub fn batch_blind_evaluate<'a, R: TryRngCore + TryCryptoRng, I>(
275276
&self,
276277
rng: &mut R,
277278
blinded_elements: &'a I,
@@ -322,7 +323,7 @@ impl<CS: CipherSuite> VoprfServer<CS> {
322323
pub fn batch_blind_evaluate_finish<
323324
'a,
324325
'b,
325-
R: RngCore + CryptoRng,
326+
R: TryRngCore + TryCryptoRng,
326327
IB: Iterator<Item = &'a BlindedElement<CS>> + ExactSizeIterator,
327328
IE,
328329
>(
@@ -599,7 +600,7 @@ mod tests {
599600
let num_iterations = 10;
600601
for _ in 0..num_iterations {
601602
let mut input = [0u8; 32];
602-
rng.fill_bytes(&mut input);
603+
rng.try_fill_bytes(&mut input).unwrap();
603604
let client_blind_result = VoprfClient::<CS>::blind(&input, &mut rng).unwrap();
604605
inputs.push(input);
605606
client_states.push(client_blind_result.state);
@@ -643,7 +644,7 @@ mod tests {
643644
let num_iterations = 10;
644645
for _ in 0..num_iterations {
645646
let mut input = [0u8; 32];
646-
rng.fill_bytes(&mut input);
647+
rng.try_fill_bytes(&mut input).unwrap();
647648
let client_blind_result = VoprfClient::<CS>::blind(&input, &mut rng).unwrap();
648649
inputs.push(input);
649650
client_states.push(client_blind_result.state);

0 commit comments

Comments
 (0)