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 fp12_mul_lazyr(fp12_t c, fp12_t a, fp12_t b) { dv6_t u0, u1, u2, u3; fp6_t t0, t1; dv6_null(u0); dv6_null(u1); dv6_null(u2); dv6_null(u3); fp6_null(t0); fp6_null(t0); fp6_null(t1); TRY { dv6_new(u0); dv6_new(u1); dv6_new(u2); dv6_new(u3); fp6_new(t0); fp6_new(t1); /* Karatsuba algorithm. */ /* u0 = a_0 * b_0. */ fp6_mul_unr(u0, a[0], b[0]); /* u1 = a_1 * b_1. */ fp6_mul_unr(u1, a[1], b[1]); /* t1 = a_0 + a_1. */ fp6_add(t0, a[0], a[1]); /* t0 = b_0 + b_1. */ fp6_add(t1, b[0], b[1]); /* u2 = (a_0 + a_1) * (b_0 + b_1) */ fp6_mul_unr(u2, t0, t1); /* c_1 = u2 - a_0b_0 - a_1b_1. */ for (int i = 0; i < 3; i++) { fp2_addc_low(u3[i], u0[i], u1[i]); fp2_subc_low(u2[i], u2[i], u3[i]); fp2_rdcn_low(c[1][i], u2[i]); } /* c_0 = a_0b_0 + v * a_1b_1. */ fp2_nord_low(u2[0], u1[2]); dv_copy(u2[1][0], u1[0][0], 2 * RLC_FP_DIGS); dv_copy(u2[1][1], u1[0][1], 2 * RLC_FP_DIGS); dv_copy(u2[2][0], u1[1][0], 2 * RLC_FP_DIGS); dv_copy(u2[2][1], u1[1][1], 2 * RLC_FP_DIGS); for (int i = 0; i < 3; i++) { fp2_addc_low(u2[i], u0[i], u2[i]); fp2_rdcn_low(c[0][i], u2[i]); } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv6_free(u0); dv6_free(u1); dv6_free(u2); dv6_free(u3); 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); } }
void fp12_mul_dxs_lazyr(fp12_t c, fp12_t a, fp12_t b) { fp6_t t0; dv6_t u0, u1, u2; fp6_null(t0); dv6_null(u0); dv6_null(u1); dv6_null(u2); TRY { fp6_new(t0); dv6_new(u0); dv6_new(u1); dv6_new(u2); if (ep2_curve_is_twist() == EP_DTYPE) { #if EP_ADD == BASIC /* t0 = a_0 * b_0. */ fp_muln_low(u0[0][0], a[0][0][0], b[0][0][0]); fp_muln_low(u0[0][1], a[0][0][1], b[0][0][0]); fp_muln_low(u0[1][0], a[0][1][0], b[0][0][0]); fp_muln_low(u0[1][1], a[0][1][1], b[0][0][0]); fp_muln_low(u0[2][0], a[0][2][0], b[0][0][0]); fp_muln_low(u0[2][1], a[0][2][1], b[0][0][0]); /* t2 = b_0 + b_1. */ fp_add(t0[0][0], b[0][0][0], b[1][0][0]); fp_copy(t0[0][1], b[1][0][1]); fp2_copy(t0[1], b[1][1]); #elif EP_ADD == PROJC /* t0 = a_0 * b_0. */ #ifdef RLC_FP_ROOM fp2_mulc_low(u0[0], a[0][0], b[0][0]); fp2_mulc_low(u0[1], a[0][1], b[0][0]); fp2_mulc_low(u0[2], a[0][2], b[0][0]); #else fp2_muln_low(u0[0], a[0][0], b[0][0]); fp2_muln_low(u0[1], a[0][1], b[0][0]); fp2_muln_low(u0[2], a[0][2], b[0][0]); #endif /* t2 = b_0 + b_1. */ fp2_add(t0[0], b[0][0], b[1][0]); fp2_copy(t0[1], b[1][1]); #endif /* t1 = a_1 * b_1. */ fp6_mul_dxs_unr_lazyr(u1, a[1], b[1]); } else { /* t0 = a_0 * b_0. */ fp6_mul_dxs_unr_lazyr(u0, a[0], b[0]); #if EP_ADD == BASIC /* t0 = a_0 * b_0. */ fp_muln_low(u1[1][0], a[1][2][0], b[1][1][0]); fp_muln_low(u1[1][1], a[1][2][1], b[1][1][0]); fp2_nord_low(u1[0], u1[1]); fp_muln_low(u1[1][0], a[1][0][0], b[1][1][0]); fp_muln_low(u1[1][1], a[1][0][1], b[1][1][0]); fp_muln_low(u1[2][0], a[1][1][0], b[1][1][0]); fp_muln_low(u1[2][1], a[1][1][1], b[1][1][0]); /* t2 = b_0 + b_1. */ fp2_copy(t0[0], b[0][0]); fp_add(t0[1][0], b[0][1][0], b[1][1][0]); fp_copy(t0[1][1], b[0][1][1]); #elif EP_ADD == PROJC /* t1 = a_1 * b_1. */ fp2_muln_low(u1[1], a[1][2], b[1][1]); fp2_nord_low(u1[0], u1[1]); fp2_muln_low(u1[1], a[1][0], b[1][1]); fp2_muln_low(u1[2], a[1][1], b[1][1]); /* t2 = b_0 + b_1. */ fp2_copy(t0[0], b[0][0]); fp2_add(t0[1], b[0][1], b[1][1]); #endif } /* 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_dxs_unr_lazyr(u2, c[1], t0); for (int i = 0; i < 3; i++) { fp2_subc_low(u2[i], u2[i], u0[i]); fp2_subc_low(u2[i], u2[i], u1[i]); } fp2_rdcn_low(c[1][0], u2[0]); fp2_rdcn_low(c[1][1], u2[1]); fp2_rdcn_low(c[1][2], u2[2]); fp2_nord_low(u2[0], u1[2]); fp2_addc_low(u0[0], u0[0], u2[0]); fp2_addc_low(u0[1], u0[1], u1[0]); fp2_addc_low(u0[2], u0[2], u1[1]); /* c_0 = a_0b_0 + v * a_1b_1. */ fp2_rdcn_low(c[0][0], u0[0]); fp2_rdcn_low(c[0][1], u0[1]); fp2_rdcn_low(c[0][2], u0[2]); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp6_free(t0); dv6_free(u0); dv6_free(u1); dv6_free(u2); } }
void fp12_mul_dxs_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); if (ep2_curve_is_twist() == EP_DTYPE) { #if EP_ADD == BASIC /* t0 = a_0 * b_0 */ fp_mul(t0[0][0], a[0][0][0], b[0][0][0]); fp_mul(t0[0][1], a[0][0][1], b[0][0][0]); fp_mul(t0[1][0], a[0][1][0], b[0][0][0]); fp_mul(t0[1][1], a[0][1][1], b[0][0][0]); fp_mul(t0[2][0], a[0][2][0], b[0][0][0]); fp_mul(t0[2][1], a[0][2][1], b[0][0][0]); /* t2 = b_0 + b_1. */ fp_add(t2[0][0], b[0][0][0], b[1][0][0]); fp_copy(t2[0][1], b[1][0][1]); fp2_copy(t2[1], b[1][1]); #elif EP_ADD == PROJC /* t0 = a_0 * b_0 */ fp2_mul(t0[0], a[0][0], b[0][0]); fp2_mul(t0[1], a[0][1], b[0][0]); fp2_mul(t0[2], a[0][2], b[0][0]); /* t2 = b_0 + b_1. */ fp2_add(t2[0], b[0][0], b[1][0]); fp2_copy(t2[1], b[1][1]); #endif /* t1 = a_1 * b_1. */ fp6_mul_dxs(t1, a[1], b[1]); } else { /* t0 = a_0 * b_0. */ fp6_mul_dxs(t0, a[0], b[0]); #if EP_ADD == BASIC /* t1 = a_1 * b_1. */ fp_mul(t2[0][0], a[1][2][0], b[1][1][0]); fp_mul(t2[0][1], a[1][2][1], b[1][1][0]); fp2_mul_nor(t1[0], t2[0]); fp_mul(t1[1][0], a[1][0][0], b[1][1][0]); fp_mul(t1[1][1], a[1][0][1], b[1][1][0]); fp_mul(t1[2][0], a[1][1][0], b[1][1][0]); fp_mul(t1[2][1], a[1][1][1], b[1][1][0]); /* t2 = b_0 + b_1. */ fp2_copy(t2[0], b[0][0]); fp_add(t2[1][0], b[0][1][0], b[1][1][0]); fp_copy(t2[1][1], b[0][1][1]); #elif EP_ADD == PROJC /* t1 = a_1 * b_1. */ fp2_mul(t2[0], a[1][2], b[1][1]); fp2_mul_nor(t1[0], t2[0]); fp2_mul(t1[1], a[1][0], b[1][1]); fp2_mul(t1[2], a[1][1], b[1][1]); /* t2 = b_0 + b_1. */ fp2_copy(t2[0], b[0][0]); fp2_add(t2[1], b[0][1], b[1][1]); #endif } /* c_1 = a_0 + a_1. */ fp6_add(c[1], a[0], a[1]); /* c_1 = (a_0 + a_1) * (b_0 + b_1) - a_0 * b_0 - a_1 * b_1. */ fp6_mul_dxs(c[1], c[1], t2); fp6_sub(c[1], c[1], t0); fp6_sub(c[1], c[1], t1); /* c_0 = a_0 * b_0 + v * a_1 * b_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); } }