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

chore: Remove AffineCoords trait #1265

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 0 additions & 24 deletions extensions/pairing/guest/src/affine_point.rs

This file was deleted.

Empty file.
80 changes: 3 additions & 77 deletions extensions/pairing/guest/src/halo2curves_shims/bls12_381/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ pub use line::*;
#[cfg(test)]
pub mod tests;

use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2, G1Affine, G2Affine};
use openvm_algebra_guest::field::{Field, FieldExtension};
use rand::Rng;
use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2};
use openvm_algebra_guest::field::FieldExtension;

use crate::{
affine_point::AffineCoords,
pairing::{Evaluatable, EvaluatedLine, FromLineMType, UnevaluatedLine},
};
use crate::pairing::{Evaluatable, EvaluatedLine, FromLineMType, UnevaluatedLine};

impl FromLineMType<Fq2> for Fq12 {
fn from_evaluated_line_m_type(line: EvaluatedLine<Fq2>) -> Fq12 {
Expand All @@ -40,73 +36,3 @@ impl Evaluatable<Fq, Fq2> for UnevaluatedLine<Fq2> {
}
}
}

impl AffineCoords<Fq> for G1Affine {
fn new(x: Fq, y: Fq) -> Self {
let mut m = G1Affine::identity();
m.x = Fq::ONE * x;
m.y = y;
m
}

fn x(&self) -> Fq {
self.x
}

fn y(&self) -> Fq {
self.y
}

fn neg(&self) -> Self {
let mut pt = *self;
pt.y = -pt.y;
pt
}

fn random(rng: &mut impl Rng) -> Self {
G1Affine::random(rng)
}

fn generator() -> Self {
G1Affine::generator()
}

fn is_infinity(&self) -> bool {
self.x == Fq::ZERO && self.y == Fq::ZERO
}
}

impl AffineCoords<Fq2> for G2Affine {
fn new(x: Fq2, y: Fq2) -> Self {
let mut m = G2Affine::identity();
m.x = Fq2::ONE * x;
m.y = y;
m
}

fn x(&self) -> Fq2 {
self.x
}

fn y(&self) -> Fq2 {
self.y
}

fn neg(&self) -> Self {
let mut pt = *self;
pt.y = -pt.y;
pt
}

fn random(rng: &mut impl Rng) -> Self {
G2Affine::random(rng)
}

fn generator() -> Self {
G2Affine::generator()
}

fn is_infinity(&self) -> bool {
self.x == Fq2::ZERO && self.y == Fq2::ZERO
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use alloc::vec::Vec;
use core::mem::transmute;

use halo2curves_axiom::bls12_381::{Fq12, MillerLoopResult};
use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2, G1Affine, G2Affine, MillerLoopResult};
use itertools::izip;
use num_bigint::BigUint;
use num_traits::Pow;
use openvm_algebra_guest::ExpBytes;
use openvm_ecc_guest::AffinePoint;
use rand::{rngs::StdRng, SeedableRng};

use crate::bls12_381::{BLS12_381_MODULUS, BLS12_381_ORDER};

Expand All @@ -29,3 +33,34 @@ pub fn assert_miller_results_eq(a: MillerLoopResult, b: Fq12) {
let a = unsafe { transmute::<MillerLoopResult, Fq12>(a) };
assert_eq!(final_exp(a), final_exp(b));
}

#[allow(non_snake_case)]
#[allow(clippy::type_complexity)]
pub fn generate_test_points_bls12_381(
rand_seeds: &[u64],
) -> (
Vec<G1Affine>,
Vec<G2Affine>,
Vec<AffinePoint<Fq>>,
Vec<AffinePoint<Fq2>>,
) {
let (P_vec, Q_vec) = rand_seeds
.iter()
.map(|seed| {
let mut rng0 = StdRng::seed_from_u64(*seed);
let p = G1Affine::random(&mut rng0);
let mut rng1 = StdRng::seed_from_u64(*seed * 2);
let q = G2Affine::random(&mut rng1);
(p, q)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
let (P_ecpoints, Q_ecpoints) = izip!(P_vec.clone(), Q_vec.clone())
.map(|(P, Q)| {
(
AffinePoint { x: P.x, y: P.y },
AffinePoint { x: Q.x, y: Q.y },
)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
(P_vec, Q_vec, P_ecpoints, Q_ecpoints)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
use alloc::vec::Vec;

use halo2curves_axiom::bls12_381::{Fq, Fq12, Fq2, G1Affine, G2Affine, G2Prepared};
use halo2curves_axiom::bls12_381::{Fq12, G1Affine, G2Affine, G2Prepared};
use openvm_ecc_guest::{algebra::Field, AffinePoint};
use rand::{rngs::StdRng, SeedableRng};
use subtle::ConditionallySelectable;

use super::generate_test_points_bls12_381;
use crate::{
halo2curves_shims::{
bls12_381::{
tests::{assert_miller_results_eq, final_exp},
Bls12_381,
},
tests::utils::generate_test_points,
halo2curves_shims::bls12_381::{
tests::{assert_miller_results_eq, final_exp},
Bls12_381,
},
pairing::{Evaluatable, LineMulMType, MillerStep, MultiMillerLoop},
};

#[allow(non_snake_case)]
fn run_miller_loop_test(rand_seeds: &[u64]) {
let (P_vec, Q_vec, P_ecpoints, Q_ecpoints) =
generate_test_points::<G1Affine, G2Affine, Fq, Fq2>(rand_seeds);
let (P_vec, Q_vec, P_ecpoints, Q_ecpoints) = generate_test_points_bls12_381(rand_seeds);

// Compare against halo2curves implementation
let g2_prepareds = Q_vec
Expand Down
74 changes: 3 additions & 71 deletions extensions/pairing/guest/src/halo2curves_shims/bn254/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ pub use line::*;
#[cfg(test)]
pub mod tests;

use halo2curves_axiom::bn256::{Fq, Fq12, Fq2, G1Affine, G2Affine};
use openvm_algebra_guest::field::{Field, FieldExtension};
use rand::Rng;
use halo2curves_axiom::bn256::{Fq, Fq12, Fq2};
use openvm_algebra_guest::field::FieldExtension;

use crate::{
affine_point::AffineCoords,
pairing::{Evaluatable, EvaluatedLine, FromLineDType, UnevaluatedLine},
};
use crate::pairing::{Evaluatable, EvaluatedLine, FromLineDType, UnevaluatedLine};

impl FromLineDType<Fq2> for Fq12 {
fn from_evaluated_line_d_type(line: EvaluatedLine<Fq2>) -> Fq12 {
Expand All @@ -41,70 +37,6 @@ impl Evaluatable<Fq, Fq2> for UnevaluatedLine<Fq2> {
}
}

impl AffineCoords<Fq> for G1Affine {
fn new(x: Fq, y: Fq) -> Self {
G1Affine { x, y }
}

fn x(&self) -> Fq {
self.x
}

fn y(&self) -> Fq {
self.y
}

fn neg(&self) -> Self {
let mut pt = *self;
pt.y = -pt.y;
pt
}

fn random(rng: &mut impl Rng) -> Self {
G1Affine::random(rng)
}

fn generator() -> Self {
G1Affine::generator()
}

fn is_infinity(&self) -> bool {
self.x == Fq::ZERO && self.y == Fq::ZERO
}
}

impl AffineCoords<Fq2> for G2Affine {
fn new(x: Fq2, y: Fq2) -> Self {
G2Affine { x, y }
}

fn x(&self) -> Fq2 {
self.x
}

fn y(&self) -> Fq2 {
self.y
}

fn neg(&self) -> Self {
let mut pt = *self;
pt.y = -pt.y;
pt
}

fn random(rng: &mut impl Rng) -> Self {
G2Affine::random(rng)
}

fn generator() -> Self {
G2Affine::generator()
}

fn is_infinity(&self) -> bool {
self.x == Fq2::ZERO && self.y == Fq2::ZERO
}
}

#[cfg(target_os = "zkvm")]
use {
axvm_platform::constants::{Custom1Funct3, SwBaseFunct7, CUSTOM_1},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use alloc::vec::Vec;
use core::mem::transmute;

use halo2curves_axiom::{
bn256::{Fq12, Gt},
bn256::{Fq, Fq12, Fq2, G1Affine, G2Affine, Gt},
pairing::MillerLoopResult,
};
use itertools::izip;
use num_bigint::BigUint;
use num_traits::Pow;
use openvm_algebra_guest::ExpBytes;
use openvm_ecc_guest::AffinePoint;
use rand::{rngs::StdRng, SeedableRng};

use crate::bn254::{BN254_MODULUS, BN254_ORDER};

Expand All @@ -31,3 +35,34 @@ pub fn assert_miller_results_eq(a: Gt, b: Fq12) {
let b = final_exp(b);
assert_eq!(unsafe { transmute::<Gt, Fq12>(a) }, b);
}

#[allow(non_snake_case)]
#[allow(clippy::type_complexity)]
pub fn generate_test_points_bn254(
rand_seeds: &[u64],
) -> (
Vec<G1Affine>,
Vec<G2Affine>,
Vec<AffinePoint<Fq>>,
Vec<AffinePoint<Fq2>>,
) {
let (P_vec, Q_vec) = rand_seeds
.iter()
.map(|seed| {
let mut rng0 = StdRng::seed_from_u64(*seed);
let p = G1Affine::random(&mut rng0);
let mut rng1 = StdRng::seed_from_u64(*seed * 2);
let q = G2Affine::random(&mut rng1);
(p, q)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
let (P_ecpoints, Q_ecpoints) = izip!(P_vec.clone(), Q_vec.clone())
.map(|(P, Q)| {
(
AffinePoint { x: P.x, y: P.y },
AffinePoint { x: Q.x, y: Q.y },
)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
(P_vec, Q_vec, P_ecpoints, Q_ecpoints)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use core::ops::Neg;

use halo2curves_axiom::bn256::{Fq, Fq2, Fr, G1Affine, G2Affine};
use itertools::izip;
Expand All @@ -7,7 +8,6 @@ use num_traits::Num;
use openvm_ecc_guest::{algebra::ExpBytes, AffinePoint};

use crate::{
affine_point::AffineCoords,
halo2curves_shims::bn254::Bn254,
pairing::{FinalExp, MultiMillerLoop},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use alloc::vec::Vec;

use halo2curves_axiom::bn256::{Fq, Fq2, G1Affine, G2Affine, G2Prepared};
use halo2curves_axiom::bn256::G2Prepared;

use super::assert_miller_results_eq;
use crate::{
halo2curves_shims::{bn254::Bn254, tests::utils::generate_test_points},
pairing::MultiMillerLoop,
};
use super::{assert_miller_results_eq, generate_test_points_bn254};
use crate::{halo2curves_shims::bn254::Bn254, pairing::MultiMillerLoop};

#[allow(non_snake_case)]
fn run_miller_loop_test(rand_seeds: &[u64]) {
let (P_vec, Q_vec, P_ecpoints, Q_ecpoints) =
generate_test_points::<G1Affine, G2Affine, Fq, Fq2>(rand_seeds);
let (P_vec, Q_vec, P_ecpoints, Q_ecpoints) = generate_test_points_bn254(rand_seeds);

// Compare against halo2curves implementation
let g2_prepareds = Q_vec
Expand Down
3 changes: 0 additions & 3 deletions extensions/pairing/guest/src/halo2curves_shims/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod bls12_381;
pub mod bn254;

#[cfg(test)]
mod tests;
2 changes: 0 additions & 2 deletions extensions/pairing/guest/src/halo2curves_shims/tests/mod.rs

This file was deleted.

Loading