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