Skip to content

Commit

Permalink
feat: implement Eq trait on curve points (#3944)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR just moves the `eq` methods on the ec point structs into `Eq`
trait impls.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: jfecher <jake@aztecprotocol.com>
  • Loading branch information
TomAFrench and jfecher committed Jan 4, 2024
1 parent cc4f3a3 commit abf751a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
25 changes: 15 additions & 10 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod affine {
use crate::ec::safe_inverse;
use crate::ec::sqrt;
use crate::ec::ZETA;
use crate::cmp::Eq;

// Curve specification
struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x)
j: Field,
Expand All @@ -32,11 +34,6 @@ mod affine {
Self {x, y, infty: false}
}

// Check for equality
fn eq(self, p: Self) -> bool {
(self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y))
}

// Check if zero
pub fn is_zero(self) -> bool {
self.infty
Expand Down Expand Up @@ -76,6 +73,12 @@ mod affine {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
(self.infty & p.infty) | (!self.infty & !p.infty & (self.x == p.x) & (self.y == p.y))
}
}

impl Curve {
// Curve constructor
pub fn new(j: Field, k: Field, gen: Point) -> Self {
Expand Down Expand Up @@ -219,6 +222,7 @@ mod curvegroup {
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::ec::tecurve::curvegroup::Curve as TECurve;
use crate::ec::tecurve::curvegroup::Point as TEPoint;
use crate::cmp::Eq;

struct Curve { // Montgomery Curve configuration (ky^2 z = x*(x^2 + j*x*z + z*z))
j: Field,
Expand All @@ -239,11 +243,6 @@ mod curvegroup {
Self {x, y, z}
}

// Check for equality
fn eq(self, p: Self) -> bool {
(self.z == p.z) | (((self.x * self.z) == (p.x * p.z)) & ((self.y * self.z) == (p.y * p.z)))
}

// Check if zero
pub fn is_zero(self) -> bool {
self.z == 0
Expand Down Expand Up @@ -277,6 +276,12 @@ mod curvegroup {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
(self.z == p.z) | (((self.x * self.z) == (p.x * p.z)) & ((self.y * self.z) == (p.y * p.z)))
}
}

impl Curve {
// Curve constructor
pub fn new(j: Field, k: Field, gen: Point) -> Self {
Expand Down
40 changes: 23 additions & 17 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod affine {
use crate::ec::safe_inverse;
use crate::ec::is_square;
use crate::ec::sqrt;
use crate::cmp::Eq;

// Curve specification
struct Curve { // Short Weierstraß curve
// Coefficients in defining equation y^2 = x^3 + ax + b
Expand All @@ -28,15 +30,6 @@ mod affine {
Self {x, y, infty: false}
}

// Check for equality
fn eq(self, p: Point) -> bool {
let Self {x: x1, y: y1, infty: inf1} = self;
let Self {x: x2, y: y2, infty: inf2} = p;

(inf1 & inf2)
| (!inf1 & !inf2 & (x1 == x2) & (y1 == y2))
}

// Check if zero
pub fn is_zero(self) -> bool {
self.eq(Point::zero())
Expand Down Expand Up @@ -65,6 +58,16 @@ mod affine {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
let Self {x: x1, y: y1, infty: inf1} = self;
let Self {x: x2, y: y2, infty: inf2} = p;

(inf1 & inf2)
| (!inf1 & !inf2 & (x1 == x2) & (y1 == y2))
}
}

impl Curve {
// Curve constructor
pub fn new(a: Field, b: Field, gen: Point) -> Curve {
Expand Down Expand Up @@ -182,6 +185,8 @@ mod curvegroup {
// Points are represented by three-dimensional Jacobian coordinates.
// See <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates> for details.
use crate::ec::swcurve::affine;
use crate::cmp::Eq;

// Curve specification
struct Curve { // Short Weierstraß curve
// Coefficients in defining equation y^2 = x^3 + axz^4 + bz^6
Expand All @@ -203,14 +208,6 @@ mod curvegroup {
Self {x, y, z}
}

// Check for equality
fn eq(self, p: Point) -> bool {
let Self {x: x1, y: y1, z: z1} = self;
let Self {x: x2, y: y2, z: z2} = p;

((z1 == 0) & (z2 == 0)) | ((z1 != 0) & (z2 != 0) & (x1*z2*z2 == x2*z1*z1) & (y1*z2*z2*z2 == y2*z1*z1*z1))
}

// Check if zero
pub fn is_zero(self) -> bool {
self.eq(Point::zero())
Expand Down Expand Up @@ -240,6 +237,15 @@ mod curvegroup {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
let Self {x: x1, y: y1, z: z1} = self;
let Self {x: x2, y: y2, z: z2} = p;

((z1 == 0) & (z2 == 0)) | ((z1 != 0) & (z2 != 0) & (x1*z2*z2 == x2*z1*z1) & (y1*z2*z2*z2 == y2*z1*z1*z1))
}
}

impl Curve {
// Curve constructor
pub fn new(a: Field, b: Field, gen: Point) -> Curve {
Expand Down
38 changes: 22 additions & 16 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod affine {
use crate::ec::montcurve::affine::Point as MPoint;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;
use crate::cmp::Eq;

// Curve specification
struct Curve { // Twisted Edwards curve
// Coefficients in defining equation ax^2 + y^2 = 1 + dx^2y^2
Expand All @@ -29,14 +31,6 @@ mod affine {
Self { x, y }
}

// Check for equality
fn eq(self, p: Point) -> bool {
let Self {x: x1, y: y1} = self;
let Self {x: x2, y: y2} = p;

(x1 == x2) & (y1 == y2)
}

// Check if zero
pub fn is_zero(self) -> bool {
self.eq(Point::zero())
Expand Down Expand Up @@ -74,6 +68,15 @@ mod affine {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
let Self {x: x1, y: y1} = self;
let Self {x: x2, y: y2} = p;

(x1 == x2) & (y1 == y2)
}
}

impl Curve {
// Curve constructor
pub fn new(a: Field, d: Field, gen: Point) -> Curve {
Expand Down Expand Up @@ -198,6 +201,8 @@ mod curvegroup {
use crate::ec::montcurve::curvegroup::Point as MPoint;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::cmp::Eq;

// Curve specification
struct Curve { // Twisted Edwards curve
// Coefficients in defining equation a(x^2 + y^2)z^2 = z^4 + dx^2y^2
Expand All @@ -220,14 +225,6 @@ mod curvegroup {
Self {x, y, t, z}
}

// Check for equality
fn eq(self, p: Point) -> bool {
let Self {x: x1, y: y1, t: _t1, z: z1} = self;
let Self {x: x2, y: y2, t: _t2, z:z2} = p;

(x1*z2 == x2*z1) & (y1*z2 == y2*z1)
}

// Check if zero
pub fn is_zero(self) -> bool {
let Self {x, y, t, z} = self;
Expand Down Expand Up @@ -259,6 +256,15 @@ mod curvegroup {
}
}

impl Eq for Point {
fn eq(self, p: Self) -> bool {
let Self {x: x1, y: y1, t: _t1, z: z1} = self;
let Self {x: x2, y: y2, t: _t2, z:z2} = p;

(x1*z2 == x2*z1) & (y1*z2 == y2*z1)
}
}

impl Curve {
// Curve constructor
pub fn new(a: Field, d: Field, gen: Point) -> Curve {
Expand Down

0 comments on commit abf751a

Please sign in to comment.