Skip to content

Commit

Permalink
Rename traits
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Varlakov <[email protected]>
  • Loading branch information
survived committed Jan 30, 2025
1 parent e2181f0 commit db67ab6
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 26 deletions.
8 changes: 4 additions & 4 deletions generic-ec-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait Curve: Debug + Copy + Eq + Ord + Hash + Default + Sync + Send + 'stati
+ Invertible
+ Zero
+ One
+ Samplable
+ FromUniformBytes
+ Zeroize
+ Copy
+ Eq
Expand Down Expand Up @@ -127,9 +127,9 @@ pub trait One {
fn is_one(x: &Self) -> Choice;
}

/// Type can be uniformly sampled
pub trait Samplable {
/// Byte array that can be converted into instance of `Self` via [`Samplable::from_uniform_bytes`]
/// Uniform instance of the type can be derived from uniformly distributed byte array
pub trait FromUniformBytes {
/// Byte array that can be converted into instance of `Self` via [`FromUniformBytes::from_uniform_bytes`]
type Bytes: ByteArray;

/// Maps uniformly distributed bytes array to uniformly distributed instance of `Self`.
Expand Down
4 changes: 2 additions & 2 deletions generic-ec-curves/benches/curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ fn bench_curve<E: Curve>(
}

fn random_scalar<E: Curve>(rng: &mut impl rand_core::RngCore) -> E::Scalar {
let mut bytes = <<E::Scalar as Samplable>::Bytes as ByteArray>::zeroes();
let mut bytes = <<E::Scalar as FromUniformBytes>::Bytes as ByteArray>::zeroes();
rng.fill_bytes(bytes.as_mut());
<E::Scalar as Samplable>::from_uniform_bytes(bytes)
<E::Scalar as FromUniformBytes>::from_uniform_bytes(bytes)
}

fn bench_bytes_reduction<E: Curve, const N: usize>(
Expand Down
2 changes: 1 addition & 1 deletion generic-ec-curves/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl generic_ec_core::One for Scalar {
}
}

impl generic_ec_core::Samplable for Scalar {
impl generic_ec_core::FromUniformBytes for Scalar {
/// 48 bytes
///
/// `L = ceil((ceil(log2(q)) + k) / 8) = ceil((256 + 128) / 8) = 48` bytes are enough to
Expand Down
5 changes: 3 additions & 2 deletions generic-ec-curves/src/rust_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use elliptic_curve::ops::Reduce;
use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint};
use elliptic_curve::{CurveArithmetic, FieldBytesSize, ScalarPrimitive};
use generic_ec_core::{
CompressedEncoding, Curve, IntegerEncoding, NoInvalidPoints, Samplable, UncompressedEncoding,
CompressedEncoding, Curve, FromUniformBytes, IntegerEncoding, NoInvalidPoints,
UncompressedEncoding,
};
use subtle::{ConditionallySelectable, ConstantTimeEq};
use zeroize::{DefaultIsZeroes, Zeroize};
Expand Down Expand Up @@ -88,7 +89,7 @@ where
for<'a> &'a C::ProjectivePoint: Mul<&'a C::Scalar, Output = C::ProjectivePoint>,
C::Scalar:
Reduce<C::Uint> + Eq + ConstantTimeEq + ConditionallySelectable + DefaultIsZeroes + Unpin,
RustCryptoScalar<C>: scalar::BytesModOrder + Samplable,
RustCryptoScalar<C>: scalar::BytesModOrder + FromUniformBytes,
for<'a> ScalarPrimitive<C>: From<&'a C::Scalar>,
FieldBytesSize<C>: ModulusSize,
X: 'static,
Expand Down
10 changes: 5 additions & 5 deletions generic-ec-curves/src/rust_crypto/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::ops::Mul;
use elliptic_curve::bigint::{ArrayEncoding, ByteArray, U256, U512};
use elliptic_curve::{Curve, CurveArithmetic, Field, Group, ScalarPrimitive};
use generic_ec_core::{
Additive, CurveGenerator, IntegerEncoding, Invertible, Multiplicative, One, Reduce, Samplable,
Zero,
Additive, CurveGenerator, FromUniformBytes, IntegerEncoding, Invertible, Multiplicative, One,
Reduce, Zero,
};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use zeroize::DefaultIsZeroes;
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<E: CurveArithmetic> One for RustCryptoScalar<E> {
}

#[cfg(feature = "secp256k1")]
impl Samplable for RustCryptoScalar<k256::Secp256k1> {
impl FromUniformBytes for RustCryptoScalar<k256::Secp256k1> {
type Bytes = [u8; 48];
fn from_uniform_bytes(bytes: Self::Bytes) -> Self {
let mut bytes_be = [0u8; 64];
Expand All @@ -94,7 +94,7 @@ impl Samplable for RustCryptoScalar<k256::Secp256k1> {
}
}
#[cfg(feature = "secp256r1")]
impl Samplable for RustCryptoScalar<p256::NistP256> {
impl FromUniformBytes for RustCryptoScalar<p256::NistP256> {
type Bytes = [u8; 48];
fn from_uniform_bytes(bytes: Self::Bytes) -> Self {
let mut bytes_be = [0u8; 64];
Expand All @@ -103,7 +103,7 @@ impl Samplable for RustCryptoScalar<p256::NistP256> {
}
}
#[cfg(feature = "stark")]
impl Samplable for RustCryptoScalar<stark_curve::StarkCurve> {
impl FromUniformBytes for RustCryptoScalar<stark_curve::StarkCurve> {
type Bytes = [u8; 48];
fn from_uniform_bytes(bytes: Self::Bytes) -> Self {
let mut bytes_be = [0u8; 64];
Expand Down
4 changes: 2 additions & 2 deletions generic-ec-zkp/src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod requires_alloc {
use alloc::{vec, vec::Vec};
use core::{iter, ops};

use generic_ec::traits::{IsZero, Samplable, Zero};
use generic_ec::traits::{IsZero, Random, Zero};
use rand_core::RngCore;

/// Polynomial $f(x) = \sum_i a_i x^i$ defined as a list of coefficients $[a_0, \dots, a_{\text{degree}}]$
Expand Down Expand Up @@ -96,7 +96,7 @@ mod requires_alloc {
}
}

impl<C: Samplable> Polynomial<C> {
impl<C: Random> Polynomial<C> {
/// Samples a random polynomial with specified degree
pub fn sample(rng: &mut impl RngCore, degree: usize) -> Self {
Self {
Expand Down
4 changes: 2 additions & 2 deletions generic-ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! ## Exposed API
//!
//! Limited API is exposed: elliptic point arithmetic (points addition, negation, multiplying at scalar), scalar
//! arithmetic (addition, multiplication, inverse modulo prime group order), and encode/decode to bytes represenstation.
//! arithmetic (addition, multiplication, inverse modulo prime group order), and encode/decode to bytes representation.
//!
//! Hash to curve, hash to scalar primitives, accessing affine coordinates of points are available for some curves through
//! `FromHash` and other traits.
Expand Down Expand Up @@ -227,7 +227,7 @@ pub mod traits {
}

/// Uniformly samples an instance of `Self` from source of randomness
pub trait Samplable {
pub trait Random {
/// Uniformly samples an instance of `Self` from source of randomness
fn random<R: rand_core::RngCore>(rng: &mut R) -> Self;
}
Expand Down
12 changes: 6 additions & 6 deletions generic-ec/src/non_zero/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use subtle::{ConstantTimeEq, CtOption};

use crate::{
as_raw::FromRaw,
core::{ByteArray, Samplable},
core::{ByteArray, FromUniformBytes},
errors::{ZeroPoint, ZeroScalar},
Curve, Point, Scalar, SecretScalar,
};
Expand Down Expand Up @@ -52,9 +52,9 @@ impl<E: Curve> NonZero<Scalar<E>> {
/// $2^{-25600}$ probability, which practically means that randomness source is broken.
pub fn random<R: RngCore>(rng: &mut R) -> Self {
match iter::repeat_with(|| {
let mut bytes = <<E::Scalar as Samplable>::Bytes as ByteArray>::zeroes();
let mut bytes = <<E::Scalar as FromUniformBytes>::Bytes as ByteArray>::zeroes();
rng.fill_bytes(bytes.as_mut());
<E::Scalar as Samplable>::from_uniform_bytes(bytes)
<E::Scalar as FromUniformBytes>::from_uniform_bytes(bytes)
})
.take(100)
.flat_map(|s| NonZero::from_scalar(Scalar::from_raw(s)))
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<E: Curve> NonZero<SecretScalar<E>> {
/// Panics if randomness source returned 100 zero scalars in a row. It happens with
/// $2^{-25600}$ probability, which practically means that randomness source is broken.
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
<Self as crate::traits::Samplable>::random(rng)
<Self as crate::traits::Random>::random(rng)
}

/// Constructs $S = 1$
Expand Down Expand Up @@ -304,13 +304,13 @@ impl<'s, E: Curve> Sum<&'s NonZero<Point<E>>> for Point<E> {
}
}

impl<E: Curve> crate::traits::Samplable for NonZero<Scalar<E>> {
impl<E: Curve> crate::traits::Random for NonZero<Scalar<E>> {
fn random<R: RngCore>(rng: &mut R) -> Self {
Self::random(rng)
}
}

impl<E: Curve> crate::traits::Samplable for NonZero<SecretScalar<E>> {
impl<E: Curve> crate::traits::Random for NonZero<SecretScalar<E>> {
fn random<R: RngCore>(rng: &mut R) -> Self {
NonZero::<Scalar<E>>::random(rng).into_secret()
}
Expand Down
2 changes: 1 addition & 1 deletion generic-ec/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<E: Curve> crate::traits::One for Scalar<E> {
}
}

impl<E: Curve> crate::traits::Samplable for Scalar<E> {
impl<E: Curve> crate::traits::Random for Scalar<E> {
fn random<R: RngCore>(rng: &mut R) -> Self {
Self::random(rng)
}
Expand Down
2 changes: 1 addition & 1 deletion generic-ec/src/secret_scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<E: Curve> fmt::Debug for SecretScalar<E> {
}
}

impl<E: Curve> crate::traits::Samplable for SecretScalar<E> {
impl<E: Curve> crate::traits::Random for SecretScalar<E> {
fn random<R: RngCore>(rng: &mut R) -> Self {
let mut scalar = Scalar::random(rng);
Self::new(&mut scalar)
Expand Down

0 comments on commit db67ab6

Please sign in to comment.