Browse Source

Simplify the field and curve tests using macros (#90)

* Simplify the field and curve tests using macros

* minor

* remove redundant code

Co-authored-by: weikeng <w.k@berkeley.edu>
update-to-latest-arkworks
Yuncong Hu 3 years ago
committed by GitHub
parent
commit
677b4ae751
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 128 additions and 2064 deletions
  1. +6
    -108
      bls12_377/src/curves/tests.rs
  2. +5
    -285
      bls12_377/src/fields/tests.rs
  3. +9
    -106
      bls12_381/src/curves/tests.rs
  4. +5
    -507
      bls12_381/src/fields/tests.rs
  5. +7
    -74
      bn254/src/curves/tests.rs
  6. +6
    -286
      bn254/src/fields/tests.rs
  7. +9
    -72
      bw6_761/src/curves/tests.rs
  8. +8
    -46
      bw6_761/src/fields/tests.rs
  9. +9
    -72
      cp6_782/src/curves/tests.rs
  10. +8
    -46
      cp6_782/src/fields/tests.rs
  11. +10
    -84
      mnt4_298/src/curves/tests.rs
  12. +4
    -41
      mnt4_298/src/fields/tests.rs
  13. +10
    -85
      mnt4_753/src/curves/tests.rs
  14. +4
    -41
      mnt4_753/src/fields/tests.rs
  15. +10
    -84
      mnt6_298/src/curves/tests.rs
  16. +4
    -22
      mnt6_298/src/fields/tests.rs
  17. +10
    -84
      mnt6_753/src/curves/tests.rs
  18. +4
    -21
      mnt6_753/src/fields/tests.rs

+ 6
- 108
bls12_377/src/curves/tests.rs

@ -17,114 +17,12 @@ use core::ops::{AddAssign, MulAssign};
use ark_algebra_test_templates::{ use ark_algebra_test_templates::{
curves::{curve_tests, edwards_tests, sw_tests}, curves::{curve_tests, edwards_tests, sw_tests},
generate_bilinearity_test, generate_g1_generator_raw_test, generate_g1_test, generate_g2_test,
groups::group_test, groups::group_test,
msm::test_var_base_msm,
}; };
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
sw_tests::<g1::Parameters>();
edwards_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
let c = rng.gen();
let d = rng.gen();
group_test::<G1TEProjective>(c, d);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let mut sa = a;
sa.mul_assign(s);
let mut sb = b;
sb.mul_assign(s);
let ans1 = Bls12_377::pairing(sa, b);
let ans2 = Bls12_377::pairing(a, sb);
let ans3 = Bls12_377::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq12::one());
assert_ne!(ans2, Fq12::one());
assert_ne!(ans3, Fq12::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq12::one());
}
#[test]
fn test_g1_generator_raw() {
let mut x = Fq::zero();
let mut i = 0;
loop {
// y^2 = x^3 + b
let mut rhs = x;
rhs.square_in_place();
rhs.mul_assign(&x);
rhs.add_assign(&g1::Parameters::COEFF_B);
if let Some(y) = rhs.sqrt() {
let p = G1Affine::new(x, if y < -y { y } else { -y }, false);
assert!(!p.is_in_correct_subgroup_assuming_on_curve());
let g1 = p.scale_by_cofactor();
if !g1.is_zero() {
assert_eq!(i, 1);
let g1 = G1Affine::from(g1);
assert!(g1.is_in_correct_subgroup_assuming_on_curve());
assert_eq!(g1, G1Affine::prime_subgroup_generator());
break;
}
}
i += 1;
x.add_assign(&Fq::one());
}
}
generate_g1_test!(bls12_377; curve_tests; sw_tests; edwards_tests; te_group_tests;);
generate_g2_test!(bls12_377; curve_tests; sw_tests;);
generate_bilinearity_test!(Bls12_377, Fq12);
generate_g1_generator_raw_test!(bls12_377, 1);

+ 5
- 285
bls12_377/src/fields/tests.rs

@ -15,80 +15,12 @@ use core::{
use crate::{Fq, Fq12, Fq2, Fq2Parameters, Fq6, Fq6Parameters, FqParameters, Fr}; use crate::{Fq, Fq12, Fq2, Fq2Parameters, Fq6, Fq6Parameters, FqParameters, Fr};
use ark_algebra_test_templates::fields::*;
pub(crate) const ITERATIONS: usize = 5;
#[test]
fn test_fr() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
primefield_test::<Fr>();
sqrt_field_test(b);
let byte_size = a.serialized_size();
field_serialization_test::<Fr>(byte_size);
}
}
#[test]
fn test_fq() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
primefield_test::<Fq>();
sqrt_field_test(a);
let byte_size = a.serialized_size();
let (_, buffer_size) = buffer_bit_byte_size(Fq::size_in_bits());
assert_eq!(byte_size, buffer_size);
field_serialization_test::<Fq>(byte_size);
}
}
#[test]
fn test_fq2() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fq2 = rng.gen();
let b: Fq2 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
}
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
let byte_size = Fq2::zero().serialized_size();
field_serialization_test::<Fq2>(byte_size);
}
#[test]
fn test_fq6() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let g: Fq6 = rng.gen();
let h: Fq6 = rng.gen();
field_test(g, h);
}
frobenius_test::<Fq6, _>(Fq::characteristic(), 13);
let byte_size = Fq6::zero().serialized_size();
field_serialization_test::<Fq6>(byte_size);
}
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
#[test]
fn test_fq12() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let g: Fq12 = rng.gen();
let h: Fq12 = rng.gen();
field_test(g, h);
}
frobenius_test::<Fq12, _>(Fq::characteristic(), 13);
let byte_size = Fq12::zero().serialized_size();
field_serialization_test::<Fq12>(byte_size);
}
generate_field_test!(bls12_377; fq2; fq6; fq12;);
generate_field_serialization_test!(bls12_377; fq2; fq6; fq12;);
#[test] #[test]
fn test_fq_repr_from() { fn test_fq_repr_from() {
@ -129,218 +61,6 @@ fn test_fq_repr_num_bits() {
assert_eq!(0, a.num_bits()); assert_eq!(0, a.num_bits());
} }
#[test]
fn test_fq_add_assign() {
// Test associativity
let mut rng = test_rng();
for _ in 0..1000 {
// Generate a, b, c and ensure (a + b) + c == a + (b + c).
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
let mut tmp2 = b;
tmp2.add_assign(&c);
tmp2.add_assign(&a);
assert_eq!(tmp1, tmp2);
}
}
#[test]
fn test_fq_sub_assign() {
let mut rng = test_rng();
for _ in 0..1000 {
// Ensure that (a - b) + (b - a) = 0.
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.sub_assign(&b);
let mut tmp2 = b;
tmp2.sub_assign(&a);
tmp1.add_assign(&tmp2);
assert!(tmp1.is_zero());
}
}
#[test]
fn test_fq_mul_assign() {
let mut rng = test_rng();
for _ in 0..1000000 {
// Ensure that (a * b) * c = a * (b * c)
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.mul_assign(&b);
tmp1.mul_assign(&c);
let mut tmp2 = b;
tmp2.mul_assign(&c);
tmp2.mul_assign(&a);
assert_eq!(tmp1, tmp2);
}
for _ in 0..1000000 {
// Ensure that r * (a + b + c) = r*a + r*b + r*c
let r = Fq::rand(&mut rng);
let mut a = Fq::rand(&mut rng);
let mut b = Fq::rand(&mut rng);
let mut c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
tmp1.mul_assign(&r);
a.mul_assign(&r);
b.mul_assign(&r);
c.mul_assign(&r);
a.add_assign(&b);
a.add_assign(&c);
assert_eq!(tmp1, a);
}
}
#[test]
fn test_fq_squaring() {
let mut rng = test_rng();
for _ in 0..1000000 {
// Ensure that (a * a) = a^2
let a = Fq::rand(&mut rng);
let mut tmp = a;
tmp.square_in_place();
let mut tmp2 = a;
tmp2.mul_assign(&a);
assert_eq!(tmp, tmp2);
}
}
#[test]
fn test_fq_inverse() {
assert!(Fq::zero().inverse().is_none());
let mut rng = test_rng();
let one = Fq::one();
for _ in 0..1000 {
// Ensure that a * a^-1 = 1
let mut a = Fq::rand(&mut rng);
let ainv = a.inverse().unwrap();
a.mul_assign(&ainv);
assert_eq!(a, one);
}
}
#[test]
fn test_fq_double_in_place() {
let mut rng = test_rng();
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fq::rand(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double_in_place();
assert_eq!(a, b);
}
}
#[test]
fn test_fq_negate() {
{
let a = -Fq::zero();
assert!(a.is_zero());
}
let mut rng = test_rng();
for _ in 0..1000 {
// Ensure (a - (-a)) = 0.
let mut a = Fq::rand(&mut rng);
let b = -a;
a.add_assign(&b);
assert!(a.is_zero());
}
}
#[test]
fn test_fq_pow() {
let mut rng = test_rng();
for i in 0..1000 {
// Exponentiate by various small numbers and ensure it consists with repeated
// multiplication.
let a = Fq::rand(&mut rng);
let target = a.pow(&[i]);
let mut c = Fq::one();
for _ in 0..i {
c.mul_assign(&a);
}
assert_eq!(c, target);
}
for _ in 0..1000 {
// Exponentiating by the modulus should have no effect in a prime field.
let a = Fq::rand(&mut rng);
assert_eq!(a, a.pow(Fq::characteristic()));
}
}
#[test]
fn test_fq_sqrt() {
let mut rng = test_rng();
assert_eq!(Fq::zero().sqrt().unwrap(), Fq::zero());
for _ in 0..1000 {
// Ensure sqrt(a^2) = a or -a
let a = Fq::rand(&mut rng);
let nega = -a;
let mut b = a;
b.square_in_place();
let b = b.sqrt().unwrap();
assert!(a == b || nega == b);
}
for _ in 0..1000 {
// Ensure sqrt(a)^2 = a for random a
let a = Fq::rand(&mut rng);
if let Some(mut tmp) = a.sqrt() {
tmp.square_in_place();
assert_eq!(a, tmp);
}
}
}
#[test] #[test]
fn test_fq_num_bits() { fn test_fq_num_bits() {
assert_eq!(FqParameters::MODULUS_BITS, 377); assert_eq!(FqParameters::MODULUS_BITS, 377);

+ 9
- 106
bls12_381/src/curves/tests.rs

@ -9,117 +9,20 @@ use ark_ff::{
BitIteratorBE, One, UniformRand, Zero, BitIteratorBE, One, UniformRand, Zero,
}; };
use ark_serialize::CanonicalSerialize; use ark_serialize::CanonicalSerialize;
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use core::ops::{AddAssign, MulAssign}; use core::ops::{AddAssign, MulAssign};
use crate::{g1, g2, Bls12_381, Fq, Fq12, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective}; use crate::{g1, g2, Bls12_381, Fq, Fq12, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_generator_raw_test, generate_g1_test,
generate_g2_test, groups::*, msm::*,
};
use ark_ec::group::Group; use ark_ec::group::Group;
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let mut sa = a;
sa.mul_assign(s);
let mut sb = b;
sb.mul_assign(s);
let ans1 = Bls12_381::pairing(sa, b);
let ans2 = Bls12_381::pairing(a, sb);
let ans3 = Bls12_381::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq12::one());
assert_ne!(ans2, Fq12::one());
assert_ne!(ans3, Fq12::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq12::one());
}
#[test]
fn test_g1_generator_raw() {
let mut x = Fq::zero();
let mut i = 0;
loop {
// y^2 = x^3 + b
let mut rhs = x;
rhs.square_in_place();
rhs.mul_assign(&x);
rhs.add_assign(&g1::Parameters::COEFF_B);
if let Some(y) = rhs.sqrt() {
let p = G1Affine::new(x, if y < -y { y } else { -y }, false);
assert!(!p.is_in_correct_subgroup_assuming_on_curve());
let g1 = p.scale_by_cofactor();
if !g1.is_zero() {
assert_eq!(i, 4);
let g1 = G1Affine::from(g1);
assert!(g1.is_in_correct_subgroup_assuming_on_curve());
assert_eq!(g1, G1Affine::prime_subgroup_generator());
break;
}
}
i += 1;
x.add_assign(&Fq::one());
}
}
generate_g1_test!(bls12_381; curve_tests; sw_tests;);
generate_g2_test!(bls12_381; curve_tests; sw_tests;);
generate_bilinearity_test!(Bls12_381, Fq12);
generate_g1_generator_raw_test!(bls12_381, 4);
#[test] #[test]
fn test_g1_endomorphism_beta() { fn test_g1_endomorphism_beta() {

+ 5
- 507
bls12_381/src/fields/tests.rs

@ -6,73 +6,17 @@ use ark_ff::{
}, },
One, UniformRand, Zero, One, UniformRand, Zero,
}; };
use core::{
use ark_std::{
cmp::Ordering, cmp::Ordering,
ops::{AddAssign, MulAssign, SubAssign}, ops::{AddAssign, MulAssign, SubAssign},
rand::Rng,
test_rng,
}; };
use crate::{Fq, Fq12, Fq12Parameters, Fq2, Fq2Parameters, Fq6, Fq6Parameters, FqParameters, Fr}; use crate::{Fq, Fq12, Fq12Parameters, Fq2, Fq2Parameters, Fq6, Fq6Parameters, FqParameters, Fr};
use ark_algebra_test_templates::fields::*;
use ark_algebra_test_templates::{fields::*, generate_field_test};
pub(crate) const ITERATIONS: usize = 5;
#[test]
fn test_fr() {
let mut rng = ark_std::test_rng();
for _ in 0..ITERATIONS {
let a: Fr = UniformRand::rand(&mut rng);
let b: Fr = UniformRand::rand(&mut rng);
field_test(a, b);
primefield_test::<Fr>();
sqrt_field_test(b);
}
}
#[test]
fn test_fq() {
let mut rng = ark_std::test_rng();
for _ in 0..ITERATIONS {
let a: Fq = UniformRand::rand(&mut rng);
let b: Fq = UniformRand::rand(&mut rng);
field_test(a, b);
primefield_test::<Fq>();
sqrt_field_test(a);
}
}
#[test]
fn test_fq2() {
let mut rng = ark_std::test_rng();
for _ in 0..ITERATIONS {
let a: Fq2 = UniformRand::rand(&mut rng);
let b: Fq2 = UniformRand::rand(&mut rng);
field_test(a, b);
sqrt_field_test(a);
}
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq6() {
let mut rng = ark_std::test_rng();
for _ in 0..ITERATIONS {
let g: Fq6 = UniformRand::rand(&mut rng);
let h: Fq6 = UniformRand::rand(&mut rng);
field_test(g, h);
}
frobenius_test::<Fq6, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq12() {
let mut rng = ark_std::test_rng();
for _ in 0..ITERATIONS {
let g: Fq12 = UniformRand::rand(&mut rng);
let h: Fq12 = UniformRand::rand(&mut rng);
field_test(g, h);
}
frobenius_test::<Fq12, _>(Fq::characteristic(), 13);
}
generate_field_test!(bls12_381; fq2; fq6; fq12;);
#[test] #[test]
fn test_negative_one() { fn test_negative_one() {
@ -1176,452 +1120,6 @@ fn test_fq_repr_add_nocarry() {
assert!(x.is_zero()); assert!(x.is_zero());
} }
#[test]
fn test_fq_add_assign() {
{
// Random number
let mut tmp = Fq::new(BigInteger384([
0x624434821df92b69,
0x503260c04fd2e2ea,
0xd9df726e0d16e8ce,
0xfbcb39adfd5dfaeb,
0x86b8a22b0c88b112,
0x165a2ed809e4201b,
]));
// Test that adding zero has no effect.
tmp.add_assign(&Fq::new(BigInteger384::from(0)));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0x624434821df92b69,
0x503260c04fd2e2ea,
0xd9df726e0d16e8ce,
0xfbcb39adfd5dfaeb,
0x86b8a22b0c88b112,
0x165a2ed809e4201b,
]))
);
// Add one and test for the result.
tmp.add_assign(&Fq::new(BigInteger384::from(1)));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0x624434821df92b6a,
0x503260c04fd2e2ea,
0xd9df726e0d16e8ce,
0xfbcb39adfd5dfaeb,
0x86b8a22b0c88b112,
0x165a2ed809e4201b,
]))
);
// Add another random number that exercises the reduction.
tmp.add_assign(&Fq::new(BigInteger384([
0x374d8f8ea7a648d8,
0xe318bb0ebb8bfa9b,
0x613d996f0a95b400,
0x9fac233cb7e4fef1,
0x67e47552d253c52,
0x5c31b227edf25da,
])));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0xdf92c410c59fc997,
0x149f1bd05a0add85,
0xd3ec393c20fba6ab,
0x37001165c1bde71d,
0x421b41c9f662408e,
0x21c38104f435f5b,
]))
);
// Add one to (q - 1) and test for the result.
tmp = Fq::new(BigInteger384([
0xb9feffffffffaaaa,
0x1eabfffeb153ffff,
0x6730d2a0f6b0f624,
0x64774b84f38512bf,
0x4b1ba7b6434bacd7,
0x1a0111ea397fe69a,
]));
tmp.add_assign(&Fq::new(BigInteger384::from(1)));
assert!(tmp.0.is_zero());
// Add a random number to another one such that the result is q - 1
tmp = Fq::new(BigInteger384([
0x531221a410efc95b,
0x72819306027e9717,
0x5ecefb937068b746,
0x97de59cd6feaefd7,
0xdc35c51158644588,
0xb2d176c04f2100,
]));
tmp.add_assign(&Fq::new(BigInteger384([
0x66ecde5bef0fe14f,
0xac2a6cf8aed568e8,
0x861d70d86483edd,
0xcc98f1b7839a22e8,
0x6ee5e2a4eae7674e,
0x194e40737930c599,
])));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0xb9feffffffffaaaa,
0x1eabfffeb153ffff,
0x6730d2a0f6b0f624,
0x64774b84f38512bf,
0x4b1ba7b6434bacd7,
0x1a0111ea397fe69a,
]))
);
// Add one to the result and test for it.
tmp.add_assign(&Fq::new(BigInteger384::from(1)));
assert!(tmp.0.is_zero());
}
// Test associativity
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Generate a, b, c and ensure (a + b) + c == a + (b + c).
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
let mut tmp2 = b;
tmp2.add_assign(&c);
tmp2.add_assign(&a);
assert_eq!(tmp1, tmp2);
}
}
#[test]
fn test_fq_sub_assign() {
{
// Test arbitrary subtraction that tests reduction.
let mut tmp = Fq::new(BigInteger384([
0x531221a410efc95b,
0x72819306027e9717,
0x5ecefb937068b746,
0x97de59cd6feaefd7,
0xdc35c51158644588,
0xb2d176c04f2100,
]));
tmp.sub_assign(&Fq::new(BigInteger384([
0x98910d20877e4ada,
0x940c983013f4b8ba,
0xf677dc9b8345ba33,
0xbef2ce6b7f577eba,
0xe1ae288ac3222c44,
0x5968bb602790806,
])));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0x748014838971292c,
0xfd20fad49fddde5c,
0xcf87f198e3d3f336,
0x3d62d6e6e41883db,
0x45a3443cd88dc61b,
0x151d57aaf755ff94,
]))
);
// Test the opposite subtraction which doesn't test reduction.
tmp = Fq::new(BigInteger384([
0x98910d20877e4ada,
0x940c983013f4b8ba,
0xf677dc9b8345ba33,
0xbef2ce6b7f577eba,
0xe1ae288ac3222c44,
0x5968bb602790806,
]));
tmp.sub_assign(&Fq::new(BigInteger384([
0x531221a410efc95b,
0x72819306027e9717,
0x5ecefb937068b746,
0x97de59cd6feaefd7,
0xdc35c51158644588,
0xb2d176c04f2100,
])));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0x457eeb7c768e817f,
0x218b052a117621a3,
0x97a8e10812dd02ed,
0x2714749e0f6c8ee3,
0x57863796abde6bc,
0x4e3ba3f4229e706,
]))
);
// Test for sensible results with zero
tmp = Fq::new(BigInteger384::from(0));
tmp.sub_assign(&Fq::new(BigInteger384::from(0)));
assert!(tmp.is_zero());
tmp = Fq::new(BigInteger384([
0x98910d20877e4ada,
0x940c983013f4b8ba,
0xf677dc9b8345ba33,
0xbef2ce6b7f577eba,
0xe1ae288ac3222c44,
0x5968bb602790806,
]));
tmp.sub_assign(&Fq::new(BigInteger384::from(0)));
assert_eq!(
tmp,
Fq::new(BigInteger384([
0x98910d20877e4ada,
0x940c983013f4b8ba,
0xf677dc9b8345ba33,
0xbef2ce6b7f577eba,
0xe1ae288ac3222c44,
0x5968bb602790806,
]))
);
}
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure that (a - b) + (b - a) = 0.
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.sub_assign(&b);
let mut tmp2 = b;
tmp2.sub_assign(&a);
tmp1.add_assign(&tmp2);
assert!(tmp1.is_zero());
}
}
#[test]
fn test_fq_mul_assign() {
let mut tmp = Fq::new(BigInteger384([
0xcc6200000020aa8a,
0x422800801dd8001a,
0x7f4f5e619041c62c,
0x8a55171ac70ed2ba,
0x3f69cc3a3d07d58b,
0xb972455fd09b8ef,
]));
tmp.mul_assign(&Fq::new(BigInteger384([
0x329300000030ffcf,
0x633c00c02cc40028,
0xbef70d925862a942,
0x4f7fa2a82a963c17,
0xdf1eb2575b8bc051,
0x1162b680fb8e9566,
])));
assert!(
tmp == Fq::new(BigInteger384([
0x9dc4000001ebfe14,
0x2850078997b00193,
0xa8197f1abb4d7bf,
0xc0309573f4bfe871,
0xf48d0923ffaf7620,
0x11d4b58c7a926e66,
]))
);
let mut rng = ark_std::test_rng();
for _ in 0..1000000 {
// Ensure that (a * b) * c = a * (b * c)
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.mul_assign(&b);
tmp1.mul_assign(&c);
let mut tmp2 = b;
tmp2.mul_assign(&c);
tmp2.mul_assign(&a);
assert_eq!(tmp1, tmp2);
}
for _ in 0..1000000 {
// Ensure that r * (a + b + c) = r*a + r*b + r*c
let r = Fq::rand(&mut rng);
let mut a = Fq::rand(&mut rng);
let mut b = Fq::rand(&mut rng);
let mut c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
tmp1.mul_assign(&r);
a.mul_assign(&r);
b.mul_assign(&r);
c.mul_assign(&r);
a.add_assign(&b);
a.add_assign(&c);
assert_eq!(tmp1, a);
}
}
#[test]
fn test_fq_squaring() {
let mut a = Fq::new(BigInteger384([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0x19ffffffffffffff,
]));
a.square_in_place();
assert_eq!(
a,
Fq::from(BigInteger384([
0x1cfb28fe7dfbbb86,
0x24cbe1731577a59,
0xcce1d4edc120e66e,
0xdc05c659b4e15b27,
0x79361e5a802c6a23,
0x24bcbe5d51b9a6f,
]))
);
let mut rng = ark_std::test_rng();
for _ in 0..1000000 {
// Ensure that (a * a) = a^2
let a = Fq::rand(&mut rng);
let mut tmp = a;
tmp.square_in_place();
let mut tmp2 = a;
tmp2.mul_assign(&a);
assert_eq!(tmp, tmp2);
}
}
#[test]
fn test_fq_inverse() {
assert!(Fq::zero().inverse().is_none());
let mut rng = ark_std::test_rng();
let one = Fq::one();
for _ in 0..1000 {
// Ensure that a * a^-1 = 1
let mut a = Fq::rand(&mut rng);
let ainv = a.inverse().unwrap();
a.mul_assign(&ainv);
assert_eq!(a, one);
}
}
#[test]
fn test_fq_double_in_place() {
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fq::rand(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double_in_place();
assert_eq!(a, b);
}
}
#[test]
fn test_fq_negate() {
{
let a = -Fq::zero();
assert!(a.is_zero());
}
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure (a - (-a)) = 0.
let mut a = Fq::rand(&mut rng);
let b = -a;
a.add_assign(&b);
assert!(a.is_zero());
}
}
#[test]
fn test_fq_pow() {
let mut rng = ark_std::test_rng();
for i in 0..1000 {
// Exponentiate by various small numbers and ensure it consists with repeated
// multiplication.
let a = Fq::rand(&mut rng);
let target = a.pow(&[i]);
let mut c = Fq::one();
for _ in 0..i {
c.mul_assign(&a);
}
assert_eq!(c, target);
}
for _ in 0..1000 {
// Exponentiating by the modulus should have no effect in a prime field.
let a = Fq::rand(&mut rng);
assert_eq!(a, a.pow(Fq::characteristic()));
}
}
#[test]
fn test_fq_sqrt() {
let mut rng = ark_std::test_rng();
assert_eq!(Fq::zero().sqrt().unwrap(), Fq::zero());
for _ in 0..1000 {
// Ensure sqrt(a^2) = a or -a
let a = Fq::rand(&mut rng);
let nega = -a;
let mut b = a;
b.square_in_place();
let b = b.sqrt().unwrap();
assert!(a == b || nega == b);
}
for _ in 0..1000 {
// Ensure sqrt(a)^2 = a for random a
let a = Fq::rand(&mut rng);
if let Some(mut tmp) = a.sqrt() {
tmp.square_in_place();
assert_eq!(a, tmp);
}
}
}
#[test] #[test]
fn test_fq_num_bits() { fn test_fq_num_bits() {
assert_eq!(FqParameters::MODULUS_BITS, 381); assert_eq!(FqParameters::MODULUS_BITS, 381);

+ 7
- 74
bn254/src/curves/tests.rs

@ -5,82 +5,15 @@ use ark_ff::{
One, Zero, One, Zero,
}; };
use ark_serialize::CanonicalSerialize; use ark_serialize::CanonicalSerialize;
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use core::ops::{AddAssign, MulAssign}; use core::ops::{AddAssign, MulAssign};
use crate::{g1, g2, Bn254, Fq, Fq12, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective}; use crate::{g1, g2, Bn254, Fq, Fq12, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_algebra_test_templates::{curves::*, groups::*};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let mut sa = a;
sa.mul_assign(s);
let mut sb = b;
sb.mul_assign(s);
let ans1 = Bn254::pairing(sa, b);
let ans2 = Bn254::pairing(a, sb);
let ans3 = Bn254::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq12::one());
assert_ne!(ans2, Fq12::one());
assert_ne!(ans3, Fq12::one());
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test, groups::*, msm::*,
};
assert_eq!(ans1.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq12::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq12::one());
}
generate_g1_test!(bn254; curve_tests; sw_tests;);
generate_g2_test!(bn254; curve_tests; sw_tests;);
generate_bilinearity_test!(Bn254, Fq12);

+ 6
- 286
bn254/src/fields/tests.rs

@ -7,87 +7,19 @@ use ark_ff::{
One, UniformRand, Zero, One, UniformRand, Zero,
}; };
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize}; use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use core::{ use core::{
cmp::Ordering, cmp::Ordering,
ops::{AddAssign, MulAssign, SubAssign}, ops::{AddAssign, MulAssign, SubAssign},
}; };
use crate::{Fq, Fq12, Fq2, Fq6, Fq6Parameters, FqParameters, Fr}; use crate::{Fq, Fq12, Fq2, Fq6, Fq6Parameters, FqParameters, Fr};
use ark_algebra_test_templates::fields::*;
pub(crate) const ITERATIONS: usize = 5;
#[test]
fn test_fr() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
primefield_test::<Fr>();
sqrt_field_test(b);
let byte_size = a.serialized_size();
field_serialization_test::<Fr>(byte_size);
}
}
#[test]
fn test_fq() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
primefield_test::<Fq>();
sqrt_field_test(a);
let byte_size = a.serialized_size();
let (_, buffer_size) = buffer_bit_byte_size(Fq::size_in_bits());
assert_eq!(byte_size, buffer_size);
field_serialization_test::<Fq>(byte_size);
}
}
#[test]
fn test_fq2() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let a: Fq2 = rng.gen();
let b: Fq2 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
}
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
let byte_size = Fq2::zero().serialized_size();
field_serialization_test::<Fq2>(byte_size);
}
#[test]
fn test_fq6() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let g: Fq6 = rng.gen();
let h: Fq6 = rng.gen();
field_test(g, h);
}
frobenius_test::<Fq6, _>(Fq::characteristic(), 13);
let byte_size = Fq6::zero().serialized_size();
field_serialization_test::<Fq6>(byte_size);
}
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
#[test]
fn test_fq12() {
let mut rng = test_rng();
for _ in 0..ITERATIONS {
let g: Fq12 = rng.gen();
let h: Fq12 = rng.gen();
field_test(g, h);
}
frobenius_test::<Fq12, _>(Fq::characteristic(), 13);
let byte_size = Fq12::zero().serialized_size();
field_serialization_test::<Fq12>(byte_size);
}
generate_field_test!(bn254; fq2; fq6; fq12;);
generate_field_serialization_test!(bn254; fq2; fq6; fq12;);
#[test] #[test]
fn test_fq_repr_from() { fn test_fq_repr_from() {
@ -125,218 +57,6 @@ fn test_fq_repr_num_bits() {
assert_eq!(0, a.num_bits()); assert_eq!(0, a.num_bits());
} }
#[test]
fn test_fq_add_assign() {
// Test associativity
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Generate a, b, c and ensure (a + b) + c == a + (b + c).
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
let mut tmp2 = b;
tmp2.add_assign(&c);
tmp2.add_assign(&a);
assert_eq!(tmp1, tmp2);
}
}
#[test]
fn test_fq_sub_assign() {
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure that (a - b) + (b - a) = 0.
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.sub_assign(&b);
let mut tmp2 = b;
tmp2.sub_assign(&a);
tmp1.add_assign(&tmp2);
assert!(tmp1.is_zero());
}
}
#[test]
fn test_fq_mul_assign() {
let mut rng = ark_std::test_rng();
for _ in 0..1000000 {
// Ensure that (a * b) * c = a * (b * c)
let a = Fq::rand(&mut rng);
let b = Fq::rand(&mut rng);
let c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.mul_assign(&b);
tmp1.mul_assign(&c);
let mut tmp2 = b;
tmp2.mul_assign(&c);
tmp2.mul_assign(&a);
assert_eq!(tmp1, tmp2);
}
for _ in 0..1000000 {
// Ensure that r * (a + b + c) = r*a + r*b + r*c
let r = Fq::rand(&mut rng);
let mut a = Fq::rand(&mut rng);
let mut b = Fq::rand(&mut rng);
let mut c = Fq::rand(&mut rng);
let mut tmp1 = a;
tmp1.add_assign(&b);
tmp1.add_assign(&c);
tmp1.mul_assign(&r);
a.mul_assign(&r);
b.mul_assign(&r);
c.mul_assign(&r);
a.add_assign(&b);
a.add_assign(&c);
assert_eq!(tmp1, a);
}
}
#[test]
fn test_fq_squaring() {
let mut rng = ark_std::test_rng();
for _ in 0..1000000 {
// Ensure that (a * a) = a^2
let a = Fq::rand(&mut rng);
let mut tmp = a;
tmp.square_in_place();
let mut tmp2 = a;
tmp2.mul_assign(&a);
assert_eq!(tmp, tmp2);
}
}
#[test]
fn test_fq_inverse() {
assert!(Fq::zero().inverse().is_none());
let mut rng = ark_std::test_rng();
let one = Fq::one();
for _ in 0..1000 {
// Ensure that a * a^-1 = 1
let mut a = Fq::rand(&mut rng);
let ainv = a.inverse().unwrap();
a.mul_assign(&ainv);
assert_eq!(a, one);
}
}
#[test]
fn test_fq_double_in_place() {
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fq::rand(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double_in_place();
assert_eq!(a, b);
}
}
#[test]
fn test_fq_negate() {
{
let a = -Fq::zero();
assert!(a.is_zero());
}
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
// Ensure (a - (-a)) = 0.
let mut a = Fq::rand(&mut rng);
let b = -a;
a.add_assign(&b);
assert!(a.is_zero());
}
}
#[test]
fn test_fq_pow() {
let mut rng = ark_std::test_rng();
for i in 0..1000 {
// Exponentiate by various small numbers and ensure it consists with repeated
// multiplication.
let a = Fq::rand(&mut rng);
let target = a.pow(&[i]);
let mut c = Fq::one();
for _ in 0..i {
c.mul_assign(&a);
}
assert_eq!(c, target);
}
for _ in 0..1000 {
// Exponentiating by the modulus should have no effect in a prime field.
let a = Fq::rand(&mut rng);
assert_eq!(a, a.pow(Fq::characteristic()));
}
}
#[test]
fn test_fq_sqrt() {
let mut rng = ark_std::test_rng();
assert_eq!(Fq::zero().sqrt().unwrap(), Fq::zero());
for _ in 0..1000 {
// Ensure sqrt(a^2) = a or -a
let a = Fq::rand(&mut rng);
let nega = -a;
let mut b = a;
b.square_in_place();
let b = b.sqrt().unwrap();
assert!(a == b || nega == b);
}
for _ in 0..1000 {
// Ensure sqrt(a)^2 = a for random a
let a = Fq::rand(&mut rng);
if let Some(mut tmp) = a.sqrt() {
tmp.square_in_place();
assert_eq!(a, tmp);
}
}
}
#[test] #[test]
fn test_fq_num_bits() { fn test_fq_num_bits() {
assert_eq!(FqParameters::MODULUS_BITS, 254); assert_eq!(FqParameters::MODULUS_BITS, 254);

+ 9
- 72
bw6_761/src/curves/tests.rs

@ -1,78 +1,15 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ec::{AffineCurve, PairingEngine};
use ark_ff::{Field, One, PrimeField}; use ark_ff::{Field, One, PrimeField};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test, groups::*, msm::*,
};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
use core::ops::MulAssign;
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = BW6_761::pairing(sa, b);
let ans2 = BW6_761::pairing(a, sb);
let ans3 = BW6_761::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq6::one());
assert_ne!(ans2, Fq6::one());
assert_ne!(ans3, Fq6::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq6::one());
}
generate_g1_test!(bw6_761; curve_tests; sw_tests;);
generate_g2_test!(bw6_761; curve_tests; sw_tests;);
generate_bilinearity_test!(BW6_761, Fq6);

+ 8
- 46
bw6_761/src/fields/tests.rs

@ -1,52 +1,14 @@
use ark_ff::{Field, PrimeField};
use ark_ff::{Field, One, PrimeField, SquareRootField, UniformRand, Zero};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize}; use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::fields::*;
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
primefield_test::<Fq>();
sqrt_field_test(a);
let byte_size = a.serialized_size();
let (_, buffer_size) = buffer_bit_byte_size(Fq::size_in_bits());
assert_eq!(byte_size, buffer_size);
field_serialization_test::<Fq>(byte_size);
}
#[test]
fn test_fq3() {
let mut rng = test_rng();
let a: Fq3 = rng.gen();
let b: Fq3 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
frobenius_test::<Fq3, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq6() {
let mut rng = test_rng();
let a: Fq6 = rng.gen();
let b: Fq6 = rng.gen();
field_test(a, b);
frobenius_test::<Fq6, _>(Fq::characteristic(), 13);
}
generate_field_test!(bw6_761; fq3; fq6;);
generate_field_serialization_test!(bw6_761;);

+ 9
- 72
cp6_782/src/curves/tests.rs

@ -1,78 +1,15 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ec::{AffineCurve, PairingEngine};
use ark_ff::{Field, One, PrimeField}; use ark_ff::{Field, One, PrimeField};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test, groups::*, msm::*,
};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
use core::ops::MulAssign;
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = CP6_782::pairing(sa, b);
let ans2 = CP6_782::pairing(a, sb);
let ans3 = CP6_782::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq6::one());
assert_ne!(ans2, Fq6::one());
assert_ne!(ans3, Fq6::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq6::one());
}
generate_g1_test!(cp6_782; curve_tests; sw_tests;);
generate_g2_test!(cp6_782; curve_tests; sw_tests;);
generate_bilinearity_test!(CP6_782, Fq6);

+ 8
- 46
cp6_782/src/fields/tests.rs

@ -1,52 +1,14 @@
use ark_ff::{Field, PrimeField};
use ark_ff::{Field, One, PrimeField, SquareRootField, UniformRand, Zero};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize}; use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::fields::*;
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
primefield_test::<Fq>();
sqrt_field_test(a);
let byte_size = a.serialized_size();
let (_, buffer_size) = buffer_bit_byte_size(Fq::size_in_bits());
assert_eq!(byte_size, buffer_size);
field_serialization_test::<Fq>(byte_size);
}
#[test]
fn test_fq3() {
let mut rng = test_rng();
let a: Fq3 = rng.gen();
let b: Fq3 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
frobenius_test::<Fq3, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq6() {
let mut rng = test_rng();
let a: Fq6 = rng.gen();
let b: Fq6 = rng.gen();
field_test(a, b);
frobenius_test::<Fq6, _>(Fq::characteristic(), 13);
}
generate_field_test!(cp6_782; fq3; fq6;);
generate_field_serialization_test!(cp6_782;);

+ 10
- 84
mnt4_298/src/curves/tests.rs

@ -1,91 +1,17 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve}; use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, PrimeField, UniformRand}; use ark_ff::{Field, One, PrimeField, UniformRand};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test,
generate_product_of_pairings_test, groups::*, msm::*,
};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
use core::ops::MulAssign;
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = MNT4_298::pairing(sa, b);
let ans2 = MNT4_298::pairing(a, sb);
let ans3 = MNT4_298::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq4::one());
assert_ne!(ans2, Fq4::one());
assert_ne!(ans3, Fq4::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq4::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq4::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq4::one());
}
#[test]
fn test_product_of_pairings() {
let rng = &mut test_rng();
let a = G1Projective::rand(rng).into_affine();
let b = G2Projective::rand(rng).into_affine();
let c = G1Projective::rand(rng).into_affine();
let d = G2Projective::rand(rng).into_affine();
let ans1 = MNT4_298::pairing(a, b) * &MNT4_298::pairing(c, d);
let ans2 = MNT4_298::product_of_pairings(&[(a.into(), b.into()), (c.into(), d.into())]);
assert_eq!(ans1, ans2);
}
generate_g1_test!(mnt4_298; curve_tests; sw_tests;);
generate_g2_test!(mnt4_298; curve_tests; sw_tests;);
generate_bilinearity_test!(MNT4_298, Fq4);
generate_product_of_pairings_test!(MNT4_298);

+ 4
- 41
mnt4_298/src/fields/tests.rs

@ -1,46 +1,9 @@
use ark_ff::Field;
use ark_std::rand::Rng;
use ark_ff::{Field, One, SquareRootField, UniformRand, Zero};
use ark_std::test_rng; use ark_std::test_rng;
use crate::*; use crate::*;
use ark_algebra_test_templates::{fields::*, generate_field_test};
use ark_algebra_test_templates::fields::*;
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fq>();
}
#[test]
fn test_fq2() {
let mut rng = test_rng();
let a: Fq2 = rng.gen();
let b: Fq2 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq4() {
let mut rng = test_rng();
let a: Fq4 = rng.gen();
let b: Fq4 = rng.gen();
field_test(a, b);
frobenius_test::<Fq4, _>(Fq::characteristic(), 13);
}
generate_field_test!(mnt4_298; fq2; fq4;);

+ 10
- 85
mnt4_753/src/curves/tests.rs

@ -1,91 +1,16 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve}; use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, PrimeField, UniformRand}; use ark_ff::{Field, One, PrimeField, UniformRand};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test,
generate_product_of_pairings_test, groups::*, msm::*,
};
use ark_algebra_test_templates::{curves::*, groups::*};
use core::ops::MulAssign;
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = MNT4_753::pairing(sa, b);
let ans2 = MNT4_753::pairing(a, sb);
let ans3 = MNT4_753::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq4::one());
assert_ne!(ans2, Fq4::one());
assert_ne!(ans3, Fq4::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq4::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq4::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq4::one());
}
#[test]
fn test_product_of_pairings() {
let rng = &mut test_rng();
let a = G1Projective::rand(rng).into_affine();
let b = G2Projective::rand(rng).into_affine();
let c = G1Projective::rand(rng).into_affine();
let d = G2Projective::rand(rng).into_affine();
let ans1 = MNT4_753::pairing(a, b) * &MNT4_753::pairing(c, d);
let ans2 = MNT4_753::product_of_pairings(&[(a.into(), b.into()), (c.into(), d.into())]);
assert_eq!(ans1, ans2);
}
generate_g1_test!(mnt4_753; curve_tests; sw_tests;);
generate_g2_test!(mnt4_753; curve_tests; sw_tests;);
generate_bilinearity_test!(MNT4_753, Fq4);
generate_product_of_pairings_test!(MNT4_753);

+ 4
- 41
mnt4_753/src/fields/tests.rs

@ -1,46 +1,9 @@
use ark_ff::Field;
use ark_std::rand::Rng;
use ark_ff::{Field, One, SquareRootField, UniformRand, Zero};
use ark_std::test_rng; use ark_std::test_rng;
use crate::*; use crate::*;
use ark_algebra_test_templates::{fields::*, generate_field_test};
use ark_algebra_test_templates::fields::*;
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fq>();
}
#[test]
fn test_fq2() {
let mut rng = test_rng();
let a: Fq2 = rng.gen();
let b: Fq2 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq4() {
let mut rng = test_rng();
let a: Fq4 = rng.gen();
let b: Fq4 = rng.gen();
field_test(a, b);
frobenius_test::<Fq4, _>(Fq::characteristic(), 13);
}
generate_field_test!(mnt4_753; fq2; fq4;);

+ 10
- 84
mnt6_298/src/curves/tests.rs

@ -1,91 +1,17 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve}; use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, PrimeField, UniformRand}; use ark_ff::{Field, One, PrimeField, UniformRand};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test,
generate_product_of_pairings_test, groups::*, msm::*,
};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
use core::ops::MulAssign;
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = MNT6_298::pairing(sa, b);
let ans2 = MNT6_298::pairing(a, sb);
let ans3 = MNT6_298::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq6::one());
assert_ne!(ans2, Fq6::one());
assert_ne!(ans3, Fq6::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq6::one());
}
#[test]
fn test_product_of_pairings() {
let rng = &mut test_rng();
let a = G1Projective::rand(rng).into_affine();
let b = G2Projective::rand(rng).into_affine();
let c = G1Projective::rand(rng).into_affine();
let d = G2Projective::rand(rng).into_affine();
let ans1 = MNT6_298::pairing(a, b) * &MNT6_298::pairing(c, d);
let ans2 = MNT6_298::product_of_pairings(&[(a.into(), b.into()), (c.into(), d.into())]);
assert_eq!(ans1, ans2);
}
generate_g1_test!(mnt6_298; curve_tests; sw_tests;);
generate_g2_test!(mnt6_298; curve_tests; sw_tests;);
generate_bilinearity_test!(MNT6_298, Fq6);
generate_product_of_pairings_test!(MNT6_298);

+ 4
- 22
mnt6_298/src/fields/tests.rs

@ -2,32 +2,14 @@ use ark_ff::{
fields::{models::fp6_2over3::*, quadratic_extension::QuadExtParameters}, fields::{models::fp6_2over3::*, quadratic_extension::QuadExtParameters},
Field, Field,
}; };
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{fields::*, generate_field_test};
use ark_algebra_test_templates::fields::*;
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fq>();
}
generate_field_test!(mnt6_298;);
#[test] #[test]
fn test_fq3() { fn test_fq3() {

+ 10
- 84
mnt6_753/src/curves/tests.rs

@ -1,91 +1,17 @@
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve}; use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, PrimeField, UniformRand}; use ark_ff::{Field, One, PrimeField, UniformRand};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test,
generate_product_of_pairings_test, groups::*, msm::*,
};
#[test]
fn test_g1_projective_curve() {
curve_tests::<G1Projective>();
use core::ops::MulAssign;
sw_tests::<g1::Parameters>();
}
#[test]
fn test_g1_projective_group() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G1Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g1_generator() {
let generator = G1Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_g2_projective_curve() {
curve_tests::<G2Projective>();
sw_tests::<g2::Parameters>();
}
#[test]
fn test_g2_projective_group() {
let mut rng = test_rng();
let a: G2Projective = rng.gen();
let b: G2Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_g2_generator() {
let generator = G2Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_bilinearity() {
let mut rng = test_rng();
let a: G1Projective = rng.gen();
let b: G2Projective = rng.gen();
let s: Fr = rng.gen();
let sa = a.mul(s.into_repr());
let sb = b.mul(s.into_repr());
let ans1 = MNT6_753::pairing(sa, b);
let ans2 = MNT6_753::pairing(a, sb);
let ans3 = MNT6_753::pairing(a, b).pow(s.into_repr());
assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
assert_ne!(ans1, Fq6::one());
assert_ne!(ans2, Fq6::one());
assert_ne!(ans3, Fq6::one());
assert_eq!(ans1.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans2.pow(Fr::characteristic()), Fq6::one());
assert_eq!(ans3.pow(Fr::characteristic()), Fq6::one());
}
#[test]
fn test_product_of_pairings() {
let rng = &mut test_rng();
let a = G1Projective::rand(rng).into_affine();
let b = G2Projective::rand(rng).into_affine();
let c = G1Projective::rand(rng).into_affine();
let d = G2Projective::rand(rng).into_affine();
let ans1 = MNT6_753::pairing(a, b) * &MNT6_753::pairing(c, d);
let ans2 = MNT6_753::product_of_pairings(&[(a.into(), b.into()), (c.into(), d.into())]);
assert_eq!(ans1, ans2);
}
generate_g1_test!(mnt6_753; curve_tests; sw_tests;);
generate_g2_test!(mnt6_753; curve_tests; sw_tests;);
generate_bilinearity_test!(MNT6_753, Fq6);
generate_product_of_pairings_test!(MNT6_753);

+ 4
- 21
mnt6_753/src/fields/tests.rs

@ -2,32 +2,15 @@ use ark_ff::{
fields::{models::fp6_2over3::*, quadratic_extension::QuadExtParameters}, fields::{models::fp6_2over3::*, quadratic_extension::QuadExtParameters},
Field, Field,
}; };
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, test_rng};
use crate::*; use crate::*;
use ark_algebra_test_templates::fields::*;
use ark_algebra_test_templates::{fields::*, generate_field_test};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fr>();
}
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
sqrt_field_test(a);
primefield_test::<Fq>();
}
generate_field_test!(mnt6_753;);
#[test] #[test]
fn test_fq3() { fn test_fq3() {

Loading…
Cancel
Save