Skip to content

Commit

Permalink
chore: cleanup curve module (#59)
Browse files Browse the repository at this point in the history
* chore: cleanup `curve` module

* tests: g2_curve

* idea: not sure if this is better or not, I prefer it?

* clippy + fmt
  • Loading branch information
Autoparallel authored May 13, 2024
1 parent 7145ba3 commit e09a73a
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 260 deletions.
117 changes: 0 additions & 117 deletions src/curves/g1_curve.rs

This file was deleted.

98 changes: 0 additions & 98 deletions src/curves/g2_curve.rs

This file was deleted.

52 changes: 24 additions & 28 deletions src/curves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,43 @@
use super::*;

pub mod g1_curve;
pub mod g2_curve;
pub mod pluto_curve;

/// Elliptic curve in Weierstrass form: `y^2 = x^3 + ax + b`
pub struct Curve<F: FiniteField> {
/// Coefficient `a` in the Weierstrass equation of this elliptic curve.
pub a: F,

/// Coefficient `b` in the Weierstrass equation of this elliptic curve.
pub b: F,

_three: F,
_two: F,
}

// TODO: This should probably have a `type ScalarField`.
/// Elliptic curve parameters for a curve over a finite field in Weierstrass form
/// `y^2 = x^3 + ax + b`
pub trait CurveParams: 'static + Copy + Clone + fmt::Debug + Default + Eq + Ord {
pub trait EllipticCurve: Copy {
/// The field for the curve coefficients.
type Coefficient: FiniteField + Into<Self::BaseField>;

/// Integer field element type
type BaseField: FiniteField + Neg + Mul;
type BaseField: FiniteField;

/// Order of this elliptic curve, i.e. number of elements in the scalar field.
const ORDER: u32;

/// Coefficient `a` in the Weierstrass equation of this elliptic curve.
const EQUATION_A: Self::BaseField;
const EQUATION_A: Self::Coefficient;

/// Coefficient `b` in the Weierstrass equation of this elliptic curve.
const EQUATION_B: Self::BaseField;
const EQUATION_B: Self::Coefficient;

/// Generator of this elliptic curve.
const GENERATOR: (Self::BaseField, Self::BaseField);
}

// TODO: A potential issue here is that you can have a point that is not on the curve created via
// this enum. This is a potential issue that should be addressed.
/// An Affine Coordinate Point on a Weierstrass elliptic curve
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum AffinePoint<C: CurveParams> {
pub enum AffinePoint<C: EllipticCurve> {
/// A point on the curve.
PointOnCurve(C::BaseField, C::BaseField),

/// The point at infinity.
Infinity,
}

impl<C: CurveParams> AffinePoint<C> {
impl<C: EllipticCurve> AffinePoint<C> {
/// Create a new point on the curve so long as it satisfies the curve equation.
///
/// ## Panics
Expand All @@ -57,15 +48,19 @@ impl<C: CurveParams> AffinePoint<C> {
// y = 31x -> y^2 = 52x^2
// x = 36 -> x^3 = 95 + 3
// 52x^2 = 98 ???
assert_eq!(y * y, x * x * x + C::EQUATION_A * x + C::EQUATION_B, "Point is not on curve");
assert_eq!(
y * y,
x * x * x + C::EQUATION_A.into() * x + C::EQUATION_B.into(),
"Point is not on curve"
);
Self::PointOnCurve(x, y)
}
}

// Example:
// Base

impl<C: CurveParams> Neg for AffinePoint<C> {
impl<C: EllipticCurve> Neg for AffinePoint<C> {
type Output = AffinePoint<C>;

fn neg(self) -> Self::Output {
Expand All @@ -79,7 +74,7 @@ impl<C: CurveParams> Neg for AffinePoint<C> {

// TODO: This should likely use a `Self::ScalarField` instead of `u32`.
/// Scalar multiplication on the rhs: P*(u32)
impl<C: CurveParams> Mul<u32> for AffinePoint<C> {
impl<C: EllipticCurve> Mul<u32> for AffinePoint<C> {
type Output = AffinePoint<C>;

fn mul(self, scalar: u32) -> Self::Output {
Expand All @@ -99,7 +94,7 @@ impl<C: CurveParams> Mul<u32> for AffinePoint<C> {
}

/// Scalar multiplication on the Lhs (u32)*P
impl<C: CurveParams> std::ops::Mul<AffinePoint<C>> for u32 {
impl<C: EllipticCurve> std::ops::Mul<AffinePoint<C>> for u32 {
type Output = AffinePoint<C>;

fn mul(self, _rhs: AffinePoint<C>) -> Self::Output {
Expand All @@ -118,7 +113,7 @@ impl<C: CurveParams> std::ops::Mul<AffinePoint<C>> for u32 {
}
}

impl<C: CurveParams> Add for AffinePoint<C> {
impl<C: EllipticCurve> Add for AffinePoint<C> {
type Output = AffinePoint<C>;

fn add(self, rhs: Self) -> Self::Output {
Expand All @@ -143,7 +138,8 @@ impl<C: CurveParams> Add for AffinePoint<C> {
// compute new point using elliptic curve point group law
// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
let lambda = if x1 == x2 && y1 == y2 {
((C::BaseField::TWO + C::BaseField::ONE) * x1 * x1 + C::EQUATION_A) / (C::BaseField::TWO * y1)
((C::BaseField::TWO + C::BaseField::ONE) * x1 * x1 + C::EQUATION_A.into())
/ (C::BaseField::TWO * y1)
} else {
(y2 - y1) / (x2 - x1)
};
Expand All @@ -154,7 +150,7 @@ impl<C: CurveParams> Add for AffinePoint<C> {
}

// NOTE: Apparently there is a faster way to do this with twisted curve methods
impl<C: CurveParams> AffinePoint<C> {
impl<C: EllipticCurve> AffinePoint<C> {
/// Compute the point doubling operation on this point.
pub fn point_doubling(self) -> AffinePoint<C> {
let (x, y) = match self {
Expand Down
Loading

0 comments on commit e09a73a

Please sign in to comment.