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