Skip to content

Commit

Permalink
p256: impl ff and group traits
Browse files Browse the repository at this point in the history
Corresponding change to #164, but for the `p256` crate.

- Impls `ff::{Field, PrimeField}` on `Scalar`
- Impls `group::{Group, Curve}` on `ProjectivePoint`
  • Loading branch information
tarcieri committed Sep 6, 2020
1 parent ebac050 commit 8bfa8b6
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use core::{
ops::{Add, AddAssign, Neg, Sub, SubAssign},
};
use elliptic_curve::{
group::{self, Group},
ff::Field,
group::{Curve, Group},
point::Generator,
rand_core::RngCore,
subtle::{Choice, ConditionallySelectable, ConstantTimeEq},
Expand Down Expand Up @@ -240,8 +241,8 @@ impl ProjectivePoint {
impl Group for ProjectivePoint {
type Scalar = Scalar;

fn random(rng: impl RngCore) -> Self {
Self::generator() * Scalar::generate_vartime(rng)
fn random(mut rng: impl RngCore) -> Self {
Self::generator() * Scalar::random(&mut rng)
}

fn identity() -> Self {
Expand All @@ -262,7 +263,7 @@ impl Group for ProjectivePoint {
}
}

impl group::Curve for ProjectivePoint {
impl Curve for ProjectivePoint {
type AffineRepr = AffinePoint;

fn to_affine(&self) -> AffinePoint {
Expand Down
6 changes: 3 additions & 3 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::{
};
use elliptic_curve::{
consts::U32,
ff,
ff::{Field, PrimeField},
ops::Invert,
rand_core::{CryptoRng, RngCore},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Scalar {
}
}

impl ff::Field for Scalar {
impl Field for Scalar {
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
Scalar::generate_vartime(rng)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ impl ff::Field for Scalar {
}
}

impl ff::PrimeField for Scalar {
impl PrimeField for Scalar {
type Repr = ElementBytes;
type ReprEndianness = byteorder::BigEndian;

Expand Down
1 change: 1 addition & 0 deletions p256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories = ["cryptography", "no-std"]
keywords = ["crypto", "ecc", "nist", "prime256v1", "secp256r1"]

[dependencies]
byteorder = { version = "1", default-features = false }
ecdsa-core = { version = "0.7", package = "ecdsa", optional = true, default-features = false }
elliptic-curve = { version = "0.5", default-features = false }
sha2 = { version = "0.9", optional = true, default-features = false }
Expand Down
116 changes: 114 additions & 2 deletions p256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
//! Projective points
use super::{AffinePoint, FieldElement, Scalar, CURVE_EQUATION_B};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::{
fmt,
iter::Sum,
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use elliptic_curve::{
ff::Field,
group::{Curve, Group},
point::Generator,
rand_core::RngCore,
subtle::{Choice, ConditionallySelectable, ConstantTimeEq},
};

Expand All @@ -16,6 +23,39 @@ pub struct ProjectivePoint {
z: FieldElement,
}

impl Group for ProjectivePoint {
type Scalar = Scalar;

fn random(mut rng: impl RngCore) -> Self {
Self::generator() * Scalar::random(&mut rng)
}

fn identity() -> Self {
ProjectivePoint::identity()
}

fn generator() -> Self {
ProjectivePoint::generator()
}

fn is_identity(&self) -> Choice {
self.ct_eq(&Self::identity())
}

#[must_use]
fn double(&self) -> Self {
ProjectivePoint::double(self)
}
}

impl Curve for ProjectivePoint {
type AffineRepr = AffinePoint;

fn to_affine(&self) -> AffinePoint {
ProjectivePoint::to_affine(self)
}
}

impl From<AffinePoint> for ProjectivePoint {
fn from(p: AffinePoint) -> Self {
let projective = ProjectivePoint {
Expand Down Expand Up @@ -49,6 +89,8 @@ impl PartialEq for ProjectivePoint {
}
}

impl Eq for ProjectivePoint {}

impl ProjectivePoint {
/// Returns the additive identity of P-256, also known as the "neutral element" or
/// "point at infinity".
Expand Down Expand Up @@ -208,6 +250,14 @@ impl Default for ProjectivePoint {
}
}

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

fn add(self, other: ProjectivePoint) -> ProjectivePoint {
ProjectivePoint::add(&self, &other)
}
}

impl Add<&ProjectivePoint> for &ProjectivePoint {
type Output = ProjectivePoint;

Expand Down Expand Up @@ -236,6 +286,14 @@ impl AddAssign<&ProjectivePoint> for ProjectivePoint {
}
}

impl Add<AffinePoint> for ProjectivePoint {
type Output = ProjectivePoint;

fn add(self, other: AffinePoint) -> ProjectivePoint {
ProjectivePoint::add_mixed(&self, &other)
}
}

impl Add<&AffinePoint> for &ProjectivePoint {
type Output = ProjectivePoint;

Expand All @@ -258,6 +316,32 @@ impl AddAssign<AffinePoint> for ProjectivePoint {
}
}

impl AddAssign<&AffinePoint> for ProjectivePoint {
fn add_assign(&mut self, rhs: &AffinePoint) {
*self = ProjectivePoint::add_mixed(self, rhs);
}
}

impl Sum for ProjectivePoint {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(ProjectivePoint::identity(), |a, b| a + b)
}
}

impl<'a> Sum<&'a ProjectivePoint> for ProjectivePoint {
fn sum<I: Iterator<Item = &'a ProjectivePoint>>(iter: I) -> Self {
iter.cloned().sum()
}
}

impl Sub<ProjectivePoint> for ProjectivePoint {
type Output = ProjectivePoint;

fn sub(self, other: ProjectivePoint) -> ProjectivePoint {
ProjectivePoint::sub(&self, &other)
}
}

impl Sub<&ProjectivePoint> for &ProjectivePoint {
type Output = ProjectivePoint;

Expand Down Expand Up @@ -286,6 +370,14 @@ impl SubAssign<&ProjectivePoint> for ProjectivePoint {
}
}

impl Sub<AffinePoint> for ProjectivePoint {
type Output = ProjectivePoint;

fn sub(self, other: AffinePoint) -> ProjectivePoint {
ProjectivePoint::sub_mixed(&self, &other)
}
}

impl Sub<&AffinePoint> for &ProjectivePoint {
type Output = ProjectivePoint;

Expand All @@ -308,6 +400,20 @@ impl SubAssign<AffinePoint> for ProjectivePoint {
}
}

impl SubAssign<&AffinePoint> for ProjectivePoint {
fn sub_assign(&mut self, rhs: &AffinePoint) {
*self = ProjectivePoint::sub_mixed(self, rhs);
}
}

impl Mul<Scalar> for ProjectivePoint {
type Output = ProjectivePoint;

fn mul(self, other: Scalar) -> ProjectivePoint {
ProjectivePoint::mul(&self, &other)
}
}

impl Mul<&Scalar> for &ProjectivePoint {
type Output = ProjectivePoint;

Expand Down Expand Up @@ -352,6 +458,12 @@ impl<'a> Neg for &'a ProjectivePoint {
}
}

impl fmt::Display for ProjectivePoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

#[cfg(test)]
mod tests {
use super::{AffinePoint, ProjectivePoint, Scalar};
Expand Down Expand Up @@ -432,7 +544,7 @@ mod tests {
ADD_TEST_VECTORS[i]
);

p = p + &generator;
p += &generator;
}
}

Expand Down
Loading

0 comments on commit 8bfa8b6

Please sign in to comment.