Skip to content

Remove MulByGenerator #1145

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

Merged
merged 2 commits into from
Apr 18, 2025
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
23 changes: 13 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ members = [
opt-level = 2

[patch.crates-io]
# https://github.com/zkcrypto/ff/pull/122
ff = { git = "https://github.com/zkcrypto/ff.git", branch = "release-0.14.0" }

# https://github.com/zkcrypto/group/pull/56
group = { git = "https://github.com/pinkforest/group.git", branch = "bump-rand-0.9" }

# https://github.com/RustCrypto/signatures/pull/913
# https://github.com/RustCrypto/signatures/pull/940
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
rfc6979 = { git = "https://github.com/RustCrypto/signatures.git" }

# https://github.com/RustCrypto/traits/pull/1777
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git" }
signature = { git = "https://github.com/RustCrypto/traits.git" }

Expand Down
4 changes: 2 additions & 2 deletions bign256/src/ecdsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use crate::{BignP256, FieldBytes, NonZeroScalar, ProjectivePoint, PublicKey, Sca
use belt_hash::{BeltHash, Digest};
use core::fmt::{self, Debug};
use elliptic_curve::{
Curve, Field, FieldBytesEncoding, PrimeField,
Curve, Field, FieldBytesEncoding, Group, PrimeField,
array::{Array, sizes::U32, typenum::Unsigned},
ops::{MulByGenerator, Reduce},
ops::Reduce,
point::AffineCoordinates,
subtle::{Choice, ConstantTimeEq},
};
Expand Down
2 changes: 1 addition & 1 deletion k256/benches/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use criterion::{
use hex_literal::hex;
use k256::{
ProjectivePoint, Scalar,
elliptic_curve::{group::ff::PrimeField, ops::LinearCombination, ops::MulByGenerator},
elliptic_curve::{Group, group::ff::PrimeField, ops::LinearCombination},
};

fn test_scalar_x() -> Scalar {
Expand Down
9 changes: 4 additions & 5 deletions k256/src/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::arithmetic::{

use core::ops::{Mul, MulAssign};
use elliptic_curve::{
ops::{LinearCombination, MulByGenerator},
ops::LinearCombination,
scalar::IsHigh,
subtle::{Choice, ConditionallySelectable, ConstantTimeEq},
};
Expand Down Expand Up @@ -386,16 +386,16 @@ fn precompute_gen_lookup_table() -> [LookupTable; 33] {
res
}

impl MulByGenerator for ProjectivePoint {
impl ProjectivePoint {
/// Calculates `k * G`, where `G` is the generator.
#[cfg(not(feature = "precomputed-tables"))]
fn mul_by_generator(k: &Scalar) -> ProjectivePoint {
pub(super) fn mul_by_generator(k: &Scalar) -> ProjectivePoint {
ProjectivePoint::GENERATOR * k
}

/// Calculates `k * G`, where `G` is the generator.
#[cfg(feature = "precomputed-tables")]
fn mul_by_generator(k: &Scalar) -> ProjectivePoint {
pub(super) fn mul_by_generator(k: &Scalar) -> ProjectivePoint {
let digits = Radix16Decomposition::<65>::new(k);
let table = *GEN_LOOKUP_TABLE;
let mut acc = table[32].select(digits.0[64]);
Expand Down Expand Up @@ -460,7 +460,6 @@ mod tests {
use crate::arithmetic::{ProjectivePoint, Scalar};
use elliptic_curve::{
Field, Group,
ops::MulByGenerator,
rand_core::{OsRng, TryRngCore},
};

Expand Down
11 changes: 7 additions & 4 deletions k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use elliptic_curve::{
ff::Field,
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
},
rand_core::RngCore,
rand_core::TryRngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
zeroize::DefaultIsZeroes,
Expand Down Expand Up @@ -402,8 +402,8 @@ impl Eq for ProjectivePoint {}
impl Group for ProjectivePoint {
type Scalar = Scalar;

fn random(mut rng: impl RngCore) -> Self {
Self::GENERATOR * Scalar::random(&mut rng)
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> core::result::Result<Self, R::Error> {
Ok(Self::GENERATOR * Scalar::try_from_rng(rng)?)
}

fn identity() -> Self {
Expand All @@ -422,6 +422,10 @@ impl Group for ProjectivePoint {
fn double(&self) -> Self {
Self::double(self)
}

fn mul_by_generator(k: &Scalar) -> Self {
Self::mul_by_generator(k)
}
}

impl GroupEncoding for ProjectivePoint {
Expand Down Expand Up @@ -680,7 +684,6 @@ mod tests {
};
use elliptic_curve::Field;
use elliptic_curve::group::{ff::PrimeField, prime::PrimeCurveAffine};
use elliptic_curve::ops::MulByGenerator;
use elliptic_curve::{BatchNormalize, group};
use rand_core::{OsRng, TryRngCore};

Expand Down
19 changes: 4 additions & 15 deletions primeorder/src/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use elliptic_curve::{
cofactor::CofactorGroup,
prime::{PrimeCurve, PrimeGroup},
},
ops::{BatchInvert, LinearCombination, MulByGenerator},
ops::{BatchInvert, LinearCombination},
point::Double,
rand_core::RngCore,
rand_core::TryRngCore,
sec1::{
CompressedPoint, EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint,
UncompressedPointSize,
Expand Down Expand Up @@ -275,8 +275,8 @@ where
{
type Scalar = Scalar<C>;

fn random(mut rng: impl RngCore) -> Self {
Self::GENERATOR * <Scalar<C> as Field>::random(&mut rng)
fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> core::result::Result<Self, R::Error> {
Ok(Self::GENERATOR * <Scalar<C> as Field>::try_from_rng(rng)?)
}

fn identity() -> Self {
Expand Down Expand Up @@ -425,17 +425,6 @@ where
// TODO(tarcieri): optimized implementation
}

impl<C> MulByGenerator for ProjectivePoint<C>
where
Self: Double,
C: PrimeCurveParams,
{
fn mul_by_generator(scalar: &Self::Scalar) -> Self {
// TODO(tarcieri): precomputed basepoint tables
Self::generator() * scalar
}
}

impl<C> PrimeGroup for ProjectivePoint<C>
where
Self: Double,
Expand Down
4 changes: 2 additions & 2 deletions sm2/src/dsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::{
};
use core::fmt::{self, Debug};
use elliptic_curve::{
Curve, FieldBytesEncoding, PrimeField,
Curve, FieldBytesEncoding, Group, PrimeField,
array::typenum::Unsigned,
ops::{MulByGenerator, Reduce},
ops::Reduce,
point::AffineCoordinates,
subtle::{Choice, ConstantTimeEq},
};
Expand Down
2 changes: 1 addition & 1 deletion sm2/src/pke/encrypting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::{borrow::ToOwned, boxed::Box, vec::Vec};
use elliptic_curve::{
Curve, Error, Group, Result,
bigint::{RandomBits, U256, Uint, Zero},
ops::{MulByGenerator, Reduce},
ops::Reduce,
pkcs8::der::Encode,
rand_core::TryCryptoRng,
sec1::ToEncodedPoint,
Expand Down