Skip to content

Conversation

@baloo
Copy link

@baloo baloo commented Feb 26, 2025

This is stacked on #126

@baloo baloo force-pushed the baloo/try_from_rng branch from 72a85c2 to 535bfe6 Compare February 26, 2025 18:48
@baloo baloo marked this pull request as draft February 26, 2025 19:41
@baloo baloo force-pushed the baloo/try_from_rng branch from 535bfe6 to 4ccfef5 Compare February 26, 2025 20:02
tarcieri pushed a commit to RustCrypto/traits that referenced this pull request Mar 4, 2025
Depends:
 - zkcrypto/ff#126
 - zkcrypto/ff#127
 
This is to provide an `ecdsa::SigningKey::try_from_rng` API
(RustCrypto/signatures#915)
@baloo baloo marked this pull request as ready for review March 5, 2025 01:15
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self;

/// 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.

@baloo baloo force-pushed the baloo/try_from_rng branch from 4ccfef5 to fed79ef Compare March 9, 2025 03:57
@baloo baloo changed the base branch from main to release-0.14.0 March 9, 2025 03:58
@baloo baloo force-pushed the baloo/try_from_rng branch from fed79ef to 94d4f18 Compare March 9, 2025 03:59
Copy link
Member

@str4d str4d left a comment

Choose a reason for hiding this comment

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

Reviewed 94d4f18.

My main question is what approach should the API take:

  • Should we require trait implementers to implement both of these manually (as the PR currently does)? Are there platforms where the infallible RNG APIs are not simply wrappers around the fallible APIs?
  • Should this become the main API that gets implemented, and Field::random gains a default method that calls Self::try_from_rng(rng).expect("must not fail")?
    • If this is what all downstream RNG APIs do, then we can do the same.
  • Should Field::try_from_rng have a default impl of Ok(Self::random(rng)) (making this a pure addition)?
    • I like this option the least, as it hides a panic inside an API that claims it reports errors.

fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self;

/// 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.

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

CHANGELOG.md Outdated
## [Unreleased]

### Added
- `Field::try_from_rng` method.
Copy link
Member

Choose a reason for hiding this comment

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

This is a change to the crate, not an addition, because it adds a new method to the Field trait without a default implementation.

@baloo baloo force-pushed the baloo/try_from_rng branch 2 times, most recently from 5dbbdee to f015932 Compare March 9, 2025 05:28
@baloo
Copy link
Author

baloo commented Mar 9, 2025

Should this become the main API that gets implemented, and Field::random gains a default method that calls Self::try_from_rng(rng).expect("must not fail")?

  • If this is what all downstream RNG APIs do, then we can do the same.

yeah I went with that, the blanket TryRngCore we get has an Infallible error and made that explicit to protect the panic against a refactor.

@baloo baloo force-pushed the baloo/try_from_rng branch 3 times, most recently from 0d75807 to 339e82c Compare March 9, 2025 05:45
Copy link
Member

@str4d str4d left a comment

Choose a reason for hiding this comment

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

utACK 339e82c once ff_derive comment is addressed.

As with #126 (review), this change will need to go through a zkcrypto RFC before we cut the final 0.14.0 release.


/// 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> {
Copy link
Member

Choose a reason for hiding this comment

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

We always use ::crate_name notation to defend against the import decisions within the module in which the derive is used (see the rest of ff_derive for examples).

Suggested change
fn try_from_rng<R: ::ff::derive::rand_core::TryRngCore + ?Sized>(rng: &mut R) -> core::result::Result<Self, R::Error> {
fn try_from_rng<R: ::ff::derive::rand_core::TryRngCore + ?Sized>(rng: &mut R) -> ::core::result::Result<Self, R::Error> {

CHANGELOG.md Outdated
- `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.
- `Field::try_from_rng` added. It accepts a `rand_core::TryRngCore` mutable reference.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- `Field::try_from_rng` added. It accepts a `rand_core::TryRngCore` mutable reference.
- `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.

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.

@baloo baloo force-pushed the baloo/try_from_rng branch from 339e82c to ae84736 Compare March 9, 2025 05:59
@baloo baloo mentioned this pull request Mar 9, 2025
Copy link
Member

@str4d str4d left a comment

Choose a reason for hiding this comment

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

utACK ae84736

@str4d str4d merged commit 1bb6345 into zkcrypto:release-0.14.0 Mar 9, 2025
11 checks passed
@baloo baloo deleted the baloo/try_from_rng branch March 9, 2025 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants