Skip to content
Merged
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this library adheres to Rust's notion of
- `ff::Field::random(rng: impl RngCore) -> Self` has been changed back to
`Field::random<R: RngCore + ?Sized>(rng: &mut R) -> Self`, to enable passing a
trait object as the RNG.
- `ff::Field::try_from_rng` is a new trait method that must be implemented by
downstreams. `Field::random` now has a default implementation that calls it.

### Removed
- `derive_bits` feature flag (use `bits` instead).
Expand Down
6 changes: 3 additions & 3 deletions ff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,12 @@ fn prime_field_impl(
const ONE: Self = R;

/// Computes a uniformly random element using rejection sampling.
fn random<R: ::ff::derive::rand_core::RngCore + ?Sized>(rng: &mut R) -> Self {
fn try_from_rng<R: ::ff::derive::rand_core::TryRngCore + ?Sized>(rng: &mut R) -> ::core::result::Result<Self, R::Error> {
loop {
let mut tmp = {
let mut repr = [0u64; #limbs];
for i in 0..#limbs {
repr[i] = rng.next_u64();
repr[i] = rng.try_next_u64()?;
}
#name(repr)
};
Expand All @@ -1285,7 +1285,7 @@ fn prime_field_impl(
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffffu64.checked_shr(REPR_SHAVE_BITS).unwrap_or(0);

if tmp.is_valid() {
return tmp
return Ok(tmp)
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ pub use bitvec::view::BitViewSized;
#[cfg(feature = "bits")]
use bitvec::{array::BitArray, order::Lsb0};

use core::convert::Infallible;
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use rand_core::RngCore;
use rand_core::{RngCore, TryRngCore};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

/// Bit representation of a field element.
Expand Down Expand Up @@ -75,7 +76,19 @@ pub trait Field:
const ONE: Self;

/// Returns an element chosen uniformly at random using a user-provided RNG.
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self;
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
Self::try_from_rng(rng)
.map_err(|e: Infallible| e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that due to a blanket impl<R: RngCore> TryRngCore<Error = Infallible> for R, this does not impose any additional constraints or require any changes to the trait method documentation.

.expect("Infallible failed")

// NOTE: once MSRV gets to 1.82 remove the map_err/expect and use
// let Ok(out) = Self::try_from_rng(rng);
// out
// See: https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#omitting-empty-types-in-pattern-matching
}

/// Returns an element chosen uniformly at random using a user-provided RNG.
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK (this will address #109), but I want to understand the ?Sized change first (#126 (comment)).

Also, this change to the trait should be documented in the changelog.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #126 (comment), ff-derive needs to be updated to generate the new API.


/// Returns true iff this element is zero.
fn is_zero(&self) -> Choice {
Expand Down
1 change: 1 addition & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod full_limbs {
use ff::Field;

let _ = F384p::random(&mut rand::rng());
let _ = F384p::try_from_rng(&mut rand::rngs::OsRng).unwrap();
}
}

Expand Down