Skip to content

Commit 663b445

Browse files
authored
Require TryInto<Non/Identity/ZeroScalar> (#1853)
This PR adds the following requirements: - `CurveArithmetic::Scalar`: `TryInto<NonZeroScalar>` - `CurveArithmetic::ProjectivePoint`: `TryInto<NonIdentity<ProjectivePoint>>` - `CurveArithmetic::AffinePoint`: `TryInto<NonIdentity<AffinePoint>>` Companion PR to RustCrypto/elliptic-curves#1193.
1 parent 22ae23d commit 663b445

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

elliptic-curve/src/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Elliptic curve arithmetic traits.
22
33
use crate::{
4-
Curve, FieldBytes, NonZeroScalar, PrimeCurve, ScalarPrimitive,
4+
Curve, Error, FieldBytes, NonZeroScalar, PrimeCurve, ScalarPrimitive,
55
ops::{Invert, LinearCombination, Mul, Reduce, ShrAssign},
66
point::{AffineCoordinates, NonIdentity},
77
scalar::{FromUintUnchecked, IsHigh},
@@ -26,7 +26,8 @@ pub trait CurveArithmetic: Curve {
2626
+ PartialEq
2727
+ Sized
2828
+ Send
29-
+ Sync;
29+
+ Sync
30+
+ TryInto<NonIdentity<Self::AffinePoint>, Error = Error>;
3031

3132
/// Elliptic curve point in projective coordinates.
3233
///
@@ -48,6 +49,7 @@ pub trait CurveArithmetic: Curve {
4849
+ Into<Self::AffinePoint>
4950
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar)]>
5051
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar); 2]>
52+
+ TryInto<NonIdentity<Self::ProjectivePoint>, Error = Error>
5153
+ group::Curve<AffineRepr = Self::AffinePoint>
5254
+ group::Group<Scalar = Self::Scalar>;
5355

@@ -80,6 +82,7 @@ pub trait CurveArithmetic: Curve {
8082
+ PartialOrd
8183
+ Reduce<Self::Uint, Bytes = FieldBytes<Self>>
8284
+ ShrAssign<usize>
85+
+ TryInto<NonZeroScalar<Self>, Error = Error>
8386
+ ff::Field
8487
+ ff::PrimeField<Repr = FieldBytes<Self>>;
8588
}

elliptic-curve/src/dev.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,14 @@ impl From<Scalar> for U256 {
408408
}
409409
}
410410

411+
impl TryFrom<Scalar> for NonZeroScalar {
412+
type Error = Error;
413+
414+
fn try_from(scalar: Scalar) -> Result<Self> {
415+
NonZeroScalar::new(scalar).into_option().ok_or(Error)
416+
}
417+
}
418+
411419
impl TryFrom<U256> for Scalar {
412420
type Error = Error;
413421

@@ -544,6 +552,14 @@ impl Mul<NonZeroScalar> for AffinePoint {
544552
}
545553
}
546554

555+
impl TryFrom<AffinePoint> for NonIdentity<AffinePoint> {
556+
type Error = Error;
557+
558+
fn try_from(affine: AffinePoint) -> Result<Self> {
559+
NonIdentity::new(affine).into_option().ok_or(Error)
560+
}
561+
}
562+
547563
/// Example projective point type
548564
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
549565
pub enum ProjectivePoint {
@@ -622,6 +638,14 @@ impl ToEncodedPoint<MockCurve> for ProjectivePoint {
622638
}
623639
}
624640

641+
impl TryFrom<ProjectivePoint> for NonIdentity<ProjectivePoint> {
642+
type Error = Error;
643+
644+
fn try_from(point: ProjectivePoint) -> Result<Self> {
645+
NonIdentity::new(point).into_option().ok_or(Error)
646+
}
647+
}
648+
625649
impl group::Group for ProjectivePoint {
626650
type Scalar = Scalar;
627651

0 commit comments

Comments
 (0)