From 0f49f6461e528c6dfd855cb55d806ad8286cd0e4 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Mon, 15 Nov 2021 11:29:06 -0800 Subject: [PATCH] removed from ProjectivePoint --- src/curve/curve_adds.rs | 9 +++----- src/curve/curve_types.rs | 47 +++++++++++++++------------------------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/curve/curve_adds.rs b/src/curve/curve_adds.rs index 32a5adcc..66e66bd0 100644 --- a/src/curve/curve_adds.rs +++ b/src/curve/curve_adds.rs @@ -11,19 +11,17 @@ impl Add> for ProjectivePoint { x: x1, y: y1, z: z1, - zero: zero1, } = self; let ProjectivePoint { x: x2, y: y2, z: z2, - zero: zero2, } = rhs; - if zero1 { + if z1 == C::BaseField::ZERO { return rhs; } - if zero2 { + if z2 == C::BaseField::ZERO { return self; } @@ -66,7 +64,6 @@ impl Add> for ProjectivePoint { x: x1, y: y1, z: z1, - zero: zero1, } = self; let AffinePoint { x: x2, @@ -74,7 +71,7 @@ impl Add> for ProjectivePoint { zero: zero2, } = rhs; - if zero1 { + if z1 == C::BaseField::ZERO { return rhs.to_projective(); } if zero2 { diff --git a/src/curve/curve_types.rs b/src/curve/curve_types.rs index 830dc7c1..3c16651e 100644 --- a/src/curve/curve_types.rs +++ b/src/curve/curve_types.rs @@ -23,7 +23,6 @@ pub trait Curve: 'static + Sync + Sized + Copy + Debug { x: Self::GENERATOR_AFFINE.x, y: Self::GENERATOR_AFFINE.y, z: Self::BaseField::ONE, - zero: false, }; fn convert(x: Self::ScalarField) -> CurveScalar { @@ -89,12 +88,13 @@ impl AffinePoint { pub fn to_projective(&self) -> ProjectivePoint { let Self { x, y, zero } = *self; - ProjectivePoint { - x, - y, - z: C::BaseField::ONE, - zero, - } + let z = if zero { + C::BaseField::ZERO + } else { + C::BaseField::ONE + }; + + ProjectivePoint { x, y, z } } pub fn batch_to_projective(affine_points: &[Self]) -> Vec> { @@ -150,7 +150,6 @@ pub struct ProjectivePoint { pub x: C::BaseField, pub y: C::BaseField, pub z: C::BaseField, - pub zero: bool, } impl ProjectivePoint { @@ -158,16 +157,10 @@ impl ProjectivePoint { x: C::BaseField::ZERO, y: C::BaseField::ZERO, z: C::BaseField::ZERO, - zero: true, }; pub fn nonzero(x: C::BaseField, y: C::BaseField, z: C::BaseField) -> Self { - let point = Self { - x, - y, - z, - zero: false, - }; + let point = Self { x, y, z }; debug_assert!(point.is_valid()); point } @@ -177,8 +170,8 @@ impl ProjectivePoint { } pub fn to_affine(&self) -> AffinePoint { - let Self { x, y, z, zero } = *self; - if zero { + let Self { x, y, z } = *self; + if z == C::BaseField::ZERO { AffinePoint::ZERO } else { let z_inv = z.inverse(); @@ -193,8 +186,8 @@ impl ProjectivePoint { let mut result = Vec::with_capacity(n); for i in 0..n { - let Self { x, y, z: _, zero } = proj_points[i]; - result.push(if zero { + let Self { x, y, z } = proj_points[i]; + result.push(if z == C::BaseField::ZERO { AffinePoint::ZERO } else { let z_inv = z_invs[i]; @@ -205,8 +198,8 @@ impl ProjectivePoint { } pub fn double(&self) -> Self { - let Self { x, y, z, zero } = *self; - if zero { + let Self { x, y, z } = *self; + if z == C::BaseField::ZERO { return ProjectivePoint::ZERO; } @@ -228,7 +221,6 @@ impl ProjectivePoint { x: x3, y: y3, z: z3, - zero: false, } } @@ -245,7 +237,6 @@ impl ProjectivePoint { x: self.x, y: -self.y, z: self.z, - zero: self.zero, } } } @@ -256,16 +247,14 @@ impl PartialEq for ProjectivePoint { x: x1, y: y1, z: z1, - zero: zero1, } = *self; let ProjectivePoint { x: x2, y: y2, z: z2, - zero: zero2, } = *other; - if zero1 || zero2 { - return zero1 == zero2; + if z1 == C::BaseField::ZERO || z2 == C::BaseField::ZERO { + return z1 == z2; } // We want to compare (x1/z1, y1/z1) == (x2/z2, y2/z2). @@ -289,7 +278,7 @@ impl Neg for ProjectivePoint { type Output = ProjectivePoint; fn neg(self) -> Self::Output { - let ProjectivePoint { x, y, z, zero } = self; - ProjectivePoint { x, y: -y, z, zero } + let ProjectivePoint { x, y, z } = self; + ProjectivePoint { x, y: -y, z } } }