Skip to content

Commit

Permalink
curve + point structs (#5)
Browse files Browse the repository at this point in the history
* curve + point structs

* Squashed commit of the following:

commit 7a7a0db
Author: Thor 🪁 <[email protected]>
Date:   Wed May 1 12:00:43 2024 -0700

    plonky3 deps added (#4)

    * dependencies added

    * cargo fmt

commit 307e995
Merge: d8f688b 344c2bd
Author: Thor 🪁 <[email protected]>
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 344c2bd
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](dependabot/fetch-metadata@v1...v2)

    ---
    updated-dependencies:
    - dependency-name: dependabot/fetch-metadata
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...

    Signed-off-by: dependabot[bot] <[email protected]>

---------

Co-authored-by: Thor Kampefner <[email protected]>
  • Loading branch information
Autoparallel and thor314 authored May 1, 2024
1 parent 7a7a0db commit 520f6ee
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[workspace]
resolver = "2"
resolver="2"

members = [
"ronkathon",
"field",
"util"
]
members=["ronkathon", "field", "util"]
70 changes: 70 additions & 0 deletions ronkathon/src/curve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use p3_field::{AbstractField, Field};

/// Elliptic curve in Weierstrass form: y^2 = x^3 + ax + b
pub struct Curve<F: Field> {
pub a: F,
pub b: F,
three: F,
two: F,
}

#[derive(Clone, Copy)]
pub struct Point<F: Field> {
x: F,
y: F,
}

#[derive(Clone, Copy)]
pub enum PointOrInfinity<F: Field> {
Point(Point<F>),
Infinity,
}

impl<F: Field> Curve<F> {
pub fn new(a: F, b: F) -> Self {
Self {
a,
b,
three: <F as AbstractField>::from_canonical_u8(3),
two: <F as AbstractField>::from_canonical_u8(2),
}
}

pub fn negate(&self, p: PointOrInfinity<F>) -> PointOrInfinity<F> {
match p {
PointOrInfinity::Point(p) => PointOrInfinity::Point(Point { x: p.x, y: -p.y }),
PointOrInfinity::Infinity => PointOrInfinity::Infinity,
}
}

pub fn add(&self, p: PointOrInfinity<F>, q: PointOrInfinity<F>) -> PointOrInfinity<F> {
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<F>, q: Point<F>) -> Point<F> {
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 }
}
}
2 changes: 2 additions & 0 deletions ronkathon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#![allow(non_snake_case)]
#![allow(clippy::clone_on_copy)]
#![allow(unused_mut)]

pub mod curve;

0 comments on commit 520f6ee

Please sign in to comment.