Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed May 8, 2024
1 parent 64a1b84 commit 4001751
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 70 deletions.
14 changes: 5 additions & 9 deletions src/curves/g1_curve.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::ops::Add;

use crate::field::{gf_101::GF101, FiniteField};

use super::CurveParams;

use crate::field::{gf_101::GF101, FiniteField};

/// The Elliptic curve $y^2=x^3+3$, i.e.
/// - a = 0
Expand All @@ -26,10 +24,9 @@ impl CurveParams for C101 {
}

mod test {
use super::*;
use crate::curves::AffinePoint;

use super::*;
type F = GF101;
type F = GF101;

#[test]
fn point_doubling() {
Expand Down Expand Up @@ -67,8 +64,8 @@ type F = GF101;
let mut g_double = g.point_doubling();
let mut count = 2;
while g_double != g && -g_double != g {
g_double = g_double.point_doubling();
count *= 2;
g_double = g_double.point_doubling();
count *= 2;
}
assert_eq!(count + 1, 17);
}
Expand Down Expand Up @@ -118,4 +115,3 @@ type F = GF101;
assert_eq!(-two_g, -expected_2g);
}
}

5 changes: 2 additions & 3 deletions src/curves/g2_curve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Add;
use crate::field::{gf_101::GF101, gf_101_2::QuadraticPlutoField, ExtensionField, FiniteField};
use super::CurveParams;

use super::CurveParams;
use crate::field::{gf_101::GF101, gf_101_2::QuadraticPlutoField, ExtensionField, FiniteField};

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct G2Curve {}
Expand All @@ -19,4 +19,3 @@ impl CurveParams for G2Curve {
const THREE: Self::FieldElement = GF101::new(3);
const TWO: Self::FieldElement = GF101::new(2);
}

100 changes: 48 additions & 52 deletions src/curves/mod.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@

use std::{
fmt,
ops::{Add, Neg},
};

use crate::field::{FiniteField, gf_101::GF101};

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

pub trait CurveParams: 'static + Copy + Clone + fmt::Debug + Default + Eq + Ord {
/// Integer field element type
type FieldElement: FiniteField + Neg;
/// 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::FieldElement;
/// Coefficient `b` in the Weierstrass equation of this elliptic curve.
const EQUATION_B: Self::FieldElement;
/// Generator of this elliptic curve.
const GENERATOR: (Self::FieldElement, Self::FieldElement);
// hack: 3 and 2 to satisfy the Add<AffinePoint> trait implementation
const THREE: Self::FieldElement;
const TWO: Self::FieldElement;

// maybe Curve::uint type is diff from PCP::fieldelement type
// maybe:
// type AffinePoint;
// type ProjectivePoint;
// type Scalar;
}
fmt,
ops::{Add, Neg},
};

use crate::field::{gf_101::GF101, FiniteField};

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

/// An Affine Coordinate Point on a Weierstrass elliptic curve
pub trait CurveParams: 'static + Copy + Clone + fmt::Debug + Default + Eq + Ord {
/// Integer field element type
type FieldElement: FiniteField + Neg;
/// 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::FieldElement;
/// Coefficient `b` in the Weierstrass equation of this elliptic curve.
const EQUATION_B: Self::FieldElement;
/// Generator of this elliptic curve.
const GENERATOR: (Self::FieldElement, Self::FieldElement);
// hack: 3 and 2 to satisfy the Add<AffinePoint> trait implementation
const THREE: Self::FieldElement;
const TWO: Self::FieldElement;

// maybe Curve::uint type is diff from PCP::fieldelement type
// maybe:
// type AffinePoint;
// type ProjectivePoint;
// type Scalar;
}

/// An Affine Coordinate Point on a Weierstrass elliptic curve
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum AffinePoint<C: CurveParams> {
XY(C::FieldElement, C::FieldElement),
Expand All @@ -60,15 +59,15 @@ impl<C: CurveParams> AffinePoint<C> {
}

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

fn neg(self) -> Self::Output {
let (x, y) = match self {
AffinePoint::XY(x, y) => (x, y),
AffinePoint::Infty => panic!("Cannot double point at infinity"),
};
AffinePoint::new(x, C::FieldElement::zero() - y)
}
type Output = AffinePoint<C>;

fn neg(self) -> Self::Output {
let (x, y) = match self {
AffinePoint::XY(x, y) => (x, y),
AffinePoint::Infty => panic!("Cannot double point at infinity"),
};
AffinePoint::new(x, C::FieldElement::zero() - y)
}
}
/// Scalar multiplication on the rhs: P*(u32)
impl<C: CurveParams> std::ops::Mul<u32> for AffinePoint<C> {
Expand Down Expand Up @@ -108,7 +107,6 @@ impl<C: CurveParams> std::ops::Mul<AffinePoint<C>> for u32 {
}
result
}

}

impl<C: CurveParams> Add for AffinePoint<C> {
Expand Down Expand Up @@ -148,7 +146,6 @@ impl<C: CurveParams> Add for AffinePoint<C> {

impl<C: CurveParams> AffinePoint<C> {
pub fn point_doubling(mut self) -> AffinePoint<C> {

let (x, y) = match self {
AffinePoint::XY(x, y) => (x, y),
AffinePoint::Infty => panic!("Cannot double point at infinity"),
Expand All @@ -160,14 +157,13 @@ impl<C: CurveParams> AffinePoint<C> {
let x_new = m * m - C::TWO * x;
let y_new = m * (C::THREE * x - m * m) - y;
AffinePoint::new(x_new, y_new)

}

pub fn generator() -> Self {
let (x,y) = C::GENERATOR;
let (x, y) = C::GENERATOR;
AffinePoint::new(x, y)
}
}

pub mod g1_curve;
pub mod g2_curve;
pub mod g1_curve;
pub mod g2_curve;
12 changes: 6 additions & 6 deletions src/field/gf_101_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ mod tests {

#[test]
fn test_generator_order() {
let generator = F2::generator();
let mut x = generator;
for _ in 1..F2::ORDER {
x *= generator;
}
assert_eq!(x, F2::one());
let generator = F2::generator();
let mut x = generator;
for _ in 1..F2::ORDER {
x *= generator;
}
assert_eq!(x, F2::one());
}
}

0 comments on commit 4001751

Please sign in to comment.