diff --git a/bign256/src/arithmetic/scalar.rs b/bign256/src/arithmetic/scalar.rs index 38a09277..5f689a01 100644 --- a/bign256/src/arithmetic/scalar.rs +++ b/bign256/src/arithmetic/scalar.rs @@ -223,6 +223,15 @@ impl From<&SecretKey> for Scalar { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error; diff --git a/bp256/src/r1/arithmetic.rs b/bp256/src/r1/arithmetic.rs index 1bf7a3fb..0e31ab28 100644 --- a/bp256/src/r1/arithmetic.rs +++ b/bp256/src/r1/arithmetic.rs @@ -2,7 +2,7 @@ use super::BrainpoolP256r1; use crate::{FieldElement, Scalar}; -use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic}; +use elliptic_curve::{CurveArithmetic, Error, PrimeCurveArithmetic}; use primeorder::{PrimeCurveParams, point_arithmetic}; /// Elliptic curve point in affine coordinates. @@ -76,3 +76,12 @@ impl From<&Scalar> for ScalarPrimitive { ScalarPrimitive::new(scalar.into()).unwrap() } } + +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} diff --git a/bp256/src/t1/arithmetic.rs b/bp256/src/t1/arithmetic.rs index 511bd678..916000cb 100644 --- a/bp256/src/t1/arithmetic.rs +++ b/bp256/src/t1/arithmetic.rs @@ -2,7 +2,7 @@ use super::BrainpoolP256t1; use crate::{FieldElement, Scalar}; -use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic}; +use elliptic_curve::{CurveArithmetic, Error, PrimeCurveArithmetic}; use primeorder::{PrimeCurveParams, point_arithmetic}; /// Elliptic curve point in affine coordinates. @@ -76,3 +76,12 @@ impl From<&Scalar> for ScalarPrimitive { ScalarPrimitive::new(scalar.into()).unwrap() } } + +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} diff --git a/bp384/src/r1/arithmetic.rs b/bp384/src/r1/arithmetic.rs index aad1882d..070abf24 100644 --- a/bp384/src/r1/arithmetic.rs +++ b/bp384/src/r1/arithmetic.rs @@ -2,7 +2,7 @@ use super::BrainpoolP384r1; use crate::{FieldElement, Scalar}; -use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic}; +use elliptic_curve::{CurveArithmetic, Error, PrimeCurveArithmetic}; use primeorder::{PrimeCurveParams, point_arithmetic}; /// Elliptic curve point in affine coordinates. @@ -82,3 +82,12 @@ impl From<&Scalar> for ScalarPrimitive { ScalarPrimitive::new(scalar.into()).unwrap() } } + +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} diff --git a/bp384/src/t1/arithmetic.rs b/bp384/src/t1/arithmetic.rs index 76f15515..adc73df8 100644 --- a/bp384/src/t1/arithmetic.rs +++ b/bp384/src/t1/arithmetic.rs @@ -2,7 +2,7 @@ use super::BrainpoolP384t1; use crate::{FieldElement, Scalar}; -use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic}; +use elliptic_curve::{CurveArithmetic, Error, PrimeCurveArithmetic}; use primeorder::{PrimeCurveParams, point_arithmetic}; /// Elliptic curve point in affine coordinates. @@ -82,3 +82,12 @@ impl From<&Scalar> for ScalarPrimitive { ScalarPrimitive::new(scalar.into()).unwrap() } } + +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} diff --git a/k256/src/arithmetic/affine.rs b/k256/src/arithmetic/affine.rs index 79245c34..cbfebdee 100644 --- a/k256/src/arithmetic/affine.rs +++ b/k256/src/arithmetic/affine.rs @@ -329,6 +329,15 @@ impl From<&PublicKey> for AffinePoint { } } +/// The constant-time alternative is available at [`NonIdentity::new()`]. +impl TryFrom for NonIdentity { + type Error = Error; + + fn try_from(affine_point: AffinePoint) -> Result { + NonIdentity::new(affine_point).into_option().ok_or(Error) + } +} + impl TryFrom for PublicKey { type Error = Error; diff --git a/k256/src/arithmetic/projective.rs b/k256/src/arithmetic/projective.rs index a9178146..161f98c2 100644 --- a/k256/src/arithmetic/projective.rs +++ b/k256/src/arithmetic/projective.rs @@ -664,6 +664,15 @@ impl From<&PublicKey> for ProjectivePoint { } } +/// The constant-time alternative is available at [`NonIdentity::new()`]. +impl TryFrom for NonIdentity { + type Error = Error; + + fn try_from(point: ProjectivePoint) -> Result { + NonIdentity::new(point).into_option().ok_or(Error) + } +} + impl TryFrom for PublicKey { type Error = Error; diff --git a/k256/src/arithmetic/scalar.rs b/k256/src/arithmetic/scalar.rs index fb402856..3d6d5535 100644 --- a/k256/src/arithmetic/scalar.rs +++ b/k256/src/arithmetic/scalar.rs @@ -12,7 +12,7 @@ use core::{ ops::{Add, AddAssign, Mul, MulAssign, Neg, Shr, ShrAssign, Sub, SubAssign}, }; use elliptic_curve::{ - Curve, ScalarPrimitive, + Curve, Error, ScalarPrimitive, bigint::{Limb, U256, U512, Word, prelude::*}, ff::{self, Field, PrimeField}, ops::{Invert, Reduce, ReduceNonZero}, @@ -413,6 +413,15 @@ impl From<&Scalar> for ScalarPrimitive { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl FromUintUnchecked for Scalar { type Uint = U256; diff --git a/p192/src/arithmetic/scalar.rs b/p192/src/arithmetic/scalar.rs index 5b24238b..706f5cc0 100644 --- a/p192/src/arithmetic/scalar.rs +++ b/p192/src/arithmetic/scalar.rs @@ -253,6 +253,15 @@ impl From<&Scalar> for ScalarPrimitive { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error; diff --git a/p224/src/arithmetic/scalar.rs b/p224/src/arithmetic/scalar.rs index c8b393ba..6ddf5af3 100644 --- a/p224/src/arithmetic/scalar.rs +++ b/p224/src/arithmetic/scalar.rs @@ -254,6 +254,15 @@ impl From<&SecretKey> for Scalar { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error; diff --git a/p256/src/arithmetic/scalar.rs b/p256/src/arithmetic/scalar.rs index 122ebd0d..e95a5bde 100644 --- a/p256/src/arithmetic/scalar.rs +++ b/p256/src/arithmetic/scalar.rs @@ -12,7 +12,7 @@ use core::{ ops::{Add, AddAssign, Mul, MulAssign, Neg, Shr, ShrAssign, Sub, SubAssign}, }; use elliptic_curve::{ - Curve, ScalarPrimitive, + Curve, Error, ScalarPrimitive, bigint::{Limb, U256, prelude::*}, group::ff::{self, Field, PrimeField}, ops::{Invert, Reduce, ReduceNonZero}, @@ -533,6 +533,15 @@ impl From<&Scalar> for ScalarBits { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl Add for Scalar { type Output = Scalar; diff --git a/p384/src/arithmetic/scalar.rs b/p384/src/arithmetic/scalar.rs index efc925fe..6dc05b1e 100644 --- a/p384/src/arithmetic/scalar.rs +++ b/p384/src/arithmetic/scalar.rs @@ -292,6 +292,15 @@ impl From<&SecretKey> for Scalar { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error; diff --git a/p521/src/arithmetic/scalar.rs b/p521/src/arithmetic/scalar.rs index 1b342771..9766d825 100644 --- a/p521/src/arithmetic/scalar.rs +++ b/p521/src/arithmetic/scalar.rs @@ -698,6 +698,15 @@ impl From<&SecretKey> for Scalar { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error; diff --git a/primeorder/src/affine.rs b/primeorder/src/affine.rs index dcfaf339..259eacfb 100644 --- a/primeorder/src/affine.rs +++ b/primeorder/src/affine.rs @@ -367,6 +367,18 @@ where } } +/// The constant-time alternative is available at [`NonIdentity::new()`]. +impl TryFrom> for NonIdentity> +where + C: PrimeCurveParams, +{ + type Error = Error; + + fn try_from(affine_point: AffinePoint) -> Result { + NonIdentity::new(affine_point).into_option().ok_or(Error) + } +} + impl TryFrom> for AffinePoint where C: PrimeCurveParams, diff --git a/primeorder/src/projective.rs b/primeorder/src/projective.rs index bec5dd1f..b032f639 100644 --- a/primeorder/src/projective.rs +++ b/primeorder/src/projective.rs @@ -477,6 +477,18 @@ where } } +/// The constant-time alternative is available at [`NonIdentity::new()`]. +impl TryFrom> for NonIdentity> +where + C: PrimeCurveParams, +{ + type Error = Error; + + fn try_from(point: ProjectivePoint) -> Result { + NonIdentity::new(point).into_option().ok_or(Error) + } +} + impl TryFrom> for PublicKey where C: PrimeCurveParams, diff --git a/sm2/src/arithmetic/scalar.rs b/sm2/src/arithmetic/scalar.rs index e732c99f..e4d25702 100644 --- a/sm2/src/arithmetic/scalar.rs +++ b/sm2/src/arithmetic/scalar.rs @@ -238,6 +238,15 @@ impl From<&SecretKey> for Scalar { } } +/// The constant-time alternative is available at [`NonZeroScalar::new()`]. +impl TryFrom for NonZeroScalar { + type Error = Error; + + fn try_from(scalar: Scalar) -> Result { + NonZeroScalar::new(scalar).into_option().ok_or(Error) + } +} + impl TryFrom for Scalar { type Error = Error;