From cdb9c4ed05793e0e3b882082d8408adc669b4a75 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 4 Jan 2024 10:42:43 +0000 Subject: [PATCH 1/8] feat: implement `Eq` trait on curve points --- noir_stdlib/src/ec/montcurve.nr | 24 ++++++++++++--------- noir_stdlib/src/ec/swcurve.nr | 38 ++++++++++++++++++--------------- noir_stdlib/src/ec/tecurve.nr | 36 +++++++++++++++++-------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/noir_stdlib/src/ec/montcurve.nr b/noir_stdlib/src/ec/montcurve.nr index 82d22837b46..20cab113855 100644 --- a/noir_stdlib/src/ec/montcurve.nr +++ b/noir_stdlib/src/ec/montcurve.nr @@ -32,11 +32,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 @@ -76,6 +71,13 @@ mod affine { } } + impl Eq for Point { + // 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)) + } + } + impl Curve { // Curve constructor pub fn new(j: Field, k: Field, gen: Point) -> Self { @@ -239,11 +241,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 @@ -277,6 +274,13 @@ mod curvegroup { } } + impl Eq for Point { + // 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))) + } + } + impl Curve { // Curve constructor pub fn new(j: Field, k: Field, gen: Point) -> Self { diff --git a/noir_stdlib/src/ec/swcurve.nr b/noir_stdlib/src/ec/swcurve.nr index e9b6f661843..3fde8cabff3 100644 --- a/noir_stdlib/src/ec/swcurve.nr +++ b/noir_stdlib/src/ec/swcurve.nr @@ -28,15 +28,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()) @@ -65,6 +56,17 @@ mod affine { } } + impl Eq for Point { + // Check for equality + 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 { @@ -203,14 +205,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()) @@ -240,6 +234,16 @@ mod curvegroup { } } + impl Eq for Point { + // Check for equality + 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 { diff --git a/noir_stdlib/src/ec/tecurve.nr b/noir_stdlib/src/ec/tecurve.nr index 849b45ff012..3b644ad1c2c 100644 --- a/noir_stdlib/src/ec/tecurve.nr +++ b/noir_stdlib/src/ec/tecurve.nr @@ -29,14 +29,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()) @@ -74,6 +66,16 @@ mod affine { } } + impl Eq for Point { + // Check for equality + 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 { @@ -220,14 +222,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; @@ -259,6 +253,16 @@ mod curvegroup { } } + impl Eq for Point { + // Check for equality + 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 { From ba03eb3885374282e1b25d5ec0ed4e9407cd70a3 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 4 Jan 2024 10:46:12 +0000 Subject: [PATCH 2/8] fix: import `Eq` trait --- noir_stdlib/src/ec/montcurve.nr | 3 +++ noir_stdlib/src/ec/swcurve.nr | 4 ++++ noir_stdlib/src/ec/tecurve.nr | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/noir_stdlib/src/ec/montcurve.nr b/noir_stdlib/src/ec/montcurve.nr index 20cab113855..e71964d3027 100644 --- a/noir_stdlib/src/ec/montcurve.nr +++ b/noir_stdlib/src/ec/montcurve.nr @@ -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, @@ -221,6 +223,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, diff --git a/noir_stdlib/src/ec/swcurve.nr b/noir_stdlib/src/ec/swcurve.nr index 3fde8cabff3..3b9dd04add5 100644 --- a/noir_stdlib/src/ec/swcurve.nr +++ b/noir_stdlib/src/ec/swcurve.nr @@ -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 @@ -184,6 +186,8 @@ mod curvegroup { // Points are represented by three-dimensional Jacobian coordinates. // See 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 diff --git a/noir_stdlib/src/ec/tecurve.nr b/noir_stdlib/src/ec/tecurve.nr index 3b644ad1c2c..94e1c74c9fb 100644 --- a/noir_stdlib/src/ec/tecurve.nr +++ b/noir_stdlib/src/ec/tecurve.nr @@ -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 @@ -200,6 +202,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 From 57fe30ca7cc184880c25f4472faf571d47d40c07 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:14:48 -0500 Subject: [PATCH 3/8] Update noir_stdlib/src/ec/montcurve.nr --- noir_stdlib/src/ec/montcurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/montcurve.nr b/noir_stdlib/src/ec/montcurve.nr index e71964d3027..4ae8c2ab650 100644 --- a/noir_stdlib/src/ec/montcurve.nr +++ b/noir_stdlib/src/ec/montcurve.nr @@ -74,7 +74,6 @@ mod affine { } impl Eq for Point { - // 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)) } From dd30169aa85acca7056a74c1799195b79ab8fad5 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:14:53 -0500 Subject: [PATCH 4/8] Update noir_stdlib/src/ec/montcurve.nr --- noir_stdlib/src/ec/montcurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/montcurve.nr b/noir_stdlib/src/ec/montcurve.nr index 4ae8c2ab650..83a17bae322 100644 --- a/noir_stdlib/src/ec/montcurve.nr +++ b/noir_stdlib/src/ec/montcurve.nr @@ -277,7 +277,6 @@ mod curvegroup { } impl Eq for Point { - // 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))) } From fc42cbfe157d8c911197b00b7e3d1bb03d544b27 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:14:58 -0500 Subject: [PATCH 5/8] Update noir_stdlib/src/ec/swcurve.nr --- noir_stdlib/src/ec/swcurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/swcurve.nr b/noir_stdlib/src/ec/swcurve.nr index 3b9dd04add5..577b3ade0b2 100644 --- a/noir_stdlib/src/ec/swcurve.nr +++ b/noir_stdlib/src/ec/swcurve.nr @@ -59,7 +59,6 @@ mod affine { } impl Eq for Point { - // Check for equality fn eq(self, p: Self) -> bool { let Self {x: x1, y: y1, infty: inf1} = self; let Self {x: x2, y: y2, infty: inf2} = p; From 4f544c4b066871dee993b5a8492ca3389dff4120 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:15:03 -0500 Subject: [PATCH 6/8] Update noir_stdlib/src/ec/swcurve.nr --- noir_stdlib/src/ec/swcurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/swcurve.nr b/noir_stdlib/src/ec/swcurve.nr index 577b3ade0b2..e64f5a7be02 100644 --- a/noir_stdlib/src/ec/swcurve.nr +++ b/noir_stdlib/src/ec/swcurve.nr @@ -238,7 +238,6 @@ mod curvegroup { } impl Eq for Point { - // Check for equality fn eq(self, p: Self) -> bool { let Self {x: x1, y: y1, z: z1} = self; let Self {x: x2, y: y2, z: z2} = p; From 55e10b7c1dbb6de8bbdc48233bf524ee8a393f0d Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:15:08 -0500 Subject: [PATCH 7/8] Update noir_stdlib/src/ec/tecurve.nr --- noir_stdlib/src/ec/tecurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/tecurve.nr b/noir_stdlib/src/ec/tecurve.nr index 94e1c74c9fb..acdfb2030e2 100644 --- a/noir_stdlib/src/ec/tecurve.nr +++ b/noir_stdlib/src/ec/tecurve.nr @@ -69,7 +69,6 @@ mod affine { } impl Eq for Point { - // Check for equality fn eq(self, p: Self) -> bool { let Self {x: x1, y: y1} = self; let Self {x: x2, y: y2} = p; From 8c7635ee2afd938d665d2d677e8cf47c9305770e Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 4 Jan 2024 10:15:13 -0500 Subject: [PATCH 8/8] Update noir_stdlib/src/ec/tecurve.nr --- noir_stdlib/src/ec/tecurve.nr | 1 - 1 file changed, 1 deletion(-) diff --git a/noir_stdlib/src/ec/tecurve.nr b/noir_stdlib/src/ec/tecurve.nr index acdfb2030e2..5333ece4c4a 100644 --- a/noir_stdlib/src/ec/tecurve.nr +++ b/noir_stdlib/src/ec/tecurve.nr @@ -257,7 +257,6 @@ mod curvegroup { } impl Eq for Point { - // Check for equality 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;