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 } }
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 }
//------------------------------------------------------ // multipulication is implemented by Karatsuba method. //------------------------------------------------------ void bn254_fp2_mul(Element z, const Element x, const Element y) { Element* t = field(z)->base->tmp; bn254_fp_addn(t[1], rep0(x), rep1(x)); // t1 = x0 + x1 bn254_fp_addn(t[2], rep0(y), rep1(y)); // t2 = y0 + y1 bn254_fp_muln(t[0], t[1], t[2]); // t0 = t1 * t2 bn254_fp_muln(t[1], rep0(x), rep0(y)); // t1 = x0 * y0 bn254_fp_muln(t[2], rep1(x), rep1(y)); // t2 = x1 * y1 bn254_fp_subn(t[0], t[0], t[1]); // bn254_fp_subn(rep1(z), t[0], t[2]); // z1 = x0*y1 + y0*x1 bn254_fp_mod(rep1(z), rep1(z)); // bn254_fp_addn(rep0(z), t[2], t[2]); // bn254_fp_addn(rep0(z), rep0(z), rep0(z)); bn254_fp_addn(rep0(z), rep0(z), t[2]); // bn254_fp_subn(rep0(z), t[1], rep0(z)); // bn254_fp_mod(rep0(z), rep0(z)); // z0 = t1 - 5*t2 }
//------------------------------------------------------ // multipulication is implemented by Karatsuba method. //------------------------------------------------------ void bn254_fp2_mul(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); } if (strcmp(x->field->field_name, "bn254_fp2a") == 0) { bn254_fp_addn(t[1], rep0(x), rep1(x)); // t1 = x0 + x1 bn254_fp_addn(t[2], rep0(y), rep1(y)); // t2 = y0 + y1 bn254_fp_muln(t[0], t[1], t[2]); // t0 = t1 * t2 bn254_fp_muln(t[1], rep0(x), rep0(y)); // t1 = x0 * y0 bn254_fp_muln(t[2], rep1(x), rep1(y)); // t2 = x1 * y1 bn254_fp_subn(t[0], t[0], t[1]); // bn254_fp_subn(rep1(z), t[0], t[2]); // z1 = x0*y1 + y0*x1 bn254_fp_mod(rep1(z), rep1(z)); // bn254_fp_addn(rep0(z), t[2], t[2]); // bn254_fp_addn(rep0(z), rep0(z), rep0(z)); bn254_fp_addn(rep0(z), rep0(z), t[2]); // bn254_fp_subn(rep0(z), t[1], rep0(z)); // bn254_fp_mod(rep0(z), rep0(z)); // z0 = t1 - 5*t2 } if (strcmp(x->field->field_name, "bn254_fp2b") == 0) { bn254_fp_addn(t[1], rep0(x), rep1(x)); // t1 = x0 + x1 bn254_fp_addn(t[2], rep0(y), rep1(y)); // t2 = y0 + y1 bn254_fp_muln(t[0], t[1], t[2]); // t0 = t1 * t2 bn254_fp_muln(t[1], rep0(x), rep0(y)); // t1 = x0 * y0 bn254_fp_muln(t[2], rep1(x), rep1(y)); // t2 = x1 * y1 bn254_fp_subn(t[0], t[0], t[1]); // (x0+x1)*(y0+y1)-x0*y0 bn254_fp_subn(rep1(z), t[0], t[2]); // z1 = x0*y1 + y0*x1 bn254_fp_mod(rep1(z), rep1(z)); // bn254_fp_sub(rep0(z), t[1], t[2]); // x0*y0 - x1*y1 bn254_fp_mod(rep0(z), rep0(z)); // z0 = t1 - t2 } //for(i=0;i<10;i++){ element_clear(t[i]);} }
void bn254_fp2_mod(Element z, const Element x) { bn254_fp_mod(rep0(z), rep0(x)); bn254_fp_mod(rep1(z), rep1(x)); }