Skip to content

Implement TryFrom<Scalar/Projective/AffinePoint> for NonIdentity/ZeroScalar #1193

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
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
9 changes: 9 additions & 0 deletions bign256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ impl From<&SecretKey> for Scalar {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<U256> for Scalar {
type Error = Error;

Expand Down
11 changes: 10 additions & 1 deletion bp256/src/r1/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}
11 changes: 10 additions & 1 deletion bp256/src/t1/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}
11 changes: 10 additions & 1 deletion bp384/src/r1/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}
11 changes: 10 additions & 1 deletion bp384/src/t1/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}
9 changes: 9 additions & 0 deletions k256/src/arithmetic/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ impl From<&PublicKey> for AffinePoint {
}
}

/// The constant-time alternative is available at [`NonIdentity::new()`].
impl TryFrom<AffinePoint> for NonIdentity<AffinePoint> {
type Error = Error;

fn try_from(affine_point: AffinePoint) -> Result<Self> {
NonIdentity::new(affine_point).into_option().ok_or(Error)
}
}

impl TryFrom<AffinePoint> for PublicKey {
type Error = Error;

Expand Down
9 changes: 9 additions & 0 deletions k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,15 @@ impl From<&PublicKey> for ProjectivePoint {
}
}

/// The constant-time alternative is available at [`NonIdentity::new()`].
impl TryFrom<ProjectivePoint> for NonIdentity<ProjectivePoint> {
type Error = Error;

fn try_from(point: ProjectivePoint) -> Result<Self> {
NonIdentity::new(point).into_option().ok_or(Error)
}
}

impl TryFrom<ProjectivePoint> for PublicKey {
type Error = Error;

Expand Down
11 changes: 10 additions & 1 deletion k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -413,6 +413,15 @@ impl From<&Scalar> for ScalarPrimitive<Secp256k1> {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl FromUintUnchecked for Scalar {
type Uint = U256;

Expand Down
9 changes: 9 additions & 0 deletions p192/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ impl From<&Scalar> for ScalarPrimitive<NistP192> {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<U192> for Scalar {
type Error = Error;

Expand Down
9 changes: 9 additions & 0 deletions p224/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ impl From<&SecretKey> for Scalar {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<Uint> for Scalar {
type Error = Error;

Expand Down
11 changes: 10 additions & 1 deletion p256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -533,6 +533,15 @@ impl From<&Scalar> for ScalarBits {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self, Error> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl Add<Scalar> for Scalar {
type Output = Scalar;

Expand Down
9 changes: 9 additions & 0 deletions p384/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ impl From<&SecretKey> for Scalar {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<U384> for Scalar {
type Error = Error;

Expand Down
9 changes: 9 additions & 0 deletions p521/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,15 @@ impl From<&SecretKey> for Scalar {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<U576> for Scalar {
type Error = Error;

Expand Down
12 changes: 12 additions & 0 deletions primeorder/src/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ where
}
}

/// The constant-time alternative is available at [`NonIdentity::new()`].
impl<C> TryFrom<AffinePoint<C>> for NonIdentity<AffinePoint<C>>
where
C: PrimeCurveParams,
{
type Error = Error;

fn try_from(affine_point: AffinePoint<C>) -> Result<Self> {
NonIdentity::new(affine_point).into_option().ok_or(Error)
}
}

impl<C> TryFrom<EncodedPoint<C>> for AffinePoint<C>
where
C: PrimeCurveParams,
Expand Down
12 changes: 12 additions & 0 deletions primeorder/src/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ where
}
}

/// The constant-time alternative is available at [`NonIdentity::new()`].
impl<C> TryFrom<ProjectivePoint<C>> for NonIdentity<ProjectivePoint<C>>
where
C: PrimeCurveParams,
{
type Error = Error;

fn try_from(point: ProjectivePoint<C>) -> Result<Self> {
NonIdentity::new(point).into_option().ok_or(Error)
}
}

impl<C> TryFrom<ProjectivePoint<C>> for PublicKey<C>
where
C: PrimeCurveParams,
Expand Down
9 changes: 9 additions & 0 deletions sm2/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ impl From<&SecretKey> for Scalar {
}
}

/// The constant-time alternative is available at [`NonZeroScalar::new()`].
impl TryFrom<Scalar> for NonZeroScalar {
type Error = Error;

fn try_from(scalar: Scalar) -> Result<Self> {
NonZeroScalar::new(scalar).into_option().ok_or(Error)
}
}

impl TryFrom<U256> for Scalar {
type Error = Error;

Expand Down