From ae763c1d677f6f82ae3e4fb84d961acb210d4f03 Mon Sep 17 00:00:00 2001 From: Waylon Jepsen <57912727+0xJepsen@users.noreply.github.com> Date: Wed, 1 May 2024 12:59:04 -0700 Subject: [PATCH 1/2] Update curve.rs comments and review. --- src/curve.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/curve.rs b/src/curve.rs index 3b1ae200..a43f52dd 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -14,6 +14,7 @@ pub struct Point { y: F, } +// Since EVERY point is either at "infinity" or not, the coproduct makes sense. #[derive(Clone, Copy)] pub enum PointOrInfinity { Point(Point), @@ -30,6 +31,7 @@ impl Curve { } } + // inverse pub fn negate(&self, p: PointOrInfinity) -> PointOrInfinity { match p { PointOrInfinity::Point(p) => PointOrInfinity::Point(Point { x: p.x, y: -p.y }), @@ -37,6 +39,7 @@ impl Curve { } } + // outer add does infinitity check pub fn add(&self, p: PointOrInfinity, q: PointOrInfinity) -> PointOrInfinity { match (p, q) { (PointOrInfinity::Infinity, _) => q, @@ -49,21 +52,24 @@ impl Curve { } fn add_points(&self, p: Point, q: Point) -> Point { - let (x1, y1) = (p.x, p.y); - let (x2, y2) = (q.x, q.y); + // https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplicationcv + let (x_p, y_p) = (p.x, p.y); + let (x_q, y_q) = (q.x, q.y); - if x1 == x2 && y1 == -y2 { + // check for zero + if x_p == x_q && y_p == -y_q { return Point { x: F::zero(), y: F::zero() }; } - let m = if x1 == x2 && y1 == y2 { - (self.three * x1 * x1 + self.a) / (self.two * y1) + // Check if point is itself, if it is you double (which is easier) + let lamda = if x_p == x_q && y_p == y_q { + (self.three * x_p * x_p + self.a) / (self.two * y_p) } else { - (y2 - y1) / (x2 - x1) + (y_q - y_p) / (x_q - x_p) }; - let x = m * m - x1 - x2; - let y = m * (x1 - x) - y1; + let x = lamda * lamda - x_p - x_q; + let y = lamda * (x_p - x) - y_p; Point { x, y } } From 70b5eda50e60aacde3cc8dafe9af4e9cb4db5ae0 Mon Sep 17 00:00:00 2001 From: Waylon Jepsen <57912727+0xJepsen@users.noreply.github.com> Date: Wed, 1 May 2024 13:01:13 -0700 Subject: [PATCH 2/2] fmt --- src/curve.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/curve.rs b/src/curve.rs index a43f52dd..4e23c704 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -14,7 +14,7 @@ pub struct Point { y: F, } -// Since EVERY point is either at "infinity" or not, the coproduct makes sense. +// Since EVERY point is either at "infinity" or not, the coproduct makes sense. #[derive(Clone, Copy)] pub enum PointOrInfinity { Point(Point),