ark-curves-cherry-picked/bls12_381/src/fields/tests.rs
bls12_381/src/fields/tests.rsRaw
1use ark_ff::{
2 biginteger::{BigInt, BigInteger, BigInteger384},
3 fields::{
4 FftField, FftParameters, Field, Fp12Parameters, Fp2Parameters, Fp6Parameters, FpParameters,
5 SquareRootField,
6 },
7 One, UniformRand, Zero,
8};
9use ark_std::{
10 cmp::Ordering,
11 ops::{AddAssign, MulAssign, SubAssign},
12 rand::Rng,
13 test_rng,
14};
15
16use crate::{Fq, Fq12, Fq12Parameters, Fq2, Fq2Parameters, Fq6, Fq6Parameters, FqParameters, Fr};
17use ark_algebra_test_templates::{fields::*, generate_field_test};
18
19generate_field_test!(bls12_381; fq2; fq6; fq12;);
20
21#[test]
22fn test_negative_one() {
23 let neg_one = Fq::new(BigInt::new([
24 0x43f5fffffffcaaae,
25 0x32b7fff2ed47fffd,
26 0x7e83a49a2e99d69,
27 0xeca8f3318332bb7a,
28 0xef148d1ea0f4c069,
29 0x40ab3263eff0206,
30 ]));
31 assert_eq!(neg_one, -Fq::one());
32}
33
34#[test]
35fn test_frob_coeffs() {
36 let nqr = -Fq::one();
37
38 assert_eq!(Fq2Parameters::FROBENIUS_COEFF_FP2_C1[0], Fq::one());
39 assert_eq!(
40 Fq2Parameters::FROBENIUS_COEFF_FP2_C1[1],
41 nqr.pow([
42 0xdcff7fffffffd555,
43 0xf55ffff58a9ffff,
44 0xb39869507b587b12,
45 0xb23ba5c279c2895f,
46 0x258dd3db21a5d66b,
47 0xd0088f51cbff34d,
48 ])
49 );
50
51 let nqr = Fq2::new(Fq::one(), Fq::one());
52
53 assert_eq!(Fq6Parameters::FROBENIUS_COEFF_FP6_C1[0], Fq2::one());
54 assert_eq!(
55 Fq6Parameters::FROBENIUS_COEFF_FP6_C1[1],
56 nqr.pow([
57 0x9354ffffffffe38e,
58 0xa395554e5c6aaaa,
59 0xcd104635a790520c,
60 0xcc27c3d6fbd7063f,
61 0x190937e76bc3e447,
62 0x8ab05f8bdd54cde,
63 ])
64 );
65 assert_eq!(
66 Fq6Parameters::FROBENIUS_COEFF_FP6_C1[2],
67 nqr.pow([
68 0xb78e0000097b2f68,
69 0xd44f23b47cbd64e3,
70 0x5cb9668120b069a9,
71 0xccea85f9bf7b3d16,
72 0xdba2c8d7adb356d,
73 0x9cd75ded75d7429,
74 0xfc65c31103284fab,
75 0xc58cb9a9b249ee24,
76 0xccf734c3118a2e9a,
77 0xa0f4304c5a256ce6,
78 0xc3f0d2f8e0ba61f8,
79 0xe167e192ebca97,
80 ])
81 );
82 assert_eq!(
83 Fq6Parameters::FROBENIUS_COEFF_FP6_C1[3],
84 nqr.pow([
85 0xdbc6fcd6f35b9e06,
86 0x997dead10becd6aa,
87 0x9dbbd24c17206460,
88 0x72b97acc6057c45e,
89 0xf8e9a230bf0c628e,
90 0x647ccb1885c63a7,
91 0xce80264fc55bf6ee,
92 0x94d8d716c3939fc4,
93 0xad78f0eb77ee6ee1,
94 0xd6fe49bfe57dc5f9,
95 0x2656d6c15c63647,
96 0xdf6282f111fa903,
97 0x1bdba63e0632b4bb,
98 0x6883597bcaa505eb,
99 0xa56d4ec90c34a982,
100 0x7e4c42823bbe90b2,
101 0xf64728aa6dcb0f20,
102 0x16e57e16ef152f,
103 ])
104 );
105 assert_eq!(
106 Fq6Parameters::FROBENIUS_COEFF_FP6_C1[4],
107 nqr.pow([
108 0x4649add3c71c6d90,
109 0x43caa6528972a865,
110 0xcda8445bbaaa0fbb,
111 0xc93dea665662aa66,
112 0x2863bc891834481d,
113 0x51a0c3f5d4ccbed8,
114 0x9210e660f90ccae9,
115 0xe2bd6836c546d65e,
116 0xf223abbaa7cf778b,
117 0xd4f10b222cf11680,
118 0xd540f5eff4a1962e,
119 0xa123a1f140b56526,
120 0x31ace500636a59f6,
121 0x3a82bc8c8dfa57a9,
122 0x648c511e217fc1f8,
123 0x36c17ffd53a4558f,
124 0x881bef5fd684eefd,
125 0x5d648dbdc5dbb522,
126 0x8fd07bf06e5e59b8,
127 0x8ddec8a9acaa4b51,
128 0x4cc1f8688e2def26,
129 0xa74e63cb492c03de,
130 0x57c968173d1349bb,
131 0x253674e02a866,
132 ])
133 );
134 assert_eq!(
135 Fq6Parameters::FROBENIUS_COEFF_FP6_C1[5],
136 nqr.pow([
137 0xf896f792732eb2be,
138 0x49c86a6d1dc593a1,
139 0xe5b31e94581f91c3,
140 0xe3da5cc0a6b20d7f,
141 0x822caef950e0bfed,
142 0x317ed950b9ee67cd,
143 0xffd664016ee3f6cd,
144 0x77d991c88810b122,
145 0x62e72e635e698264,
146 0x905e1a1a2d22814a,
147 0xf5b7ab3a3f33d981,
148 0x175871b0bc0e25dd,
149 0x1e2e9a63df5c3772,
150 0xe888b1f7445b149d,
151 0x9551c19e5e7e2c24,
152 0xecf21939a3d2d6be,
153 0xd830dbfdab72dbd4,
154 0x7b34af8d622d40c0,
155 0x3df6d20a45671242,
156 0xaf86bee30e21d98,
157 0x41064c1534e5df5d,
158 0xf5f6cabd3164c609,
159 0xa5d14bdf2b7ee65,
160 0xa718c069defc9138,
161 0xdb1447e770e3110e,
162 0xc1b164a9e90af491,
163 0x7180441f9d251602,
164 0x1fd3a5e6a9a893e,
165 0x1e17b779d54d5db,
166 0x3c7afafe3174,
167 ])
168 );
169
170 assert_eq!(Fq6Parameters::FROBENIUS_COEFF_FP6_C2[0], Fq2::one());
171 assert_eq!(
172 Fq6Parameters::FROBENIUS_COEFF_FP6_C2[1],
173 nqr.pow([
174 0x26a9ffffffffc71c,
175 0x1472aaa9cb8d5555,
176 0x9a208c6b4f20a418,
177 0x984f87adf7ae0c7f,
178 0x32126fced787c88f,
179 0x11560bf17baa99bc,
180 ])
181 );
182 assert_eq!(
183 Fq6Parameters::FROBENIUS_COEFF_FP6_C2[2],
184 nqr.pow([
185 0x6f1c000012f65ed0,
186 0xa89e4768f97ac9c7,
187 0xb972cd024160d353,
188 0x99d50bf37ef67a2c,
189 0x1b74591af5b66adb,
190 0x139aebbdaebae852,
191 0xf8cb862206509f56,
192 0x8b1973536493dc49,
193 0x99ee698623145d35,
194 0x41e86098b44ad9cd,
195 0x87e1a5f1c174c3f1,
196 0x1c2cfc325d7952f,
197 ])
198 );
199 assert_eq!(
200 Fq6Parameters::FROBENIUS_COEFF_FP6_C2[3],
201 nqr.pow([
202 0xb78df9ade6b73c0c,
203 0x32fbd5a217d9ad55,
204 0x3b77a4982e40c8c1,
205 0xe572f598c0af88bd,
206 0xf1d344617e18c51c,
207 0xc8f996310b8c74f,
208 0x9d004c9f8ab7eddc,
209 0x29b1ae2d87273f89,
210 0x5af1e1d6efdcddc3,
211 0xadfc937fcafb8bf3,
212 0x4cadad82b8c6c8f,
213 0x1bec505e223f5206,
214 0x37b74c7c0c656976,
215 0xd106b2f7954a0bd6,
216 0x4ada9d9218695304,
217 0xfc988504777d2165,
218 0xec8e5154db961e40,
219 0x2dcafc2dde2a5f,
220 ])
221 );
222 assert_eq!(
223 Fq6Parameters::FROBENIUS_COEFF_FP6_C2[4],
224 nqr.pow([
225 0x8c935ba78e38db20,
226 0x87954ca512e550ca,
227 0x9b5088b775541f76,
228 0x927bd4ccacc554cd,
229 0x50c779123068903b,
230 0xa34187eba9997db0,
231 0x2421ccc1f21995d2,
232 0xc57ad06d8a8dacbd,
233 0xe44757754f9eef17,
234 0xa9e2164459e22d01,
235 0xaa81ebdfe9432c5d,
236 0x424743e2816aca4d,
237 0x6359ca00c6d4b3ed,
238 0x750579191bf4af52,
239 0xc918a23c42ff83f0,
240 0x6d82fffaa748ab1e,
241 0x1037debfad09ddfa,
242 0xbac91b7b8bb76a45,
243 0x1fa0f7e0dcbcb370,
244 0x1bbd9153595496a3,
245 0x9983f0d11c5bde4d,
246 0x4e9cc796925807bc,
247 0xaf92d02e7a269377,
248 0x4a6ce9c0550cc,
249 ])
250 );
251 assert_eq!(
252 Fq6Parameters::FROBENIUS_COEFF_FP6_C2[5],
253 nqr.pow([
254 0xf12def24e65d657c,
255 0x9390d4da3b8b2743,
256 0xcb663d28b03f2386,
257 0xc7b4b9814d641aff,
258 0x4595df2a1c17fdb,
259 0x62fdb2a173dccf9b,
260 0xffacc802ddc7ed9a,
261 0xefb3239110216245,
262 0xc5ce5cc6bcd304c8,
263 0x20bc34345a450294,
264 0xeb6f56747e67b303,
265 0x2eb0e361781c4bbb,
266 0x3c5d34c7beb86ee4,
267 0xd11163ee88b6293a,
268 0x2aa3833cbcfc5849,
269 0xd9e4327347a5ad7d,
270 0xb061b7fb56e5b7a9,
271 0xf6695f1ac45a8181,
272 0x7beda4148ace2484,
273 0x15f0d7dc61c43b30,
274 0x820c982a69cbbeba,
275 0xebed957a62c98c12,
276 0x14ba297be56fdccb,
277 0x4e3180d3bdf92270,
278 0xb6288fcee1c6221d,
279 0x8362c953d215e923,
280 0xe300883f3a4a2c05,
281 0x3fa74bcd535127c,
282 0x3c2f6ef3aa9abb6,
283 0x78f5f5fc62e8,
284 ])
285 );
286
287 assert_eq!(Fq12Parameters::FROBENIUS_COEFF_FP12_C1[0], Fq2::one());
288 assert_eq!(
289 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[1],
290 nqr.pow([
291 0x49aa7ffffffff1c7,
292 0x51caaaa72e35555,
293 0xe688231ad3c82906,
294 0xe613e1eb7deb831f,
295 0xc849bf3b5e1f223,
296 0x45582fc5eeaa66f,
297 ])
298 );
299 assert_eq!(
300 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[2],
301 nqr.pow([
302 0xdbc7000004bd97b4,
303 0xea2791da3e5eb271,
304 0x2e5cb340905834d4,
305 0xe67542fcdfbd9e8b,
306 0x86dd1646bd6d9ab6,
307 0x84e6baef6baeba14,
308 0x7e32e188819427d5,
309 0x62c65cd4d924f712,
310 0x667b9a6188c5174d,
311 0x507a18262d12b673,
312 0xe1f8697c705d30fc,
313 0x70b3f0c975e54b,
314 ])
315 );
316 assert_eq!(
317 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[3],
318 nqr.pow(vec![
319 0x6de37e6b79adcf03,
320 0x4cbef56885f66b55,
321 0x4edde9260b903230,
322 0x395cbd66302be22f,
323 0xfc74d1185f863147,
324 0x323e658c42e31d3,
325 0x67401327e2adfb77,
326 0xca6c6b8b61c9cfe2,
327 0xd6bc7875bbf73770,
328 0xeb7f24dff2bee2fc,
329 0x8132b6b60ae31b23,
330 0x86fb1417888fd481,
331 0x8dedd31f03195a5d,
332 0x3441acbde55282f5,
333 0x52b6a764861a54c1,
334 0x3f2621411ddf4859,
335 0xfb23945536e58790,
336 0xb72bf0b778a97,
337 ])
338 );
339 assert_eq!(
340 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[4],
341 nqr.pow(vec![
342 0xa324d6e9e38e36c8,
343 0xa1e5532944b95432,
344 0x66d4222ddd5507dd,
345 0xe49ef5332b315533,
346 0x1431de448c1a240e,
347 0xa8d061faea665f6c,
348 0x490873307c866574,
349 0xf15eb41b62a36b2f,
350 0x7911d5dd53e7bbc5,
351 0x6a78859116788b40,
352 0x6aa07af7fa50cb17,
353 0x5091d0f8a05ab293,
354 0x98d6728031b52cfb,
355 0x1d415e4646fd2bd4,
356 0xb246288f10bfe0fc,
357 0x9b60bffea9d22ac7,
358 0x440df7afeb42777e,
359 0x2eb246dee2edda91,
360 0xc7e83df8372f2cdc,
361 0x46ef6454d65525a8,
362 0x2660fc344716f793,
363 0xd3a731e5a49601ef,
364 0x2be4b40b9e89a4dd,
365 0x129b3a7015433,
366 ])
367 );
368 assert_eq!(
369 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[5],
370 nqr.pow(vec![
371 0xfc4b7bc93997595f,
372 0xa4e435368ee2c9d0,
373 0xf2d98f4a2c0fc8e1,
374 0xf1ed2e60535906bf,
375 0xc116577ca8705ff6,
376 0x98bf6ca85cf733e6,
377 0x7feb3200b771fb66,
378 0x3becc8e444085891,
379 0x31739731af34c132,
380 0xc82f0d0d169140a5,
381 0xfadbd59d1f99ecc0,
382 0xbac38d85e0712ee,
383 0x8f174d31efae1bb9,
384 0x744458fba22d8a4e,
385 0x4aa8e0cf2f3f1612,
386 0x76790c9cd1e96b5f,
387 0x6c186dfed5b96dea,
388 0x3d9a57c6b116a060,
389 0x1efb690522b38921,
390 0x857c35f718710ecc,
391 0xa083260a9a72efae,
392 0xfafb655e98b26304,
393 0x52e8a5ef95bf732,
394 0x538c6034ef7e489c,
395 0xed8a23f3b8718887,
396 0x60d8b254f4857a48,
397 0x38c0220fce928b01,
398 0x80fe9d2f354d449f,
399 0xf0bdbbceaa6aed,
400 0x1e3d7d7f18ba,
401 ])
402 );
403 assert_eq!(
404 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[6],
405 nqr.pow(vec![
406 0x21219610a012ba3c,
407 0xa5c19ad35375325,
408 0x4e9df1e497674396,
409 0xfb05b717c991c6ef,
410 0x4a1265bca93a32f2,
411 0xd875ff2a7bdc1f66,
412 0xc6d8754736c771b2,
413 0x2d80c759ba5a2ae7,
414 0x138a20df4b03cc1a,
415 0xc22d07fe68e93024,
416 0xd1dc474d3b433133,
417 0xc22aa5e75044e5c,
418 0xf657c6fbf9c17ebf,
419 0xc591a794a58660d,
420 0x2261850ee1453281,
421 0xd17d3bd3b7f5efb4,
422 0xf00cec8ec507d01,
423 0x2a6a775657a00ae6,
424 0x5f098a12ff470719,
425 0x409d194e7b5c5afa,
426 0x1d66478e982af5b,
427 0xda425a5b5e01ca3f,
428 0xf77e4f78747e903c,
429 0x177d49f73732c6fc,
430 0xa9618fecabe0e1f4,
431 0xba5337eac90bd080,
432 0x66fececdbc35d4e7,
433 0xa4cd583203d9206f,
434 0x98391632ceeca596,
435 0x4946b76e1236ad3f,
436 0xa0dec64e60e711a1,
437 0xfcb41ed3605013,
438 0x8ca8f9692ae1e3a9,
439 0xd3078bfc28cc1baf,
440 0xf0536f764e982f82,
441 0x3125f1a2656,
442 ])
443 );
444 assert_eq!(
445 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[7],
446 nqr.pow(vec![
447 0x742754a1f22fdb,
448 0x2a1955c2dec3a702,
449 0x9747b28c796d134e,
450 0xc113a0411f59db79,
451 0x3bb0fa929853bfc1,
452 0x28c3c25f8f6fb487,
453 0xbc2b6c99d3045b34,
454 0x98fb67d6badde1fd,
455 0x48841d76a24d2073,
456 0xd49891145fe93ae6,
457 0xc772b9c8e74d4099,
458 0xccf4e7b9907755bb,
459 0x9cf47b25d42fd908,
460 0x5616a0c347fc445d,
461 0xff93b7a7ad1b8a6d,
462 0xac2099256b78a77a,
463 0x7804a95b02892e1c,
464 0x5cf59ca7bfd69776,
465 0xa7023502acd3c866,
466 0xc76f4982fcf8f37,
467 0x51862a5a57ac986e,
468 0x38b80ed72b1b1023,
469 0x4a291812066a61e1,
470 0xcd8a685eff45631,
471 0x3f40f708764e4fa5,
472 0x8aa0441891285092,
473 0x9eff60d71cdf0a9,
474 0x4fdd9d56517e2bfa,
475 0x1f3c80d74a28bc85,
476 0x24617417c064b648,
477 0x7ddda1e4385d5088,
478 0xf9e132b11dd32a16,
479 0xcc957cb8ef66ab99,
480 0xd4f206d37cb752c5,
481 0x40de343f28ad616b,
482 0x8d1f24379068f0e3,
483 0x6f31d7947ea21137,
484 0x27311f9c32184061,
485 0x9eea0664cc78ce5f,
486 0x7d4151f6fea9a0da,
487 0x454096fa75bd571a,
488 0x4fe0f20ecb,
489 ])
490 );
491 assert_eq!(
492 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[8],
493 nqr.pow(vec![
494 0x802f5720d0b25710,
495 0x6714f0a258b85c7c,
496 0x31394c90afdf16e,
497 0xe9d2b0c64f957b19,
498 0xe67c0d9c5e7903ee,
499 0x3156fdc5443ea8ef,
500 0x7c4c50524d88c892,
501 0xc99dc8990c0ad244,
502 0xd37ababf3649a896,
503 0x76fe4b838ff7a20c,
504 0xcf69ee2cec728db3,
505 0xb83535548e5f41,
506 0x371147684ccb0c23,
507 0x194f6f4fa500db52,
508 0xc4571dc78a4c5374,
509 0xe4d46d479999ca97,
510 0x76b6785a615a151c,
511 0xcceb8bcea7eaf8c1,
512 0x80d87a6fbe5ae687,
513 0x6a97ddddb85ce85,
514 0xd783958f26034204,
515 0x7144506f2e2e8590,
516 0x948693d377aef166,
517 0x8364621ed6f96056,
518 0xf021777c4c09ee2d,
519 0xc6cf5e746ecd50b,
520 0xa2337b7aa22743df,
521 0xae753f8bbacab39c,
522 0xfc782a9e34d3c1cc,
523 0x21b827324fe494d9,
524 0x5692ce350ed03b38,
525 0xf323a2b3cd0481b0,
526 0xe859c97a4ccad2e3,
527 0x48434b70381e4503,
528 0x46042d62e4132ed8,
529 0x48c4d6f56122e2f2,
530 0xf87711ab9f5c1af7,
531 0xb14b7a054759b469,
532 0x8eb0a96993ffa9aa,
533 0x9b21fb6fc58b760c,
534 0xf3abdd115d2e7d25,
535 0xf7beac3d4d12409c,
536 0x40a5585cce69bf03,
537 0x697881e1ba22d5a8,
538 0x3d6c04e6ad373fd9,
539 0x849871bf627be886,
540 0x550f4b9b71b28ef9,
541 0x81d2e0d78,
542 ])
543 );
544 assert_eq!(
545 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[9],
546 nqr.pow(vec![
547 0x4af4accf7de0b977,
548 0x742485e21805b4ee,
549 0xee388fbc4ac36dec,
550 0x1e199da57ad178a,
551 0xc27c12b292c6726a,
552 0x162e6ed84505b5e8,
553 0xe191683f336e09df,
554 0x17deb7e8d1e0fce6,
555 0xd944f19ad06f5836,
556 0x4c5f5e59f6276026,
557 0xf1ba9c7c148a38a8,
558 0xd205fe2dba72b326,
559 0x9a2cf2a4c289824e,
560 0x4f47ad512c39e24d,
561 0xc5894d984000ea09,
562 0x2974c03ff7cf01fa,
563 0xfcd243b48cb99a22,
564 0x2b5150c9313ac1e8,
565 0x9089f37c7fc80eda,
566 0x989540cc9a7aea56,
567 0x1ab1d4e337e63018,
568 0x42b546c30d357e43,
569 0x1c6abc04f76233d9,
570 0x78b3b8d88bf73e47,
571 0x151c4e4c45dc68e6,
572 0x519a79c4f54397ed,
573 0x93f5b51535a127c5,
574 0x5fc51b6f52fa153e,
575 0x2e0504f2d4a965c3,
576 0xc85bd3a3da52bffe,
577 0x98c60957a46a89ef,
578 0x48c03b5976b91cae,
579 0xc6598040a0a61438,
580 0xbf0b49dc255953af,
581 0xb78dff905b628ab4,
582 0x68140b797ba74ab8,
583 0x116cf037991d1143,
584 0x2f7fe82e58acb0b8,
585 0xc20bf7a8f7be5d45,
586 0x86c2905c338d5709,
587 0xff13a3ae6c8ace3d,
588 0xb6f95e2282d08337,
589 0xd49f7b313e9cbf29,
590 0xf794517193a1ce8c,
591 0x39641fecb596a874,
592 0x411c4c4edf462fb3,
593 0x3f8cd55c10cf25b4,
594 0x2bdd7ea165e860b6,
595 0xacd7d2cef4caa193,
596 0x6558a1d09a05f96,
597 0x1f52b5f5b546fc20,
598 0x4ee22a5a8c250c12,
599 0xd3a63a54a205b6b3,
600 0xd2ff5be8,
601 ])
602 );
603 assert_eq!(
604 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[10],
605 nqr.pow(vec![
606 0xe5953a4f96cdda44,
607 0x336b2d734cbc32bb,
608 0x3f79bfe3cd7410e,
609 0x267ae19aaa0f0332,
610 0x85a9c4db78d5c749,
611 0x90996b046b5dc7d8,
612 0x8945eae9820afc6a,
613 0x2644ddea2b036bd,
614 0x39898e35ac2e3819,
615 0x2574eab095659ab9,
616 0x65953d51ac5ea798,
617 0xc6b8c7afe6752466,
618 0x40e9e993e9286544,
619 0x7e0ad34ad9700ea0,
620 0xac1015eba2c69222,
621 0x24f057a19239b5d8,
622 0x2043b48c8a3767eb,
623 0x1117c124a75d7ff4,
624 0x433cfd1a09fb3ce7,
625 0x25b087ce4bcf7fb,
626 0xbcee0dc53a3e5bdb,
627 0xbffda040cf028735,
628 0xf7cf103a25512acc,
629 0x31d4ecda673130b9,
630 0xea0906dab18461e6,
631 0x5a40585a5ac3050d,
632 0x803358fc14fd0eda,
633 0x3678ca654eada770,
634 0x7b91a1293a45e33e,
635 0xcd5e5b8ea8530e43,
636 0x21ae563ab34da266,
637 0xecb00dad60df8894,
638 0x77fe53e652facfef,
639 0x9b7d1ad0b00244ec,
640 0xe695df5ca73f801,
641 0x23cdb21feeab0149,
642 0x14de113e7ea810d9,
643 0x52600cd958dac7e7,
644 0xc83392c14667e488,
645 0x9f808444bc1717fc,
646 0x56facb4bcf7c788f,
647 0x8bcad53245fc3ca0,
648 0xdef661e83f27d81c,
649 0x37d4ebcac9ad87e5,
650 0x6fe8b24f5cdb9324,
651 0xee08a26c1197654c,
652 0xc98b22f65f237e9a,
653 0xf54873a908ed3401,
654 0x6e1cb951d41f3f3,
655 0x290b2250a54e8df6,
656 0x7f36d51eb1db669e,
657 0xb08c7ed81a6ee43e,
658 0x95e1c90fb092f680,
659 0x429e4afd0e8b820,
660 0x2c14a83ee87d715c,
661 0xf37267575cfc8af5,
662 0xb99e9afeda3c2c30,
663 0x8f0f69da75792d5a,
664 0x35074a85a533c73,
665 0x156ed119,
666 ])
667 );
668 assert_eq!(
669 Fq12Parameters::FROBENIUS_COEFF_FP12_C1[11],
670 nqr.pow(vec![
671 0x107db680942de533,
672 0x6262b24d2052393b,
673 0x6136df824159ebc,
674 0xedb052c9970c5deb,
675 0xca813aea916c3777,
676 0xf49dacb9d76c1788,
677 0x624941bd372933bb,
678 0xa5e60c2520638331,
679 0xb38b661683411074,
680 0x1d2c9af4c43d962b,
681 0x17d807a0f14aa830,
682 0x6e6581a51012c108,
683 0x668a537e5b35e6f5,
684 0x6c396cf3782dca5d,
685 0x33b679d1bff536ed,
686 0x736cce41805d90aa,
687 0x8a562f369eb680bf,
688 0x9f61aa208a11ded8,
689 0x43dd89dd94d20f35,
690 0xcf84c6610575c10a,
691 0x9f318d49cf2fe8e6,
692 0xbbc6e5f25a6e434e,
693 0x6528c433d11d987b,
694 0xffced71cc48c0e8a,
695 0x4cbb1474f4cb2a26,
696 0x66a035c0b28b7231,
697 0xa6f2875faa1a82ae,
698 0xdd1ea3deff818b02,
699 0xe0cfdf0dcdecf701,
700 0x9aefa231f2f6d23,
701 0xfb251297efa06746,
702 0x5a40d367df985538,
703 0x1ea31d69ab506fed,
704 0xc64ea8280e89a73f,
705 0x969acf9f2d4496f4,
706 0xe84c9181ee60c52c,
707 0xc60f27fc19fc6ca4,
708 0x760b33d850154048,
709 0x84f69080f66c8457,
710 0xc0192ba0fabf640e,
711 0xd2c338765c23a3a8,
712 0xa7838c20f02cec6c,
713 0xb7cf01d020572877,
714 0xd63ffaeba0be200a,
715 0xf7492baeb5f041ac,
716 0x8602c5212170d117,
717 0xad9b2e83a5a42068,
718 0x2461829b3ba1083e,
719 0x7c34650da5295273,
720 0xdc824ba800a8265a,
721 0xd18d9b47836af7b2,
722 0x3af78945c58cbf4d,
723 0x7ed9575b8596906c,
724 0x6d0c133895009a66,
725 0x53bc1247ea349fe1,
726 0x6b3063078d41aa7a,
727 0x6184acd8cd880b33,
728 0x76f4d15503fd1b96,
729 0x7a9afd61eef25746,
730 0xce974aadece60609,
731 0x88ca59546a8ceafd,
732 0x6d29391c41a0ac07,
733 0x443843a60e0f46a6,
734 0xa1590f62fd2602c7,
735 0x536d5b15b514373f,
736 0x22d582b,
737 ])
738 );
739}
740
741#[test]
742fn test_neg_one() {
743 let o = -Fq::one();
744
745 let thing: [u64; 6] = [
746 0x43f5fffffffcaaae,
747 0x32b7fff2ed47fffd,
748 0x7e83a49a2e99d69,
749 0xeca8f3318332bb7a,
750 0xef148d1ea0f4c069,
751 0x40ab3263eff0206,
752 ];
753 println!("{:?}", thing);
754 let negative_one = Fq::new(BigInt::new(thing));
755
756 assert_eq!(negative_one, o);
757}
758
759#[test]
760fn test_fq_repr_from() {
761 assert_eq!(BigInt::from(100u64), BigInt::new([100, 0, 0, 0, 0, 0]));
762}
763
764#[test]
765fn test_fq_repr_is_odd() {
766 assert!(!BigInteger384::from(0).is_odd());
767 assert!(BigInteger384::from(0).is_even());
768 assert!(BigInteger384::from(1).is_odd());
769 assert!(!BigInteger384::from(1).is_even());
770 assert!(!BigInteger384::from(324834872).is_odd());
771 assert!(BigInteger384::from(324834872).is_even());
772 assert!(BigInteger384::from(324834873).is_odd());
773 assert!(!BigInteger384::from(324834873).is_even());
774}
775
776#[test]
777fn test_fq_repr_is_zero() {
778 assert!(BigInteger384::from(0u64).is_zero());
779 assert!(!BigInteger384::from(1u64).is_zero());
780 assert!(!BigInt::new([0, 0, 0, 0, 1, 0]).is_zero());
781}
782
783#[test]
784fn test_fq_repr_div2() {
785 let mut a = BigInt::new([
786 0x8b0ad39f8dd7482a,
787 0x147221c9a7178b69,
788 0x54764cb08d8a6aa0,
789 0x8519d708e1d83041,
790 0x41f82777bd13fdb,
791 0xf43944578f9b771b,
792 ]);
793 a.div2();
794 assert_eq!(
795 a,
796 BigInt::new([
797 0xc58569cfc6eba415,
798 0xa3910e4d38bc5b4,
799 0xaa3b265846c53550,
800 0xc28ceb8470ec1820,
801 0x820fc13bbde89fed,
802 0x7a1ca22bc7cdbb8d,
803 ])
804 );
805 for _ in 0..10 {
806 a.div2();
807 }
808 assert_eq!(
809 a,
810 BigInt::new([
811 0x6d31615a73f1bae9,
812 0x54028e443934e2f1,
813 0x82a8ec99611b14d,
814 0xfb70a33ae11c3b06,
815 0xe36083f04eef7a27,
816 0x1e87288af1f36e,
817 ])
818 );
819 for _ in 0..300 {
820 a.div2();
821 }
822 assert_eq!(
823 a,
824 BigInt::new([0x7288af1f36ee3608, 0x1e8, 0x0, 0x0, 0x0, 0x0])
825 );
826 for _ in 0..50 {
827 a.div2();
828 }
829 assert_eq!(a, BigInt::new([0x7a1ca2, 0x0, 0x0, 0x0, 0x0, 0x0]));
830 for _ in 0..22 {
831 a.div2();
832 }
833 assert_eq!(a, BigInt::new([0x1, 0x0, 0x0, 0x0, 0x0, 0x0]));
834 a.div2();
835 assert!(a.is_zero());
836}
837
838#[test]
839fn test_fq_repr_divn() {
840 let mut a = BigInt::new([
841 0xaa5cdd6172847ffd,
842 0x43242c06aed55287,
843 0x9ddd5b312f3dd104,
844 0xc5541fd48046b7e7,
845 0x16080cf4071e0b05,
846 0x1225f2901aea514e,
847 ]);
848 a.divn(0);
849 assert_eq!(
850 a,
851 BigInt::new([
852 0xaa5cdd6172847ffd,
853 0x43242c06aed55287,
854 0x9ddd5b312f3dd104,
855 0xc5541fd48046b7e7,
856 0x16080cf4071e0b05,
857 0x1225f2901aea514e,
858 ])
859 );
860 a.divn(1);
861 assert_eq!(
862 a,
863 BigInt::new([
864 0xd52e6eb0b9423ffe,
865 0x21921603576aa943,
866 0xceeead98979ee882,
867 0xe2aa0fea40235bf3,
868 0xb04067a038f0582,
869 0x912f9480d7528a7,
870 ])
871 );
872 a.divn(50);
873 assert_eq!(
874 a,
875 BigInt::new([
876 0x8580d5daaa50f54b,
877 0xab6625e7ba208864,
878 0x83fa9008d6fcf3bb,
879 0x19e80e3c160b8aa,
880 0xbe52035d4a29c2c1,
881 0x244,
882 ])
883 );
884 a.divn(130);
885 assert_eq!(
886 a,
887 BigInt::new([
888 0xa0fea40235bf3cee,
889 0x4067a038f0582e2a,
890 0x2f9480d7528a70b0,
891 0x91,
892 0x0,
893 0x0,
894 ])
895 );
896 a.divn(64);
897 assert_eq!(
898 a,
899 BigInt::new([0x4067a038f0582e2a, 0x2f9480d7528a70b0, 0x91, 0x0, 0x0, 0x0])
900 );
901}
902
903#[test]
904fn test_fq_repr_mul2() {
905 let mut a = BigInteger384::from(23712937547);
906 a.mul2();
907 assert_eq!(a, BigInt::new([0xb0acd6c96, 0x0, 0x0, 0x0, 0x0, 0x0]));
908 for _ in 0..60 {
909 a.mul2();
910 }
911 assert_eq!(
912 a,
913 BigInt::new([0x6000000000000000, 0xb0acd6c9, 0x0, 0x0, 0x0, 0x0])
914 );
915 for _ in 0..300 {
916 a.mul2();
917 }
918 assert_eq!(
919 a,
920 BigInt::new([0x0, 0x0, 0x0, 0x0, 0x0, 0xcd6c960000000000])
921 );
922 for _ in 0..17 {
923 a.mul2();
924 }
925 assert_eq!(
926 a,
927 BigInt::new([0x0, 0x0, 0x0, 0x0, 0x0, 0x2c00000000000000])
928 );
929 for _ in 0..6 {
930 a.mul2();
931 }
932 assert!(a.is_zero());
933}
934
935#[test]
936fn test_fq_repr_num_bits() {
937 let mut a = BigInteger384::from(0);
938 assert_eq!(0, a.num_bits());
939 a = BigInteger384::from(1);
940 for i in 1..385 {
941 assert_eq!(i, a.num_bits());
942 a.mul2();
943 }
944 assert_eq!(0, a.num_bits());
945}
946
947#[test]
948fn test_fq_repr_sub_noborrow() {
949 let mut rng = ark_std::test_rng();
950
951 let mut t = BigInt::new([
952 0x827a4a08041ebd9,
953 0x3c239f3dcc8f0d6b,
954 0x9ab46a912d555364,
955 0x196936b17b43910b,
956 0xad0eb3948a5c34fd,
957 0xd56f7b5ab8b5ce8,
958 ]);
959 t.sub_noborrow(&BigInt::new([
960 0xc7867917187ca02b,
961 0x5d75679d4911ffef,
962 0x8c5b3e48b1a71c15,
963 0x6a427ae846fd66aa,
964 0x7a37e7265ee1eaf9,
965 0x7c0577a26f59d5,
966 ]));
967 assert!(
968 t == BigInt::new([
969 0x40a12b8967c54bae,
970 0xdeae37a0837d0d7b,
971 0xe592c487bae374e,
972 0xaf26bbc934462a61,
973 0x32d6cc6e2b7a4a03,
974 0xcdaf23e091c0313,
975 ])
976 );
977
978 for _ in 0..1000 {
979 let mut a = BigInteger384::rand(&mut rng);
980 a.0[5] >>= 30;
981 let mut b = a;
982 for _ in 0..10 {
983 b.mul2();
984 }
985 let mut c = b;
986 for _ in 0..10 {
987 c.mul2();
988 }
989
990 assert!(a < b);
991 assert!(b < c);
992
993 let mut csub_ba = c;
994 csub_ba.sub_noborrow(&b);
995 csub_ba.sub_noborrow(&a);
996
997 let mut csub_ab = c;
998 csub_ab.sub_noborrow(&a);
999 csub_ab.sub_noborrow(&b);
1000
1001 assert_eq!(csub_ab, csub_ba);
1002 }
1003
1004 // Subtracting q+1 from q should produce -1 (mod 2**384)
1005 let mut qplusone = BigInt::new([
1006 0xb9feffffffffaaab,
1007 0x1eabfffeb153ffff,
1008 0x6730d2a0f6b0f624,
1009 0x64774b84f38512bf,
1010 0x4b1ba7b6434bacd7,
1011 0x1a0111ea397fe69a,
1012 ]);
1013 qplusone.sub_noborrow(&BigInt::new([
1014 0xb9feffffffffaaac,
1015 0x1eabfffeb153ffff,
1016 0x6730d2a0f6b0f624,
1017 0x64774b84f38512bf,
1018 0x4b1ba7b6434bacd7,
1019 0x1a0111ea397fe69a,
1020 ]));
1021 assert_eq!(
1022 qplusone,
1023 BigInt::new([
1024 0xffffffffffffffff,
1025 0xffffffffffffffff,
1026 0xffffffffffffffff,
1027 0xffffffffffffffff,
1028 0xffffffffffffffff,
1029 0xffffffffffffffff,
1030 ])
1031 );
1032}
1033
1034#[test]
1035fn test_fq_repr_add_nocarry() {
1036 let mut rng = ark_std::test_rng();
1037
1038 let mut t = BigInt::new([
1039 0x827a4a08041ebd9,
1040 0x3c239f3dcc8f0d6b,
1041 0x9ab46a912d555364,
1042 0x196936b17b43910b,
1043 0xad0eb3948a5c34fd,
1044 0xd56f7b5ab8b5ce8,
1045 ]);
1046 t.add_nocarry(&BigInt::new([
1047 0xc7867917187ca02b,
1048 0x5d75679d4911ffef,
1049 0x8c5b3e48b1a71c15,
1050 0x6a427ae846fd66aa,
1051 0x7a37e7265ee1eaf9,
1052 0x7c0577a26f59d5,
1053 ]));
1054 assert!(
1055 t == BigInt::new([
1056 0xcfae1db798be8c04,
1057 0x999906db15a10d5a,
1058 0x270fa8d9defc6f79,
1059 0x83abb199c240f7b6,
1060 0x27469abae93e1ff6,
1061 0xdd2fd2d4dfab6be,
1062 ])
1063 );
1064
1065 // Test for the associativity of addition.
1066 for _ in 0..1000 {
1067 let mut a = BigInteger384::rand(&mut rng);
1068 let mut b = BigInteger384::rand(&mut rng);
1069 let mut c = BigInteger384::rand(&mut rng);
1070
1071 // Unset the first few bits, so that overflow won't occur.
1072 a.0[5] >>= 3;
1073 b.0[5] >>= 3;
1074 c.0[5] >>= 3;
1075
1076 let mut abc = a;
1077 abc.add_nocarry(&b);
1078 abc.add_nocarry(&c);
1079
1080 let mut acb = a;
1081 acb.add_nocarry(&c);
1082 acb.add_nocarry(&b);
1083
1084 let mut bac = b;
1085 bac.add_nocarry(&a);
1086 bac.add_nocarry(&c);
1087
1088 let mut bca = b;
1089 bca.add_nocarry(&c);
1090 bca.add_nocarry(&a);
1091
1092 let mut cab = c;
1093 cab.add_nocarry(&a);
1094 cab.add_nocarry(&b);
1095
1096 let mut cba = c;
1097 cba.add_nocarry(&b);
1098 cba.add_nocarry(&a);
1099
1100 assert_eq!(abc, acb);
1101 assert_eq!(abc, bac);
1102 assert_eq!(abc, bca);
1103 assert_eq!(abc, cab);
1104 assert_eq!(abc, cba);
1105 }
1106
1107 // Adding 1 to (2^384 - 1) should produce zero
1108 let mut x = BigInt::new([
1109 0xffffffffffffffff,
1110 0xffffffffffffffff,
1111 0xffffffffffffffff,
1112 0xffffffffffffffff,
1113 0xffffffffffffffff,
1114 0xffffffffffffffff,
1115 ]);
1116 x.add_nocarry(&BigInteger384::from(1));
1117 assert!(x.is_zero());
1118}
1119
1120#[test]
1121fn test_fq_num_bits() {
1122 assert_eq!(FqParameters::MODULUS_BITS, 381);
1123 assert_eq!(FqParameters::CAPACITY, 380);
1124}
1125
1126#[test]
1127fn test_fq_root_of_unity() {
1128 assert_eq!(FqParameters::TWO_ADICITY, 1);
1129 assert_eq!(
1130 Fq::multiplicative_generator(),
1131 Fq::from(BigInteger384::from(2))
1132 );
1133 assert_eq!(
1134 Fq::multiplicative_generator().pow([
1135 0xdcff7fffffffd555,
1136 0xf55ffff58a9ffff,
1137 0xb39869507b587b12,
1138 0xb23ba5c279c2895f,
1139 0x258dd3db21a5d66b,
1140 0xd0088f51cbff34d,
1141 ]),
1142 Fq::two_adic_root_of_unity()
1143 );
1144 assert_eq!(
1145 Fq::two_adic_root_of_unity().pow([1 << FqParameters::TWO_ADICITY]),
1146 Fq::one()
1147 );
1148 assert!(Fq::multiplicative_generator().sqrt().is_none());
1149}
1150
1151// #[test]
1152// fn fq_field_tests() {
1153// ::tests::field::random_field_tests::<Fq>();
1154// ::tests::field::random_sqrt_tests::<Fq>();
1155// ::tests::field::random_frobenius_tests::<Fq, _>(Fq::char(), 13);
1156// ::tests::field::from_str_tests::<Fq>();
1157// }
1158
1159#[test]
1160fn test_fq_ordering() {
1161 // BigInteger384's ordering is well-tested, but we still need to make sure the
1162 // Fq elements aren't being compared in Montgomery form.
1163 for i in 0..100 {
1164 assert!(Fq::from(BigInteger384::from(i + 1)) > Fq::from(BigInteger384::from(i)));
1165 }
1166}
1167
1168// #[test]
1169// fn fq_repr_tests() {
1170// ::tests::repr::random_repr_tests::<BigInteger384>();
1171// }
1172
1173#[test]
1174fn test_fq_legendre() {
1175 use ark_ff::fields::LegendreSymbol::*;
1176
1177 assert_eq!(QuadraticResidue, Fq::one().legendre());
1178 assert_eq!(Zero, Fq::zero().legendre());
1179
1180 assert_eq!(
1181 QuadraticNonResidue,
1182 Fq::from(BigInteger384::from(2)).legendre()
1183 );
1184 assert_eq!(
1185 QuadraticResidue,
1186 Fq::from(BigInteger384::from(4)).legendre()
1187 );
1188
1189 let e = BigInt::new([
1190 0x52a112f249778642,
1191 0xd0bedb989b7991f,
1192 0xdad3b6681aa63c05,
1193 0xf2efc0bb4721b283,
1194 0x6057a98f18c24733,
1195 0x1022c2fd122889e4,
1196 ]);
1197 assert_eq!(QuadraticNonResidue, Fq::from(e).legendre());
1198 let e = BigInt::new([
1199 0x6dae594e53a96c74,
1200 0x19b16ca9ba64b37b,
1201 0x5c764661a59bfc68,
1202 0xaa346e9b31c60a,
1203 0x346059f9d87a9fa9,
1204 0x1d61ac6bfd5c88b,
1205 ]);
1206 assert_eq!(QuadraticResidue, Fq::from(e).legendre());
1207}
1208
1209#[test]
1210fn test_fq2_ordering() {
1211 let mut a = Fq2::new(Fq::zero(), Fq::zero());
1212
1213 let mut b = a.clone();
1214
1215 assert!(a.cmp(&b) == Ordering::Equal);
1216 b.c0.add_assign(&Fq::one());
1217 assert!(a.cmp(&b) == Ordering::Less);
1218 a.c0.add_assign(&Fq::one());
1219 assert!(a.cmp(&b) == Ordering::Equal);
1220 b.c1.add_assign(&Fq::one());
1221 assert!(a.cmp(&b) == Ordering::Less);
1222 a.c0.add_assign(&Fq::one());
1223 assert!(a.cmp(&b) == Ordering::Less);
1224 a.c1.add_assign(&Fq::one());
1225 assert!(a.cmp(&b) == Ordering::Greater);
1226 b.c0.add_assign(&Fq::one());
1227 assert!(a.cmp(&b) == Ordering::Equal);
1228}
1229
1230#[test]
1231fn test_fq2_basics() {
1232 assert_eq!(Fq2::new(Fq::zero(), Fq::zero(),), Fq2::zero());
1233 assert_eq!(Fq2::new(Fq::one(), Fq::zero(),), Fq2::one());
1234 assert!(Fq2::zero().is_zero());
1235 assert!(!Fq2::one().is_zero());
1236 assert!(!Fq2::new(Fq::zero(), Fq::one(),).is_zero());
1237}
1238
1239#[test]
1240fn test_fq2_squaring() {
1241 let a = Fq2::new(Fq::one(), Fq::one()).square(); // u + 1
1242 assert_eq!(a, Fq2::new(Fq::zero(), Fq::from(BigInteger384::from(2)),)); // 2u
1243
1244 let a = Fq2::new(Fq::zero(), Fq::one()).square(); // u
1245 assert_eq!(a, {
1246 let neg1 = -Fq::one();
1247 Fq2::new(neg1, Fq::zero())
1248 }); // -1
1249
1250 let mut a = Fq2::new(
1251 Fq::from(BigInt::new([
1252 0x9c2c6309bbf8b598,
1253 0x4eef5c946536f602,
1254 0x90e34aab6fb6a6bd,
1255 0xf7f295a94e58ae7c,
1256 0x41b76dcc1c3fbe5e,
1257 0x7080c5fa1d8e042,
1258 ])),
1259 Fq::from(BigInt::new([
1260 0x38f473b3c870a4ab,
1261 0x6ad3291177c8c7e5,
1262 0xdac5a4c911a4353e,
1263 0xbfb99020604137a0,
1264 0xfc58a7b7be815407,
1265 0x10d1615e75250a21,
1266 ])),
1267 );
1268 a.square_in_place();
1269 assert_eq!(
1270 a,
1271 Fq2::new(
1272 Fq::from(BigInt::new([
1273 0xf262c28c538bcf68,
1274 0xb9f2a66eae1073ba,
1275 0xdc46ab8fad67ae0,
1276 0xcb674157618da176,
1277 0x4cf17b5893c3d327,
1278 0x7eac81369c43361,
1279 ])),
1280 Fq::from(BigInt::new([
1281 0xc1579cf58e980cf8,
1282 0xa23eb7e12dd54d98,
1283 0xe75138bce4cec7aa,
1284 0x38d0d7275a9689e1,
1285 0x739c983042779a65,
1286 0x1542a61c8a8db994,
1287 ])),
1288 )
1289 );
1290}
1291
1292#[test]
1293fn test_fq2_mul() {
1294 let mut a = Fq2::new(
1295 Fq::from(BigInt::new([
1296 0x85c9f989e1461f03,
1297 0xa2e33c333449a1d6,
1298 0x41e461154a7354a3,
1299 0x9ee53e7e84d7532e,
1300 0x1c202d8ed97afb45,
1301 0x51d3f9253e2516f,
1302 ])),
1303 Fq::from(BigInt::new([
1304 0xa7348a8b511aedcf,
1305 0x143c215d8176b319,
1306 0x4cc48081c09b8903,
1307 0x9533e4a9a5158be,
1308 0x7a5e1ecb676d65f9,
1309 0x180c3ee46656b008,
1310 ])),
1311 );
1312 a.mul_assign(&Fq2::new(
1313 Fq::from(BigInt::new([
1314 0xe21f9169805f537e,
1315 0xfc87e62e179c285d,
1316 0x27ece175be07a531,
1317 0xcd460f9f0c23e430,
1318 0x6c9110292bfa409,
1319 0x2c93a72eb8af83e,
1320 ])),
1321 Fq::from(BigInt::new([
1322 0x4b1c3f936d8992d4,
1323 0x1d2a72916dba4c8a,
1324 0x8871c508658d1e5f,
1325 0x57a06d3135a752ae,
1326 0x634cd3c6c565096d,
1327 0x19e17334d4e93558,
1328 ])),
1329 ));
1330 assert_eq!(
1331 a,
1332 Fq2::new(
1333 Fq::from(BigInt::new([
1334 0x95b5127e6360c7e4,
1335 0xde29c31a19a6937e,
1336 0xf61a96dacf5a39bc,
1337 0x5511fe4d84ee5f78,
1338 0x5310a202d92f9963,
1339 0x1751afbe166e5399,
1340 ])),
1341 Fq::from(BigInt::new([
1342 0x84af0e1bd630117a,
1343 0x6c63cd4da2c2aa7,
1344 0x5ba6e5430e883d40,
1345 0xc975106579c275ee,
1346 0x33a9ac82ce4c5083,
1347 0x1ef1a36c201589d,
1348 ])),
1349 )
1350 );
1351}
1352
1353#[test]
1354fn test_fq2_inverse() {
1355 assert!(Fq2::zero().inverse().is_none());
1356
1357 let a = Fq2::new(
1358 Fq::from(BigInt::new([
1359 0x85c9f989e1461f03,
1360 0xa2e33c333449a1d6,
1361 0x41e461154a7354a3,
1362 0x9ee53e7e84d7532e,
1363 0x1c202d8ed97afb45,
1364 0x51d3f9253e2516f,
1365 ])),
1366 Fq::from(BigInt::new([
1367 0xa7348a8b511aedcf,
1368 0x143c215d8176b319,
1369 0x4cc48081c09b8903,
1370 0x9533e4a9a5158be,
1371 0x7a5e1ecb676d65f9,
1372 0x180c3ee46656b008,
1373 ])),
1374 );
1375 let a = a.inverse().unwrap();
1376 assert_eq!(
1377 a,
1378 Fq2::new(
1379 Fq::from(BigInt::new([
1380 0x70300f9bcb9e594,
1381 0xe5ecda5fdafddbb2,
1382 0x64bef617d2915a8f,
1383 0xdfba703293941c30,
1384 0xa6c3d8f9586f2636,
1385 0x1351ef01941b70c4,
1386 ])),
1387 Fq::from(BigInt::new([
1388 0x8c39fd76a8312cb4,
1389 0x15d7b6b95defbff0,
1390 0x947143f89faedee9,
1391 0xcbf651a0f367afb2,
1392 0xdf4e54f0d3ef15a6,
1393 0x103bdf241afb0019,
1394 ])),
1395 )
1396 );
1397}
1398
1399#[test]
1400fn test_fq2_addition() {
1401 let mut a = Fq2::new(
1402 Fq::from(BigInt::new([
1403 0x2d0078036923ffc7,
1404 0x11e59ea221a3b6d2,
1405 0x8b1a52e0a90f59ed,
1406 0xb966ce3bc2108b13,
1407 0xccc649c4b9532bf3,
1408 0xf8d295b2ded9dc,
1409 ])),
1410 Fq::from(BigInt::new([
1411 0x977df6efcdaee0db,
1412 0x946ae52d684fa7ed,
1413 0xbe203411c66fb3a5,
1414 0xb3f8afc0ee248cad,
1415 0x4e464dea5bcfd41e,
1416 0x12d1137b8a6a837,
1417 ])),
1418 );
1419 a.add_assign(&Fq2::new(
1420 Fq::from(BigInt::new([
1421 0x619a02d78dc70ef2,
1422 0xb93adfc9119e33e8,
1423 0x4bf0b99a9f0dca12,
1424 0x3b88899a42a6318f,
1425 0x986a4a62fa82a49d,
1426 0x13ce433fa26027f5,
1427 ])),
1428 Fq::from(BigInt::new([
1429 0x66323bf80b58b9b9,
1430 0xa1379b6facf6e596,
1431 0x402aef1fb797e32f,
1432 0x2236f55246d0d44d,
1433 0x4c8c1800eb104566,
1434 0x11d6e20e986c2085,
1435 ])),
1436 ));
1437 assert_eq!(
1438 a,
1439 Fq2::new(
1440 Fq::from(BigInt::new([
1441 0x8e9a7adaf6eb0eb9,
1442 0xcb207e6b3341eaba,
1443 0xd70b0c7b481d23ff,
1444 0xf4ef57d604b6bca2,
1445 0x65309427b3d5d090,
1446 0x14c715d5553f01d2,
1447 ])),
1448 Fq::from(BigInt::new([
1449 0xfdb032e7d9079a94,
1450 0x35a2809d15468d83,
1451 0xfe4b23317e0796d5,
1452 0xd62fa51334f560fa,
1453 0x9ad265eb46e01984,
1454 0x1303f3465112c8bc,
1455 ])),
1456 )
1457 );
1458}
1459
1460#[test]
1461fn test_fq2_subtraction() {
1462 let mut a = Fq2::new(
1463 Fq::from(BigInt::new([
1464 0x2d0078036923ffc7,
1465 0x11e59ea221a3b6d2,
1466 0x8b1a52e0a90f59ed,
1467 0xb966ce3bc2108b13,
1468 0xccc649c4b9532bf3,
1469 0xf8d295b2ded9dc,
1470 ])),
1471 Fq::from(BigInt::new([
1472 0x977df6efcdaee0db,
1473 0x946ae52d684fa7ed,
1474 0xbe203411c66fb3a5,
1475 0xb3f8afc0ee248cad,
1476 0x4e464dea5bcfd41e,
1477 0x12d1137b8a6a837,
1478 ])),
1479 );
1480 a.sub_assign(&Fq2::new(
1481 Fq::from(BigInt::new([
1482 0x619a02d78dc70ef2,
1483 0xb93adfc9119e33e8,
1484 0x4bf0b99a9f0dca12,
1485 0x3b88899a42a6318f,
1486 0x986a4a62fa82a49d,
1487 0x13ce433fa26027f5,
1488 ])),
1489 Fq::from(BigInt::new([
1490 0x66323bf80b58b9b9,
1491 0xa1379b6facf6e596,
1492 0x402aef1fb797e32f,
1493 0x2236f55246d0d44d,
1494 0x4c8c1800eb104566,
1495 0x11d6e20e986c2085,
1496 ])),
1497 ));
1498 assert_eq!(
1499 a,
1500 Fq2::new(
1501 Fq::from(BigInt::new([
1502 0x8565752bdb5c9b80,
1503 0x7756bed7c15982e9,
1504 0xa65a6be700b285fe,
1505 0xe255902672ef6c43,
1506 0x7f77a718021c342d,
1507 0x72ba14049fe9881,
1508 ])),
1509 Fq::from(BigInt::new([
1510 0xeb4abaf7c255d1cd,
1511 0x11df49bc6cacc256,
1512 0xe52617930588c69a,
1513 0xf63905f39ad8cb1f,
1514 0x4cd5dd9fb40b3b8f,
1515 0x957411359ba6e4c,
1516 ])),
1517 )
1518 );
1519}
1520
1521#[test]
1522fn test_fq2_negation() {
1523 let mut a = Fq2::new(
1524 Fq::from(BigInt::new([
1525 0x2d0078036923ffc7,
1526 0x11e59ea221a3b6d2,
1527 0x8b1a52e0a90f59ed,
1528 0xb966ce3bc2108b13,
1529 0xccc649c4b9532bf3,
1530 0xf8d295b2ded9dc,
1531 ])),
1532 Fq::from(BigInt::new([
1533 0x977df6efcdaee0db,
1534 0x946ae52d684fa7ed,
1535 0xbe203411c66fb3a5,
1536 0xb3f8afc0ee248cad,
1537 0x4e464dea5bcfd41e,
1538 0x12d1137b8a6a837,
1539 ])),
1540 );
1541 a = -a;
1542 assert_eq!(
1543 a,
1544 Fq2::new(
1545 Fq::from(BigInt::new([
1546 0x8cfe87fc96dbaae4,
1547 0xcc6615c8fb0492d,
1548 0xdc167fc04da19c37,
1549 0xab107d49317487ab,
1550 0x7e555df189f880e3,
1551 0x19083f5486a10cbd,
1552 ])),
1553 Fq::from(BigInt::new([
1554 0x228109103250c9d0,
1555 0x8a411ad149045812,
1556 0xa9109e8f3041427e,
1557 0xb07e9bc405608611,
1558 0xfcd559cbe77bd8b8,
1559 0x18d400b280d93e62,
1560 ])),
1561 )
1562 );
1563}
1564
1565#[test]
1566fn test_fq2_doubling() {
1567 let mut a = Fq2::new(
1568 Fq::from(BigInt::new([
1569 0x2d0078036923ffc7,
1570 0x11e59ea221a3b6d2,
1571 0x8b1a52e0a90f59ed,
1572 0xb966ce3bc2108b13,
1573 0xccc649c4b9532bf3,
1574 0xf8d295b2ded9dc,
1575 ])),
1576 Fq::from(BigInt::new([
1577 0x977df6efcdaee0db,
1578 0x946ae52d684fa7ed,
1579 0xbe203411c66fb3a5,
1580 0xb3f8afc0ee248cad,
1581 0x4e464dea5bcfd41e,
1582 0x12d1137b8a6a837,
1583 ])),
1584 );
1585 a.double_in_place();
1586 assert_eq!(
1587 a,
1588 Fq2::new(
1589 Fq::from(BigInt::new([
1590 0x5a00f006d247ff8e,
1591 0x23cb3d4443476da4,
1592 0x1634a5c1521eb3da,
1593 0x72cd9c7784211627,
1594 0x998c938972a657e7,
1595 0x1f1a52b65bdb3b9,
1596 ])),
1597 Fq::from(BigInt::new([
1598 0x2efbeddf9b5dc1b6,
1599 0x28d5ca5ad09f4fdb,
1600 0x7c4068238cdf674b,
1601 0x67f15f81dc49195b,
1602 0x9c8c9bd4b79fa83d,
1603 0x25a226f714d506e,
1604 ])),
1605 )
1606 );
1607}
1608
1609#[test]
1610fn test_fq2_frobenius_map() {
1611 let mut a = Fq2::new(
1612 Fq::from(BigInt::new([
1613 0x2d0078036923ffc7,
1614 0x11e59ea221a3b6d2,
1615 0x8b1a52e0a90f59ed,
1616 0xb966ce3bc2108b13,
1617 0xccc649c4b9532bf3,
1618 0xf8d295b2ded9dc,
1619 ])),
1620 Fq::from(BigInt::new([
1621 0x977df6efcdaee0db,
1622 0x946ae52d684fa7ed,
1623 0xbe203411c66fb3a5,
1624 0xb3f8afc0ee248cad,
1625 0x4e464dea5bcfd41e,
1626 0x12d1137b8a6a837,
1627 ])),
1628 );
1629 a.frobenius_map(0);
1630 assert_eq!(
1631 a,
1632 Fq2::new(
1633 Fq::from(BigInt::new([
1634 0x2d0078036923ffc7,
1635 0x11e59ea221a3b6d2,
1636 0x8b1a52e0a90f59ed,
1637 0xb966ce3bc2108b13,
1638 0xccc649c4b9532bf3,
1639 0xf8d295b2ded9dc,
1640 ])),
1641 Fq::from(BigInt::new([
1642 0x977df6efcdaee0db,
1643 0x946ae52d684fa7ed,
1644 0xbe203411c66fb3a5,
1645 0xb3f8afc0ee248cad,
1646 0x4e464dea5bcfd41e,
1647 0x12d1137b8a6a837,
1648 ])),
1649 )
1650 );
1651 a.frobenius_map(1);
1652 assert_eq!(
1653 a,
1654 Fq2::new(
1655 Fq::from(BigInt::new([
1656 0x2d0078036923ffc7,
1657 0x11e59ea221a3b6d2,
1658 0x8b1a52e0a90f59ed,
1659 0xb966ce3bc2108b13,
1660 0xccc649c4b9532bf3,
1661 0xf8d295b2ded9dc,
1662 ])),
1663 Fq::from(BigInt::new([
1664 0x228109103250c9d0,
1665 0x8a411ad149045812,
1666 0xa9109e8f3041427e,
1667 0xb07e9bc405608611,
1668 0xfcd559cbe77bd8b8,
1669 0x18d400b280d93e62,
1670 ])),
1671 )
1672 );
1673 a.frobenius_map(1);
1674 assert_eq!(
1675 a,
1676 Fq2::new(
1677 Fq::from(BigInt::new([
1678 0x2d0078036923ffc7,
1679 0x11e59ea221a3b6d2,
1680 0x8b1a52e0a90f59ed,
1681 0xb966ce3bc2108b13,
1682 0xccc649c4b9532bf3,
1683 0xf8d295b2ded9dc,
1684 ])),
1685 Fq::from(BigInt::new([
1686 0x977df6efcdaee0db,
1687 0x946ae52d684fa7ed,
1688 0xbe203411c66fb3a5,
1689 0xb3f8afc0ee248cad,
1690 0x4e464dea5bcfd41e,
1691 0x12d1137b8a6a837,
1692 ])),
1693 )
1694 );
1695 a.frobenius_map(2);
1696 assert_eq!(
1697 a,
1698 Fq2::new(
1699 Fq::from(BigInt::new([
1700 0x2d0078036923ffc7,
1701 0x11e59ea221a3b6d2,
1702 0x8b1a52e0a90f59ed,
1703 0xb966ce3bc2108b13,
1704 0xccc649c4b9532bf3,
1705 0xf8d295b2ded9dc,
1706 ])),
1707 Fq::from(BigInt::new([
1708 0x977df6efcdaee0db,
1709 0x946ae52d684fa7ed,
1710 0xbe203411c66fb3a5,
1711 0xb3f8afc0ee248cad,
1712 0x4e464dea5bcfd41e,
1713 0x12d1137b8a6a837,
1714 ])),
1715 )
1716 );
1717}
1718
1719#[test]
1720fn test_fq2_legendre() {
1721 use ark_ff::fields::LegendreSymbol::*;
1722
1723 assert_eq!(Zero, Fq2::zero().legendre());
1724 // i^2 = -1
1725 let mut m1 = -Fq2::one();
1726 assert_eq!(QuadraticResidue, m1.legendre());
1727 m1 = Fq6Parameters::mul_fp2_by_nonresidue(&m1);
1728 assert_eq!(QuadraticNonResidue, m1.legendre());
1729}
1730
1731#[test]
1732fn test_fq2_mul_nonresidue() {
1733 let mut rng = ark_std::test_rng();
1734
1735 let nqr = Fq2::new(Fq::one(), Fq::one());
1736
1737 for _ in 0..1000 {
1738 let mut a = Fq2::rand(&mut rng);
1739 let mut b = a;
1740 a = Fq6Parameters::mul_fp2_by_nonresidue(&a);
1741 b.mul_assign(&nqr);
1742
1743 assert_eq!(a, b);
1744 }
1745}
1746
1747#[test]
1748fn test_fq6_mul_nonresidue() {
1749 let mut rng = ark_std::test_rng();
1750
1751 let nqr = Fq6::new(Fq2::zero(), Fq2::one(), Fq2::zero());
1752
1753 for _ in 0..1000 {
1754 let mut a = Fq6::rand(&mut rng);
1755 let mut b = a;
1756 a = Fq12Parameters::mul_fp6_by_nonresidue(&a);
1757 b.mul_assign(&nqr);
1758
1759 assert_eq!(a, b);
1760 }
1761}
1762
1763#[test]
1764fn test_fq6_mul_by_1() {
1765 let mut rng = ark_std::test_rng();
1766
1767 for _ in 0..1000 {
1768 let c1 = Fq2::rand(&mut rng);
1769 let mut a = Fq6::rand(&mut rng);
1770 let mut b = a;
1771
1772 a.mul_by_1(&c1);
1773 b.mul_assign(&Fq6::new(Fq2::zero(), c1, Fq2::zero()));
1774
1775 assert_eq!(a, b);
1776 }
1777}
1778
1779#[test]
1780fn test_fq6_mul_by_01() {
1781 let mut rng = ark_std::test_rng();
1782
1783 for _ in 0..1000 {
1784 let c0 = Fq2::rand(&mut rng);
1785 let c1 = Fq2::rand(&mut rng);
1786 let mut a = Fq6::rand(&mut rng);
1787 let mut b = a;
1788
1789 a.mul_by_01(&c0, &c1);
1790 b.mul_assign(&Fq6::new(c0, c1, Fq2::zero()));
1791
1792 assert_eq!(a, b);
1793 }
1794}
1795
1796#[test]
1797fn test_fq12_mul_by_014() {
1798 let mut rng = ark_std::test_rng();
1799
1800 for _ in 0..1000 {
1801 let c0 = Fq2::rand(&mut rng);
1802 let c1 = Fq2::rand(&mut rng);
1803 let c5 = Fq2::rand(&mut rng);
1804 let mut a = Fq12::rand(&mut rng);
1805 let mut b = a;
1806
1807 a.mul_by_014(&c0, &c1, &c5);
1808 b.mul_assign(&Fq12::new(
1809 Fq6::new(c0, c1, Fq2::zero()),
1810 Fq6::new(Fq2::zero(), c5, Fq2::zero()),
1811 ));
1812
1813 assert_eq!(a, b);
1814 }
1815}