diff --git a/Cargo.lock b/Cargo.lock index 9ee8e0055..192ae78f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -293,7 +293,7 @@ dependencies = [ [[package]] name = "group" version = "0.13.0" -source = "git+https://github.com/pinkforest/group.git?branch=bump-rand-0.9#06ac6fb11ced26fbf980ee65e74fced4da66ec3e" +source = "git+https://github.com/baloo/group.git?branch=baloo%2Ftry_from_rng#b0d6ea48fe55327b11ea03f9a965d9e16bb83adc" dependencies = [ "ff", "rand_core", diff --git a/Cargo.toml b/Cargo.toml index 21a734666..2df244b64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,4 +31,5 @@ crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint.git" } ff = { git = "https://github.com/zkcrypto/ff.git", branch = "release-0.14.0" } # https://github.com/zkcrypto/group/pull/56 -group = { git = "https://github.com/pinkforest/group.git", branch = "bump-rand-0.9" } +# https://github.com/zkcrypto/group/pull/57 +group = { git = "https://github.com/baloo/group.git", branch = "baloo/try_from_rng" } diff --git a/aead/src/lib.rs b/aead/src/lib.rs index e2307cb2a..140c6e19a 100644 --- a/aead/src/lib.rs +++ b/aead/src/lib.rs @@ -139,7 +139,7 @@ pub trait AeadCore { /// See [`AeadCore::generate_nonce`] documentation for requirements for /// random nonces. #[cfg(feature = "rand_core")] - fn generate_nonce_with_rng(rng: &mut R) -> Nonce { + fn generate_nonce_with_rng(rng: &mut R) -> Nonce { let mut nonce = Nonce::::default(); rng.fill_bytes(&mut nonce); nonce @@ -150,7 +150,7 @@ pub trait AeadCore { /// See [`AeadCore::generate_nonce`] documentation for requirements for /// random nonces. #[cfg(feature = "rand_core")] - fn try_generate_nonce_with_rng( + fn try_generate_nonce_with_rng( rng: &mut R, ) -> core::result::Result, R::Error> { let mut nonce = Nonce::::default(); diff --git a/crypto-common/src/lib.rs b/crypto-common/src/lib.rs index 240e1257d..3e070ad4c 100644 --- a/crypto-common/src/lib.rs +++ b/crypto-common/src/lib.rs @@ -196,7 +196,7 @@ pub trait KeyInit: KeySizeUser + Sized { /// Generate random key using the provided [`CryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn generate_key_with_rng(rng: &mut R) -> Key { + fn generate_key_with_rng(rng: &mut R) -> Key { let mut key = Key::::default(); rng.fill_bytes(&mut key); key @@ -205,7 +205,9 @@ pub trait KeyInit: KeySizeUser + Sized { /// Generate random key using the provided [`TryCryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn try_generate_key_with_rng(rng: &mut R) -> Result, R::Error> { + fn try_generate_key_with_rng( + rng: &mut R, + ) -> Result, R::Error> { let mut key = Key::::default(); rng.try_fill_bytes(&mut key)?; Ok(key) @@ -250,7 +252,7 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random key using the provided [`CryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn generate_key_with_rng(rng: &mut R) -> Key { + fn generate_key_with_rng(rng: &mut R) -> Key { let mut key = Key::::default(); rng.fill_bytes(&mut key); key @@ -259,7 +261,9 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random key using the provided [`TryCryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn try_generate_key_with_rng(rng: &mut R) -> Result, R::Error> { + fn try_generate_key_with_rng( + rng: &mut R, + ) -> Result, R::Error> { let mut key = Key::::default(); rng.try_fill_bytes(&mut key)?; Ok(key) @@ -277,7 +281,7 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random IV using the provided [`CryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn generate_iv_with_rng(rng: &mut R) -> Iv { + fn generate_iv_with_rng(rng: &mut R) -> Iv { let mut iv = Iv::::default(); rng.fill_bytes(&mut iv); iv @@ -286,7 +290,9 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random IV using the provided [`TryCryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn try_generate_iv_with_rng(rng: &mut R) -> Result, R::Error> { + fn try_generate_iv_with_rng( + rng: &mut R, + ) -> Result, R::Error> { let mut iv = Iv::::default(); rng.try_fill_bytes(&mut iv)?; Ok(iv) @@ -304,7 +310,7 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random key and IV using the provided [`CryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn generate_key_iv_with_rng(rng: &mut R) -> (Key, Iv) { + fn generate_key_iv_with_rng(rng: &mut R) -> (Key, Iv) { let key = Self::generate_key_with_rng(rng); let iv = Self::generate_iv_with_rng(rng); (key, iv) @@ -313,7 +319,7 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { /// Generate random key and IV using the provided [`TryCryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn try_generate_key_iv_with_rng( + fn try_generate_key_iv_with_rng( rng: &mut R, ) -> Result<(Key, Iv), R::Error> { let key = Self::try_generate_key_with_rng(rng)?; @@ -357,7 +363,7 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized { /// Generate random IV using the provided [`CryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn generate_iv_with_rng(rng: &mut R) -> Iv { + fn generate_iv_with_rng(rng: &mut R) -> Iv { let mut iv = Iv::::default(); rng.fill_bytes(&mut iv); iv @@ -366,7 +372,9 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized { /// Generate random IV using the provided [`TryCryptoRng`]. #[cfg(feature = "rand_core")] #[inline] - fn try_generate_iv_with_rng(rng: &mut R) -> Result, R::Error> { + fn try_generate_iv_with_rng( + rng: &mut R, + ) -> Result, R::Error> { let mut iv = Iv::::default(); rng.try_fill_bytes(&mut iv)?; Ok(iv) diff --git a/elliptic-curve/src/dev.rs b/elliptic-curve/src/dev.rs index 4f1c58811..ea333960e 100644 --- a/elliptic-curve/src/dev.rs +++ b/elliptic-curve/src/dev.rs @@ -10,7 +10,7 @@ use crate::{ error::{Error, Result}, ops::{Invert, LinearCombination, MulByGenerator, Reduce, ShrAssign}, point::AffineCoordinates, - rand_core::{RngCore, TryRngCore}, + rand_core::TryRngCore, scalar::{FromUintUnchecked, IsHigh}, sec1::{CompressedPoint, FromEncodedPoint, ToEncodedPoint}, subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}, @@ -575,7 +575,7 @@ impl ToEncodedPoint for ProjectivePoint { impl group::Group for ProjectivePoint { type Scalar = Scalar; - fn random(_rng: impl RngCore) -> Self { + fn try_from_rng(_rng: &mut R) -> core::result::Result { unimplemented!(); } diff --git a/elliptic-curve/src/ecdh.rs b/elliptic-curve/src/ecdh.rs index baf4d9a98..74143999a 100644 --- a/elliptic-curve/src/ecdh.rs +++ b/elliptic-curve/src/ecdh.rs @@ -108,7 +108,7 @@ where C: CurveArithmetic, { /// Generate a cryptographically random [`EphemeralSecret`]. - pub fn random(rng: &mut R) -> Self { + pub fn random(rng: &mut R) -> Self { Self { scalar: NonZeroScalar::random(rng), } diff --git a/elliptic-curve/src/point/non_identity.rs b/elliptic-curve/src/point/non_identity.rs index c118852c1..d76463e84 100644 --- a/elliptic-curve/src/point/non_identity.rs +++ b/elliptic-curve/src/point/non_identity.rs @@ -58,9 +58,9 @@ where P: ConditionallySelectable + ConstantTimeEq + Curve + Default, { /// Generate a random `NonIdentity`. - pub fn random(mut rng: R) -> Self { + pub fn random(rng: &mut R) -> Self { loop { - if let Some(point) = Self::new(P::random(&mut rng)).into() { + if let Some(point) = Self::new(P::random(rng)).into() { break point; } } diff --git a/elliptic-curve/src/scalar/blinded.rs b/elliptic-curve/src/scalar/blinded.rs index 6638e84cc..9828edb33 100644 --- a/elliptic-curve/src/scalar/blinded.rs +++ b/elliptic-curve/src/scalar/blinded.rs @@ -38,7 +38,7 @@ where C: CurveArithmetic, { /// Create a new [`BlindedScalar`] from a scalar and a [`CryptoRng`]. - pub fn new(scalar: Scalar, rng: &mut R) -> Self { + pub fn new(scalar: Scalar, rng: &mut R) -> Self { Self { scalar, mask: Scalar::::random(rng), diff --git a/elliptic-curve/src/scalar/primitive.rs b/elliptic-curve/src/scalar/primitive.rs index b6720b0a5..3772cb81f 100644 --- a/elliptic-curve/src/scalar/primitive.rs +++ b/elliptic-curve/src/scalar/primitive.rs @@ -65,7 +65,7 @@ where pub const MODULUS: C::Uint = C::ORDER; /// Generate a random [`ScalarPrimitive`]. - pub fn random(rng: &mut R) -> Self { + pub fn random(rng: &mut R) -> Self { Self { inner: C::Uint::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()), } diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index f1559c8c4..9e9df1f38 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -94,7 +94,7 @@ where /// Generate a random [`SecretKey`]. #[cfg(feature = "arithmetic")] - pub fn random(rng: &mut R) -> Self + pub fn random(rng: &mut R) -> Self where C: CurveArithmetic, { diff --git a/kem/src/lib.rs b/kem/src/lib.rs index 06365882a..415764764 100644 --- a/kem/src/lib.rs +++ b/kem/src/lib.rs @@ -20,7 +20,7 @@ pub trait Encapsulate { type Error: Debug; /// Encapsulates a fresh shared secret - fn encapsulate(&self, rng: &mut impl CryptoRng) -> Result<(EK, SS), Self::Error>; + fn encapsulate(&self, rng: &mut R) -> Result<(EK, SS), Self::Error>; } /// A value that can be used to decapsulate an encapsulated key. diff --git a/password-hash/src/salt.rs b/password-hash/src/salt.rs index d96abe291..c42f8b6ef 100644 --- a/password-hash/src/salt.rs +++ b/password-hash/src/salt.rs @@ -203,7 +203,7 @@ pub struct SaltString { impl SaltString { /// Generate a random B64-encoded [`SaltString`] from [`CryptoRng`]. #[cfg(feature = "rand_core")] - pub fn from_rng(rng: &mut R) -> Self { + pub fn from_rng(rng: &mut R) -> Self { let mut bytes = [0u8; Salt::RECOMMENDED_LENGTH]; rng.fill_bytes(&mut bytes); Self::encode_b64(&bytes).expect(INVARIANT_VIOLATED_MSG) @@ -211,7 +211,9 @@ impl SaltString { /// Generate a random B64-encoded [`SaltString`] from [`TryCryptoRng`]. #[cfg(feature = "rand_core")] - pub fn try_from_rng(rng: &mut R) -> core::result::Result { + pub fn try_from_rng( + rng: &mut R, + ) -> core::result::Result { let mut bytes = [0u8; Salt::RECOMMENDED_LENGTH]; rng.try_fill_bytes(&mut bytes)?; let salt = Self::encode_b64(&bytes).expect(INVARIANT_VIOLATED_MSG);