arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bls12_377/src/fields/tests.rs
bls12_377/src/fields/tests.rsRaw
  1use ark_algebra_test_templates::*;
  2use ark_ff::{
  3    biginteger::{BigInt, BigInteger, BigInteger384},
  4    fields::{FftField, Field, Fp6Config, PrimeField},
  5    Fp384, One, UniformRand, Zero,
  6};
  7use ark_std::{
  8    cmp::Ordering,
  9    ops::{AddAssign, MulAssign},
 10    test_rng,
 11};
 12
 13use crate::{Fq, Fq12, Fq2, Fq6, Fq6Config, Fr};
 14
 15test_field!(fr; Fr; mont_prime_field);
 16test_field!(fq; Fq; mont_prime_field);
 17test_field!(fq2; Fq2);
 18test_field!(fq6; Fq6);
 19test_field!(fq12; Fq12);
 20
 21#[test]
 22fn test_fq_repr_from() {
 23    assert_eq!(BigInt::from(100u64), BigInt::new([100, 0, 0, 0, 0, 0]));
 24}
 25
 26#[test]
 27fn test_fq_repr_is_odd() {
 28    assert!(!BigInteger384::from(0u64).is_odd());
 29    assert!(BigInteger384::from(0u64).is_even());
 30    assert!(BigInteger384::from(1u64).is_odd());
 31    assert!(!BigInteger384::from(1u64).is_even());
 32    assert!(!BigInteger384::from(324834872u64).is_odd());
 33    assert!(BigInteger384::from(324834872u64).is_even());
 34    assert!(BigInteger384::from(324834873u64).is_odd());
 35    assert!(!BigInteger384::from(324834873u64).is_even());
 36}
 37
 38#[test]
 39fn test_fq_repr_is_zero() {
 40    assert!(BigInteger384::from(0u64).is_zero());
 41    assert!(!BigInteger384::from(1u64).is_zero());
 42    assert!(!BigInt::new([0, 0, 0, 0, 1, 0]).is_zero());
 43}
 44
 45#[test]
 46fn test_fq_repr_num_bits() {
 47    let mut a = BigInteger384::from(0u64);
 48    assert_eq!(0, a.num_bits());
 49    a = BigInteger384::from(1u64);
 50    for i in 1..385 {
 51        assert_eq!(i, a.num_bits());
 52        a.mul2();
 53    }
 54    assert_eq!(0, a.num_bits());
 55}
 56
 57#[test]
 58fn test_fq_num_bits() {
 59    assert_eq!(Fq::MODULUS_BIT_SIZE, 377);
 60}
 61
 62#[test]
 63fn test_fq_root_of_unity() {
 64    assert_eq!(Fq::TWO_ADICITY, 46);
 65    assert_eq!(
 66        Fq::GENERATOR.pow([
 67            0x7510c00000021423,
 68            0x88bee82520005c2d,
 69            0x67cc03d44e3c7bcd,
 70            0x1701b28524ec688b,
 71            0xe9185f1443ab18ec,
 72            0x6b8
 73        ]),
 74        Fq::TWO_ADIC_ROOT_OF_UNITY
 75    );
 76    assert_eq!(
 77        Fq::TWO_ADIC_ROOT_OF_UNITY.pow([1 << Fq::TWO_ADICITY]),
 78        Fq::one()
 79    );
 80    assert!(Fq::GENERATOR.sqrt().is_none());
 81}
 82
 83#[test]
 84fn test_fq_ordering() {
 85    // BigInteger384's ordering is well-tested, but we still need to make sure the
 86    // Fq elements aren't being compared in Montgomery form.
 87    for i in 0..100u64 {
 88        assert!(Fq::from(Fp384::from(i + 1)) > Fq::from(Fp384::from(i)));
 89    }
 90}
 91
 92#[test]
 93fn test_fq_legendre() {
 94    use ark_ff::fields::LegendreSymbol::*;
 95
 96    assert_eq!(QuadraticResidue, Fq::one().legendre());
 97    assert_eq!(Zero, Fq::zero().legendre());
 98    assert_eq!(QuadraticResidue, Fq::from(Fp384::from(4u64)).legendre());
 99    assert_eq!(QuadraticNonResidue, Fq::from(Fp384::from(5u64)).legendre());
100}
101
102#[test]
103fn test_fq2_ordering() {
104    let mut a = Fq2::new(Fq::zero(), Fq::zero());
105    let mut b = a.clone();
106
107    assert!(a.cmp(&b) == Ordering::Equal);
108    b.c0.add_assign(&Fq::one());
109    assert!(a.cmp(&b) == Ordering::Less);
110    a.c0.add_assign(&Fq::one());
111    assert!(a.cmp(&b) == Ordering::Equal);
112    b.c1.add_assign(&Fq::one());
113    assert!(a.cmp(&b) == Ordering::Less);
114    a.c0.add_assign(&Fq::one());
115    assert!(a.cmp(&b) == Ordering::Less);
116    a.c1.add_assign(&Fq::one());
117    assert!(a.cmp(&b) == Ordering::Greater);
118    b.c0.add_assign(&Fq::one());
119    assert!(a.cmp(&b) == Ordering::Equal);
120}
121
122#[test]
123fn test_fq2_basics() {
124    assert_eq!(Fq2::new(Fq::zero(), Fq::zero(),), Fq2::zero());
125    assert_eq!(Fq2::new(Fq::one(), Fq::zero(),), Fq2::one());
126    assert!(Fq2::zero().is_zero());
127    assert!(!Fq2::one().is_zero());
128    assert!(!Fq2::new(Fq::zero(), Fq::one(),).is_zero());
129}
130
131#[test]
132fn test_fq2_legendre() {
133    use ark_ff::fields::LegendreSymbol::*;
134
135    assert_eq!(Zero, Fq2::zero().legendre());
136    // i^2 = -1
137    let mut m1 = -Fq2::one();
138    assert_eq!(QuadraticResidue, m1.legendre());
139    m1 = Fq6Config::mul_fp2_by_nonresidue(&m1);
140    assert_eq!(QuadraticNonResidue, m1.legendre());
141}
142
143#[test]
144fn test_fq6_mul_by_1() {
145    let mut rng = test_rng();
146
147    for _ in 0..1000 {
148        let c1 = Fq2::rand(&mut rng);
149        let mut a = Fq6::rand(&mut rng);
150        let mut b = a;
151
152        a.mul_by_1(&c1);
153        b.mul_assign(&Fq6::new(Fq2::zero(), c1, Fq2::zero()));
154
155        assert_eq!(a, b);
156    }
157}
158
159#[test]
160fn test_fq6_mul_by_01() {
161    let mut rng = test_rng();
162
163    for _ in 0..1000 {
164        let c0 = Fq2::rand(&mut rng);
165        let c1 = Fq2::rand(&mut rng);
166        let mut a = Fq6::rand(&mut rng);
167        let mut b = a;
168
169        a.mul_by_01(&c0, &c1);
170        b.mul_assign(&Fq6::new(c0, c1, Fq2::zero()));
171
172        assert_eq!(a, b);
173    }
174}
175
176#[test]
177fn test_fq12_mul_by_014() {
178    let mut rng = test_rng();
179
180    for _ in 0..1000 {
181        let c0 = Fq2::rand(&mut rng);
182        let c1 = Fq2::rand(&mut rng);
183        let c5 = Fq2::rand(&mut rng);
184        let mut a = Fq12::rand(&mut rng);
185        let mut b = a;
186
187        a.mul_by_014(&c0, &c1, &c5);
188        b.mul_assign(&Fq12::new(
189            Fq6::new(c0, c1, Fq2::zero()),
190            Fq6::new(Fq2::zero(), c5, Fq2::zero()),
191        ));
192
193        assert_eq!(a, b);
194    }
195}
196
197#[test]
198fn test_fq12_mul_by_034() {
199    let mut rng = test_rng();
200
201    for _ in 0..1000 {
202        let c0 = Fq2::rand(&mut rng);
203        let c3 = Fq2::rand(&mut rng);
204        let c4 = Fq2::rand(&mut rng);
205        let mut a = Fq12::rand(&mut rng);
206        let mut b = a;
207
208        a.mul_by_034(&c0, &c3, &c4);
209        b.mul_assign(&Fq12::new(
210            Fq6::new(c0, Fq2::zero(), Fq2::zero()),
211            Fq6::new(c3, c4, Fq2::zero()),
212        ));
213
214        assert_eq!(a, b);
215    }
216}