void fp12_inv(fp12_t c, fp12_t a) { fp6_t t0; fp6_t t1; fp6_null(t0); fp6_null(t1); TRY { fp6_new(t0); fp6_new(t1); fp6_sqr(t0, a[0]); fp6_sqr(t1, a[1]); fp6_mul_art(t1, t1); fp6_sub(t0, t0, t1); fp6_inv(t0, t0); fp6_mul(c[0], a[0], t0); fp6_neg(c[1], a[1]); fp6_mul(c[1], c[1], t0); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp6_free(t0); fp6_free(t1); } }
void fp12_sqr_basic(fp12_t c, fp12_t a) { fp6_t t0, t1; fp6_null(t0); fp6_null(t1); TRY { fp6_new(t0); fp6_new(t1); fp6_add(t0, a[0], a[1]); fp6_mul_art(t1, a[1]); fp6_add(t1, a[0], t1); fp6_mul(t0, t0, t1); fp6_mul(c[1], a[0], a[1]); fp6_sub(c[0], t0, c[1]); fp6_mul_art(t1, c[1]); fp6_sub(c[0], c[0], t1); fp6_dbl(c[1], c[1]); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp6_free(t0); fp6_free(t1); } }
void fp18_inv(fp18_t c, fp18_t a) { fp6_t v0; fp6_t v1; fp6_t v2; fp6_t t0; fp6_null(v0); fp6_null(v1); fp6_null(v2); fp6_null(t0); TRY { fp6_new(v0); fp6_new(v1); fp6_new(v2); fp6_new(t0); /* v0 = a_0^2 - E * a_1 * a_2. */ fp6_sqr(t0, a[0]); fp6_mul(v0, a[1], a[2]); fp6_mul_art(v2, v0); fp6_sub(v0, t0, v2); /* v1 = E * a_2^2 - a_0 * a_1. */ fp6_sqr(t0, a[2]); fp6_mul_art(v2, t0); fp6_mul(v1, a[0], a[1]); fp6_sub(v1, v2, v1); /* v2 = a_1^2 - a_0 * a_2. */ fp6_sqr(t0, a[1]); fp6_mul(v2, a[0], a[2]); fp6_sub(v2, t0, v2); fp6_mul(t0, a[1], v2); fp6_mul_art(c[1], t0); fp6_mul(c[0], a[0], v0); fp6_mul(t0, a[2], v1); fp6_mul_art(c[2], t0); fp6_add(t0, c[0], c[1]); fp6_add(t0, t0, c[2]); fp6_inv(t0, t0); fp6_mul(c[0], v0, t0); fp6_mul(c[1], v1, t0); fp6_mul(c[2], v2, t0); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp6_free(v0); fp6_free(v1); fp6_free(v2); fp6_free(t0); } }
void fp12_mul_basic(fp12_t c, fp12_t a, fp12_t b) { fp6_t t0, t1, t2; fp6_null(t0); fp6_null(t1); fp6_null(t2); TRY { fp6_new(t0); fp6_new(t1); fp6_new(t2); /* Karatsuba algorithm. */ /* t0 = a_0 * b_0. */ fp6_mul(t0, a[0], b[0]); /* t1 = a_1 * b_1. */ fp6_mul(t1, a[1], b[1]); /* t2 = b_0 + b_1. */ fp6_add(t2, b[0], b[1]); /* c_1 = a_0 + a_1. */ fp6_add(c[1], a[0], a[1]); /* c_1 = (a_0 + a_1) * (b_0 + b_1) */ fp6_mul(c[1], c[1], t2); fp6_sub(c[1], c[1], t0); fp6_sub(c[1], c[1], t1); /* c_0 = a_0b_0 + v * a_1b_1. */ fp6_mul_art(t1, t1); fp6_add(c[0], t0, t1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp6_free(t0); fp6_free(t1); fp6_free(t2); } }