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;