Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relax Sized requirements on the rng #1777

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
4 changes: 2 additions & 2 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub trait AeadCore {
/// See [`AeadCore::generate_nonce`] documentation for requirements for
/// random nonces.
#[cfg(feature = "rand_core")]
fn generate_nonce_with_rng<R: CryptoRng>(rng: &mut R) -> Nonce<Self> {
fn generate_nonce_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Nonce<Self> {
let mut nonce = Nonce::<Self>::default();
rng.fill_bytes(&mut nonce);
nonce
Expand All @@ -141,7 +141,7 @@ pub trait AeadCore {
/// See [`AeadCore::generate_nonce`] documentation for requirements for
/// random nonces.
#[cfg(feature = "rand_core")]
fn try_generate_nonce_with_rng<R: TryCryptoRng>(
fn try_generate_nonce_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> core::result::Result<Nonce<Self>, R::Error> {
let mut nonce = Nonce::<Self>::default();
Expand Down
28 changes: 18 additions & 10 deletions crypto-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: CryptoRng>(rng: &mut R) -> Key<Self> {
fn generate_key_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
key
Expand All @@ -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<R: TryCryptoRng>(rng: &mut R) -> Result<Key<Self>, R::Error> {
fn try_generate_key_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Key<Self>, R::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
Expand Down Expand Up @@ -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<R: CryptoRng>(rng: &mut R) -> Key<Self> {
fn generate_key_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
key
Expand All @@ -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<R: TryCryptoRng>(rng: &mut R) -> Result<Key<Self>, R::Error> {
fn try_generate_key_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Key<Self>, R::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
Expand All @@ -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<R: CryptoRng>(rng: &mut R) -> Iv<Self> {
fn generate_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
Expand All @@ -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<R: TryCryptoRng>(rng: &mut R) -> Result<Iv<Self>, R::Error> {
fn try_generate_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Iv<Self>, R::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
Expand All @@ -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<R: CryptoRng>(rng: &mut R) -> (Key<Self>, Iv<Self>) {
fn generate_key_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> (Key<Self>, Iv<Self>) {
let key = Self::generate_key_with_rng(rng);
let iv = Self::generate_iv_with_rng(rng);
(key, iv)
Expand All @@ -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<R: TryCryptoRng>(
fn try_generate_key_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<(Key<Self>, Iv<Self>), R::Error> {
let key = Self::try_generate_key_with_rng(rng)?;
Expand Down Expand Up @@ -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<R: CryptoRng>(rng: &mut R) -> Iv<Self> {
fn generate_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
Expand All @@ -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<R: TryCryptoRng>(rng: &mut R) -> Result<Iv<Self>, R::Error> {
fn try_generate_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Iv<Self>, R::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -575,7 +575,7 @@ impl ToEncodedPoint<MockCurve> for ProjectivePoint {
impl group::Group for ProjectivePoint {
type Scalar = Scalar;

fn random(_rng: impl RngCore) -> Self {
fn try_from_rng<R: TryRngCore + ?Sized>(_rng: &mut R) -> core::result::Result<Self, R::Error> {
unimplemented!();
}

Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ where
C: CurveArithmetic,
{
/// Generate a cryptographically random [`EphemeralSecret`].
pub fn random<R: CryptoRng>(rng: &mut R) -> Self {
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
Self {
scalar: NonZeroScalar::random(rng),
}
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/point/non_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ where
P: ConditionallySelectable + ConstantTimeEq + Curve + Default,
{
/// Generate a random `NonIdentity<ProjectivePoint>`.
pub fn random<R: CryptoRng>(mut rng: R) -> Self {
pub fn random<R: CryptoRng + ?Sized>(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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/scalar/blinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
C: CurveArithmetic,
{
/// Create a new [`BlindedScalar`] from a scalar and a [`CryptoRng`].
pub fn new<R: CryptoRng>(scalar: Scalar<C>, rng: &mut R) -> Self {
pub fn new<R: CryptoRng + ?Sized>(scalar: Scalar<C>, rng: &mut R) -> Self {
Self {
scalar,
mask: Scalar::<C>::random(rng),
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/scalar/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
pub const MODULUS: C::Uint = C::ORDER;

/// Generate a random [`ScalarPrimitive`].
pub fn random<R: CryptoRng>(rng: &mut R) -> Self {
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
Self {
inner: C::Uint::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()),
}
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ where

/// Generate a random [`SecretKey`].
#[cfg(feature = "arithmetic")]
pub fn random<R: CryptoRng>(rng: &mut R) -> Self
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
where
C: CurveArithmetic,
{
Expand Down
6 changes: 4 additions & 2 deletions password-hash/src/salt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,17 @@ pub struct SaltString {
impl SaltString {
/// Generate a random B64-encoded [`SaltString`] from [`CryptoRng`].
#[cfg(feature = "rand_core")]
pub fn from_rng<R: CryptoRng>(rng: &mut R) -> Self {
pub fn from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
let mut bytes = [0u8; Salt::RECOMMENDED_LENGTH];
rng.fill_bytes(&mut bytes);
Self::encode_b64(&bytes).expect(INVARIANT_VIOLATED_MSG)
}

/// Generate a random B64-encoded [`SaltString`] from [`TryCryptoRng`].
#[cfg(feature = "rand_core")]
pub fn try_from_rng<R: TryCryptoRng>(rng: &mut R) -> core::result::Result<Self, R::Error> {
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> core::result::Result<Self, R::Error> {
let mut bytes = [0u8; Salt::RECOMMENDED_LENGTH];
rng.try_fill_bytes(&mut bytes)?;
let salt = Self::encode_b64(&bytes).expect(INVARIANT_VIOLATED_MSG);
Expand Down