Skip to content

Require TryInto<Non/Identity/ZeroScalar> #1853

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
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
7 changes: 5 additions & 2 deletions elliptic-curve/src/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Elliptic curve arithmetic traits.

use crate::{
Curve, FieldBytes, NonZeroScalar, PrimeCurve, ScalarPrimitive,
Curve, Error, FieldBytes, NonZeroScalar, PrimeCurve, ScalarPrimitive,
ops::{Invert, LinearCombination, Mul, Reduce, ShrAssign},
point::{AffineCoordinates, NonIdentity},
scalar::{FromUintUnchecked, IsHigh},
Expand All @@ -26,7 +26,8 @@ pub trait CurveArithmetic: Curve {
+ PartialEq
+ Sized
+ Send
+ Sync;
+ Sync
+ TryInto<NonIdentity<Self::AffinePoint>, Error = Error>;

/// Elliptic curve point in projective coordinates.
///
Expand All @@ -48,6 +49,7 @@ pub trait CurveArithmetic: Curve {
+ Into<Self::AffinePoint>
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar)]>
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar); 2]>
+ TryInto<NonIdentity<Self::ProjectivePoint>, Error = Error>
+ group::Curve<AffineRepr = Self::AffinePoint>
+ group::Group<Scalar = Self::Scalar>;

Expand Down Expand Up @@ -80,6 +82,7 @@ pub trait CurveArithmetic: Curve {
+ PartialOrd
+ Reduce<Self::Uint, Bytes = FieldBytes<Self>>
+ ShrAssign<usize>
+ TryInto<NonZeroScalar<Self>, Error = Error>
+ ff::Field
+ ff::PrimeField<Repr = FieldBytes<Self>>;
}
Expand Down
24 changes: 24 additions & 0 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ impl From<Scalar> for U256 {
}
}

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 Expand Up @@ -544,6 +552,14 @@ impl Mul<NonZeroScalar> for AffinePoint {
}
}

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

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

/// Example projective point type
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProjectivePoint {
Expand Down Expand Up @@ -622,6 +638,14 @@ impl ToEncodedPoint<MockCurve> for ProjectivePoint {
}
}

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 group::Group for ProjectivePoint {
type Scalar = Scalar;

Expand Down