void ep2_frb(ep2_t r, ep2_t p, int i) { switch (i) { case 0: ep2_copy(r, p); break; case 1: fp2_frb(r->x, p->x, 1); fp2_frb(r->y, p->y, 1); if (ep2_curve_is_twist() == EP_MTYPE) { fp2_mul_frb(r->x, r->x, 1, 4); fp2_mul_art(r->x, r->x); fp2_mul_art(r->y, r->y); } else { fp2_mul_frb(r->x, r->x, 1, 2); } fp2_mul_frb(r->y, r->y, 1, 3); break; case 2: if (ep2_curve_is_twist() == EP_MTYPE) { fp2_mul_frb(r->x, p->x, 2, 4); } else { fp2_mul_frb(r->x, p->x, 2, 2); } fp2_neg(r->y, p->y); break; case 3: if (ep2_curve_is_twist() == EP_MTYPE) { fp2_frb(r->x, p->x, 1); fp2_frb(r->y, p->y, 1); fp2_mul_frb(r->x, r->x, 1, 4); fp2_mul_frb(r->x, r->x, 2, 4); fp2_mul_art(r->x, r->x); fp2_mul_frb(r->y, r->y, 1, 3); fp2_mul_art(r->y, r->y); fp2_neg(r->y, r->y); } else { fp2_frb(r->x, p->x, 1); fp2_mul_frb(r->x, r->x, 3, 2); fp_neg(r->y[0], p->y[0]); fp_copy(r->y[1], p->y[1]); fp2_mul_frb(r->y, r->y, 1, 3); } break; } r->norm = 1; fp_set_dig(r->z[0], 1); fp_zero(r->z[1]); }
void pp_dbl_k12_basic(fp12_t l, ep2_t r, ep2_t q, ep_t p) { fp2_t s; ep2_t t; int one = 1, zero = 0; fp2_null(s); ep2_null(t); TRY { fp2_new(s); ep2_new(t); ep2_copy(t, q); ep2_dbl_slp_basic(r, s, q); fp12_zero(l); if (ep2_curve_is_twist() == EP_MTYPE) { one ^= 1; zero ^= 1; } fp_mul(l[one][zero][0], s[0], p->x); fp_mul(l[one][zero][1], s[1], p->x); fp2_mul(l[one][one], s, t->x); fp2_sub(l[one][one], t->y, l[one][one]); fp_copy(l[zero][zero][0], p->y); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(s); ep2_free(t); } }
void ep2_frb(ep2_t r, ep2_t p, int i) { ep2_copy(r, p); switch (i) { case 1: fp2_frb(r->x, r->x, 1); fp2_frb(r->y, r->y, 1); fp2_frb(r->z, r->z, 1); if (ep2_curve_is_twist() == EP_MTYPE) { fp2_mul_frb(r->x, r->x, 1, 4); fp2_mul_art(r->x, r->x); fp2_mul_art(r->y, r->y); } else { fp2_mul_frb(r->x, r->x, 1, 2); } fp2_mul_frb(r->y, r->y, 1, 3); break; case 2: if (ep2_curve_is_twist() == EP_MTYPE) { fp2_mul_frb(r->x, r->x, 2, 4); } else { fp2_mul_frb(r->x, r->x, 2, 2); } fp2_neg(r->y, r->y); break; case 3: if (ep2_curve_is_twist() == EP_MTYPE) { fp2_frb(r->x, r->x, 1); fp2_frb(r->y, r->y, 1); fp2_frb(r->z, r->z, 1); fp2_mul_frb(r->x, r->x, 1, 4); fp2_mul_frb(r->x, r->x, 2, 4); fp2_mul_art(r->x, r->x); fp2_mul_frb(r->y, r->y, 1, 3); fp2_mul_art(r->y, r->y); fp2_neg(r->y, r->y); } else { fp2_frb(r->x, r->x, 1); fp2_mul_frb(r->x, r->x, 3, 2); fp_neg(r->y[0], r->y[0]); fp_copy(r->y[1], r->y[1]); fp2_mul_frb(r->y, r->y, 1, 3); } break; } }
/** * Doubles a point represented in affine coordinates on an ordinary prime * elliptic curve. * * @param[out] r - the result. * @param[out] s - the resulting slope. * @param[in] p - the point to double. */ static void ep2_dbl_basic_imp(ep2_t r, fp2_t s, ep2_t p) { fp2_t t0, t1, t2; fp2_null(t0); fp2_null(t1); fp2_null(t2); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); /* t0 = 1/(2 * y1). */ fp2_dbl(t0, p->y); fp2_inv(t0, t0); /* t1 = 3 * x1^2 + a. */ fp2_sqr(t1, p->x); fp2_copy(t2, t1); fp2_dbl(t1, t1); fp2_add(t1, t1, t2); if (ep2_curve_is_twist()) { switch (ep_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fp_set_dig(t2[0], 1); fp2_mul_art(t2, t2); fp2_mul_art(t2, t2); fp2_add(t1, t1, t2); break; case OPT_DIGIT: fp_set_dig(t2[0], ep_curve_get_a()[0]); fp2_mul_art(t2, t2); fp2_mul_art(t2, t2); fp2_add(t1, t1, t2); break; default: fp_copy(t2[0], ep_curve_get_a()); fp_zero(t2[1]); fp2_mul_art(t2, t2); fp2_mul_art(t2, t2); fp2_add(t1, t1, t2); break; } } /* t1 = (3 * x1^2 + a)/(2 * y1). */ fp2_mul(t1, t1, t0); if (s != NULL) { fp2_copy(s, t1); } /* t2 = t1^2. */ fp2_sqr(t2, t1); /* x3 = t1^2 - 2 * x1. */ fp2_dbl(t0, p->x); fp2_sub(t0, t2, t0); /* y3 = t1 * (x1 - x3) - y1. */ fp2_sub(t2, p->x, t0); fp2_mul(t1, t1, t2); fp2_sub(r->y, t1, p->y); fp2_copy(r->x, t0); fp2_copy(r->z, p->z); r->norm = 1; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_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); } }
void pp_dbl_lit_k12(fp12_t l, ep_t r, ep_t p, ep2_t q) { fp_t t0, t1, t2, t3, t4, t5, t6; int one = 1, zero = 0; fp_null(t0); fp_null(t1); fp_null(t2); fp_null(t3); fp_null(t4); fp_null(t5); fp_null(t6); TRY { fp_new(t0); fp_new(t1); fp_new(t2); fp_new(t3); fp_new(t4); fp_new(t5); fp_new(t6); fp_sqr(t0, p->x); fp_sqr(t1, p->y); fp_sqr(t2, p->z); fp_mul(t4, ep_curve_get_b(), t2); fp_dbl(t3, t4); fp_add(t3, t3, t4); fp_add(t4, p->x, p->y); fp_sqr(t4, t4); fp_sub(t4, t4, t0); fp_sub(t4, t4, t1); fp_add(t5, p->y, p->z); fp_sqr(t5, t5); fp_sub(t5, t5, t1); fp_sub(t5, t5, t2); fp_dbl(t6, t3); fp_add(t6, t6, t3); fp_sub(r->x, t1, t6); fp_mul(r->x, r->x, t4); fp_add(r->y, t1, t6); fp_sqr(r->y, r->y); fp_sqr(t4, t3); fp_dbl(t6, t4); fp_add(t6, t6, t4); fp_dbl(t6, t6); fp_dbl(t6, t6); fp_sub(r->y, r->y, t6); fp_mul(r->z, t1, t5); fp_dbl(r->z, r->z); fp_dbl(r->z, r->z); r->norm = 0; if (ep2_curve_is_twist() == EP_MTYPE) { one ^= 1; zero ^= 1; } fp2_dbl(l[zero][one], q->x); fp2_add(l[zero][one], l[zero][one], q->x); fp_mul(l[zero][one][0], l[zero][one][0], t0); fp_mul(l[zero][one][1], l[zero][one][1], t0); fp_sub(l[zero][zero][0], t3, t1); fp_zero(l[zero][zero][1]); fp_mul(l[one][one][0], q->y[0], t5); fp_mul(l[one][one][1], q->y[1], t5); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); fp_free(t3); fp_free(t4); fp_free(t5); fp_free(t6); } }
void pp_dbl_k12_projc_lazyr(fp12_t l, ep2_t r, ep2_t q, ep_t p) { fp2_t t0, t1, t2, t3, t4, t5, t6; dv2_t u0, u1; int one = 1, zero = 0; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); dv2_null(u0); dv2_null(u1); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); dv2_new(u0); dv2_new(u1); if (ep2_curve_is_twist() == EP_MTYPE) { one ^= 1; zero ^= 1; } if (ep_curve_opt_b() == RLC_TWO) { /* C = z1^2. */ fp2_sqr(t0, q->z); /* B = y1^2. */ fp2_sqr(t1, q->y); /* t5 = B + C. */ fp2_add(t5, t0, t1); /* t3 = E = 3b'C = 3C * (1 - i). */ fp2_dbl(t3, t0); fp2_add(t0, t0, t3); fp_add(t2[0], t0[0], t0[1]); fp_sub(t2[1], t0[1], t0[0]); /* t0 = x1^2. */ fp2_sqr(t0, q->x); /* t4 = A = (x1 * y1)/2. */ fp2_mul(t4, q->x, q->y); fp_hlv(t4[0], t4[0]); fp_hlv(t4[1], t4[1]); /* t3 = F = 3E. */ fp2_dbl(t3, t2); fp2_add(t3, t3, t2); /* x3 = A * (B - F). */ fp2_sub(r->x, t1, t3); fp2_mul(r->x, r->x, t4); /* G = (B + F)/2. */ fp2_add(t3, t1, t3); fp_hlv(t3[0], t3[0]); fp_hlv(t3[1], t3[1]); /* y3 = G^2 - 3E^2. */ fp2_sqrn_low(u0, t2); fp2_addd_low(u1, u0, u0); fp2_addd_low(u1, u1, u0); fp2_sqrn_low(u0, t3); fp2_subc_low(u0, u0, u1); /* H = (Y + Z)^2 - B - C. */ fp2_add(t3, q->y, q->z); fp2_sqr(t3, t3); fp2_sub(t3, t3, t5); fp2_rdcn_low(r->y, u0); /* z3 = B * H. */ fp2_mul(r->z, t1, t3); /* l11 = E - B. */ fp2_sub(l[1][1], t2, t1); /* l10 = (3 * xp) * t0. */ fp_mul(l[one][zero][0], p->x, t0[0]); fp_mul(l[one][zero][1], p->x, t0[1]); /* l01 = F * (-yp). */ fp_mul(l[zero][zero][0], t3[0], p->y); fp_mul(l[zero][zero][1], t3[1], p->y); } else { /* A = x1^2. */ fp2_sqr(t0, q->x); /* B = y1^2. */ fp2_sqr(t1, q->y); /* C = z1^2. */ fp2_sqr(t2, q->z); /* D = 3bC, for general b. */ fp2_dbl(t3, t2); fp2_add(t3, t3, t2); ep2_curve_get_b(t4); fp2_mul(t3, t3, t4); /* E = (x1 + y1)^2 - A - B. */ fp2_add(t4, q->x, q->y); fp2_sqr(t4, t4); fp2_sub(t4, t4, t0); fp2_sub(t4, t4, t1); /* F = (y1 + z1)^2 - B - C. */ fp2_add(t5, q->y, q->z); fp2_sqr(t5, t5); fp2_sub(t5, t5, t1); fp2_sub(t5, t5, t2); /* G = 3D. */ fp2_dbl(t6, t3); fp2_add(t6, t6, t3); /* x3 = E * (B - G). */ fp2_sub(r->x, t1, t6); fp2_mul(r->x, r->x, t4); /* y3 = (B + G)^2 -12D^2. */ fp2_add(t6, t6, t1); fp2_sqr(t6, t6); fp2_sqr(t2, t3); fp2_dbl(r->y, t2); fp2_dbl(t2, r->y); fp2_dbl(r->y, t2); fp2_add(r->y, r->y, t2); fp2_sub(r->y, t6, r->y); /* z3 = 4B * F. */ fp2_dbl(r->z, t1); fp2_dbl(r->z, r->z); fp2_mul(r->z, r->z, t5); /* l00 = D - B. */ fp2_sub(l[one][one], t3, t1); /* l10 = (3 * xp) * A. */ fp_mul(l[one][zero][0], p->x, t0[0]); fp_mul(l[one][zero][1], p->x, t0[1]); /* l01 = F * (-yp). */ fp_mul(l[zero][zero][0], t5[0], p->y); fp_mul(l[zero][zero][1], t5[1], p->y); } r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_free(t2); fp2_free(t3); fp2_free(t4); fp2_free(t5); fp2_free(t6); dv2_free(u0); dv2_free(u1); } }