Skip to content

Commit aae3e77

Browse files
committed
merge field group and subgroup check
2 parents 9205ecf + 36f33d7 commit aae3e77

File tree

4 files changed

+284
-44
lines changed

4 files changed

+284
-44
lines changed

g16ckt/examples/pairing_gate_counts.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ fn main() {
211211
move || {
212212
run_and_print("test_ell_montgomery", inputs, move |ctx, w| {
213213
let f0 = fq12_one_const();
214-
let coeffs = pairing::ell_coeffs_montgomery(ctx, &w.g2);
214+
// ignoring _is_valid because this function is used only for benchmarking over valid inputs
215+
let (coeffs, _is_valid) = pairing::ell_coeffs_montgomery(ctx, &w.g2);
215216
// Take first coeff triple and evaluate once
216217
let c = coeffs.into_iter().next().unwrap();
217218
let _f1 = pairing::ell_montgomery(ctx, &f0, &c, &w.g1);

g16ckt/src/gadgets/bn254/g2.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ impl WiresArity for DecompressedG2Wires {
809809

810810
#[cfg(test)]
811811
mod tests {
812-
use ark_ec::{CurveGroup, PrimeGroup, VariableBaseMSM};
812+
use ark_ec::{AffineRepr, CurveGroup, PrimeGroup, VariableBaseMSM};
813813
use ark_ff::{Field, UniformRand};
814814
use ark_serialize::CanonicalSerialize;
815815
use rand::{Rng, SeedableRng};
@@ -1285,4 +1285,37 @@ mod tests {
12851285
});
12861286
assert_eq!(out.output_value[0], ref_is_on_curve);
12871287
}
1288+
1289+
#[test]
1290+
fn test_cofactor_clearing() {
1291+
let mut rng = ChaCha20Rng::seed_from_u64(112);
1292+
for _ in 0..5 {
1293+
// sufficient sample size to sample both valid and invalid points
1294+
let x = ark_bn254::Fq2::rand(&mut rng);
1295+
let a1 = ark_bn254::Fq2::sqrt(&((x * x * x) + ark_bn254::g2::Config::COEFF_B));
1296+
let (y, ref_is_valid) = if let Some(a1) = a1 {
1297+
// if it is possible to take square root, you have found correct y,
1298+
(a1, true)
1299+
} else {
1300+
// else generate some random value
1301+
(ark_bn254::Fq2::rand(&mut rng), false)
1302+
};
1303+
let pt = ark_bn254::G2Affine::new_unchecked(x, y);
1304+
1305+
let pt = pt.into_group();
1306+
const COFACTOR: &[u64] = &[
1307+
0x345f2299c0f9fa8d,
1308+
0x06ceecda572a2489,
1309+
0xb85045b68181585e,
1310+
0x30644e72e131a029,
1311+
];
1312+
let pt = pt.mul_bigint(COFACTOR);
1313+
let pt = pt.into_affine();
1314+
// if it's a valid point, it should be on curve and subgroup (after cofactor clearing)
1315+
assert_eq!(
1316+
ref_is_valid,
1317+
pt.is_on_curve() && pt.is_in_correct_subgroup_assuming_on_curve()
1318+
);
1319+
}
1320+
}
12881321
}

0 commit comments

Comments
 (0)