Beispiel #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);
}
Beispiel #2
0
void bn254_fp6_from_oct(Element x, const unsigned char *os, const size_t size)
{
    mpz_t quo, rem;

    if (size < 190) {
        fprintf(stderr, "error: please set up the enought buffer for element\n");
        exit(300);
    }

    mpz_init(quo);
    mpz_init(rem);

    mpz_import(quo, size, 1, sizeof(*os), 1, 0, os);

    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep0(rep0(x))), rem);
    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep0(rep1(x))), rem);
    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep0(rep2(x))), rem);
    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep1(rep0(x))), rem);
    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep1(rep1(x))), rem);
    mpz_tdiv_qr(quo, rem, quo, field(x)->base->base->order);
    mpz_set(rep(rep1(rep2(x))), rem);

    mpz_clear(quo);
    mpz_clear(rem);
}
Beispiel #3
0
//--------------------------------------------------------
//   z = x * (y1, 0, y2)
//--------------------------------------------------------
void bn254_fp6_mul_fp2_4(Element z, const Element x, const Element y1, const Element y2)
{
    Element *v = field(z)->base->tmp;

    if (field(y1)->ID != bn254_fp2 || field(y2)->ID != bn254_fp2)
    {
        fprintf(stderr, "error: input should be element in bn254_fp6\n");
        exit(200);
    }

    bn254_fp2_mul(v[0], rep0(x), y1);
    bn254_fp2_mul(v[1], rep2(x), y2);

    bn254_fp2_add(v[3], rep0(x), rep2(x));
    bn254_fp2_add(v[4], y1, y2);
    bn254_fp2_mul(rep2(z), v[3], v[4]);
    bn254_fp2_sub(rep2(z), rep2(z), v[0]);
    bn254_fp2_sub(rep2(z), rep2(z), v[1]);

    bn254_fp2_mul(v[3], rep1(x), y2);
    bn254_fp2_mul(v[4], rep1(x), y1);

    bn254_fp2_xi_mul(v[1], v[1]);
    bn254_fp2_xi_mul(v[3], v[3]);

    bn254_fp2_add(rep0(z), v[0], v[3]);
    bn254_fp2_add(rep1(z), v[1], v[4]);
}
Beispiel #4
0
int bn254_fp2_cmp(const Element x, const Element y)
{
    if (bn254_fp_cmp(rep1(x), rep1(y)) == 0)
    {
        if (bn254_fp_cmp(rep0(x), rep0(y)) == 0) { return 0; }
    }
    return 1;
}
Beispiel #5
0
void bn254_fp2_sqrn(Element z, const Element x)
{
    Element *t = field(z)->base->tmp;

    bn254_fp_addn(t[0], rep0(x), rep1(x));
    bn254_fp_sub(t[1], rep0(x), rep1(x));
    bn254_fp_muln(rep0(z), t[0], t[1]);
    bn254_fp_addn(t[0], rep0(x), rep0(x));
    bn254_fp_muln(rep1(z), t[0], rep1(x));
}
Beispiel #6
0
void bn254_fp2_xi_mul(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    bn254_fp_add(t[0], rep1(x), rep1(x));
    bn254_fp_add(t[0], t[0], t[0]);
    bn254_fp_add(t[0], t[0], rep1(x));
    bn254_fp_set(rep1(z), rep0(x));
    bn254_fp_neg(rep0(z), t[0]);
}
Beispiel #7
0
//--------------------------------------------------------
//   z = x * gamma ( Fp12 : Fp6[x]/x^2-gamma )
//--------------------------------------------------------
void bn254_fp6_gm_mul(Element z, const Element x)
{
    if (z == x) {
        fprintf(stderr, "fail gm mul\n");
        exit(500);
    }

    bn254_fp2_xi_mul(rep0(z), rep2(x));
    bn254_fp2_set(rep1(z), rep0(x));
    bn254_fp2_set(rep2(z), rep1(x));
}
Beispiel #8
0
//--------------------------------------------------------
//   z = x * (y, 0, 0)
//--------------------------------------------------------
void bn254_fp6_mul_fp2(Element z, const Element x, const Element y)
{
    if (field(y)->ID != bn254_fp2)
    {
        fprintf(stderr, "error: input should be element in bn254_fp6\n");
        exit(200);
    }

    bn254_fp2_mul(rep0(z), rep0(x), y);
    bn254_fp2_mul(rep1(z), rep1(x), y);
    bn254_fp2_mul(rep2(z), rep2(x), y);
}
Beispiel #9
0
void bn254_fp6_get_str(char *s, const Element x)
{
    char s0[65], s1[65], s2[65], s3[65], s4[65], s5[65];

    bn254_fp_get_str(s0, rep0(rep0(x)));
    bn254_fp_get_str(s1, rep0(rep1(x)));
    bn254_fp_get_str(s2, rep0(rep2(x)));
    bn254_fp_get_str(s3, rep1(rep0(x)));
    bn254_fp_get_str(s4, rep1(rep1(x)));
    bn254_fp_get_str(s5, rep1(rep2(x)));

    sprintf(s, "%s %s %s %s %s %s", s0, s1, s2, s3, s4, s5);
}
Beispiel #10
0
//-------------------------------------------
//  i/o operation (octet string)
//-------------------------------------------
void bn254_fp6_to_mpz(mpz_t a, const Element x)
{
    mpz_mul(a, rep(rep1(rep2(x))), field(x)->base->base->order);   // a = rep12*p
    mpz_add(a, a, rep(rep1(rep1(x))));   // a = a + rep11
    mpz_mul(a, a, field(x)->base->base->order);   // a = a*p
    mpz_add(a, a, rep(rep1(rep0(x))));   // a = a + rep10
    mpz_mul(a, a, field(x)->base->base->order);   // a = a*p
    mpz_add(a, a, rep(rep0(rep2(x))));   // a = a + rep02
    mpz_mul(a, a, field(x)->base->base->order);   // a = a*p
    mpz_add(a, a, rep(rep0(rep1(x))));   // a = a + rep01
    mpz_mul(a, a, field(x)->base->base->order);   // a = a*p
    mpz_add(a, a, rep(rep0(rep0(x))));   // a = a + rep00
}
Beispiel #11
0
void bn254_fp2_inv(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    if (strcmp(x->field->field_name, "bn254_fp2a") == 0)
    {
        bn254_fp_muln(t[1], rep1(x), rep1(x)); // t1 = a1^2
        bn254_fp_addn(t[0], t[1], t[1]);
        bn254_fp_addn(t[0], t[0], t[0]);
        bn254_fp_addn(t[1], t[1], t[0]);      // t1 = 5*a1^2
        bn254_fp_muln(t[0], rep0(x), rep0(x));// t0 = a0^2
        bn254_fp_addn(t[0], t[0], t[1]);      // t0 = t0 - t1
        bn254_fp_inv(t[1], t[0]);             // t1 = t0^-1
        bn254_fp_mul(rep0(z), rep0(x), t[1]); // c0 = a0*t1
        bn254_fp_mul(rep1(z), rep1(x), t[1]); // c1 = a1*t1
        bn254_fp_neg(rep1(z), rep1(z));       // c1 = -1*a1*t1
    }

    if (strcmp(x->field->field_name, "bn254_fp2b") == 0)
    {
        bn254_fp_muln(t[1], rep1(x), rep1(x));// t1 = a1^2
        bn254_fp_muln(t[0], rep0(x), rep0(x));// t0 = a0^2
        bn254_fp_addn(t[0], t[0], t[1]);      // t0 = t0 + t1 ( beta = -1 )
        bn254_fp_inv(t[1], t[0]);             // t1 = t0^-1
        bn254_fp_mul(rep0(z), rep0(x), t[1]); // c0 = a0*t1
        bn254_fp_mul(rep1(z), rep1(x), t[1]); // c1 = a1*t1
        bn254_fp_neg(rep1(z), rep1(z));       // c1 = -1*a1*t1
    }
}
Beispiel #12
0
void bn254_fp2_sqr(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    if (strcmp(x->field->field_name, "bn254_fp2a") == 0)
    {
        bn254_fp_addn(t[0], rep1(x), rep1(x)); //
        bn254_fp_muln(t[0], t[0], rep0(x));    // t0 = 2*x1*x0
        bn254_fp_addp(t[1], rep0(x));
        bn254_fp_subn(t[1], t[1], rep1(x));    // t1 = x0-x1
        bn254_fp_addn(t[2], rep1(x), rep1(x)); //
        bn254_fp_addn(t[2], t[2], t[2]);       //
        bn254_fp_addn(t[2], t[2], rep1(x));    //
        bn254_fp_addn(t[2], t[2], rep0(x));    // t2 = 5*x1 + x0
        bn254_fp_muln(t[1], t[1], t[2]);       // t1 = t1 * t2
        bn254_fp_mod(rep1(z), t[0]);           // c1 = t0
        bn254_fp_addn(t[0], t[0], t[0]);       //
        bn254_fp_subn(t[1], t[1], t[0]);       // t1 = 2*t0*t1
        bn254_fp_mod(rep0(z), t[1]);           // c0 = t1
    }

    if (strcmp(x->field->field_name, "bn254_fp2b") == 0)
    {
        bn254_fp_addn(t[0], rep1(x), rep1(x)); // t0 = 2*x1
        bn254_fp_muln(t[0], t[0], rep0(x));    // t0 = 2*x1*x0
        bn254_fp_addn(t[1], rep0(x), rep1(x)); // t1 = x0+x1
        bn254_fp_subn(t[2], rep0(x), rep1(x)); // t2 = x0-x1
        bn254_fp_muln(t[1], t[1], t[2]);       // t1 = t1*t2
        bn254_fp_mod(rep1(z), t[0]);           // c1 = t0
        bn254_fp_mod(rep0(z), t[1]);		   // c0 = t1
    }
}
Beispiel #13
0
void bn254_fp2_inv(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    bn254_fp_muln(t[1], rep1(x), rep1(x)); // t1 = a1^2
    bn254_fp_addn(t[0], t[1], t[1]);
    bn254_fp_addn(t[0], t[0], t[0]);
    bn254_fp_addn(t[1], t[1], t[0]);      // t1 = 5*a1^2
    bn254_fp_muln(t[0], rep0(x), rep0(x));// t0 = a0^2
    bn254_fp_addn(t[0], t[0], t[1]);      // t0 = t0 - t1
    bn254_fp_inv(t[1], t[0]);             // t1 = t0^-1
    bn254_fp_mul(rep0(z), rep0(x), t[1]); // c0 = a0*t1
    bn254_fp_mul(rep1(z), rep1(x), t[1]); // c1 = a1*t1
    bn254_fp_neg(rep1(z), rep1(z));       // c1 = -1*a1*t1
}
Beispiel #14
0
int bn254_fp2_is_one(const Element x)
{
    if (bn254_fp_is_zero(rep1(x)))
    {
        return (bn254_fp_is_one(rep0(x)));
    }
    return FALSE;
}
Beispiel #15
0
void bn254_fp6_set_str(Element x, const char *s)
{
    int i = 0;
    int len = strlen(s);

    char msg[400], *p, *c[5];

    if (len > 400) {
        fprintf(stderr, "error: input string is too long, string must be smaller than 400\n");
        exit(200);
    }

    strcpy(msg, s);

    p = msg;

    while ((*p) != '\0')
    {
        if ((*p) == ' ') {
            if (i < 5) {
                c[i] = p;
            }
            i++;
        }
        p++;
    }

    if (i != 5) {
        fprintf(stderr, "error: input string is not correct\n");
        exit(200);
    }

    (*c[0]) = '\0';
    (*c[1]) = '\0';
    (*c[2]) = '\0';
    (*c[3]) = '\0';
    (*c[4]) = '\0';

    bn254_fp_set_str(rep0(rep0(x)), msg);
    bn254_fp_set_str(rep0(rep1(x)), ++c[0]);
    bn254_fp_set_str(rep0(rep2(x)), ++c[1]);
    bn254_fp_set_str(rep1(rep0(x)), ++c[2]);
    bn254_fp_set_str(rep1(rep1(x)), ++c[3]);
    bn254_fp_set_str(rep1(rep2(x)), ++c[4]);
}
Beispiel #16
0
//--------------------------------------------------------
//  multiplication of element of fp and element of fp^2
//--------------------------------------------------------
void bn254_fp2_mul_p(Element z, const Element x, const Element y)
{
    if (field(x)->ID == bn254_fp)
    {
        bn254_fp_mul(rep0(z), x, rep0(y));
        bn254_fp_mul(rep1(z), x, rep1(y));
    }
    else if (field(y)->ID == bn254_fp)
    {
        bn254_fp_mul(rep0(z), rep0(x), y);
        bn254_fp_mul(rep1(z), rep1(x), y);
    }
    else
    {
        fprintf(stderr, "error: input should be element in bn254_fp2\n");
        exit(200);
    }
}
Beispiel #17
0
void bn254_fp2_get_str(char *s, const Element x)
{
    char s1[65], s2[65];

    bn254_fp_get_str(s1, rep0(x));
    bn254_fp_get_str(s2, rep1(x));

    sprintf(s, "%s %s", s1, s2);
}
Beispiel #18
0
void bn254_fp2_sqr(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    bn254_fp_addn(t[0], rep1(x), rep1(x)); //
    bn254_fp_muln(t[0], t[0], rep0(x));    // t0 = 2*x1*x0
    bn254_fp_addp(t[1], rep0(x));
    bn254_fp_subn(t[1], t[1], rep1(x));    // t1 = x0-x1
    bn254_fp_addn(t[2], rep1(x), rep1(x)); //
    bn254_fp_addn(t[2], t[2], t[2]);       //
    bn254_fp_addn(t[2], t[2], rep1(x));    //
    bn254_fp_addn(t[2], t[2], rep0(x));    // t2 = 5*x1 + x0
    bn254_fp_muln(t[1], t[1], t[2]);       // t1 = t1 * t2
    bn254_fp_mod(rep1(z), t[0]);           // c1 = t0
    bn254_fp_addn(t[0], t[0], t[0]);       //
    bn254_fp_subn(t[1], t[1], t[0]);       // t1 = 2*t0*t1
    bn254_fp_mod(rep0(z), t[1]);           // c0 = t1
}
Beispiel #19
0
//-------------------------------------------
//  initialization, clear, set
//-------------------------------------------
void bn254_fp2_init(Element x)
{
    x->data = (void *)malloc(sizeof(Element) * 2);

    if (x->data == NULL) { fprintf(stderr, "fail: allocate in fp2 init\n"); exit(100); }

    element_init(rep0(x), field(x)->base);
    element_init(rep1(x), field(x)->base);
}
Beispiel #20
0
void bn254_fp2_muln(Element z, const Element x, const Element y)
{
    Element* t = field(z)->base->tmp;
    //int i;
    //Element* t = (Element *)malloc(sizeof(Element)*10);
    //for(i=0;i<10;i++){ element_init(t[i], field(z)->base); }

    bn254_fp_muln(t[0], rep0(x), rep0(y)); // t0 = x0 * y0
    bn254_fp_muln(t[1], rep1(x), rep1(y)); // t1 = x0 * y1
    bn254_fp_addn(t[2], rep0(x), rep1(x)); // t2 = x0 + x1
    bn254_fp_addn(t[3], rep0(y), rep1(y)); // t2 = y0 + y1
    bn254_fp_muln(t[4], t[2], t[3]);	   // t4 = t2 * t3
    bn254_fp_addn(t[5], t[0], t[1]);	   // t5 = t0 + t1
    bn254_fp_subn(rep1(z), t[4], t[5]);	   // t5 = t4 - t5
    bn254_fp_sub(t[6], t[0], t[1]);		   // t6 = t0 - t1
    bn254_fp_OP2(rep0(z), t[6]);

    //for(i=0;i<10;i++){ element_clear(t[i]); }
}
Beispiel #21
0
void bn254_fp2_clear(Element x)
{
    if (x->data != NULL)
    {
        element_clear(rep0(x));
        element_clear(rep1(x));

        free(x->data);
        x->data = NULL;
    }
}
Beispiel #22
0
void bn254_fp6_inv(Element z, const Element x)
{
    Element *t = field(z)->base->tmp;

    bn254_fp2_sqr(t[0], rep0(x));   // t0 = a0^2
    bn254_fp2_sqr(t[1], rep1(x));   // t1 = a1^2
    bn254_fp2_sqr(t[2], rep2(x));   // t2 = a2^2
    bn254_fp2_mul(t[3], rep0(x), rep1(x));   // t3 = a0*a1
    bn254_fp2_mul(t[4], rep0(x), rep2(x));   // t4 = a0*a2
    bn254_fp2_mul(t[5], rep1(x), rep2(x));   // t5 = a1*a2

    //-------------------------
    // c0 = t0 - xi*t5
    //-------------------------
    bn254_fp2_xi_mul(t[5], t[5]);     // c0 = xi*t5
    bn254_fp2_sub(t[0], t[0], t[5]);  // t0 = t0 - c0

    //-------------------------
    // c1 = xi*t2 - t3
    //-------------------------
    bn254_fp2_xi_mul(t[2], t[2]);     //
    bn254_fp2_sub(t[2], t[2], t[3]);  // t2 = xi*ts - t3

    //-------------------------
    // c2 = t1*t4
    //-------------------------
    bn254_fp2_sub(t[1], t[1], t[4]);  // t1 = t1-t4

    bn254_fp2_mul(t[4], rep0(x), t[0]); // t4 = a0*c0
    bn254_fp2_mul(t[3], rep2(x), t[2]); // t3 = a2*c1
    bn254_fp2_xi_mul(t[3], t[3]);       // t3 = t3*xi
    bn254_fp2_add(t[4], t[4], t[3]);    // t4 = t4+t3
    bn254_fp2_mul(t[3], rep1(x), t[1]); // t3 = a1*c2
    bn254_fp2_xi_mul(t[3], t[3]);       // t3 = t3*xi
    bn254_fp2_add(t[4], t[4], t[3]);    // t4 = t4+t3
    bn254_fp2_inv(t[4], t[4]);          // t4 = t4^-1

    bn254_fp2_mul(rep0(z), t[0], t[4]);
    bn254_fp2_mul(rep1(z), t[2], t[4]);
    bn254_fp2_mul(rep2(z), t[1], t[4]);
}
Beispiel #23
0
void bn254_fp6_sqr(Element z, const Element x)
{
    Element *t = field(z)->base->tmp;

    bn254_fp2_mul(t[0], rep0(x), rep1(x));
    bn254_fp2_add(t[0], t[0], t[0]);  // t0 = 2*a0*a1
    bn254_fp2_sqr(t[1], rep2(x));     // t1 = a2^2

    //-------------------------
    // c1 = t1*xi + t0
    //-------------------------
    bn254_fp2_xi_mul(t[2], t[1]);    //
    bn254_fp2_add(t[2], t[2], t[0]); // t2 = t1*xi + t0

    //-------------------------
    // c2 = t0 - t1
    //-------------------------
    bn254_fp2_sub(t[0], t[0], t[1]); // t0 = t0 - t1

    bn254_fp2_sqr(t[1], rep0(x));              // t1 = a0^2
    bn254_fp2_sub(rep0(z), rep0(x), rep1(x));  //
    bn254_fp2_add(rep0(z), rep0(z), rep2(x));  // v0 = a0 - a1 + a2
    bn254_fp2_mul(rep1(z), rep1(x), rep2(x));  //
    bn254_fp2_add(rep1(z), rep1(z), rep1(z));  // v1 = 2*a1*a2
    bn254_fp2_sqr(rep2(z), rep0(z));           // v2 = v0^2

    //-------------------------
    // c0 = v1*xi + t2
    //-------------------------
    bn254_fp2_xi_mul(rep0(z), rep1(z));    // c0 = v1*xi
    bn254_fp2_add(rep0(z), rep0(z), t[1]); // c0 = c0 + t1

    //-------------------------
    // c2 = c2 + t0 + t1 - t2
    //-------------------------
    bn254_fp2_add(rep2(z), rep2(z), t[0]);    // c2 = v2 + t0
    bn254_fp2_add(rep2(z), rep2(z), rep1(z)); // c2 = c2 + v1
    bn254_fp2_sub(rep2(z), rep2(z), t[1]);    // c2 = c2 - t2

    bn254_fp2_set(rep1(z), t[2]);
}
Beispiel #24
0
void bn254_fp2_xi_mul(Element z, const Element x)
{
    Element* t = field(z)->base->tmp;

    if (strcmp(x->field->field_name, "bn254_fp2a") == 0)
    {
        bn254_fp_add(t[0], rep1(x), rep1(x));
        bn254_fp_add(t[0], t[0], t[0]);
        bn254_fp_add(t[0], t[0], rep1(x));
        bn254_fp_set(rep1(z), rep0(x));
        bn254_fp_neg(rep0(z), t[0]);
    }

    if (strcmp(x->field->field_name, "bn254_fp2b") == 0)
    {
        bn254_fp_sub(t[0], rep0(x), rep1(x));
        bn254_fp_add(t[1], rep0(x), rep1(x));
        bn254_fp_set(rep0(z), t[0]);
        bn254_fp_set(rep1(z), t[1]);
    }
}
Beispiel #25
0
int bn254_fp6_is_one(const Element x)
{
    if (bn254_fp2_is_zero(rep2(x)))
    {
        if (bn254_fp2_is_zero(rep1(x)))
        {
            if (bn254_fp2_is_one(rep0(x))) {
                return TRUE;
            }
        }
    }
    return FALSE;
}
Beispiel #26
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;
}
Beispiel #27
0
void bn254_fp6_mul(Element z, const Element x, const Element y)
{
    Element *t = field(z)->base->tmp;

    bn254_fp2_mul(t[0], rep0(x), rep0(y));  // t0 = a0*b0
    bn254_fp2_mul(t[1], rep1(x), rep1(y));  // t1 = a1*b1
    bn254_fp2_mul(t[2], rep2(x), rep2(y));  // t2 = a2*b2

    //------------------------------------------
    //  c0 = ((a1+a2)*(b1+b2)-t1-t2)*xi + t0
    //------------------------------------------
    bn254_fp2_add(t[4], rep1(x), rep2(x));  // c0 = a1+a2
    bn254_fp2_add(t[3], rep1(y), rep2(y));  // t3 = b1+b2
    bn254_fp2_mul(t[4], t[4], t[3]);        // c0 = c0*t3
    bn254_fp2_sub(t[4], t[4], t[1]);        //
    bn254_fp2_sub(t[4], t[4], t[2]);        // c0 = c0-t1-t2
    bn254_fp2_xi_mul(t[4], t[4]);           // c0 = c0*xi
    bn254_fp2_add(t[4], t[4], t[0]);        // c0 = c0 + t0

    //------------------------------------------
    //  c1 = (a0+a1)*(b0+b1) - t0 - t1 + xi*t2
    //------------------------------------------
    bn254_fp2_add(t[3], rep0(x), rep1(x));    // t3 = a0+a1
    bn254_fp2_add(rep1(z), rep0(y), rep1(y)); // c1 = b0+b1
    bn254_fp2_mul(t[3], t[3], rep1(z));       // t3 = t3*c1
    bn254_fp2_sub(t[3], t[3], t[0]);          //
    bn254_fp2_sub(t[3], t[3], t[1]);          // t3 = t3 - t0 - t1
    bn254_fp2_xi_mul(rep1(z), t[2]);          // c1 = xi*t2
    bn254_fp2_add(rep1(z), rep1(z), t[3]);    // c1 = c0 + t3

    //------------------------------------------
    //  c2 = (a0+a2)*(b0+b2) - t0 - t2 + t1
    //------------------------------------------
    bn254_fp2_add(t[3], rep0(x), rep2(x));    // t3 = a0+a2
    bn254_fp2_add(rep2(z), rep0(y), rep2(y)); // c2 = b0+b2
    bn254_fp2_mul(rep2(z), rep2(z), t[3]);    // c2 = c2*t3
    bn254_fp2_sub(rep2(z), rep2(z), t[0]);    //
    bn254_fp2_sub(rep2(z), rep2(z), t[2]);    // c2 = c2 - t0 - t2
    bn254_fp2_add(rep2(z), rep2(z), t[1]);    // c2 = c2 + t1
    bn254_fp2_set(rep0(z), t[4]);

}
Beispiel #28
0
void bn254_fp2_set_str(Element x, const char *s)
{
    int i = 0;
    int len = strlen(s);

    char msg[140], *p, *c = NULL;

    if (len > 140) { fprintf(stderr, "error: input string is too long, string must be smaller than 140\n"); exit(200); }

    strcpy(msg, s);

    p = msg;

    while ((*p) != '\0') { if ((*p) == ' ') { if (i == 0) { c = p; } i++; } p++; }

    if (i != 1) { fprintf(stderr, "error: input string is not correct\n"); exit(200); }

    (*c) = '\0';

    bn254_fp_set_str(rep0(x), msg);
    bn254_fp_set_str(rep1(x), ++c);
}
Beispiel #29
0
void bn254_fp2_set_one(Element x)
{
    bn254_fp_set_one(rep0(x));
    bn254_fp_set_zero(rep1(x));
}
Beispiel #30
0
void bn254_fp2_set_fp(Element z, const Element x, const Element y)
{
    bn254_fp_set(rep0(z), x);
    bn254_fp_set(rep1(z), y);
}