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_ff::{
   3    biginteger::{BigInt, BigInteger, BigInteger384},
   4    fields::{FftField, Field, Fp12Config, Fp2Config, Fp6Config, PrimeField},
   5    One, UniformRand, Zero,
   6};
   7use ark_std::{
   8    cmp::Ordering,
   9    ops::{AddAssign, MulAssign, SubAssign},
  10    vec,
  11};
  12
  13use crate::{Fq, Fq12, Fq12Config, Fq2, Fq2Config, Fq6, Fq6Config, Fr};
  14
  15test_field!(fr; Fr; mont_prime_field);
  16test_field!(fq; Fq; mont_prime_field);
  17test_field!(fq2; Fq2);
  18test_field!(fq6; Fq6);
  19test_field!(fq12; Fq12);
  20
  21#[test]
  22fn test_negative_one() {
  23    let neg_one = Fq::new_unchecked(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!(Fq2Config::FROBENIUS_COEFF_FP2_C1[0], Fq::one());
  39    assert_eq!(
  40        Fq2Config::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!(Fq6Config::FROBENIUS_COEFF_FP6_C1[0], Fq2::one());
  54    assert_eq!(
  55        Fq6Config::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        Fq6Config::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        Fq6Config::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        Fq6Config::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        Fq6Config::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!(Fq6Config::FROBENIUS_COEFF_FP6_C2[0], Fq2::one());
 171    assert_eq!(
 172        Fq6Config::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        Fq6Config::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        Fq6Config::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        Fq6Config::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        Fq6Config::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!(Fq12Config::FROBENIUS_COEFF_FP12_C1[0], Fq2::one());
 288    assert_eq!(
 289        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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        Fq12Config::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_fq_repr_from() {
 743    assert_eq!(BigInt::from(100u64), BigInt::new([100, 0, 0, 0, 0, 0]));
 744}
 745
 746#[test]
 747fn test_fq_repr_is_odd() {
 748    assert!(!BigInteger384::from(0u64).is_odd());
 749    assert!(BigInteger384::from(0u64).is_even());
 750    assert!(BigInteger384::from(1u64).is_odd());
 751    assert!(!BigInteger384::from(1u64).is_even());
 752    assert!(!BigInteger384::from(324834872u64).is_odd());
 753    assert!(BigInteger384::from(324834872u64).is_even());
 754    assert!(BigInteger384::from(324834873u64).is_odd());
 755    assert!(!BigInteger384::from(324834873u64).is_even());
 756}
 757
 758#[test]
 759fn test_fq_repr_is_zero() {
 760    assert!(BigInteger384::from(0u64).is_zero());
 761    assert!(!BigInteger384::from(1u64).is_zero());
 762    assert!(!BigInt::new([0, 0, 0, 0, 1, 0]).is_zero());
 763}
 764
 765#[test]
 766fn test_fq_repr_div2() {
 767    let mut a = BigInt::new([
 768        0x8b0ad39f8dd7482a,
 769        0x147221c9a7178b69,
 770        0x54764cb08d8a6aa0,
 771        0x8519d708e1d83041,
 772        0x41f82777bd13fdb,
 773        0xf43944578f9b771b,
 774    ]);
 775    a.div2();
 776    assert_eq!(
 777        a,
 778        BigInt::new([
 779            0xc58569cfc6eba415,
 780            0xa3910e4d38bc5b4,
 781            0xaa3b265846c53550,
 782            0xc28ceb8470ec1820,
 783            0x820fc13bbde89fed,
 784            0x7a1ca22bc7cdbb8d,
 785        ])
 786    );
 787    for _ in 0..10 {
 788        a.div2();
 789    }
 790    assert_eq!(
 791        a,
 792        BigInt::new([
 793            0x6d31615a73f1bae9,
 794            0x54028e443934e2f1,
 795            0x82a8ec99611b14d,
 796            0xfb70a33ae11c3b06,
 797            0xe36083f04eef7a27,
 798            0x1e87288af1f36e,
 799        ])
 800    );
 801    for _ in 0..300 {
 802        a.div2();
 803    }
 804    assert_eq!(
 805        a,
 806        BigInt::new([0x7288af1f36ee3608, 0x1e8, 0x0, 0x0, 0x0, 0x0])
 807    );
 808    for _ in 0..50 {
 809        a.div2();
 810    }
 811    assert_eq!(a, BigInt::new([0x7a1ca2, 0x0, 0x0, 0x0, 0x0, 0x0]));
 812    for _ in 0..22 {
 813        a.div2();
 814    }
 815    assert_eq!(a, BigInt::new([0x1, 0x0, 0x0, 0x0, 0x0, 0x0]));
 816    a.div2();
 817    assert!(a.is_zero());
 818}
 819
 820#[test]
 821fn test_fq_repr_divn() {
 822    let mut a = BigInt::new([
 823        0xaa5cdd6172847ffd,
 824        0x43242c06aed55287,
 825        0x9ddd5b312f3dd104,
 826        0xc5541fd48046b7e7,
 827        0x16080cf4071e0b05,
 828        0x1225f2901aea514e,
 829    ]);
 830    a.divn(0);
 831    assert_eq!(
 832        a,
 833        BigInt::new([
 834            0xaa5cdd6172847ffd,
 835            0x43242c06aed55287,
 836            0x9ddd5b312f3dd104,
 837            0xc5541fd48046b7e7,
 838            0x16080cf4071e0b05,
 839            0x1225f2901aea514e,
 840        ])
 841    );
 842    a.divn(1);
 843    assert_eq!(
 844        a,
 845        BigInt::new([
 846            0xd52e6eb0b9423ffe,
 847            0x21921603576aa943,
 848            0xceeead98979ee882,
 849            0xe2aa0fea40235bf3,
 850            0xb04067a038f0582,
 851            0x912f9480d7528a7,
 852        ])
 853    );
 854    a.divn(50);
 855    assert_eq!(
 856        a,
 857        BigInt::new([
 858            0x8580d5daaa50f54b,
 859            0xab6625e7ba208864,
 860            0x83fa9008d6fcf3bb,
 861            0x19e80e3c160b8aa,
 862            0xbe52035d4a29c2c1,
 863            0x244,
 864        ])
 865    );
 866    a.divn(130);
 867    assert_eq!(
 868        a,
 869        BigInt::new([
 870            0xa0fea40235bf3cee,
 871            0x4067a038f0582e2a,
 872            0x2f9480d7528a70b0,
 873            0x91,
 874            0x0,
 875            0x0,
 876        ])
 877    );
 878    a.divn(64);
 879    assert_eq!(
 880        a,
 881        BigInt::new([0x4067a038f0582e2a, 0x2f9480d7528a70b0, 0x91, 0x0, 0x0, 0x0])
 882    );
 883}
 884
 885#[test]
 886fn test_fq_repr_mul2() {
 887    let mut a = BigInteger384::from(23712937547u64);
 888    a.mul2();
 889    assert_eq!(a, BigInt::new([0xb0acd6c96, 0x0, 0x0, 0x0, 0x0, 0x0]));
 890    for _ in 0..60 {
 891        a.mul2();
 892    }
 893    assert_eq!(
 894        a,
 895        BigInt::new([0x6000000000000000, 0xb0acd6c9, 0x0, 0x0, 0x0, 0x0])
 896    );
 897    for _ in 0..300 {
 898        a.mul2();
 899    }
 900    assert_eq!(
 901        a,
 902        BigInt::new([0x0, 0x0, 0x0, 0x0, 0x0, 0xcd6c960000000000])
 903    );
 904    for _ in 0..17 {
 905        a.mul2();
 906    }
 907    assert_eq!(
 908        a,
 909        BigInt::new([0x0, 0x0, 0x0, 0x0, 0x0, 0x2c00000000000000])
 910    );
 911    for _ in 0..6 {
 912        a.mul2();
 913    }
 914    assert!(a.is_zero());
 915}
 916
 917#[test]
 918fn test_fq_repr_num_bits() {
 919    let mut a = BigInteger384::from(0u64);
 920    assert_eq!(0, a.num_bits());
 921    a = BigInteger384::from(1u64);
 922    for i in 1..385 {
 923        assert_eq!(i, a.num_bits());
 924        a.mul2();
 925    }
 926    assert_eq!(0, a.num_bits());
 927}
 928
 929#[test]
 930fn test_fq_repr_sub_noborrow() {
 931    let mut rng = ark_std::test_rng();
 932
 933    let mut t = BigInt::new([
 934        0x827a4a08041ebd9,
 935        0x3c239f3dcc8f0d6b,
 936        0x9ab46a912d555364,
 937        0x196936b17b43910b,
 938        0xad0eb3948a5c34fd,
 939        0xd56f7b5ab8b5ce8,
 940    ]);
 941    t.sub_with_borrow(&BigInt::new([
 942        0xc7867917187ca02b,
 943        0x5d75679d4911ffef,
 944        0x8c5b3e48b1a71c15,
 945        0x6a427ae846fd66aa,
 946        0x7a37e7265ee1eaf9,
 947        0x7c0577a26f59d5,
 948    ]));
 949    assert!(
 950        t == BigInt::new([
 951            0x40a12b8967c54bae,
 952            0xdeae37a0837d0d7b,
 953            0xe592c487bae374e,
 954            0xaf26bbc934462a61,
 955            0x32d6cc6e2b7a4a03,
 956            0xcdaf23e091c0313,
 957        ])
 958    );
 959
 960    for _ in 0..1000 {
 961        let mut a = BigInteger384::rand(&mut rng);
 962        a.0[5] >>= 30;
 963        let mut b = a;
 964        for _ in 0..10 {
 965            b.mul2();
 966        }
 967        let mut c = b;
 968        for _ in 0..10 {
 969            c.mul2();
 970        }
 971
 972        assert!(a < b);
 973        assert!(b < c);
 974
 975        let mut csub_ba = c;
 976        csub_ba.sub_with_borrow(&b);
 977        csub_ba.sub_with_borrow(&a);
 978
 979        let mut csub_ab = c;
 980        csub_ab.sub_with_borrow(&a);
 981        csub_ab.sub_with_borrow(&b);
 982
 983        assert_eq!(csub_ab, csub_ba);
 984    }
 985
 986    // Subtracting q+1 from q should produce -1 (mod 2**384)
 987    let mut qplusone = BigInt::new([
 988        0xb9feffffffffaaab,
 989        0x1eabfffeb153ffff,
 990        0x6730d2a0f6b0f624,
 991        0x64774b84f38512bf,
 992        0x4b1ba7b6434bacd7,
 993        0x1a0111ea397fe69a,
 994    ]);
 995    qplusone.sub_with_borrow(&BigInt::new([
 996        0xb9feffffffffaaac,
 997        0x1eabfffeb153ffff,
 998        0x6730d2a0f6b0f624,
 999        0x64774b84f38512bf,
1000        0x4b1ba7b6434bacd7,
1001        0x1a0111ea397fe69a,
1002    ]));
1003    assert_eq!(
1004        qplusone,
1005        BigInt::new([
1006            0xffffffffffffffff,
1007            0xffffffffffffffff,
1008            0xffffffffffffffff,
1009            0xffffffffffffffff,
1010            0xffffffffffffffff,
1011            0xffffffffffffffff,
1012        ])
1013    );
1014}
1015
1016#[test]
1017fn test_fq_repr_add_nocarry() {
1018    let mut rng = ark_std::test_rng();
1019
1020    let mut t = BigInt::new([
1021        0x827a4a08041ebd9,
1022        0x3c239f3dcc8f0d6b,
1023        0x9ab46a912d555364,
1024        0x196936b17b43910b,
1025        0xad0eb3948a5c34fd,
1026        0xd56f7b5ab8b5ce8,
1027    ]);
1028    t.add_with_carry(&BigInt::new([
1029        0xc7867917187ca02b,
1030        0x5d75679d4911ffef,
1031        0x8c5b3e48b1a71c15,
1032        0x6a427ae846fd66aa,
1033        0x7a37e7265ee1eaf9,
1034        0x7c0577a26f59d5,
1035    ]));
1036    assert!(
1037        t == BigInt::new([
1038            0xcfae1db798be8c04,
1039            0x999906db15a10d5a,
1040            0x270fa8d9defc6f79,
1041            0x83abb199c240f7b6,
1042            0x27469abae93e1ff6,
1043            0xdd2fd2d4dfab6be,
1044        ])
1045    );
1046
1047    // Test for the associativity of addition.
1048    for _ in 0..1000 {
1049        let mut a = BigInteger384::rand(&mut rng);
1050        let mut b = BigInteger384::rand(&mut rng);
1051        let mut c = BigInteger384::rand(&mut rng);
1052
1053        // Unset the first few bits, so that overflow won't occur.
1054        a.0[5] >>= 3;
1055        b.0[5] >>= 3;
1056        c.0[5] >>= 3;
1057
1058        let mut abc = a;
1059        abc.add_with_carry(&b);
1060        abc.add_with_carry(&c);
1061
1062        let mut acb = a;
1063        acb.add_with_carry(&c);
1064        acb.add_with_carry(&b);
1065
1066        let mut bac = b;
1067        bac.add_with_carry(&a);
1068        bac.add_with_carry(&c);
1069
1070        let mut bca = b;
1071        bca.add_with_carry(&c);
1072        bca.add_with_carry(&a);
1073
1074        let mut cab = c;
1075        cab.add_with_carry(&a);
1076        cab.add_with_carry(&b);
1077
1078        let mut cba = c;
1079        cba.add_with_carry(&b);
1080        cba.add_with_carry(&a);
1081
1082        assert_eq!(abc, acb);
1083        assert_eq!(abc, bac);
1084        assert_eq!(abc, bca);
1085        assert_eq!(abc, cab);
1086        assert_eq!(abc, cba);
1087    }
1088
1089    // Adding 1 to (2^384 - 1) should produce zero
1090    let mut x = BigInt::new([
1091        0xffffffffffffffff,
1092        0xffffffffffffffff,
1093        0xffffffffffffffff,
1094        0xffffffffffffffff,
1095        0xffffffffffffffff,
1096        0xffffffffffffffff,
1097    ]);
1098    x.add_with_carry(&BigInteger384::from(1u64));
1099    assert!(x.is_zero());
1100}
1101
1102#[test]
1103fn test_fq2_sqrt() {
1104    // 1 has a sqrt in the base field
1105    let fq2_sqrt = Fq2::new(Fq::one(), Fq::zero()).sqrt().unwrap();
1106    assert_eq!(fq2_sqrt.c0, Fq::one());
1107    assert_eq!(fq2_sqrt.c1, Fq::zero());
1108
1109    // -1 has no sqrt in the base field, but should be valid in Fq2
1110    let fq2_sqrt = Fq2::new(-Fq::one(), Fq::zero()).sqrt().unwrap();
1111    assert_eq!(fq2_sqrt.c0, Fq::zero());
1112    assert_eq!(fq2_sqrt.c1, Fq::one());
1113}
1114
1115#[test]
1116fn test_fq_num_bits() {
1117    assert_eq!(Fq::MODULUS_BIT_SIZE, 381);
1118}
1119
1120#[test]
1121fn test_fq_root_of_unity() {
1122    assert_eq!(Fq::TWO_ADICITY, 1);
1123    assert_eq!(Fq::GENERATOR, Fq::from(BigInteger384::from(2u64)));
1124    assert_eq!(
1125        Fq::GENERATOR.pow([
1126            0xdcff7fffffffd555,
1127            0xf55ffff58a9ffff,
1128            0xb39869507b587b12,
1129            0xb23ba5c279c2895f,
1130            0x258dd3db21a5d66b,
1131            0xd0088f51cbff34d,
1132        ]),
1133        Fq::TWO_ADIC_ROOT_OF_UNITY
1134    );
1135    assert_eq!(
1136        Fq::TWO_ADIC_ROOT_OF_UNITY.pow([1 << Fq::TWO_ADICITY]),
1137        Fq::one()
1138    );
1139    assert!(Fq::GENERATOR.sqrt().is_none());
1140}
1141
1142// #[test]
1143// fn fq_field_tests() {
1144//     ::tests::field::random_field_tests::<Fq>();
1145//     ::tests::field::random_sqrt_tests::<Fq>();
1146//     ::tests::field::random_frobenius_tests::<Fq, _>(Fq::char(), 13);
1147//     ::tests::field::from_str_tests::<Fq>();
1148// }
1149
1150#[test]
1151fn test_fq_ordering() {
1152    // BigInteger384's ordering is well-tested, but we still need to make sure the
1153    // Fq elements aren't being compared in Montgomery form.
1154    for i in 0..100u64 {
1155        assert!(Fq::from(BigInteger384::from(i + 1)) > Fq::from(BigInteger384::from(i)));
1156    }
1157}
1158
1159// #[test]
1160// fn fq_repr_tests() {
1161//     ::tests::repr::random_repr_tests::<BigInteger384>();
1162// }
1163
1164#[test]
1165fn test_fq_legendre() {
1166    use ark_ff::fields::LegendreSymbol::*;
1167
1168    assert_eq!(QuadraticResidue, Fq::one().legendre());
1169    assert_eq!(Zero, Fq::zero().legendre());
1170
1171    assert_eq!(
1172        QuadraticNonResidue,
1173        Fq::from(BigInteger384::from(2u64)).legendre()
1174    );
1175    assert_eq!(
1176        QuadraticResidue,
1177        Fq::from(BigInteger384::from(4u64)).legendre()
1178    );
1179
1180    let e = BigInt::new([
1181        0x52a112f249778642,
1182        0xd0bedb989b7991f,
1183        0xdad3b6681aa63c05,
1184        0xf2efc0bb4721b283,
1185        0x6057a98f18c24733,
1186        0x1022c2fd122889e4,
1187    ]);
1188    assert_eq!(QuadraticNonResidue, Fq::from(e).legendre());
1189    let e = BigInt::new([
1190        0x6dae594e53a96c74,
1191        0x19b16ca9ba64b37b,
1192        0x5c764661a59bfc68,
1193        0xaa346e9b31c60a,
1194        0x346059f9d87a9fa9,
1195        0x1d61ac6bfd5c88b,
1196    ]);
1197    assert_eq!(QuadraticResidue, Fq::from(e).legendre());
1198}
1199
1200#[test]
1201fn test_fq2_ordering() {
1202    let mut a = Fq2::new(Fq::zero(), Fq::zero());
1203
1204    let mut b = a.clone();
1205
1206    assert!(a.cmp(&b) == Ordering::Equal);
1207    b.c0.add_assign(&Fq::one());
1208    assert!(a.cmp(&b) == Ordering::Less);
1209    a.c0.add_assign(&Fq::one());
1210    assert!(a.cmp(&b) == Ordering::Equal);
1211    b.c1.add_assign(&Fq::one());
1212    assert!(a.cmp(&b) == Ordering::Less);
1213    a.c0.add_assign(&Fq::one());
1214    assert!(a.cmp(&b) == Ordering::Less);
1215    a.c1.add_assign(&Fq::one());
1216    assert!(a.cmp(&b) == Ordering::Greater);
1217    b.c0.add_assign(&Fq::one());
1218    assert!(a.cmp(&b) == Ordering::Equal);
1219}
1220
1221#[test]
1222fn test_fq2_basics() {
1223    assert_eq!(Fq2::new(Fq::zero(), Fq::zero(),), Fq2::zero());
1224    assert_eq!(Fq2::new(Fq::one(), Fq::zero(),), Fq2::one());
1225    assert!(Fq2::zero().is_zero());
1226    assert!(!Fq2::one().is_zero());
1227    assert!(!Fq2::new(Fq::zero(), Fq::one(),).is_zero());
1228}
1229
1230#[test]
1231fn test_fq2_squaring() {
1232    let a = Fq2::new(Fq::one(), Fq::one()).square(); // u + 1
1233    assert_eq!(
1234        a,
1235        Fq2::new(Fq::zero(), Fq::from(BigInteger384::from(2u64)),)
1236    ); // 2u
1237
1238    let a = Fq2::new(Fq::zero(), Fq::one()).square(); // u
1239    assert_eq!(a, {
1240        let neg1 = -Fq::one();
1241        Fq2::new(neg1, Fq::zero())
1242    }); // -1
1243
1244    let mut a = Fq2::new(
1245        Fq::from(BigInt::new([
1246            0x9c2c6309bbf8b598,
1247            0x4eef5c946536f602,
1248            0x90e34aab6fb6a6bd,
1249            0xf7f295a94e58ae7c,
1250            0x41b76dcc1c3fbe5e,
1251            0x7080c5fa1d8e042,
1252        ])),
1253        Fq::from(BigInt::new([
1254            0x38f473b3c870a4ab,
1255            0x6ad3291177c8c7e5,
1256            0xdac5a4c911a4353e,
1257            0xbfb99020604137a0,
1258            0xfc58a7b7be815407,
1259            0x10d1615e75250a21,
1260        ])),
1261    );
1262    a.square_in_place();
1263    assert_eq!(
1264        a,
1265        Fq2::new(
1266            Fq::from(BigInt::new([
1267                0xf262c28c538bcf68,
1268                0xb9f2a66eae1073ba,
1269                0xdc46ab8fad67ae0,
1270                0xcb674157618da176,
1271                0x4cf17b5893c3d327,
1272                0x7eac81369c43361,
1273            ])),
1274            Fq::from(BigInt::new([
1275                0xc1579cf58e980cf8,
1276                0xa23eb7e12dd54d98,
1277                0xe75138bce4cec7aa,
1278                0x38d0d7275a9689e1,
1279                0x739c983042779a65,
1280                0x1542a61c8a8db994,
1281            ])),
1282        )
1283    );
1284}
1285
1286#[test]
1287fn test_fq2_mul() {
1288    let mut a = Fq2::new(
1289        Fq::from(BigInt::new([
1290            0x85c9f989e1461f03,
1291            0xa2e33c333449a1d6,
1292            0x41e461154a7354a3,
1293            0x9ee53e7e84d7532e,
1294            0x1c202d8ed97afb45,
1295            0x51d3f9253e2516f,
1296        ])),
1297        Fq::from(BigInt::new([
1298            0xa7348a8b511aedcf,
1299            0x143c215d8176b319,
1300            0x4cc48081c09b8903,
1301            0x9533e4a9a5158be,
1302            0x7a5e1ecb676d65f9,
1303            0x180c3ee46656b008,
1304        ])),
1305    );
1306    a.mul_assign(&Fq2::new(
1307        Fq::from(BigInt::new([
1308            0xe21f9169805f537e,
1309            0xfc87e62e179c285d,
1310            0x27ece175be07a531,
1311            0xcd460f9f0c23e430,
1312            0x6c9110292bfa409,
1313            0x2c93a72eb8af83e,
1314        ])),
1315        Fq::from(BigInt::new([
1316            0x4b1c3f936d8992d4,
1317            0x1d2a72916dba4c8a,
1318            0x8871c508658d1e5f,
1319            0x57a06d3135a752ae,
1320            0x634cd3c6c565096d,
1321            0x19e17334d4e93558,
1322        ])),
1323    ));
1324    assert_eq!(
1325        a,
1326        Fq2::new(
1327            Fq::from(BigInt::new([
1328                0x95b5127e6360c7e4,
1329                0xde29c31a19a6937e,
1330                0xf61a96dacf5a39bc,
1331                0x5511fe4d84ee5f78,
1332                0x5310a202d92f9963,
1333                0x1751afbe166e5399,
1334            ])),
1335            Fq::from(BigInt::new([
1336                0x84af0e1bd630117a,
1337                0x6c63cd4da2c2aa7,
1338                0x5ba6e5430e883d40,
1339                0xc975106579c275ee,
1340                0x33a9ac82ce4c5083,
1341                0x1ef1a36c201589d,
1342            ])),
1343        )
1344    );
1345}
1346
1347#[test]
1348fn test_fq2_inverse() {
1349    assert!(Fq2::zero().inverse().is_none());
1350
1351    let a = Fq2::new(
1352        Fq::from(BigInt::new([
1353            0x85c9f989e1461f03,
1354            0xa2e33c333449a1d6,
1355            0x41e461154a7354a3,
1356            0x9ee53e7e84d7532e,
1357            0x1c202d8ed97afb45,
1358            0x51d3f9253e2516f,
1359        ])),
1360        Fq::from(BigInt::new([
1361            0xa7348a8b511aedcf,
1362            0x143c215d8176b319,
1363            0x4cc48081c09b8903,
1364            0x9533e4a9a5158be,
1365            0x7a5e1ecb676d65f9,
1366            0x180c3ee46656b008,
1367        ])),
1368    );
1369    let a = a.inverse().unwrap();
1370    assert_eq!(
1371        a,
1372        Fq2::new(
1373            Fq::from(BigInt::new([
1374                0x70300f9bcb9e594,
1375                0xe5ecda5fdafddbb2,
1376                0x64bef617d2915a8f,
1377                0xdfba703293941c30,
1378                0xa6c3d8f9586f2636,
1379                0x1351ef01941b70c4,
1380            ])),
1381            Fq::from(BigInt::new([
1382                0x8c39fd76a8312cb4,
1383                0x15d7b6b95defbff0,
1384                0x947143f89faedee9,
1385                0xcbf651a0f367afb2,
1386                0xdf4e54f0d3ef15a6,
1387                0x103bdf241afb0019,
1388            ])),
1389        )
1390    );
1391}
1392
1393#[test]
1394fn test_fq2_addition() {
1395    let mut a = Fq2::new(
1396        Fq::from(BigInt::new([
1397            0x2d0078036923ffc7,
1398            0x11e59ea221a3b6d2,
1399            0x8b1a52e0a90f59ed,
1400            0xb966ce3bc2108b13,
1401            0xccc649c4b9532bf3,
1402            0xf8d295b2ded9dc,
1403        ])),
1404        Fq::from(BigInt::new([
1405            0x977df6efcdaee0db,
1406            0x946ae52d684fa7ed,
1407            0xbe203411c66fb3a5,
1408            0xb3f8afc0ee248cad,
1409            0x4e464dea5bcfd41e,
1410            0x12d1137b8a6a837,
1411        ])),
1412    );
1413    a.add_assign(&Fq2::new(
1414        Fq::from(BigInt::new([
1415            0x619a02d78dc70ef2,
1416            0xb93adfc9119e33e8,
1417            0x4bf0b99a9f0dca12,
1418            0x3b88899a42a6318f,
1419            0x986a4a62fa82a49d,
1420            0x13ce433fa26027f5,
1421        ])),
1422        Fq::from(BigInt::new([
1423            0x66323bf80b58b9b9,
1424            0xa1379b6facf6e596,
1425            0x402aef1fb797e32f,
1426            0x2236f55246d0d44d,
1427            0x4c8c1800eb104566,
1428            0x11d6e20e986c2085,
1429        ])),
1430    ));
1431    assert_eq!(
1432        a,
1433        Fq2::new(
1434            Fq::from(BigInt::new([
1435                0x8e9a7adaf6eb0eb9,
1436                0xcb207e6b3341eaba,
1437                0xd70b0c7b481d23ff,
1438                0xf4ef57d604b6bca2,
1439                0x65309427b3d5d090,
1440                0x14c715d5553f01d2,
1441            ])),
1442            Fq::from(BigInt::new([
1443                0xfdb032e7d9079a94,
1444                0x35a2809d15468d83,
1445                0xfe4b23317e0796d5,
1446                0xd62fa51334f560fa,
1447                0x9ad265eb46e01984,
1448                0x1303f3465112c8bc,
1449            ])),
1450        )
1451    );
1452}
1453
1454#[test]
1455fn test_fq2_subtraction() {
1456    let mut a = Fq2::new(
1457        Fq::from(BigInt::new([
1458            0x2d0078036923ffc7,
1459            0x11e59ea221a3b6d2,
1460            0x8b1a52e0a90f59ed,
1461            0xb966ce3bc2108b13,
1462            0xccc649c4b9532bf3,
1463            0xf8d295b2ded9dc,
1464        ])),
1465        Fq::from(BigInt::new([
1466            0x977df6efcdaee0db,
1467            0x946ae52d684fa7ed,
1468            0xbe203411c66fb3a5,
1469            0xb3f8afc0ee248cad,
1470            0x4e464dea5bcfd41e,
1471            0x12d1137b8a6a837,
1472        ])),
1473    );
1474    a.sub_assign(&Fq2::new(
1475        Fq::from(BigInt::new([
1476            0x619a02d78dc70ef2,
1477            0xb93adfc9119e33e8,
1478            0x4bf0b99a9f0dca12,
1479            0x3b88899a42a6318f,
1480            0x986a4a62fa82a49d,
1481            0x13ce433fa26027f5,
1482        ])),
1483        Fq::from(BigInt::new([
1484            0x66323bf80b58b9b9,
1485            0xa1379b6facf6e596,
1486            0x402aef1fb797e32f,
1487            0x2236f55246d0d44d,
1488            0x4c8c1800eb104566,
1489            0x11d6e20e986c2085,
1490        ])),
1491    ));
1492    assert_eq!(
1493        a,
1494        Fq2::new(
1495            Fq::from(BigInt::new([
1496                0x8565752bdb5c9b80,
1497                0x7756bed7c15982e9,
1498                0xa65a6be700b285fe,
1499                0xe255902672ef6c43,
1500                0x7f77a718021c342d,
1501                0x72ba14049fe9881,
1502            ])),
1503            Fq::from(BigInt::new([
1504                0xeb4abaf7c255d1cd,
1505                0x11df49bc6cacc256,
1506                0xe52617930588c69a,
1507                0xf63905f39ad8cb1f,
1508                0x4cd5dd9fb40b3b8f,
1509                0x957411359ba6e4c,
1510            ])),
1511        )
1512    );
1513}
1514
1515#[test]
1516fn test_fq2_negation() {
1517    let mut a = Fq2::new(
1518        Fq::from(BigInt::new([
1519            0x2d0078036923ffc7,
1520            0x11e59ea221a3b6d2,
1521            0x8b1a52e0a90f59ed,
1522            0xb966ce3bc2108b13,
1523            0xccc649c4b9532bf3,
1524            0xf8d295b2ded9dc,
1525        ])),
1526        Fq::from(BigInt::new([
1527            0x977df6efcdaee0db,
1528            0x946ae52d684fa7ed,
1529            0xbe203411c66fb3a5,
1530            0xb3f8afc0ee248cad,
1531            0x4e464dea5bcfd41e,
1532            0x12d1137b8a6a837,
1533        ])),
1534    );
1535    a = -a;
1536    assert_eq!(
1537        a,
1538        Fq2::new(
1539            Fq::from(BigInt::new([
1540                0x8cfe87fc96dbaae4,
1541                0xcc6615c8fb0492d,
1542                0xdc167fc04da19c37,
1543                0xab107d49317487ab,
1544                0x7e555df189f880e3,
1545                0x19083f5486a10cbd,
1546            ])),
1547            Fq::from(BigInt::new([
1548                0x228109103250c9d0,
1549                0x8a411ad149045812,
1550                0xa9109e8f3041427e,
1551                0xb07e9bc405608611,
1552                0xfcd559cbe77bd8b8,
1553                0x18d400b280d93e62,
1554            ])),
1555        )
1556    );
1557}
1558
1559#[test]
1560fn test_fq2_doubling() {
1561    let mut a = Fq2::new(
1562        Fq::from(BigInt::new([
1563            0x2d0078036923ffc7,
1564            0x11e59ea221a3b6d2,
1565            0x8b1a52e0a90f59ed,
1566            0xb966ce3bc2108b13,
1567            0xccc649c4b9532bf3,
1568            0xf8d295b2ded9dc,
1569        ])),
1570        Fq::from(BigInt::new([
1571            0x977df6efcdaee0db,
1572            0x946ae52d684fa7ed,
1573            0xbe203411c66fb3a5,
1574            0xb3f8afc0ee248cad,
1575            0x4e464dea5bcfd41e,
1576            0x12d1137b8a6a837,
1577        ])),
1578    );
1579    a.double_in_place();
1580    assert_eq!(
1581        a,
1582        Fq2::new(
1583            Fq::from(BigInt::new([
1584                0x5a00f006d247ff8e,
1585                0x23cb3d4443476da4,
1586                0x1634a5c1521eb3da,
1587                0x72cd9c7784211627,
1588                0x998c938972a657e7,
1589                0x1f1a52b65bdb3b9,
1590            ])),
1591            Fq::from(BigInt::new([
1592                0x2efbeddf9b5dc1b6,
1593                0x28d5ca5ad09f4fdb,
1594                0x7c4068238cdf674b,
1595                0x67f15f81dc49195b,
1596                0x9c8c9bd4b79fa83d,
1597                0x25a226f714d506e,
1598            ])),
1599        )
1600    );
1601}
1602
1603#[test]
1604fn test_fq2_frobenius_map() {
1605    let mut a = Fq2::new(
1606        Fq::from(BigInt::new([
1607            0x2d0078036923ffc7,
1608            0x11e59ea221a3b6d2,
1609            0x8b1a52e0a90f59ed,
1610            0xb966ce3bc2108b13,
1611            0xccc649c4b9532bf3,
1612            0xf8d295b2ded9dc,
1613        ])),
1614        Fq::from(BigInt::new([
1615            0x977df6efcdaee0db,
1616            0x946ae52d684fa7ed,
1617            0xbe203411c66fb3a5,
1618            0xb3f8afc0ee248cad,
1619            0x4e464dea5bcfd41e,
1620            0x12d1137b8a6a837,
1621        ])),
1622    );
1623    a.frobenius_map(0);
1624    assert_eq!(
1625        a,
1626        Fq2::new(
1627            Fq::from(BigInt::new([
1628                0x2d0078036923ffc7,
1629                0x11e59ea221a3b6d2,
1630                0x8b1a52e0a90f59ed,
1631                0xb966ce3bc2108b13,
1632                0xccc649c4b9532bf3,
1633                0xf8d295b2ded9dc,
1634            ])),
1635            Fq::from(BigInt::new([
1636                0x977df6efcdaee0db,
1637                0x946ae52d684fa7ed,
1638                0xbe203411c66fb3a5,
1639                0xb3f8afc0ee248cad,
1640                0x4e464dea5bcfd41e,
1641                0x12d1137b8a6a837,
1642            ])),
1643        )
1644    );
1645    a.frobenius_map(1);
1646    assert_eq!(
1647        a,
1648        Fq2::new(
1649            Fq::from(BigInt::new([
1650                0x2d0078036923ffc7,
1651                0x11e59ea221a3b6d2,
1652                0x8b1a52e0a90f59ed,
1653                0xb966ce3bc2108b13,
1654                0xccc649c4b9532bf3,
1655                0xf8d295b2ded9dc,
1656            ])),
1657            Fq::from(BigInt::new([
1658                0x228109103250c9d0,
1659                0x8a411ad149045812,
1660                0xa9109e8f3041427e,
1661                0xb07e9bc405608611,
1662                0xfcd559cbe77bd8b8,
1663                0x18d400b280d93e62,
1664            ])),
1665        )
1666    );
1667    a.frobenius_map(1);
1668    assert_eq!(
1669        a,
1670        Fq2::new(
1671            Fq::from(BigInt::new([
1672                0x2d0078036923ffc7,
1673                0x11e59ea221a3b6d2,
1674                0x8b1a52e0a90f59ed,
1675                0xb966ce3bc2108b13,
1676                0xccc649c4b9532bf3,
1677                0xf8d295b2ded9dc,
1678            ])),
1679            Fq::from(BigInt::new([
1680                0x977df6efcdaee0db,
1681                0x946ae52d684fa7ed,
1682                0xbe203411c66fb3a5,
1683                0xb3f8afc0ee248cad,
1684                0x4e464dea5bcfd41e,
1685                0x12d1137b8a6a837,
1686            ])),
1687        )
1688    );
1689    a.frobenius_map(2);
1690    assert_eq!(
1691        a,
1692        Fq2::new(
1693            Fq::from(BigInt::new([
1694                0x2d0078036923ffc7,
1695                0x11e59ea221a3b6d2,
1696                0x8b1a52e0a90f59ed,
1697                0xb966ce3bc2108b13,
1698                0xccc649c4b9532bf3,
1699                0xf8d295b2ded9dc,
1700            ])),
1701            Fq::from(BigInt::new([
1702                0x977df6efcdaee0db,
1703                0x946ae52d684fa7ed,
1704                0xbe203411c66fb3a5,
1705                0xb3f8afc0ee248cad,
1706                0x4e464dea5bcfd41e,
1707                0x12d1137b8a6a837,
1708            ])),
1709        )
1710    );
1711}
1712
1713#[test]
1714fn test_fq2_legendre() {
1715    use ark_ff::fields::LegendreSymbol::*;
1716
1717    assert_eq!(Zero, Fq2::zero().legendre());
1718    // i^2 = -1
1719    let mut m1 = -Fq2::one();
1720    assert_eq!(QuadraticResidue, m1.legendre());
1721    m1 = Fq6Config::mul_fp2_by_nonresidue(&m1);
1722    assert_eq!(QuadraticNonResidue, m1.legendre());
1723}
1724
1725#[test]
1726fn test_fq2_mul_nonresidue() {
1727    let mut rng = ark_std::test_rng();
1728
1729    let nqr = Fq2::new(Fq::one(), Fq::one());
1730
1731    for _ in 0..1000 {
1732        let mut a = Fq2::rand(&mut rng);
1733        let mut b = a;
1734        a = Fq6Config::mul_fp2_by_nonresidue(&a);
1735        b.mul_assign(&nqr);
1736
1737        assert_eq!(a, b);
1738    }
1739}
1740
1741#[test]
1742fn test_fq6_mul_nonresidue() {
1743    let mut rng = ark_std::test_rng();
1744
1745    let nqr = Fq6::new(Fq2::zero(), Fq2::one(), Fq2::zero());
1746
1747    for _ in 0..1000 {
1748        let mut a = Fq6::rand(&mut rng);
1749        let mut b = a;
1750        a = Fq12Config::mul_fp6_by_nonresidue(&a);
1751        b.mul_assign(&nqr);
1752
1753        assert_eq!(a, b);
1754    }
1755}
1756
1757#[test]
1758fn test_fq6_mul_by_1() {
1759    let mut rng = ark_std::test_rng();
1760
1761    for _ in 0..1000 {
1762        let c1 = Fq2::rand(&mut rng);
1763        let mut a = Fq6::rand(&mut rng);
1764        let mut b = a;
1765
1766        a.mul_by_1(&c1);
1767        b.mul_assign(&Fq6::new(Fq2::zero(), c1, Fq2::zero()));
1768
1769        assert_eq!(a, b);
1770    }
1771}
1772
1773#[test]
1774fn test_fq6_mul_by_01() {
1775    let mut rng = ark_std::test_rng();
1776
1777    for _ in 0..1000 {
1778        let c0 = Fq2::rand(&mut rng);
1779        let c1 = Fq2::rand(&mut rng);
1780        let mut a = Fq6::rand(&mut rng);
1781        let mut b = a;
1782
1783        a.mul_by_01(&c0, &c1);
1784        b.mul_assign(&Fq6::new(c0, c1, Fq2::zero()));
1785
1786        assert_eq!(a, b);
1787    }
1788}
1789
1790#[test]
1791fn test_fq12_mul_by_014() {
1792    let mut rng = ark_std::test_rng();
1793
1794    for _ in 0..1000 {
1795        let c0 = Fq2::rand(&mut rng);
1796        let c1 = Fq2::rand(&mut rng);
1797        let c5 = Fq2::rand(&mut rng);
1798        let mut a = Fq12::rand(&mut rng);
1799        let mut b = a;
1800
1801        a.mul_by_014(&c0, &c1, &c5);
1802        b.mul_assign(&Fq12::new(
1803            Fq6::new(c0, c1, Fq2::zero()),
1804            Fq6::new(Fq2::zero(), c5, Fq2::zero()),
1805        ));
1806
1807        assert_eq!(a, b);
1808    }
1809}