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