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 @@ -12,6 +12,8 @@ and this library adheres to Rust's notion of
- `group::Group::random(rng: impl RngCore) -> Self` has been changed to
`Group::random<R: RngCore + ?Sized>(rng: &mut R) -> Self`, to enable passing a
trait object as the RNG.
- `group::Group::try_from_rng` is a new trait method that must be implemented by
downstreams. `Group::random` now has a default implementation that calls it.

## [0.13.0] - 2022-12-06
### Changed
Expand Down
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ extern crate alloc;
// Re-export ff to make version-matching easier.
pub use ff;

use core::convert::Infallible;
use core::fmt;
use core::iter::Sum;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use ff::PrimeField;
use rand_core::RngCore;
use rand_core::{RngCore, TryRngCore};
use subtle::{Choice, CtOption};

pub mod cofactor;
Expand Down Expand Up @@ -76,7 +77,22 @@ pub trait Group:
/// this group.
///
/// This function is non-deterministic, and samples from the 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 from the non-identity elements of
/// this group.
///
/// This function is non-deterministic, and samples from the user-provided RNG.
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Having trouble rebasing because the substantive change ended up in a merge commit which doesn't survive rebases.


/// Returns the additive identity, also known as the "neutral element".
fn identity() -> Self;
Expand Down