diff --git a/Cargo.lock b/Cargo.lock index dd51cc370..f313544aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -373,8 +373,7 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "crypto-bigint" version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97bb4a855e3b10f84c4e7e895a7de01db7f9a7b7eb7f73ed9773fd52ac686451" +source = "git+https://github.com/RustCrypto/crypto-bigint#ec6615fad09d26f8518378f1fdfe96a635ddab2d" dependencies = [ "cpubits", "ctutils", @@ -434,8 +433,7 @@ dependencies = [ [[package]] name = "ecdsa" version = "0.17.0-rc.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f7195c1205cec99025b3004e8034e1ddc12746333aa0a3945df7f9b80882ca2" +source = "git+https://github.com/RustCrypto/signatures#9556832958dbdeb804f0291a4e81ee1ec2ecf677" dependencies = [ "der", "digest", @@ -490,7 +488,7 @@ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "elliptic-curve" version = "0.14.0-rc.35" -source = "git+https://github.com/RustCrypto/traits#751f0c10a39336a12093cacf381a6a5d58595151" +source = "git+https://github.com/RustCrypto/traits#d1ca96d9a456337c74e1aa8e1582a7e26447dbef" dependencies = [ "base16ct", "crypto-bigint", diff --git a/Cargo.toml b/Cargo.toml index 4a684072e..c135b158c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,6 @@ primefield = { path = "primefield" } primeorder = { path = "primeorder" } wnaf = { path = "wnaf" } +crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint" } +ecdsa = { git = "https://github.com/RustCrypto/signatures" } elliptic-curve = { git = "https://github.com/RustCrypto/traits" } diff --git a/bignp256/src/ecdsa/signing.rs b/bignp256/src/ecdsa/signing.rs index ae3ad86ca..26f2636c2 100644 --- a/bignp256/src/ecdsa/signing.rs +++ b/bignp256/src/ecdsa/signing.rs @@ -19,8 +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, Generate, Group, PrimeField, + Curve, Field, Generate, Group, PrimeField, array::{Array, sizes::U32, typenum::Unsigned}, + field, ops::Reduce, point::AffineCoordinates, subtle::{Choice, ConstantTimeEq}, @@ -116,7 +117,7 @@ impl PrehashSigner for SigningKey { // 2. Generate 𝑘 ← rand(1,..,𝑞-1) let k = Scalar::from_repr(bign_genk::generate_k::( &self.secret_scalar.to_bytes(), - &FieldBytesEncoding::::encode_field_bytes(BignP256::ORDER.as_ref()), + &field::uint_to_bytes::(&BignP256::ORDER), &h.to_bytes(), &[], )) diff --git a/bignp256/src/lib.rs b/bignp256/src/lib.rs index 241123145..fcca2d2ec 100644 --- a/bignp256/src/lib.rs +++ b/bignp256/src/lib.rs @@ -30,11 +30,7 @@ extern crate alloc; pub use elliptic_curve::{self, bigint::U256}; -use elliptic_curve::{ - Error, FieldBytesEncoding, - bigint::{ArrayEncoding, Odd}, - consts::U32, -}; +use elliptic_curve::{ByteOrder, Error, bigint::Odd, consts::U32}; #[cfg(feature = "arithmetic")] pub use { @@ -100,6 +96,7 @@ impl elliptic_curve::Curve for BignP256 { /// Order of BIGN P-256's elliptic curve group (i.e. scalar modulus). const ORDER: Odd = Odd::::from_be_hex(ORDER_HEX); + const FIELD_ENDIANNESS: ByteOrder = ByteOrder::LittleEndian; } impl elliptic_curve::PrimeCurve for BignP256 {} @@ -128,16 +125,6 @@ pub type FieldBytes = elliptic_curve::FieldBytes; /// SEC1 encoded point. pub type Sec1Point = elliptic_curve::sec1::Sec1Point; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U256::from_le_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_le_byte_array() - } -} - /// Non-zero scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/bp256/src/lib.rs b/bp256/src/lib.rs index ab38e30f6..32e3b92e6 100644 --- a/bp256/src/lib.rs +++ b/bp256/src/lib.rs @@ -61,7 +61,7 @@ pub(crate) use crate::arithmetic::field::FieldElement; use elliptic_curve::{ array::{Array, typenum::U32}, - bigint::{ArrayEncoding, Odd}, + bigint::Odd, }; /// Byte representation of a base/scalar field element of a given curve. @@ -69,11 +69,3 @@ pub type FieldBytes = Array; const ORDER_HEX: &str = "a9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7"; const ORDER: Odd = Odd::::from_be_hex(ORDER_HEX); - -fn decode_field_bytes(field_bytes: &FieldBytes) -> U256 { - U256::from_be_byte_array(*field_bytes) -} - -fn encode_field_bytes(uint: &U256) -> FieldBytes { - uint.to_be_byte_array() -} diff --git a/bp256/src/r1.rs b/bp256/src/r1.rs index cb393b282..80ee94ba3 100644 --- a/bp256/src/r1.rs +++ b/bp256/src/r1.rs @@ -14,7 +14,6 @@ pub use { use crate::ORDER; use elliptic_curve::{ - FieldBytesEncoding, bigint::{Odd, U256}, consts::U32, }; @@ -57,16 +56,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - crate::decode_field_bytes(field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - crate::encode_field_bytes(self) - } -} - /// brainpoolP256r1 secret key. pub type SecretKey = elliptic_curve::SecretKey; diff --git a/bp256/src/t1.rs b/bp256/src/t1.rs index e91cc519c..c1c66ea6f 100644 --- a/bp256/src/t1.rs +++ b/bp256/src/t1.rs @@ -14,7 +14,6 @@ pub use { use crate::ORDER; use elliptic_curve::{ - FieldBytesEncoding, bigint::{Odd, U256}, consts::U32, }; @@ -57,16 +56,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &crate::r1::FieldBytes) -> Self { - crate::decode_field_bytes(field_bytes) - } - - fn encode_field_bytes(&self) -> crate::r1::FieldBytes { - crate::encode_field_bytes(self) - } -} - /// brainpoolP256t1 secret key. pub type SecretKey = elliptic_curve::SecretKey; diff --git a/bp384/src/lib.rs b/bp384/src/lib.rs index b4405202b..2c4e79e4d 100644 --- a/bp384/src/lib.rs +++ b/bp384/src/lib.rs @@ -72,11 +72,3 @@ pub type FieldBytes = Array; const ORDER_HEX: &str = "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565"; const ORDER: Odd = Odd::::from_be_hex(ORDER_HEX); - -fn decode_field_bytes(field_bytes: &FieldBytes) -> U384 { - U384::from_be_byte_array(*field_bytes) -} - -fn encode_field_bytes(uint: &U384) -> FieldBytes { - uint.to_be_byte_array() -} diff --git a/bp384/src/r1.rs b/bp384/src/r1.rs index d1778d116..f8bda3938 100644 --- a/bp384/src/r1.rs +++ b/bp384/src/r1.rs @@ -14,7 +14,6 @@ pub use { use crate::ORDER; use elliptic_curve::{ - FieldBytesEncoding, bigint::{Odd, U384}, consts::U48, }; @@ -57,16 +56,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U384 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - crate::decode_field_bytes(field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - crate::encode_field_bytes(self) - } -} - /// brainpoolP384r1 secret key. pub type SecretKey = elliptic_curve::SecretKey; diff --git a/bp384/src/t1.rs b/bp384/src/t1.rs index 16ec7d2bf..15237ad0a 100644 --- a/bp384/src/t1.rs +++ b/bp384/src/t1.rs @@ -14,7 +14,6 @@ pub use { use crate::ORDER; use elliptic_curve::{ - FieldBytesEncoding, bigint::{Odd, U384}, consts::U48, }; @@ -57,16 +56,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U384 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - crate::decode_field_bytes(field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - crate::encode_field_bytes(self) - } -} - /// brainpoolP384t1 secret key. pub type SecretKey = elliptic_curve::SecretKey; diff --git a/ed448-goldilocks/src/lib.rs b/ed448-goldilocks/src/lib.rs index 2aa6e3f52..f355da954 100644 --- a/ed448-goldilocks/src/lib.rs +++ b/ed448-goldilocks/src/lib.rs @@ -82,7 +82,7 @@ pub use montgomery::{MontgomeryPoint, ProjectiveMontgomeryPoint}; pub use sign::*; use elliptic_curve::{ - Curve, FieldBytesEncoding, PrimeCurve, + ByteOrder, Curve, PrimeCurve, array::typenum::{U56, U57}, bigint::{ArrayEncoding, Odd, U448}, point::PointCompression, @@ -105,6 +105,7 @@ impl Curve for Ed448 { type Uint = U448; const ORDER: Odd = ORDER; + const FIELD_ENDIANNESS: ByteOrder = ByteOrder::LittleEndian; } impl PrimeCurve for Ed448 {} @@ -113,18 +114,6 @@ impl PointCompression for Ed448 { const COMPRESS_POINTS: bool = true; } -impl FieldBytesEncoding for U448 { - fn decode_field_bytes(field_bytes: &Ed448FieldBytes) -> Self { - U448::from_le_slice(field_bytes) - } - - fn encode_field_bytes(&self) -> Ed448FieldBytes { - let mut data = Ed448FieldBytes::default(); - data.copy_from_slice(&self.to_le_byte_array()[..]); - data - } -} - impl elliptic_curve::CurveArithmetic for Ed448 { type AffinePoint = AffinePoint; type ProjectivePoint = EdwardsPoint; @@ -161,18 +150,6 @@ impl PointCompression for Decaf448 { const COMPRESS_POINTS: bool = true; } -impl FieldBytesEncoding for U448 { - fn decode_field_bytes(field_bytes: &Decaf448FieldBytes) -> Self { - U448::from_le_slice(field_bytes) - } - - fn encode_field_bytes(&self) -> Decaf448FieldBytes { - let mut data = Decaf448FieldBytes::default(); - data.copy_from_slice(&self.to_le_byte_array()[..]); - data - } -} - impl elliptic_curve::CurveArithmetic for Decaf448 { type AffinePoint = DecafAffinePoint; type ProjectivePoint = DecafPoint; diff --git a/k256/src/lib.rs b/k256/src/lib.rs index 746a625c6..19d46d310 100644 --- a/k256/src/lib.rs +++ b/k256/src/lib.rs @@ -67,9 +67,8 @@ pub use elliptic_curve::pkcs8; pub use sha2; use elliptic_curve::{ - FieldBytesEncoding, array::Array, - bigint::{ArrayEncoding, Odd}, + bigint::Odd, consts::{U32, U33, U64}, }; @@ -127,16 +126,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U256::from_be_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_be_byte_array() - } -} - /// Bytes used by a wide reduction: twice the width of [`FieldBytes`]. pub type WideBytes = Array; diff --git a/p192/src/lib.rs b/p192/src/lib.rs index 785779f00..016e8f04c 100644 --- a/p192/src/lib.rs +++ b/p192/src/lib.rs @@ -33,9 +33,8 @@ pub use arithmetic::{AffinePoint, ProjectivePoint, scalar::Scalar}; pub use elliptic_curve::pkcs8; use elliptic_curve::{ - FieldBytesEncoding, array::Array, - bigint::{ArrayEncoding, Odd, U192}, + bigint::{Odd, U192}, consts::{U24, U25}, }; @@ -80,16 +79,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U192 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U192::from_be_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_be_byte_array() - } -} - /// Non-zero NIST P-192 scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/p224/src/arithmetic/scalar.rs b/p224/src/arithmetic/scalar.rs index fbdab8436..aabe595c4 100644 --- a/p224/src/arithmetic/scalar.rs +++ b/p224/src/arithmetic/scalar.rs @@ -10,11 +10,12 @@ //! Apache License (Version 2.0), and the BSD 1-Clause License; //! users may pick which license to apply. -use crate::{FieldBytes, FieldBytesEncoding, NistP224, ORDER_HEX, Uint}; +use crate::{FieldBytes, NistP224, ORDER_HEX, Uint}; use elliptic_curve::{ Curve as _, bigint::{Limb, cpubits}, ff::PrimeField, + field, ops::Reduce, scalar::{FromUintUnchecked, IsHigh}, subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, CtOption}, @@ -137,8 +138,7 @@ impl Reduce for Scalar { impl Reduce for Scalar { #[inline] fn reduce(bytes: &FieldBytes) -> Self { - let w = >::decode_field_bytes(bytes); - Self::reduce(&w) + Self::reduce(&field::bytes_to_uint::(bytes)) } } diff --git a/p224/src/lib.rs b/p224/src/lib.rs index d379cc312..9fd6f28c4 100644 --- a/p224/src/lib.rs +++ b/p224/src/lib.rs @@ -36,7 +36,6 @@ pub use arithmetic::{AffinePoint, ProjectivePoint, scalar::Scalar}; pub use elliptic_curve::pkcs8; use elliptic_curve::{ - FieldBytesEncoding, array::Array, bigint::{Odd, cpubits}, consts::{U28, U29}, @@ -98,8 +97,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for Uint {} - /// Non-zero NIST P-256 scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/p256/src/lib.rs b/p256/src/lib.rs index ca4ff1dbe..bffc34c68 100644 --- a/p256/src/lib.rs +++ b/p256/src/lib.rs @@ -53,12 +53,7 @@ pub use arithmetic::field::FieldElement; #[cfg(feature = "pkcs8")] pub use elliptic_curve::pkcs8; -use elliptic_curve::{ - FieldBytesEncoding, - array::Array, - bigint::{ArrayEncoding, Odd}, - consts::U33, -}; +use elliptic_curve::{array::Array, bigint::Odd, consts::U33}; /// Order of NIST P-256's elliptic curve group (i.e. scalar modulus) serialized /// as hexadecimal. @@ -147,16 +142,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// Byte array containing a serialized field element value (base field or scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U256::from_be_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_be_byte_array() - } -} - /// Non-zero NIST P-256 scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/p384/src/lib.rs b/p384/src/lib.rs index 5c638c6f6..13276ffe3 100644 --- a/p384/src/lib.rs +++ b/p384/src/lib.rs @@ -77,7 +77,7 @@ pub use hash2curve; #[cfg(feature = "pkcs8")] pub use elliptic_curve::pkcs8; -use elliptic_curve::{FieldBytesEncoding, array::Array, bigint::ArrayEncoding, consts::U49}; +use elliptic_curve::{array::Array, consts::U49}; /// Order of NIST P-384's elliptic curve group (i.e. scalar modulus) in hexadecimal. const ORDER_HEX: &str = "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"; @@ -126,16 +126,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for U384 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U384::from_be_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_be_byte_array() - } -} - /// Non-zero NIST P-384 scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/p521/src/arithmetic/field.rs b/p521/src/arithmetic/field.rs index 7dfb79157..ad46abc58 100644 --- a/p521/src/arithmetic/field.rs +++ b/p521/src/arithmetic/field.rs @@ -18,10 +18,11 @@ use core::{ ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}, }; use elliptic_curve::{ - Error, FieldBytesEncoding, Generate, + Error, Generate, array::Array, bigint::{Word, cpubits, modular::Retrieve}, ff::{self, Field, PrimeField}, + field::bytes_to_uint, ops::{BatchInvert, Invert}, rand_core::TryRng, subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeLess, CtOption}, @@ -84,8 +85,7 @@ impl FieldElement { /// Create a [`FieldElement`] from a canonical big-endian representation. pub fn from_bytes(repr: &FieldBytes) -> CtOption { - let uint = >::decode_field_bytes(repr); - Self::from_uint(uint) + Self::from_uint(bytes_to_uint::(repr)) } /// Decode [`FieldElement`] from a big endian byte slice. diff --git a/p521/src/arithmetic/hash2curve.rs b/p521/src/arithmetic/hash2curve.rs index 7ad20c206..e792b8029 100644 --- a/p521/src/arithmetic/hash2curve.rs +++ b/p521/src/arithmetic/hash2curve.rs @@ -1,9 +1,9 @@ use super::FieldElement; -use crate::{AffinePoint, NistP521, ProjectivePoint, Scalar, Uint}; +use crate::{AffinePoint, NistP521, ProjectivePoint, Scalar}; use elliptic_curve::{ - FieldBytesEncoding, array::Array, consts::{U32, U98}, + field, ops::Reduce, subtle::Choice, }; @@ -26,12 +26,12 @@ impl Reduce> for FieldElement { let mut d0 = Array::default(); d0[17..].copy_from_slice(&value[0..49]); - let u0 = >::decode_field_bytes(&d0); + let u0 = field::bytes_to_uint::(&d0); let d0 = FieldElement::from_uint_unchecked(u0); let mut d1 = Array::default(); d1[17..].copy_from_slice(&value[49..]); - let u1 = >::decode_field_bytes(&d1); + let u1 = field::bytes_to_uint::(&d1); let d1 = FieldElement::from_uint_unchecked(u1); d0 * F_2_392 + d1 @@ -84,12 +84,12 @@ impl Reduce> for Scalar { let mut d0 = Array::default(); d0[17..].copy_from_slice(&value[0..49]); - let u0 = >::decode_field_bytes(&d0); + let u0 = field::bytes_to_uint::(&d0); let d0 = Scalar::reduce(&u0); let mut d1 = Array::default(); d1[17..].copy_from_slice(&value[49..]); - let u1 = >::decode_field_bytes(&d1); + let u1 = field::bytes_to_uint::(&d1); let d1 = Scalar::reduce(&u1); d0 * F_2_392 + d1 diff --git a/p521/src/arithmetic/scalar.rs b/p521/src/arithmetic/scalar.rs index 742abbc1c..040f89566 100644 --- a/p521/src/arithmetic/scalar.rs +++ b/p521/src/arithmetic/scalar.rs @@ -2,11 +2,12 @@ //! //! Arithmetic implementations are provided by `primefield` and `crypto-bigint`. -use crate::{FieldBytes, FieldBytesEncoding, NistP521, ORDER_HEX, Uint}; +use crate::{FieldBytes, NistP521, ORDER_HEX, Uint}; use elliptic_curve::{ Curve as _, bigint::Limb, ff::PrimeField, + field, ops::Reduce, scalar::{FromUintUnchecked, IsHigh}, subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, CtOption}, @@ -77,7 +78,7 @@ impl Reduce for Scalar { impl Reduce for Scalar { #[inline] fn reduce(bytes: &FieldBytes) -> Self { - >::reduce(&FieldBytesEncoding::::decode_field_bytes(bytes)) + >::reduce(&field::bytes_to_uint::(bytes)) } } diff --git a/p521/src/lib.rs b/p521/src/lib.rs index 2b98f041c..bc1a8ea76 100644 --- a/p521/src/lib.rs +++ b/p521/src/lib.rs @@ -53,7 +53,6 @@ pub use elliptic_curve; pub use elliptic_curve::pkcs8; use elliptic_curve::{ - FieldBytesEncoding, array::Array, bigint::{Odd, cpubits}, consts::{U66, U67}, @@ -120,8 +119,6 @@ pub type Sec1Point = elliptic_curve::sec1::Sec1Point; /// scalar). pub type FieldBytes = elliptic_curve::FieldBytes; -impl FieldBytesEncoding for Uint {} - /// Non-zero NIST P-521 scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar; diff --git a/primeorder/src/affine.rs b/primeorder/src/affine.rs index 5843e6c7a..0d141fbcf 100644 --- a/primeorder/src/affine.rs +++ b/primeorder/src/affine.rs @@ -5,7 +5,8 @@ use crate::{PrimeCurveParams, ProjectivePoint}; use core::borrow::Borrow; use elliptic_curve::{ - Error, FieldBytes, FieldBytesEncoding, Generate, PublicKey, Result, Scalar, + Error, FieldBytes, Generate, PublicKey, Result, Scalar, + bigint::modular::Retrieve, ctutils::{self, CtGt as _, CtSelect as _}, ff::{Field, PrimeField}, group::{CurveAffine, GroupEncoding}, @@ -62,12 +63,11 @@ where /// Conditionally negate [`AffinePoint`] for use with point compaction. fn to_compact(self) -> Self { let neg_self = -self; - let choice = C::Uint::decode_field_bytes(&self.y.to_repr()) - .ct_gt(&C::Uint::decode_field_bytes(&neg_self.y.to_repr())); - + let gt = self.y.retrieve().ct_gt(&neg_self.y.retrieve()); + let y = C::FieldElement::conditional_select(&self.y, &neg_self.y, gt.into()); Self { x: self.x, - y: C::FieldElement::conditional_select(&self.y, &neg_self.y, choice.into()), + y, infinity: self.infinity, } } diff --git a/sm2/src/dsa/signing.rs b/sm2/src/dsa/signing.rs index db0ce0a2b..a4a04dadc 100644 --- a/sm2/src/dsa/signing.rs +++ b/sm2/src/dsa/signing.rs @@ -20,8 +20,9 @@ use crate::{ }; use core::fmt::{self, Debug}; use elliptic_curve::{ - Curve, FieldBytesEncoding, Group, PrimeField, + Curve, Group, PrimeField, array::typenum::Unsigned, + field, ops::Reduce, point::AffineCoordinates, subtle::{Choice, ConstantTimeEq}, @@ -222,7 +223,7 @@ fn sign_prehash_rfc6979(secret_scalar: &Scalar, prehash: &[u8], data: &[u8]) -> // A3: pick a random number k in [1, n-1] via a random number generator let k = Scalar::from_repr(rfc6979::generate_k::( &secret_scalar.to_repr(), - &FieldBytesEncoding::::encode_field_bytes(Sm2::ORDER.as_ref()), + &field::uint_to_bytes::(&Sm2::ORDER), &e.to_bytes(), data, )) diff --git a/sm2/src/lib.rs b/sm2/src/lib.rs index e367287c3..182d1b49c 100644 --- a/sm2/src/lib.rs +++ b/sm2/src/lib.rs @@ -74,9 +74,8 @@ pub use arithmetic::{AffinePoint, ProjectivePoint, scalar::Scalar}; pub use elliptic_curve::pkcs8; use elliptic_curve::{ - FieldBytesEncoding, array::Array, - bigint::{ArrayEncoding, Odd}, + bigint::Odd, consts::{U32, U33, U65}, }; @@ -136,16 +135,6 @@ pub type FieldBytes = elliptic_curve::FieldBytes; /// Size of a SM2 field element serialized as bytes. pub type FieldBytesSize = elliptic_curve::FieldBytesSize; -impl FieldBytesEncoding for U256 { - fn decode_field_bytes(field_bytes: &FieldBytes) -> Self { - U256::from_be_byte_array(*field_bytes) - } - - fn encode_field_bytes(&self) -> FieldBytes { - self.to_be_byte_array() - } -} - /// Non-zero scalar field element. #[cfg(feature = "arithmetic")] pub type NonZeroScalar = elliptic_curve::NonZeroScalar;