Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: general extensions and documentation #58

Merged
merged 8 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions src/curves/g1_curve.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
//! Elliptic curve `y^2=x^3+3`` over the prime field GF(101).

use super::*;

// TODO: An empty struct like this and G2Curve is a code smell. We should probably
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a design @thor314 had taken I believe from artworks? (correct me if I am wrong). I am okay thinking about a single struct too.

Copy link
Contributor Author

@Autoparallel Autoparallel May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could at least do a single struct like

struct CurveGroup<F: FiniteField> {
    _phantom: F,
}

This way you can just use one struct and define it over whatever field you'd like.

I think there's probably something even nicer we could do to better utilize the Curve as we have as of now.

// have a single struct that can be instantiated with the correct parameters (i.e., just
// allows for extension fields.).

/// The Elliptic curve $y^2=x^3+3$, i.e.
/// - a = 0
/// - b = 3
/// - generator todo
/// - order todo
/// - field element type todo, but mock with u64 - bad thor, u64 does not implement p3_field
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct C101;
pub struct G1Curve;

impl CurveParams for C101 {
type FieldElement = GF101;
impl CurveParams for G1Curve {
type BaseField = GF101;

const EQUATION_A: Self::FieldElement = GF101::new(0);
const EQUATION_B: Self::FieldElement = GF101::new(3);
const GENERATOR: (Self::FieldElement, Self::FieldElement) = (GF101::new(1), Self::TWO);
const EQUATION_A: Self::BaseField = GF101::new(0);
const EQUATION_B: Self::BaseField = GF101::new(3);
const GENERATOR: (Self::BaseField, Self::BaseField) = (GF101::ONE, GF101::TWO);
const ORDER: u32 = GF101::ORDER;
const THREE: Self::FieldElement = GF101::new(3);
const TWO: Self::FieldElement = GF101::new(2);
}

#[cfg(test)]
Expand All @@ -26,37 +30,37 @@ mod tests {

#[test]
fn point_doubling() {
let g = AffinePoint::<C101>::generator();
let g = AffinePoint::<G1Curve>::generator();

let two_g = g.point_doubling();
let expected_2g = AffinePoint::<C101>::new(GF101::new(68), GF101::new(74));
let expected_negative_2g = AffinePoint::<C101>::new(GF101::new(68), GF101::new(27));
let expected_2g = AffinePoint::<G1Curve>::new(GF101::new(68), GF101::new(74));
let expected_negative_2g = AffinePoint::<G1Curve>::new(GF101::new(68), GF101::new(27));
assert_eq!(two_g, expected_2g);
assert_eq!(-two_g, expected_negative_2g);

let four_g = two_g.point_doubling();
let expected_4g = AffinePoint::<C101>::new(GF101::new(65), GF101::new(98));
let expected_negative_4g = AffinePoint::<C101>::new(GF101::new(65), GF101::new(3));
let expected_4g = AffinePoint::<G1Curve>::new(GF101::new(65), GF101::new(98));
let expected_negative_4g = AffinePoint::<G1Curve>::new(GF101::new(65), GF101::new(3));
assert_eq!(four_g, expected_4g);
assert_eq!(-four_g, expected_negative_4g);

let eight_g = four_g.point_doubling();
let expected_8g = AffinePoint::<C101>::new(GF101::new(18), GF101::new(49));
let expected_negative_8g = AffinePoint::<C101>::new(GF101::new(18), GF101::new(52));
let expected_8g = AffinePoint::<G1Curve>::new(GF101::new(18), GF101::new(49));
let expected_negative_8g = AffinePoint::<G1Curve>::new(GF101::new(18), GF101::new(52));
assert_eq!(eight_g, expected_8g);
assert_eq!(-eight_g, expected_negative_8g);

let sixteen_g = eight_g.point_doubling();
let expected_16g = AffinePoint::<C101>::new(GF101::new(1), GF101::new(99));
let expected_negative_16g = AffinePoint::<C101>::new(GF101::new(1), GF101::new(2));
let expected_16g = AffinePoint::<G1Curve>::new(GF101::new(1), GF101::new(99));
let expected_negative_16g = AffinePoint::<G1Curve>::new(GF101::new(1), GF101::new(2));
assert_eq!(sixteen_g, expected_16g);
assert_eq!(-sixteen_g, expected_negative_16g);
assert_eq!(g, -sixteen_g);
}

#[test]
fn order_17() {
let g = AffinePoint::<C101>::generator();
let g = AffinePoint::<G1Curve>::generator();
let mut g_double = g.point_doubling();
let mut count = 2;
while g_double != g && -g_double != g {
Expand All @@ -68,34 +72,34 @@ mod tests {

#[test]
fn point_addition() {
let g = AffinePoint::<C101>::generator();
let g = AffinePoint::<G1Curve>::generator();
let two_g = g.point_doubling();
let three_g = g + two_g;
let expected_3g = AffinePoint::<C101>::new(GF101::new(26), GF101::new(45));
let expected_negative_3g = AffinePoint::<C101>::new(GF101::new(26), GF101::new(56));
let expected_3g = AffinePoint::<G1Curve>::new(GF101::new(26), GF101::new(45));
let expected_negative_3g = AffinePoint::<G1Curve>::new(GF101::new(26), GF101::new(56));
assert_eq!(three_g, expected_3g);
assert_eq!(-three_g, expected_negative_3g);

let four_g = g + three_g;
let expected_4g = AffinePoint::<C101>::new(GF101::new(65), GF101::new(98));
let expected_negative_4g = AffinePoint::<C101>::new(GF101::new(65), GF101::new(3));
let expected_4g = AffinePoint::<G1Curve>::new(GF101::new(65), GF101::new(98));
let expected_negative_4g = AffinePoint::<G1Curve>::new(GF101::new(65), GF101::new(3));
assert_eq!(four_g, expected_4g);
assert_eq!(-four_g, expected_negative_4g);

let five_g = g + four_g;
let expected_5g = AffinePoint::<C101>::new(GF101::new(12), GF101::new(32));
let expected_negative_5g = AffinePoint::<C101>::new(GF101::new(12), GF101::new(69));
let expected_5g = AffinePoint::<G1Curve>::new(GF101::new(12), GF101::new(32));
let expected_negative_5g = AffinePoint::<G1Curve>::new(GF101::new(12), GF101::new(69));
assert_eq!(five_g, expected_5g);
assert_eq!(-five_g, expected_negative_5g);

assert_eq!(g + AffinePoint::new_infty(), g);
assert_eq!(AffinePoint::new_infty() + g, g);
assert_eq!(g + (-g), AffinePoint::new_infty());
assert_eq!(g + AffinePoint::Infinity, g);
assert_eq!(AffinePoint::Infinity + g, g);
assert_eq!(g + (-g), AffinePoint::Infinity);
}

#[test]
fn scalar_multiplication_rhs() {
let g = AffinePoint::<C101>::generator();
let g = AffinePoint::<G1Curve>::generator();
let two_g = g * 2;
let expected_2g = g.point_doubling();
assert_eq!(two_g, expected_2g);
Expand All @@ -104,7 +108,7 @@ mod tests {

#[test]
fn scalar_multiplication_lhs() {
let g = AffinePoint::<C101>::generator();
let g = AffinePoint::<G1Curve>::generator();
let two_g = 2 * g;
let expected_2g = g.point_doubling();
assert_eq!(two_g, expected_2g);
Expand Down
43 changes: 22 additions & 21 deletions src/curves/g2_curve.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
use self::field::gf_101_2::Ext2;
//! This module implements the G2 curve which is defined over the base field `GF101` and the
//! extension field `GF101^2`.

use super::*;

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct G2Curve {}
// The Elliptic curve $y^2=x^3+3$, i.e.
// TODO: This seems unecessary?
/// A container to implement curve parameters for the G2 curve.
/// The Elliptic curve $y^2=x^3+3$, i.e.
// a = 0
// b = 3
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct G2Curve;

impl CurveParams for G2Curve {
type FieldElement = Ext2<GF101>;
type BaseField = Ext<2, GF101>;

const EQUATION_A: Self::FieldElement = Ext2::<GF101>::ZERO;
const EQUATION_B: Self::FieldElement = Ext2::<GF101>::new(GF101::new(3), GF101::ZERO);
const GENERATOR: (Self::FieldElement, Self::FieldElement) = (
Ext2::<GF101>::new(GF101::new(36), GF101::ZERO),
Ext2::<GF101>::new(GF101::ZERO, GF101::new(31)),
const EQUATION_A: Self::BaseField = Ext::<2, GF101>::ZERO;
const EQUATION_B: Self::BaseField = Ext::<2, GF101>::new([GF101::new(3), GF101::ZERO]);
const GENERATOR: (Self::BaseField, Self::BaseField) = (
Ext::<2, GF101>::new([GF101::new(36), GF101::ZERO]),
Ext::<2, GF101>::new([GF101::ZERO, GF101::new(31)]),
);
const ORDER: u32 = 289;
// extension field subgroup should have order r^2 where r is order of first group
const THREE: Ext2<GF101> = Ext2::<GF101>::new(GF101::new(3), GF101::ZERO);
const TWO: Ext2<GF101> = Ext2::<GF101>::new(GF101::TWO, GF101::ZERO);
}

// a naive impl with affine point

impl G2Curve {
pub fn on_curve(x: Ext2<GF101>, y: Ext2<GF101>) -> (Ext2<GF101>, Ext2<GF101>) {
/// Create a new point on the curve so long as it satisfies the curve equation.
pub fn on_curve(x: Ext<2, GF101>, y: Ext<2, GF101>) -> (Ext<2, GF101>, Ext<2, GF101>) {
println!("X: {:?}, Y: {:?}", x, y);
// TODO Continue working on this
// ( x ) ( y ) ( x , y )
Expand All @@ -37,12 +39,12 @@ impl G2Curve {
// check if there are any x terms, if not, element is in base field
let mut lhs = x;
let mut rhs = y;
if lhs.value[1] != GF101::ZERO {
if lhs.coeffs[1] != GF101::ZERO {
lhs = x * x * (-GF101::new(2)) - Self::EQUATION_B;
} else {
lhs = x * x * x - Self::EQUATION_B;
}
if y.value[1] != GF101::ZERO {
if y.coeffs[1] != GF101::ZERO {
// y has degree two so if there is a x -> there will be an x^2 term which we substitude with
// -2 since... TODO explain this and relationship to embedding degree
rhs *= -GF101::new(2);
Expand All @@ -57,20 +59,19 @@ impl G2Curve {
#[cfg(test)]
mod tests {
use super::*;
use crate::curves::AffinePoint;

#[test]
fn point_doubling() {
let g = AffinePoint::<G2Curve>::generator();
let two_g = g.point_doubling();

let expected_2g = AffinePoint::<G2Curve>::new(
Ext2::<GF101>::new(GF101::new(90), GF101::ZERO),
Ext2::<GF101>::new(GF101::ZERO, GF101::new(82)),
Ext::<2, GF101>::new([GF101::new(90), GF101::ZERO]),
Ext::<2, GF101>::new([GF101::ZERO, GF101::new(82)]),
);
let expected_g = AffinePoint::<G2Curve>::new(
Ext2::<GF101>::new(GF101::new(36), GF101::ZERO),
Ext2::<GF101>::new(GF101::ZERO, GF101::new(31)),
Ext::<2, GF101>::new([GF101::new(36), GF101::ZERO]),
Ext::<2, GF101>::new([GF101::ZERO, GF101::new(31)]),
);

assert_eq!(two_g, expected_2g);
Expand Down
Loading
Loading