Skip to content
Open
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
3 changes: 2 additions & 1 deletion g16ckt/examples/pairing_gate_counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ fn main() {
move || {
run_and_print("test_ell_montgomery", inputs, move |ctx, w| {
let f0 = fq12_one_const();
let coeffs = pairing::ell_coeffs_montgomery(ctx, &w.g2);
// ignoring _is_valid because this function is used only for benchmarking over valid inputs
let (coeffs, _is_valid) = pairing::ell_coeffs_montgomery(ctx, &w.g2);
// Take first coeff triple and evaluate once
let c = coeffs.into_iter().next().unwrap();
let _f1 = pairing::ell_montgomery(ctx, &f0, &c, &w.g1);
Expand Down
39 changes: 37 additions & 2 deletions g16ckt/src/gadgets/bn254/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,10 @@ impl G2Projective {

#[cfg(test)]
mod tests {
use ark_ec::{CurveGroup, VariableBaseMSM};
use ark_ff::UniformRand;
use ark_ec::{
AffineRepr, CurveGroup, PrimeGroup, VariableBaseMSM, short_weierstrass::SWCurveConfig,
};
use ark_ff::{Field, UniformRand};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;

Expand Down Expand Up @@ -873,4 +875,37 @@ mod tests {
let actual_result = G2Projective::from_bits_unchecked(circuit_result.output_value.clone());
assert_eq!(actual_result, G2Projective::as_montgomery(result));
}

#[test]
fn test_cofactor_clearing() {
let mut rng = ChaCha20Rng::seed_from_u64(112);
for _ in 0..5 {
// sufficient sample size to sample both valid and invalid points
let x = ark_bn254::Fq2::rand(&mut rng);
let a1 = ark_bn254::Fq2::sqrt(&((x * x * x) + ark_bn254::g2::Config::COEFF_B));
let (y, ref_is_valid) = if let Some(a1) = a1 {
// if it is possible to take square root, you have found correct y,
(a1, true)
} else {
// else generate some random value
(ark_bn254::Fq2::rand(&mut rng), false)
};
let pt = ark_bn254::G2Affine::new_unchecked(x, y);

let pt = pt.into_group();
const COFACTOR: &[u64] = &[
0x345f2299c0f9fa8d,
0x06ceecda572a2489,
0xb85045b68181585e,
0x30644e72e131a029,
];
let pt = pt.mul_bigint(COFACTOR);
let pt = pt.into_affine();
// if it's a valid point, it should be on curve and subgroup (after cofactor clearing)
assert_eq!(
ref_is_valid,
pt.is_on_curve() && pt.is_in_correct_subgroup_assuming_on_curve()
);
}
}
}
Loading
Loading