diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index 1c55378fe..d13365992 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -69,6 +69,7 @@ extern crate alloc; extern crate std; pub mod ops; +pub mod point; pub mod scalar; #[cfg(feature = "dev")] @@ -83,7 +84,6 @@ pub mod sec1; pub mod weierstrass; mod error; -mod point; mod secret_key; #[cfg(feature = "arithmetic")] diff --git a/elliptic-curve/src/point/non_identity.rs b/elliptic-curve/src/point/non_identity.rs index 075e3a94d..81c31cd19 100644 --- a/elliptic-curve/src/point/non_identity.rs +++ b/elliptic-curve/src/point/non_identity.rs @@ -1,6 +1,6 @@ //! Non-identity point type. -use core::ops::Deref; +use core::ops::{Deref, Mul}; use group::{prime::PrimeCurveAffine, Curve, GroupEncoding}; use rand_core::{CryptoRng, RngCore}; @@ -9,6 +9,8 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; #[cfg(feature = "serde")] use serdect::serde::{de, ser, Deserialize, Serialize}; +use crate::{CurveArithmetic, NonZeroScalar, Scalar}; + /// Non-identity point type. /// /// This type ensures that its value is not the identity point, ala `core::num::NonZero*`. @@ -22,13 +24,22 @@ pub struct NonIdentity

{ impl

NonIdentity

where - P: ConditionallySelectable + ConstantTimeEq + Default + GroupEncoding, + P: ConditionallySelectable + ConstantTimeEq + Default, { /// Create a [`NonIdentity`] from a point. pub fn new(point: P) -> CtOption { CtOption::new(Self { point }, !point.ct_eq(&P::default())) } + pub(crate) fn new_unchecked(point: P) -> Self { + Self { point } + } +} + +impl

NonIdentity

+where + P: ConditionallySelectable + ConstantTimeEq + Default + GroupEncoding, +{ /// Decode a [`NonIdentity`] from its encoding. pub fn from_repr(repr: &P::Repr) -> CtOption { Self::from_bytes(repr) @@ -44,7 +55,7 @@ impl NonIdentity

{ impl

NonIdentity

where - P: ConditionallySelectable + ConstantTimeEq + Curve + Default + GroupEncoding, + P: ConditionallySelectable + ConstantTimeEq + Curve + Default, { /// Generate a random `NonIdentity`. pub fn random(mut rng: impl CryptoRng + RngCore) -> Self { @@ -129,6 +140,32 @@ where } } +impl Mul> for NonIdentity

+where + C: CurveArithmetic, + P: Copy + Mul, Output = P>, +{ + type Output = NonIdentity

; + + fn mul(self, rhs: NonZeroScalar) -> Self::Output { + &self * &rhs + } +} + +impl Mul<&NonZeroScalar> for &NonIdentity

+where + C: CurveArithmetic, + P: Copy + Mul, Output = P>, +{ + type Output = NonIdentity

; + + fn mul(self, rhs: &NonZeroScalar) -> Self::Output { + NonIdentity { + point: self.point * *rhs.as_ref(), + } + } +} + #[cfg(feature = "serde")] impl

Serialize for NonIdentity

where diff --git a/elliptic-curve/src/public_key.rs b/elliptic-curve/src/public_key.rs index a916cd8a8..045a1aa6a 100644 --- a/elliptic-curve/src/public_key.rs +++ b/elliptic-curve/src/public_key.rs @@ -155,6 +155,11 @@ where self.point.into() } + /// Convert this [`PublicKey`] to a [`NonIdentity`] of the inner [`AffinePoint`] + pub fn to_non_identity(&self) -> NonIdentity> { + NonIdentity::new_unchecked(self.point) + } + /// Parse a [`JwkEcKey`] JSON Web Key (JWK) into a [`PublicKey`]. #[cfg(feature = "jwk")] pub fn from_jwk(jwk: &JwkEcKey) -> Result @@ -271,7 +276,7 @@ where P: Copy + Into>, { fn from(value: NonIdentity

) -> Self { - PublicKey::from(&value) + Self::from(&value) } } @@ -287,6 +292,24 @@ where } } +impl From> for NonIdentity> +where + C: CurveArithmetic, +{ + fn from(value: PublicKey) -> Self { + Self::from(&value) + } +} + +impl From<&PublicKey> for NonIdentity> +where + C: CurveArithmetic, +{ + fn from(value: &PublicKey) -> Self { + PublicKey::to_non_identity(value) + } +} + #[cfg(feature = "sec1")] impl PartialOrd for PublicKey where