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::{
  2    fields::*, generate_field_serialization_test, generate_field_test,
  3};
  4use ark_ff::{
  5    biginteger::{BigInt, BigInteger, BigInteger384},
  6    fields::{FftField, Field, Fp6Config, PrimeField, SquareRootField},
  7    One, UniformRand, Zero,
  8};
  9use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
 10use ark_std::{rand::Rng, test_rng};
 11use core::{
 12    cmp::Ordering,
 13    ops::{AddAssign, MulAssign, SubAssign},
 14};
 15
 16use crate::{Fq, Fq12, Fq2, Fq6, Fq6Config, FqConfig, Fr, FrConfig};
 17
 18generate_field_test!(bls12_377; fq2; fq6; fq12; mont(6, 4); );
 19generate_field_serialization_test!(bls12_377; fq2; fq6; 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(BigInteger384::from(i + 1)) > Fq::from(BigInteger384::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!(
 99        QuadraticResidue,
100        Fq::from(BigInteger384::from(4u64)).legendre()
101    );
102    assert_eq!(
103        QuadraticNonResidue,
104        Fq::from(BigInteger384::from(5u64)).legendre()
105    );
106}
107
108#[test]
109fn test_fq2_ordering() {
110    let mut a = Fq2::new(Fq::zero(), Fq::zero());
111    let mut b = a.clone();
112
113    assert!(a.cmp(&b) == Ordering::Equal);
114    b.c0.add_assign(&Fq::one());
115    assert!(a.cmp(&b) == Ordering::Less);
116    a.c0.add_assign(&Fq::one());
117    assert!(a.cmp(&b) == Ordering::Equal);
118    b.c1.add_assign(&Fq::one());
119    assert!(a.cmp(&b) == Ordering::Less);
120    a.c0.add_assign(&Fq::one());
121    assert!(a.cmp(&b) == Ordering::Less);
122    a.c1.add_assign(&Fq::one());
123    assert!(a.cmp(&b) == Ordering::Greater);
124    b.c0.add_assign(&Fq::one());
125    assert!(a.cmp(&b) == Ordering::Equal);
126}
127
128#[test]
129fn test_fq2_basics() {
130    assert_eq!(Fq2::new(Fq::zero(), Fq::zero(),), Fq2::zero());
131    assert_eq!(Fq2::new(Fq::one(), Fq::zero(),), Fq2::one());
132    assert!(Fq2::zero().is_zero());
133    assert!(!Fq2::one().is_zero());
134    assert!(!Fq2::new(Fq::zero(), Fq::one(),).is_zero());
135}
136
137#[test]
138fn test_fq2_legendre() {
139    use ark_ff::fields::LegendreSymbol::*;
140
141    assert_eq!(Zero, Fq2::zero().legendre());
142    // i^2 = -1
143    let mut m1 = -Fq2::one();
144    assert_eq!(QuadraticResidue, m1.legendre());
145    m1 = Fq6Config::mul_fp2_by_nonresidue(&m1);
146    assert_eq!(QuadraticNonResidue, m1.legendre());
147}
148
149#[test]
150fn test_fq6_mul_by_1() {
151    let mut rng = test_rng();
152
153    for _ in 0..1000 {
154        let c1 = Fq2::rand(&mut rng);
155        let mut a = Fq6::rand(&mut rng);
156        let mut b = a;
157
158        a.mul_by_1(&c1);
159        b.mul_assign(&Fq6::new(Fq2::zero(), c1, Fq2::zero()));
160
161        assert_eq!(a, b);
162    }
163}
164
165#[test]
166fn test_fq6_mul_by_01() {
167    let mut rng = test_rng();
168
169    for _ in 0..1000 {
170        let c0 = Fq2::rand(&mut rng);
171        let c1 = Fq2::rand(&mut rng);
172        let mut a = Fq6::rand(&mut rng);
173        let mut b = a;
174
175        a.mul_by_01(&c0, &c1);
176        b.mul_assign(&Fq6::new(c0, c1, Fq2::zero()));
177
178        assert_eq!(a, b);
179    }
180}
181
182#[test]
183fn test_fq12_mul_by_014() {
184    let mut rng = test_rng();
185
186    for _ in 0..1000 {
187        let c0 = Fq2::rand(&mut rng);
188        let c1 = Fq2::rand(&mut rng);
189        let c5 = Fq2::rand(&mut rng);
190        let mut a = Fq12::rand(&mut rng);
191        let mut b = a;
192
193        a.mul_by_014(&c0, &c1, &c5);
194        b.mul_assign(&Fq12::new(
195            Fq6::new(c0, c1, Fq2::zero()),
196            Fq6::new(Fq2::zero(), c5, Fq2::zero()),
197        ));
198
199        assert_eq!(a, b);
200    }
201}
202
203#[test]
204fn test_fq12_mul_by_034() {
205    let mut rng = test_rng();
206
207    for _ in 0..1000 {
208        let c0 = Fq2::rand(&mut rng);
209        let c3 = Fq2::rand(&mut rng);
210        let c4 = Fq2::rand(&mut rng);
211        let mut a = Fq12::rand(&mut rng);
212        let mut b = a;
213
214        a.mul_by_034(&c0, &c3, &c4);
215        b.mul_assign(&Fq12::new(
216            Fq6::new(c0, Fq2::zero(), Fq2::zero()),
217            Fq6::new(c3, c4, Fq2::zero()),
218        ));
219
220        assert_eq!(a, b);
221    }
222}