From 66ac8a38c0d9a5d538e21dee9b570ea61cb9d52b Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Sun, 9 Mar 2025 21:48:15 -0700 Subject: [PATCH 1/2] use `Group::try_from_rng` --- Cargo.lock | 6 +++--- Cargo.toml | 6 +++++- k256/src/arithmetic/projective.rs | 6 +++--- primeorder/src/projective.rs | 6 +++--- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 367dd339c..985046895 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -383,7 +383,7 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "elliptic-curve" version = "0.14.0-rc.1" -source = "git+https://github.com/RustCrypto/traits.git#204a4e030fa98863429ccd3797e12f9e7c45dc33" +source = "git+https://github.com/RustCrypto/traits.git#3620aba4f1e81e506b46a5f88c47f7ee3a7b87e0" dependencies = [ "base16ct", "base64ct", @@ -484,7 +484,7 @@ dependencies = [ [[package]] name = "group" version = "0.13.0" -source = "git+https://github.com/pinkforest/group.git?branch=bump-rand-0.9#06ac6fb11ced26fbf980ee65e74fced4da66ec3e" +source = "git+https://github.com/baloo/group.git?branch=baloo%2Ftry_from_rng#b0d6ea48fe55327b11ea03f9a965d9e16bb83adc" dependencies = [ "ff", "rand_core 0.9.3", @@ -1132,7 +1132,7 @@ dependencies = [ [[package]] name = "signature" version = "2.3.0-pre.6" -source = "git+https://github.com/RustCrypto/traits.git#204a4e030fa98863429ccd3797e12f9e7c45dc33" +source = "git+https://github.com/RustCrypto/traits.git#3620aba4f1e81e506b46a5f88c47f7ee3a7b87e0" dependencies = [ "digest", "rand_core 0.9.3", diff --git a/Cargo.toml b/Cargo.toml index 19f942acc..11941b786 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,12 +23,16 @@ opt-level = 2 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/zkcrypto/group/pull/57 +# https://github.com/zkcrypto/group/pull/58 +# https://github.com/zkcrypto/group/pull/59 +group = { git = "https://github.com/baloo/group.git", branch = "baloo/try_from_rng" } # https://github.com/RustCrypto/signatures/pull/913 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" } diff --git a/k256/src/arithmetic/projective.rs b/k256/src/arithmetic/projective.rs index 84eae3626..1a2b0a981 100644 --- a/k256/src/arithmetic/projective.rs +++ b/k256/src/arithmetic/projective.rs @@ -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, @@ -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(rng: &mut R) -> core::result::Result { + Ok(Self::GENERATOR * Scalar::try_from_rng(rng)?) } fn identity() -> Self { diff --git a/primeorder/src/projective.rs b/primeorder/src/projective.rs index 8699ffa62..174d9dc2d 100644 --- a/primeorder/src/projective.rs +++ b/primeorder/src/projective.rs @@ -19,7 +19,7 @@ use elliptic_curve::{ }, ops::{BatchInvert, LinearCombination, MulByGenerator}, point::Double, - rand_core::RngCore, + rand_core::TryRngCore, sec1::{ CompressedPoint, EncodedPoint, FromEncodedPoint, ModulusSize, ToEncodedPoint, UncompressedPointSize, @@ -275,8 +275,8 @@ where { type Scalar = Scalar; - fn random(mut rng: impl RngCore) -> Self { - Self::GENERATOR * as Field>::random(&mut rng) + fn try_from_rng(rng: &mut R) -> core::result::Result { + Ok(Self::GENERATOR * as Field>::try_from_rng(rng)?) } fn identity() -> Self { From fa8b3bb90cbb65e43af513b3c83b57d7c8b8ee1b Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Sun, 9 Mar 2025 21:48:15 -0700 Subject: [PATCH 2/2] MulByGenerator is now in `group::Group` --- Cargo.lock | 23 +++++++++++++---------- Cargo.toml | 10 +--------- bign256/src/ecdsa/signing.rs | 4 ++-- k256/benches/scalar.rs | 2 +- k256/src/arithmetic/mul.rs | 9 ++++----- k256/src/arithmetic/projective.rs | 5 ++++- primeorder/src/projective.rs | 13 +------------ sm2/src/dsa/signing.rs | 4 ++-- sm2/src/pke/encrypting.rs | 2 +- 9 files changed, 29 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 985046895..fdf01717b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,7 +363,7 @@ dependencies = [ [[package]] name = "ecdsa" version = "0.17.0-pre.9" -source = "git+https://github.com/RustCrypto/signatures.git#8324be36081a8fdf85d3b50bd769ab59f71b0289" +source = "git+https://github.com/RustCrypto/signatures.git#34cd7792c248e36693d617968d38c14d4ab4618a" dependencies = [ "der", "digest", @@ -383,7 +383,7 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "elliptic-curve" version = "0.14.0-rc.1" -source = "git+https://github.com/RustCrypto/traits.git#3620aba4f1e81e506b46a5f88c47f7ee3a7b87e0" +source = "git+https://github.com/RustCrypto/traits.git#829328e9405143cc86b81c297b39f1a6161700c8" dependencies = [ "base16ct", "base64ct", @@ -423,8 +423,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "ff" -version = "0.13.1" -source = "git+https://github.com/zkcrypto/ff.git?branch=release-0.14.0#241caff9bcedafbe279b5a4d875461f66b3f9701" +version = "0.14.0-pre.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d42dd26f5790eda47c1a2158ea4120e32c35ddc9a7743c98a292accc01b54ef3" dependencies = [ "bitvec", "ff_derive", @@ -434,8 +435,9 @@ dependencies = [ [[package]] name = "ff_derive" -version = "0.13.1" -source = "git+https://github.com/zkcrypto/ff.git?branch=release-0.14.0#241caff9bcedafbe279b5a4d875461f66b3f9701" +version = "0.14.0-pre.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9266df7c7c72e5a865a447aca13bf480d7310eaa4f84de117c33e361d4da8888" dependencies = [ "addchain", "num-bigint 0.3.3", @@ -483,8 +485,9 @@ dependencies = [ [[package]] name = "group" -version = "0.13.0" -source = "git+https://github.com/baloo/group.git?branch=baloo%2Ftry_from_rng#b0d6ea48fe55327b11ea03f9a965d9e16bb83adc" +version = "0.14.0-pre.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ff6a0b2dd4b981b1ae9e3e6830ab146771f3660d31d57bafd9018805a91b0f1" dependencies = [ "ff", "rand_core 0.9.3", @@ -999,7 +1002,7 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rfc6979" version = "0.5.0-pre.4" -source = "git+https://github.com/RustCrypto/signatures.git#8324be36081a8fdf85d3b50bd769ab59f71b0289" +source = "git+https://github.com/RustCrypto/signatures.git#34cd7792c248e36693d617968d38c14d4ab4618a" dependencies = [ "hmac", "subtle", @@ -1132,7 +1135,7 @@ dependencies = [ [[package]] name = "signature" version = "2.3.0-pre.6" -source = "git+https://github.com/RustCrypto/traits.git#3620aba4f1e81e506b46a5f88c47f7ee3a7b87e0" +source = "git+https://github.com/RustCrypto/traits.git#829328e9405143cc86b81c297b39f1a6161700c8" dependencies = [ "digest", "rand_core 0.9.3", diff --git a/Cargo.toml b/Cargo.toml index 11941b786..0c6618635 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,16 +19,8 @@ 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 -# https://github.com/zkcrypto/group/pull/57 -# https://github.com/zkcrypto/group/pull/58 -# https://github.com/zkcrypto/group/pull/59 -group = { git = "https://github.com/baloo/group.git", branch = "baloo/try_from_rng" } - # 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" } diff --git a/bign256/src/ecdsa/signing.rs b/bign256/src/ecdsa/signing.rs index b055f8ab7..396a662a7 100644 --- a/bign256/src/ecdsa/signing.rs +++ b/bign256/src/ecdsa/signing.rs @@ -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}, }; diff --git a/k256/benches/scalar.rs b/k256/benches/scalar.rs index 2a15f5b6b..a9c3385ff 100644 --- a/k256/benches/scalar.rs +++ b/k256/benches/scalar.rs @@ -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 { diff --git a/k256/src/arithmetic/mul.rs b/k256/src/arithmetic/mul.rs index 552318f12..cc7e81fa7 100644 --- a/k256/src/arithmetic/mul.rs +++ b/k256/src/arithmetic/mul.rs @@ -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}, }; @@ -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]); @@ -460,7 +460,6 @@ mod tests { use crate::arithmetic::{ProjectivePoint, Scalar}; use elliptic_curve::{ Field, Group, - ops::MulByGenerator, rand_core::{OsRng, TryRngCore}, }; diff --git a/k256/src/arithmetic/projective.rs b/k256/src/arithmetic/projective.rs index 1a2b0a981..9feb5ae2f 100644 --- a/k256/src/arithmetic/projective.rs +++ b/k256/src/arithmetic/projective.rs @@ -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 { @@ -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}; diff --git a/primeorder/src/projective.rs b/primeorder/src/projective.rs index 174d9dc2d..e8b5e39cb 100644 --- a/primeorder/src/projective.rs +++ b/primeorder/src/projective.rs @@ -17,7 +17,7 @@ use elliptic_curve::{ cofactor::CofactorGroup, prime::{PrimeCurve, PrimeGroup}, }, - ops::{BatchInvert, LinearCombination, MulByGenerator}, + ops::{BatchInvert, LinearCombination}, point::Double, rand_core::TryRngCore, sec1::{ @@ -425,17 +425,6 @@ where // TODO(tarcieri): optimized implementation } -impl MulByGenerator for ProjectivePoint -where - Self: Double, - C: PrimeCurveParams, -{ - fn mul_by_generator(scalar: &Self::Scalar) -> Self { - // TODO(tarcieri): precomputed basepoint tables - Self::generator() * scalar - } -} - impl PrimeGroup for ProjectivePoint where Self: Double, diff --git a/sm2/src/dsa/signing.rs b/sm2/src/dsa/signing.rs index 64dc800a5..107e07454 100644 --- a/sm2/src/dsa/signing.rs +++ b/sm2/src/dsa/signing.rs @@ -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}, }; diff --git a/sm2/src/pke/encrypting.rs b/sm2/src/pke/encrypting.rs index 3924efa24..a0cd16cc0 100644 --- a/sm2/src/pke/encrypting.rs +++ b/sm2/src/pke/encrypting.rs @@ -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,