-
Notifications
You must be signed in to change notification settings - Fork 126
adds a Field::try_from_rng method #127
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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) | ||
| .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>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concept ACK (this will address #109), but I want to understand the Also, this change to the trait should be documented in the changelog.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #126 (comment), |
||
|
|
||
| /// Returns true iff this element is zero. | ||
| fn is_zero(&self) -> Choice { | ||
|
|
||
There was a problem hiding this comment.
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.