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 } }
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 }
//-------------------------------------------------------- // 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); } }
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; }