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