Skip to content

Commit e5102ca

Browse files
committed
added bn254 to test curves
1 parent 329016e commit e5102ca

File tree

8 files changed

+144
-0
lines changed

8 files changed

+144
-0
lines changed

test-curves/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ bn384_small_two_adicity_scalar_field = []
4747
bn384_small_two_adicity_base_field = []
4848
bn384_small_two_adicity_curve = [ "bn384_small_two_adicity_scalar_field", "bn384_small_two_adicity_base_field" ]
4949

50+
bn254_scalar_field = []
51+
bn254_base_field = []
52+
bn254 = [ "bn254_scalar_field", "bn254_base_field" ]
53+
5054
secp256k1 = []
5155

5256
[[bench]]
@@ -79,3 +83,9 @@ name = "field_mul_u64"
7983
path = "benches/field_mul_u64.rs"
8084
harness = false
8185
required-features = ["secp256k1"]
86+
87+
[[bench]]
88+
name = "bn254"
89+
path = "benches/bn254.rs"
90+
harness = false
91+
required-features = ["bn254"]

test-curves/benches/bn254.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use ark_algebra_bench_templates::{bench, criterion_main, field_common, paste, prime_field, sqrt};
2+
// Import BN254 types
3+
use ark_test_curves::bn254::{Fq, Fr, G1Projective as G1};
4+
5+
// Instantiate benchmarks for BN254
6+
bench!(Name = "BN254", Group = G1, ScalarField = Fr, BaseField = Fq,);

test-curves/src/bn254/fq.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use ark_ff::fields::{Fp256, MontBackend, MontConfig};
2+
3+
/// Defines the parameters for the base field `Fq` of the BN254 curve.
4+
#[derive(MontConfig)]
5+
#[modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583"]
6+
#[generator = "3"]
7+
pub struct FqConfig;
8+
9+
/// The base field `Fq` of the BN254 curve.
10+
pub type Fq = Fp256<MontBackend<FqConfig, 4>>;
11+
12+
pub const FQ_ONE: Fq = ark_ff::MontFp!("1");
13+
pub const FQ_ZERO: Fq = ark_ff::MontFp!("0");

test-curves/src/bn254/fr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use ark_ff::fields::{Fp256, MontBackend, MontConfig};
2+
// use ark_ff_macros::{MontConfig, MontFp}; // Keep MontFp for constants
3+
4+
/// Defines the parameters for the scalar field `Fr` of the BN254 curve.
5+
#[derive(MontConfig)]
6+
#[modulus = "21888242871839275222246405745257275088548364400416034343698204186575808495617"]
7+
#[generator = "5"]
8+
// Define Small subgroup base/power if needed, otherwise macro defaults are fine
9+
// #[small_subgroup_base = "3"]
10+
// #[small_subgroup_power = "2"]
11+
pub struct FrConfig;
12+
13+
/// The scalar field `Fr` of the BN254 curve.
14+
pub type Fr = Fp256<MontBackend<FrConfig, 4>>;
15+
16+
pub const FR_ONE: Fr = ark_ff::MontFp!("1");
17+
pub const FR_ZERO: Fr = ark_ff::MontFp!("0");

test-curves/src/bn254/g1.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use ark_ec::models::short_weierstrass::{
2+
Affine, Projective, SWCurveConfig,
3+
};
4+
use ark_ec::CurveConfig;
5+
use ark_ff::{Field, MontFp, Zero, AdditiveGroup};
6+
7+
use crate::bn254::{Fq, Fr}; // Assuming Fq is defined in fq.rs
8+
9+
#[derive(Clone, Default, PartialEq, Eq)]
10+
pub struct G1Config;
11+
12+
impl CurveConfig for G1Config {
13+
type BaseField = Fq;
14+
type ScalarField = Fr;
15+
16+
/// COFACTOR = 1
17+
const COFACTOR: &'static [u64] = &[1];
18+
19+
/// COFACTOR_INV = COFACTOR^{-1} mod r = 1
20+
const COFACTOR_INV: Self::ScalarField = Fr::ONE;
21+
}
22+
23+
impl SWCurveConfig for G1Config {
24+
/// COEFF_A = 0
25+
const COEFF_A: Self::BaseField = Fq::ZERO;
26+
27+
/// COEFF_B = 3
28+
const COEFF_B: Self::BaseField = MontFp!("3");
29+
30+
/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
31+
const GENERATOR: Affine<Self> = Affine::new_unchecked(G1_GENERATOR_X, G1_GENERATOR_Y);
32+
33+
#[inline(always)]
34+
fn mul_by_a(_base: Self::BaseField) -> Self::BaseField {
35+
// A is zero, avoids a multiply
36+
Self::BaseField::zero()
37+
}
38+
39+
#[inline(always)]
40+
fn is_in_correct_subgroup_assuming_on_curve(_p: &Affine<Self>) -> bool {
41+
// The cofactor is 1, so any point on the curve is in the correct subgroup.
42+
true
43+
}
44+
}
45+
46+
/// G1_GENERATOR_X = 1
47+
pub const G1_GENERATOR_X: Fq = MontFp!("1");
48+
49+
/// G1_GENERATOR_Y = 2
50+
pub const G1_GENERATOR_Y: Fq = MontFp!("2");
51+
52+
pub type G1Affine = Affine<G1Config>;
53+
pub type G1Projective = Projective<G1Config>;

test-curves/src/bn254/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[cfg(feature = "bn254_base_field")]
2+
pub mod fq;
3+
#[cfg(feature = "bn254_base_field")]
4+
pub use fq::*;
5+
6+
#[cfg(feature = "bn254_scalar_field")]
7+
pub mod fr;
8+
#[cfg(feature = "bn254_scalar_field")]
9+
pub use fr::*;
10+
11+
#[cfg(feature = "bn254")] // Use the main bn254 feature
12+
pub mod g1;
13+
#[cfg(feature = "bn254")] // Use the main bn254 feature
14+
pub use g1::*;
15+
16+
// TODO: Define G2, GT, Pairing for BN254 similarly if needed
17+
18+
#[cfg(test)]
19+
mod test; // Renamed from tests to test.rs

test-curves/src/bn254/test.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(unused_imports)]
2+
use ark_ec::{
3+
models::short_weierstrass::SWCurveConfig, // Keep this as G1 is SW
4+
pairing::Pairing,
5+
AffineRepr, CurveGroup, PrimeGroup,
6+
};
7+
use ark_ff::{Field, One, UniformRand, Zero};
8+
use ark_std::{rand::Rng, test_rng};
9+
10+
// Add imports for the newly defined types
11+
use crate::bn254::{Fq, FqConfig, Fr, FrConfig, G1Affine, G1Projective};
12+
13+
use ark_algebra_test_templates::*;
14+
use ark_std::ops::{AddAssign, MulAssign, SubAssign};
15+
16+
test_field!(fr; Fr; mont_prime_field);
17+
// Uncomment Fq test
18+
test_field!(fq; Fq; mont_prime_field);
19+
20+
// Uncomment G1 test for Short Weierstrass
21+
test_group!(g1; G1Projective; sw);
22+
23+
// Add other tests for G2, Pairing etc. as needed

test-curves/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ pub mod bn384_small_two_adicity;
3030
#[cfg(feature = "secp256k1")]
3131
pub mod secp256k1;
3232

33+
#[cfg(feature = "bn254")]
34+
pub mod bn254;
35+
3336
pub mod fp128;

0 commit comments

Comments
 (0)