From 520f6ee8367437d47e412f4cf3a220644f15ba7e Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Wed, 1 May 2024 12:09:58 -0700 Subject: [PATCH] curve + point structs (#5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * curve + point structs * Squashed commit of the following: commit 7a7a0db7e8ebb313ab79df8f4e7f17c6f953a64d Author: Thor 🪁 <7041313+thor314@users.noreply.github.com> Date: Wed May 1 12:00:43 2024 -0700 plonky3 deps added (#4) * dependencies added * cargo fmt commit 307e995b05f9f68c734452e37f61a67c63a1a1d2 Merge: d8f688b 344c2bd Author: Thor 🪁 <7041313+thor314@users.noreply.github.com> Date: Wed May 1 11:52:54 2024 -0700 Merge pull request #2 from pluto/dependabot/github_actions/dependabot/fetch-metadata-2 Bump dependabot/fetch-metadata from 1 to 2 commit 344c2bdf8f1bcf8aa420cf5d7c825b3e66e14cc5 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed May 1 18:29:21 2024 +0000 Bump dependabot/fetch-metadata from 1 to 2 Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1 to 2. - [Release notes](https://github.com/dependabot/fetch-metadata/releases) - [Commits](https://github.com/dependabot/fetch-metadata/compare/v1...v2) --- updated-dependencies: - dependency-name: dependabot/fetch-metadata dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --------- Co-authored-by: Thor Kampefner --- Cargo.toml | 8 ++--- ronkathon/src/curve.rs | 70 ++++++++++++++++++++++++++++++++++++++++++ ronkathon/src/lib.rs | 2 ++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 ronkathon/src/curve.rs diff --git a/Cargo.toml b/Cargo.toml index 2af310f..04dccd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,4 @@ [workspace] -resolver = "2" +resolver="2" -members = [ - "ronkathon", - "field", - "util" -] +members=["ronkathon", "field", "util"] diff --git a/ronkathon/src/curve.rs b/ronkathon/src/curve.rs new file mode 100644 index 0000000..3b1ae20 --- /dev/null +++ b/ronkathon/src/curve.rs @@ -0,0 +1,70 @@ +use p3_field::{AbstractField, Field}; + +/// Elliptic curve in Weierstrass form: y^2 = x^3 + ax + b +pub struct Curve { + pub a: F, + pub b: F, + three: F, + two: F, +} + +#[derive(Clone, Copy)] +pub struct Point { + x: F, + y: F, +} + +#[derive(Clone, Copy)] +pub enum PointOrInfinity { + Point(Point), + Infinity, +} + +impl Curve { + pub fn new(a: F, b: F) -> Self { + Self { + a, + b, + three: ::from_canonical_u8(3), + two: ::from_canonical_u8(2), + } + } + + pub fn negate(&self, p: PointOrInfinity) -> PointOrInfinity { + match p { + PointOrInfinity::Point(p) => PointOrInfinity::Point(Point { x: p.x, y: -p.y }), + PointOrInfinity::Infinity => PointOrInfinity::Infinity, + } + } + + pub fn add(&self, p: PointOrInfinity, q: PointOrInfinity) -> PointOrInfinity { + match (p, q) { + (PointOrInfinity::Infinity, _) => q, + (_, PointOrInfinity::Infinity) => p, + (PointOrInfinity::Point(p), PointOrInfinity::Point(q)) => { + let r = self.add_points(p, q); + PointOrInfinity::Point(Point { x: r.x, y: r.y }) + }, + } + } + + fn add_points(&self, p: Point, q: Point) -> Point { + let (x1, y1) = (p.x, p.y); + let (x2, y2) = (q.x, q.y); + + if x1 == x2 && y1 == -y2 { + return Point { x: F::zero(), y: F::zero() }; + } + + let m = if x1 == x2 && y1 == y2 { + (self.three * x1 * x1 + self.a) / (self.two * y1) + } else { + (y2 - y1) / (x2 - x1) + }; + + let x = m * m - x1 - x2; + let y = m * (x1 - x) - y1; + + Point { x, y } + } +} diff --git a/ronkathon/src/lib.rs b/ronkathon/src/lib.rs index f6e899f..94dbbf9 100644 --- a/ronkathon/src/lib.rs +++ b/ronkathon/src/lib.rs @@ -5,3 +5,5 @@ #![allow(non_snake_case)] #![allow(clippy::clone_on_copy)] #![allow(unused_mut)] + +pub mod curve;