Ejemplo n.º 1
0
int bn254_fp6_is_sqr(const Element x)
{
    int k = 1;

    Element *t = field(x)->base->tmp;

    if (element_is_zero(x)) {
        return FALSE;
    }

    k *= bn254_fp2_is_sqr(rep2(x)) ? 1 : -1;

    bn254_fp2_sqr(t[1], rep1(x));
    bn254_fp2_mul(t[2], rep0(x), rep2(x));
    bn254_fp2_sub(t[1], t[1], t[2]);      // t1 = x1^2-x0*x2
    bn254_fp2_mul(t[2], rep0(x), rep1(x));
    bn254_fp2_sqr(t[3], rep2(x));
    bn254_fp2_xi_mul(t[3], t[3]);
    bn254_fp2_sub(t[2], t[2], t[3]);      // t2 = x0*x1-x2^2*xi
    bn254_fp2_inv(t[1], t[1]);
    bn254_fp2_mul(t[1], t[1], t[2]);      // t1 = t2 / t1

    bn254_fp2_inv(t[2], rep2(x));
    bn254_fp2_mul(t[3], t[2], rep1(x));
    bn254_fp2_sub(t[3], t[3], t[1]);
    bn254_fp2_mul(t[3], t[3], t[1]);      // t3 = ((x1/x2)-t1)t1

    bn254_fp2_mul(t[2], t[2], rep0(x));
    bn254_fp2_sub(t[2], t[2], t[3]);      // t2 = (x0/x2)-t3

    k *= bn254_fp2_is_sqr(t[2]) ? 1 : -1;

    return (k == 1);
}
Ejemplo n.º 2
0
int bn254_fp2_is_sqr(const Element x)
{
    int hr = FALSE;

    Element *t = field(x)->base->tmp;

    if (element_is_zero(x)) { return FALSE; }

    bn254_fp_inv(t[0], rep1(x));
    bn254_fp_mul(t[0], t[0], rep0(x));
    bn254_fp_sqr(t[0], t[0]);
    bn254_fp_add(t[0], t[0], field(x)->irre_poly[0]);

    hr = bn254_fp_is_sqr(t[0]);

    return hr;
}
Ejemplo n.º 3
0
//============================================
//   四則演算のテストプログラム
//============================================
void test_arithmetic_operation(Field f)
{
    int i;
    unsigned long long int t1, t2;
    Element a, b, c, d;
    Element d1, d2, d3;

    mpz_t exp;

    //--------------------
    //  init
    //--------------------
    element_init(a, f);
    element_init(b, f);
    element_init(c, f);
    element_init(d, f);

    element_init(d1, f->base);
    element_init(d2, f->base);
    element_init(d3, f->base);

    //--------------------
    //  add
    //--------------------
    element_set_str(a, "17A767D1D0F35B9B2CE7CF00A9D036B7E087E24F1CBFFEF8C599F75DFDAD470B 107B87895D05703A57532E47A7BD8CA9C6406B5378C7E81F064749BB848490B2 44C128CE2774FC5C7022584909212BAB973CE5CA964D1B6A0ED9CCED1FA77DD 61C632B1BA796F688FF4A04475242B4116296B8A873B6C535C090278D88A7DB 685D0DA4D199864C925BAC4FD9EDBC9522367F43054E28E4E529A0FBF9BA604 7533624DC55FB89EDD5417E8F7E88E0B4A1F4CABD11353BEB3BD55FB62E7555");
    element_set_str(b, "19A7AA060E14E19A21FEF364C2BA8C3015EE7951DC51B928A55CE8C77E8998D9 43D2B55CCF3C20793C1402EE521F63E0DA933666152D14E6B35670281BB24E7 1A27F4DA83812A83B3189BAA7B9CB92F8401337F9334D9E155F66F31F33DE5DC 189A48DEB41106F112E6CCC46C55110E9BCF02C2B727B373CC6E46F3F80147F5 1A6243EC30C85C55E948EFF298055B8B5991E3CBD578CAB7DF4C1E52427329B7 2331BDFFF829F8D50BC7FD2BEF8607985B8C3F4805BA05F0F5CAB75DAD1E0194");
    element_set_str(d, "DDE16D341C72D770070A7CCE5A5C0A679741C60E111B81FEC7680257C36DFE3 14B8B2DF29F93241EB146E768CDF82E7D3E99EB9DA1AB96D717CB0BE063FB599 1E74076765F87A497A1AC12F0C2ECBEA3D7501DC3C99AB97F6E40C00C5385DB9 1EB6AC09CFB89DE79BE616C8B3A753C2AD31997B5F9B6A39022ED71B8589EFD0 20E814C67DE1F4BAB26EAAB795A43754ABB54BC005CDAD462D9EB862020ECFBB 713F920373EE4A0AB272411F81F8E37932BF4D2AACB3B2B62862CBD634C76E8");

    element_add(c, a, b);

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_add(c, a, b); }
    t2 = rdtsc();

    printf("element add: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  sub
    //--------------------
    element_set(d, c);
    element_sub(c, c, d);

    assert(element_is_zero(c));

    //--------------------
    //  mul
    //--------------------
    element_mul(c, a, b);

    element_set_str(d1, "12B60569C8620CA5D70141D319878E5B060A4AA80413BEC52B5173D61E8A3387 1EF2082B9DCA3ABF201AA99B0230714369969A04C6064A7E9288B9551F95CA36");
    element_set_str(d2, "17B103CB3F5567B2C3A06E07CD31923CD87D72406E23B25B5066E61D029991F1 1791B2FB67A6FE4BC47F7719F2B5720ED863499E582E787A3EE2F513B4BDB4CF");
    element_set_str(d3, "2264428EB266F97E47163B2BE433146D0D5FBD32F6552D6079B4D85719F10635 14418C5689124B593F626719E1A07B7D0A2DA48E5BFEC13BDE16F0B8CC521F9B");

    element_set(((Element *)d->data)[0], d1);
    element_set(((Element *)d->data)[1], d2);
    element_set(((Element *)d->data)[2], d3);

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_mul(c, a, b); }
    t2 = rdtsc();

    printf("element mul: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  sqr
    //--------------------
    element_sqr(c, a);
    element_mul(d, a, a);

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_sqr(c, a); }
    t2 = rdtsc();

    printf("element sqr: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  random
    //--------------------
    element_random(b);

    //--------------------
    //  inv
    //--------------------
    element_mul(c, a, b);
    element_inv(b, b);
    element_mul(c, c, b);
    element_inv(d, a);
    element_mul(d, a, d);

    assert(element_cmp(c, a) == 0);
    assert(element_is_one(d));

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_inv(c, a); }
    t2 = rdtsc();

    printf("element inv: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  pow
    //--------------------
    mpz_init(exp);

    mpz_set_str(exp, "AA4A2EE5234E0E95E8BB01F6B4A67F0EE8F2ADC1AA153C48D163AA85F3F534C", 16);

    element_pow(c, a, exp);
    element_set_str(d, "1CE3C3F32AC26429F4B8CDA845C051A4E88297DCCF33466FE03DDBCB06D7F83C 12AB1B1C4204A483CB230C3A25F3D3498CBC006D7DFF3F9D3565544BF09EEECD 16449675C385AA37E54DC18FABD35B84D1714E22991BEBD5CE7EFFBBEB110517 4B8B39A2E4C86D3BBD58B771A220F62E988136ECBB92D1213B3D268152A4E14 5C3CF6A5663F06A4F805674B29A6D2D7B9F0A8DA7E366EC239420E78B5BFAB6 C762C8ECC272929A992D55227F9C2927A04731122265C0E217B9B47AF524AA1");

    assert(element_cmp(c, d) == 0);

    mpz_set(exp, f->order);

    for (i = 0; i < 50; i++)
    {
        element_random(a);

        element_pow(b, a, exp);

        assert(element_cmp(b, a) == 0);
    }

    t1 = rdtsc();
    for (i = 0; i < M; i++) { element_pow(b, a, exp); }
    t2 = rdtsc();

    printf("element pow with order: %.2lf [clock]\n", (double)(t2 - t1) / M);

    mpz_clear(exp);

    //--------------------
    //  clear
    //--------------------
    element_clear(a);
    element_clear(b);
    element_clear(c);
    element_clear(d);

    element_clear(d1);
    element_clear(d2);
    element_clear(d3);
}
Ejemplo n.º 4
0
//============================================
//   四則演算のテストプログラム
//============================================
void test_arithmetic_operation(Field f)
{
    int i;
    unsigned long long int t1, t2;
    Element a, b, c, d;

    char loop[] = "100";

    mpz_t e, exp;

    //--------------------
    //  init
    //--------------------
    element_init(a, f);
    element_init(b, f);
    element_init(c, f);
    element_init(d, f);

    //--------------------
    //  add
    //--------------------
    element_set_str(a, "1C12C39A2AD14054EDC9EE504301127AFFEEAADC59A78B50FCFFED87AC6EB8BF 20E1A922384561EA82602CD664D85D442DAC5D391E142ABB3CFEC2A095C22DF9");
    element_set_str(b, "F1B91250A124F268B8239185B23B31EB25179A11A9A0398E61B701F7D4F7265 20D206C5F7D007EDBA34A4B041622289D64F04CA28CEAC490619585AA14F7B2F");
    element_set_str(d, "7BD59BA97A27FBD2AD60CD0173FC358353DE53D5C418EE8649AFDA729BE2B23 1E42B4E392D45A19EE1EB6EE1F557D8C86F922C32EE2D702C497BAFB3711A927");

    element_add(c, a, b);

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_add(c, a, b); }
    t2 = rdtsc();

    printf("element add: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  sub
    //--------------------
    element_set(d, c);
    element_sub(c, c, d);

    assert(element_is_zero(c));

    //--------------------
    //  mul
    //--------------------
    element_mul(c, a, b);
    element_set_str(d, "1D0562FF0AB317FFDE555320A7072D2B29C07077E08996CE5F093BB8E4200B2C 9B04361A24DC7F37C8BD09A7C51A9D8577168AD021BF2B4AC3D67552F481B1A");

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_mul(c, a, b); }
    t2 = rdtsc();

    printf("element mul: %.2lf [clock]\n", (double)(t2 - t1) / N);

    mpz_init_set_str(e, "1B45F16C848B9C476C1D2FF1FD60A0D0C19BBA6F3ECE3CF6C5FCE4FAB7CAD4FF", 16);

    element_pow(c, a, e);
    element_set_str(d, "B40190CE812CB4F668A839952128D19B1748F3BB19E902480D089AF9053A6D2 19DA59F09C3C20472C3BD19A4FC95BCAF266B9D1539AAD23E3C67C4F3A7CA51D");

    assert(element_cmp(c, d) == 0);

    mpz_clear(e);

    //--------------------
    //  sqr
    //--------------------
    element_sqr(c, a);
    element_mul(d, a, a);

    assert(element_cmp(c, d) == 0);

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_sqr(c, a); }
    t2 = rdtsc();

    printf("element sqr: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  random
    //--------------------
    element_random(a);
    element_random(b);

    //--------------------
    //  inv
    //--------------------
    element_mul(c, a, b);
    element_inv(b, b);
    element_mul(c, c, b);
    element_inv(d, a);
    element_mul(d, a, d);

    assert(element_cmp(c, a) == 0);
    assert(element_is_one(d));

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_inv(b, a); }
    t2 = rdtsc();

    printf("element inv: %.2lf [clock]\n", (double)(t2 - t1) / N);

    //--------------------
    //  pow
    //--------------------
    mpz_init_set_str(exp, loop, 10);

    element_set_one(b);

    for (i = 0; i < atoi(loop); i++) { element_mul(b, b, a); }

    element_pow(c, a, exp);

    assert(element_cmp(b, c) == 0);

    mpz_set(exp, f->order);

    for (i = 0; i < 100; i++)
    {
        element_random(a);
        element_pow(b, a, exp);

        assert(element_cmp(b, a) == 0);
    }

    t1 = rdtsc();
    for (i = 0; i < N; i++) { element_pow(b, a, exp); }
    t2 = rdtsc();

    printf("element pow with order: %.2lf [clock]\n", (double)(t2 - t1) / N);

    mpz_clear(exp);

    //--------------------
    //  clear
    //--------------------
    element_clear(a);
    element_clear(b);
    element_clear(c);
    element_clear(d);
}