void ep2_rhs(fp2_t rhs, ep2_t p) { fp2_t t0; fp2_t t1; fp2_null(t0); fp2_null(t1); TRY { fp2_new(t0); fp2_new(t1); /* t0 = x1^2. */ fp2_sqr(t0, p->x); /* t1 = x1^3. */ fp2_mul(t1, t0, p->x); ep2_curve_get_a(t0); fp2_mul(t0, p->x, t0); fp2_add(t1, t1, t0); ep2_curve_get_b(t0); fp2_add(t1, t1, t0); fp2_copy(rhs, t1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); } }
/** * 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); ep2_curve_get_a(t2); fp2_add(t1, t1, t2); /* 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 fp6_inv(fp6_t c, fp6_t a) { fp2_t v0; fp2_t v1; fp2_t v2; fp2_t t0; fp2_null(v0); fp2_null(v1); fp2_null(v2); fp2_null(t0); TRY { fp2_new(v0); fp2_new(v1); fp2_new(v2); fp2_new(t0); /* v0 = a_0^2 - E * a_1 * a_2. */ fp2_sqr(t0, a[0]); fp2_mul(v0, a[1], a[2]); fp2_mul_nor(v2, v0); fp2_sub(v0, t0, v2); /* v1 = E * a_2^2 - a_0 * a_1. */ fp2_sqr(t0, a[2]); fp2_mul_nor(v2, t0); fp2_mul(v1, a[0], a[1]); fp2_sub(v1, v2, v1); /* v2 = a_1^2 - a_0 * a_2. */ fp2_sqr(t0, a[1]); fp2_mul(v2, a[0], a[2]); fp2_sub(v2, t0, v2); fp2_mul(t0, a[1], v2); fp2_mul_nor(c[1], t0); fp2_mul(c[0], a[0], v0); fp2_mul(t0, a[2], v1); fp2_mul_nor(c[2], t0); fp2_add(t0, c[0], c[1]); fp2_add(t0, t0, c[2]); fp2_inv(t0, t0); fp2_mul(c[0], v0, t0); fp2_mul(c[1], v1, t0); fp2_mul(c[2], v2, t0); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(v0); fp2_free(v1); fp2_free(v2); fp2_free(t0); } }
void pp_map_weilp_k2(fp2_t r, ep_t p, ep_t q) { ep_t _p[1], _q[1], t0[1], t1[1]; fp2_t r0, r1; bn_t n; ep_null(_p[0]); ep_null(_q[0]); ep_null(t0[0]); ep_null(t1[0]); fp2_null(r0); fp2_null(r1); bn_null(n); TRY { ep_new(_p[0]); ep_new(_q[0]); ep_new(t0[0]); ep_new(t1[0]); fp2_new(r0); fp2_new(r1); bn_new(n); ep_norm(_p[0], p); ep_norm(_q[0], q); ep_curve_get_ord(n); /* Since p has order n, we do not have to perform last iteration. */ bn_sub_dig(n, n, 1); fp2_set_dig(r0, 1); fp2_set_dig(r1, 1); if (!ep_is_infty(_p[0]) && !ep_is_infty(_q[0])) { pp_mil_lit_k2(r0, t0, _p, _q, 1, n); pp_mil_k2(r1, t1, _q, _p, 1, n); fp2_inv(r1, r1); fp2_mul(r0, r0, r1); fp2_inv(r1, r0); fp2_inv_uni(r0, r0); } fp2_mul(r, r0, r1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { ep_free(_p[0]); ep_free(_q[0]); ep_free(t0[0]); ep_free(t1[0]); fp2_free(r0); fp2_free(r1); bn_free(n); } }
void ep2_curve_clean(void) { ctx_t *ctx = core_get(); #ifdef EP_PRECO for (int i = 0; i < EP_TABLE; i++) { fp2_free(ctx->ep2_pre[i].x); fp2_free(ctx->ep2_pre[i].y); fp2_free(ctx->ep2_pre[i].z); } #endif bn_clean(&(ctx->ep2_r)); bn_clean(&(ctx->ep2_h)); }
void fp2_norm_low(fp2_t c, fp2_t a) { fp2_t t; bn_t b; fp2_null(t); bn_null(b); TRY { fp2_new(t); bn_new(b); #if FP_PRIME == 158 fp_dbl(t[0], a[0]); fp_dbl(t[0], t[0]); fp_sub(t[0], t[0], a[1]); fp_dbl(t[1], a[1]); fp_dbl(t[1], t[1]); fp_add(c[1], a[0], t[1]); fp_copy(c[0], t[0]); #elif defined(FP_QNRES) /* If p = 3 mod 8, (1 + i) is a QNR/CNR. */ fp_neg(t[0], a[1]); fp_add(c[1], a[0], a[1]); fp_add(c[0], t[0], a[0]); #else switch (fp_prime_get_mod8()) { case 3: /* If p = 3 mod 8, (1 + u) is a QNR/CNR. */ fp_neg(t[0], a[1]); fp_add(c[1], a[0], a[1]); fp_add(c[0], t[0], a[0]); break; case 5: /* If p = 5 mod 8, (u) is a QNR/CNR. */ fp2_mul_art(c, a); break; case 7: /* If p = 7 mod 8, we choose (2^(lg_4(b-1)) + u) as QNR/CNR. */ fp2_mul_art(t, a); fp2_dbl(c, a); fp_prime_back(b, ep_curve_get_b()); for (int i = 1; i < bn_bits(b) / 2; i++) { fp2_dbl(c, c); } fp2_add(c, c, t); break; default: THROW(ERR_NO_VALID); break; } #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t); bn_free(b); } }
void ep2_map(ep2_t p, uint8_t *msg, int len) { bn_t x; fp2_t t0; uint8_t digest[MD_LEN]; bn_null(x); fp2_null(t0); TRY { bn_new(x); fp2_new(t0); md_map(digest, msg, len); bn_read_bin(x, digest, MIN(FP_BYTES, MD_LEN)); fp_prime_conv(p->x[0], x); fp_zero(p->x[1]); fp_set_dig(p->z[0], 1); fp_zero(p->z[1]); while (1) { ep2_rhs(t0, p); if (fp2_srt(p->y, t0)) { p->norm = 1; break; } fp_add_dig(p->x[0], p->x[0], 1); } switch (ep_param_get()) { case BN_P158: case BN_P254: case BN_P256: case BN_P638: ep2_mul_cof_bn(p, p); break; case B12_P638: ep2_mul_cof_b12(p, p); break; default: /* Now, multiply by cofactor to get the correct group. */ ep2_curve_get_cof(x); if (bn_bits(x) < BN_DIGIT) { ep2_mul_dig(p, p, x->dp[0]); } else { ep2_mul(p, p, x); } break; } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(x); fp2_free(t0); } }
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 fp2_write_bin(uint8_t *bin, int len, fp2_t a, int pack) { fp2_t t; fp2_null(t); TRY { fp2_new(t); if (pack && fp2_test_uni(a)) { if (len != FP_BYTES + 1) { THROW(ERR_NO_BUFFER); } else { fp2_pck(t, t); fp_write_bin(bin, FP_BYTES, a[0]); bin[FP_BYTES] = fp_get_bit(a[1], 0); } } else { if (len != 2 * FP_BYTES) { THROW(ERR_NO_BUFFER); } else { fp_write_bin(bin, FP_BYTES, a[0]); fp_write_bin(bin + FP_BYTES, FP_BYTES, a[1]); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t); } }
/** * Doubles a point represented in affine coordinates on an ordinary prime * elliptic curve. * * @param[out] r - the result. * @param[in] p - the point to double. */ static void ep2_dbl_projc_imp(ep2_t r, ep2_t p) { fp2_t t0, t1, t2, t3; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_sqr(t0, p->x); fp2_add(t2, t0, t0); fp2_add(t0, t2, t0); fp2_sqr(t3, p->y); fp2_mul(t1, t3, p->x); fp2_add(t1, t1, t1); fp2_add(t1, t1, t1); fp2_sqr(r->x, t0); fp2_add(t2, t1, t1); fp2_sub(r->x, r->x, t2); fp2_mul(r->z, p->z, p->y); fp2_add(r->z, r->z, r->z); fp2_add(t3, t3, t3); fp2_sqr(t3, t3); fp2_add(t3, t3, t3); fp2_sub(t1, t1, r->x); fp2_mul(r->y, t0, t1); fp2_sub(r->y, r->y, t3); r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_free(t2); fp2_free(t3); } }
/** * Compute the Miller loop for pairings of type G_1 x G_2 over the bits of a * given parameter. * * @param[out] r - the result. * @param[out] t - the resulting point. * @param[in] p - the first pairing argument in affine coordinates. * @param[in] q - the second pairing argument in affine coordinates. * @param[in] a - the loop parameter. */ static void pp_mil_lit_k2(fp2_t r, ep_t *t, ep_t *p, ep_t *q, int m, bn_t a) { fp2_t l, _l; ep_t _q[m]; int i, j; fp2_null(_l); ep_null(_q); TRY { fp2_new(_l); for (j = 0; j < m; j++) { ep_null(_q[j]); ep_new(_q[j]); ep_copy(t[j], p[j]); ep_neg(_q[j], q[j]); } for (i = bn_bits(a) - 2; i >= 0; i--) { fp2_sqr(r, r); for (j = 0; j < m; j++) { pp_dbl_k2(l, t[j], t[j], _q[j]); fp_copy(_l[0], l[1]); fp_copy(_l[1], l[0]); fp2_mul(r, r, _l); if (bn_get_bit(a, i)) { pp_add_k2(l, t[j], p[j], q[j]); fp_copy(_l[0], l[1]); fp_copy(_l[1], l[0]); fp2_mul(r, r, _l); } } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(_l); fp2_free(m); ep_free(_q); } }
void fp2_inv_sim(fp2_t *c, fp2_t *a, int n) { int i; fp2_t u, t[n]; for (i = 0; i < n; i++) { fp2_null(t[i]); } fp2_null(u); TRY { for (i = 0; i < n; i++) { fp2_new(t[i]); } fp2_new(u); fp2_copy(c[0], a[0]); fp2_copy(t[0], a[0]); for (i = 1; i < n; i++) { fp2_copy(t[i], a[i]); fp2_mul(c[i], c[i - 1], t[i]); } fp2_inv(u, c[n - 1]); for (i = n - 1; i > 0; i--) { fp2_mul(c[i], c[i - 1], u); fp2_mul(u, u, t[i]); } fp2_copy(c[0], u); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { for (i = 0; i < n; i++) { fp2_free(t[i]); } fp2_free(u); } }
void fp2_mul_nor_basic(fp2_t c, fp2_t a) { fp2_t t; bn_t b; fp2_null(t); bn_null(b); TRY { fp2_new(t); bn_new(b); #ifdef FP_QNRES /* If p = 3 mod 8, (1 + i) is a QNR/CNR. */ fp_neg(t[0], a[1]); fp_add(c[1], a[0], a[1]); fp_add(c[0], t[0], a[0]); #else switch (fp_prime_get_mod8()) { case 3: /* If p = 3 mod 8, (1 + u) is a QNR/CNR. */ fp_neg(t[0], a[1]); fp_add(c[1], a[0], a[1]); fp_add(c[0], t[0], a[0]); break; case 1: case 5: /* If p = 5 mod 8, (u) is a QNR/CNR. */ fp2_mul_art(c, a); break; case 7: /* If p = 7 mod 8, we choose (4 + u) is a QNR/CNR. */ fp2_mul_art(t, a); fp2_dbl(c, a); fp2_dbl(c, c); fp2_add(c, c, t); break; default: THROW(ERR_NO_VALID); } #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t); bn_free(b); } }
static void fp4_mul_unr(dv2_t e, dv2_t f, fp2_t a, fp2_t b, fp2_t c, fp2_t d) { fp2_t t0, t1; dv2_t u0, u1; fp2_null(t0); fp2_null(t1); dv2_null(u0); dv2_null(u1); TRY { fp2_new(t0); fp2_new(t1); dv2_new(u0); dv2_new(u1); #ifdef FP_SPACE fp2_mulc_low(u0, a, c); fp2_mulc_low(u1, b, d); fp2_addn_low(t0, c, d); fp2_addn_low(t1, a, b); #else fp2_muln_low(u0, a, c); fp2_muln_low(u1, b, d); fp2_addm_low(t0, c, d); fp2_addm_low(t1, a, b); #endif fp2_muln_low(f, t1, t0); fp2_subc_low(f, f, u0); fp2_subc_low(f, f, u1); fp2_norh_low(e, u1); fp2_addc_low(e, e, u0); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); dv2_free(t1); dv2_free(u0); dv2_free(u1); } }
void fp2_mul_frb(fp2_t c, fp2_t a, int i, int j) { ctx_t *ctx = core_get(); if (i == 2) { fp_mul(c[0], a[0], ctx->fp2_p2[j - 1]); fp_mul(c[1], a[1], ctx->fp2_p2[j - 1]); } else { #if ALLOC == AUTO if (i == 1) { fp2_mul(c, a, ctx->fp2_p[j - 1]); } else { fp2_mul(c, a, ctx->fp2_p3[j - 1]); } #else fp2_t t; fp2_null(t); TRY { fp2_new(t); if (i == 1) { fp_copy(t[0], ctx->fp2_p[j - 1][0]); fp_copy(t[1], ctx->fp2_p[j - 1][1]); } else { fp_copy(t[0], ctx->fp2_p3[j - 1][0]); fp_copy(t[1], ctx->fp2_p3[j - 1][1]); } fp2_mul(c, a, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t); } #endif } }
static void fp4_sqr_unr(dv2_t c, dv2_t d, fp2_t a, fp2_t b) { fp2_t t; dv2_t u0, u1; fp2_null(t); dv2_null(u0); dv2_null(u1); TRY { fp2_new(t); dv2_new(u0); dv2_new(u1); /* t0 = a^2. */ fp2_sqrn_low(u0, a); /* t1 = b^2. */ fp2_sqrn_low(u1, b); fp2_addm_low(t, a, b); /* c = a^2 + b^2 * E. */ fp2_norh_low(c, u1); fp2_addc_low(c, c, u0); /* d = (a + b)^2 - a^2 - b^2 = 2 * a * b. */ fp2_addc_low(u1, u1, u0); fp2_sqrn_low(d, t); fp2_subc_low(d, d, u1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t); dv2_free(u0); dv2_free(u1); } }
void fp12_sqr_lazyr(fp12_t c, fp12_t a) { fp2_t t0, t1, t2, t3; dv2_t u0, u1, u2, u3, u4, u5, u6, u7, u8, u9; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); dv2_null(u0); dv2_null(u1); dv2_null(u2); dv2_null(u3); dv2_null(u4); dv2_null(u5); dv2_null(u6); dv2_null(u7); dv2_null(u8); dv2_null(u9); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); dv2_new(u0); dv2_new(u1); dv2_new(u2); dv2_new(u3); dv2_new(u4); dv2_new(u5); dv2_new(u6); dv2_new(u7); dv2_new(u8); dv2_new(u9); /* a0 = (a00, a11). */ /* a1 = (a10, a02). */ /* a2 = (a01, a12). */ /* (t0,t1) = a0^2 */ fp4_sqr_unr(u0, u1, a[0][0], a[1][1]); /* (t2,t3) = 2 * a1 * a2 */ fp4_mul_unr(u2, u3, a[1][0], a[0][2], a[0][1], a[1][2]); fp2_addc_low(u2, u2, u2); fp2_addc_low(u3, u3, u3); /* (t4,t5) = a2^2. */ fp4_sqr_unr(u4, u5, a[0][1], a[1][2]); /* c2 = a0 + a2. */ fp2_addm_low(t2, a[0][0], a[0][1]); fp2_addm_low(t3, a[1][1], a[1][2]); /* (t6,t7) = (a0 + a2 + a1)^2. */ fp2_addm_low(t0, t2, a[1][0]); fp2_addm_low(t1, t3, a[0][2]); fp4_sqr_unr(u6, u7, t0, t1); /* c2 = (a0 + a2 - a1)^2. */ fp2_subm_low(t2, t2, a[1][0]); fp2_subm_low(t3, t3, a[0][2]); fp4_sqr_unr(u8, u9, t2, t3); /* c2 = (c2 + (t6,t7))/2. */ #ifdef FP_SPACE fp2_addd_low(u8, u8, u6); fp2_addd_low(u9, u9, u7); #else fp2_addc_low(u8, u8, u6); fp2_addc_low(u9, u9, u7); #endif fp_hlvd_low(u8[0], u8[0]); fp_hlvd_low(u8[1], u8[1]); fp_hlvd_low(u9[0], u9[0]); fp_hlvd_low(u9[1], u9[1]); /* (t6,t7) = (t6,t7) - c2 - (t2,t3). */ fp2_subc_low(u6, u6, u8); fp2_subc_low(u7, u7, u9); fp2_subc_low(u6, u6, u2); fp2_subc_low(u7, u7, u3); /* c2 = c2 - (t0,t1) - (t4,t5). */ fp2_subc_low(u8, u8, u0); fp2_subc_low(u9, u9, u1); fp2_subc_low(u8, u8, u4); fp2_subc_low(u9, u9, u5); fp2_rdcn_low(c[0][1], u8); fp2_rdcn_low(c[1][2], u9); /* c1 = (t6,t7) + (t4,t5) * E. */ fp2_nord_low(u9, u5); fp2_addc_low(u6, u6, u9); fp2_addc_low(u7, u7, u4); fp2_rdcn_low(c[1][0], u6); fp2_rdcn_low(c[0][2], u7); /* c0 = (t0,t1) + (t2,t3) * E. */ fp2_nord_low(u9, u3); fp2_addc_low(u0, u0, u9); fp2_addc_low(u1, u1, u2); fp2_rdcn_low(c[0][0], u0); fp2_rdcn_low(c[1][1], u1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_free(t2); fp2_free(t3); dv2_free(u0); dv2_free(u1); dv2_free(u2); dv2_free(u3); dv2_free(u4); dv2_free(u5); dv2_free(u6); dv2_free(u7); dv2_free(u8); dv2_free(u9); } }
void fp12_sqr_pck_lazyr(fp12_t c, fp12_t a) { fp2_t t0, t1; dv2_t u0, u1, u2, u3, u4; fp2_null(t0); fp2_null(t1); dv2_null(u0); dv2_null(u1); dv2_null(u2); dv2_null(u3); dv2_null(u4); TRY { fp2_new(t0); fp2_new(t1); dv2_new(u0); dv2_new(u1); dv2_new(u2); dv2_new(u3); dv2_new(u4); fp2_sqrn_low(u0, a[0][1]); fp2_sqrn_low(u1, a[1][2]); fp2_norh_low(u4, u1); fp2_addm_low(t0, a[0][1], a[1][2]); fp2_sqrn_low(u2, t0); fp2_addd_low(u3, u0, u1); fp2_subc_low(u3, u2, u3); fp2_rdcn_low(t0, u3); fp2_sqrn_low(u2, a[1][0]); fp2_addm_low(t1, a[1][0], a[0][2]); fp2_sqrn_low(u3, t1); fp2_norm_low(t1, t0); fp2_addm_low(t0, t1, a[1][0]); fp2_dblm_low(t0, t0); fp2_addm_low(c[1][0], t0, t1); fp2_sqrn_low(u1, a[0][2]); fp2_addc_low(u4, u0, u4); fp2_rdcn_low(t0, u4); fp2_subm_low(t1, t0, a[0][2]); fp2_dblm_low(t1, t1); fp2_norh_low(u4, u1); fp2_addm_low(c[0][2], t1, t0); fp2_addc_low(u4, u2, u4); fp2_rdcn_low(t0, u4); fp2_subm_low(t1, t0, a[0][1]); fp2_dblm_low(t1, t1); fp2_addd_low(u0, u1, u2); fp2_addm_low(c[0][1], t1, t0); fp2_subc_low(u3, u3, u0); fp2_rdcn_low(t0, u3); fp2_addm_low(t1, t0, a[1][2]); fp2_dblm_low(t1, t1); fp2_addm_low(c[1][2], t1, t0); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); dv2_free(u0); dv2_free(u1); dv2_free(u2); dv2_free(u3); dv2_free(u4); } }
/** * Doubles a point represented in affine coordinates on an ordinary prime * elliptic curve. * * @param[out] r - the result. * @param[in] p - the point to double. */ static void ep2_dbl_projc_imp(ep2_t r, ep2_t p) { fp2_t t0, t1, t2, t3, t4, t5; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); TRY { if (ep_curve_opt_a() == OPT_ZERO) { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_sqr(t0, p->x); fp2_add(t2, t0, t0); fp2_add(t0, t2, t0); fp2_sqr(t3, p->y); fp2_mul(t1, t3, p->x); fp2_add(t1, t1, t1); fp2_add(t1, t1, t1); fp2_sqr(r->x, t0); fp2_add(t2, t1, t1); fp2_sub(r->x, r->x, t2); fp2_mul(r->z, p->z, p->y); fp2_add(r->z, r->z, r->z); fp2_add(t3, t3, t3); fp2_sqr(t3, t3); fp2_add(t3, t3, t3); fp2_sub(t1, t1, r->x); fp2_mul(r->y, t0, t1); fp2_sub(r->y, r->y, t3); } else { /* dbl-2007-bl formulas: 1M + 8S + 1*a + 10add + 1*8 + 2*2 + 1*3 */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl */ /* t0 = x1^2, t1 = y1^2, t2 = y1^4. */ fp2_sqr(t0, p->x); fp2_sqr(t1, p->y); fp2_sqr(t2, t1); if (!p->norm) { /* t3 = z1^2. */ fp2_sqr(t3, p->z); if (ep_curve_get_a() == OPT_ZERO) { /* z3 = 2 * y1 * z1. */ fp2_mul(r->z, p->y, p->z); fp2_dbl(r->z, r->z); } else { /* z3 = (y1 + z1)^2 - y1^2 - z1^2. */ fp2_add(r->z, p->y, p->z); fp2_sqr(r->z, r->z); fp2_sub(r->z, r->z, t1); fp2_sub(r->z, r->z, t3); } } else { /* z3 = 2 * y1. */ fp2_dbl(r->z, p->y); } /* t4 = S = 2*((x1 + y1^2)^2 - x1^2 - y1^4). */ fp2_add(t4, p->x, t1); fp2_sqr(t4, t4); fp2_sub(t4, t4, t0); fp2_sub(t4, t4, t2); fp2_dbl(t4, t4); /* t5 = M = 3 * x1^2 + a * z1^4. */ fp2_dbl(t5, t0); fp2_add(t5, t5, t0); ep2_curve_get_a(t0); if (!p->norm) { fp2_sqr(t3, t3); fp2_mul(t1, t0, t3); fp2_add(t5, t5, t1); } else { fp2_add(t5, t5, t0); } /* x3 = T = M^2 - 2 * S. */ fp2_sqr(r->x, t5); fp2_dbl(t1, t4); fp2_sub(r->x, r->x, t1); /* y3 = M * (S - T) - 8 * y1^4. */ fp2_dbl(t2, t2); fp2_dbl(t2, t2); fp2_dbl(t2, t2); fp2_sub(t4, t4, r->x); fp2_mul(t5, t5, t4); fp2_sub(r->y, t5, t2); } r->norm = 0; 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); } }
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); } }
void pp_map_sim_weilp_k2(fp2_t r, ep_t *p, ep_t *q, int m) { ep_t _p[m], _q[m], t0[m], t1[m]; fp2_t r0, r1; bn_t n; int i, j; fp2_null(r0); fp2_null(r1); bn_null(r); TRY { fp2_new(r0); fp2_new(r1); bn_new(n); for (i = 0; i < m; i++) { ep_null(_p[i]); ep_null(_q[i]); ep_null(t0[i]); ep_null(t1[i]); ep_new(_p[i]); ep_new(_q[i]); ep_new(t0[i]); ep_new(t1[i]); } j = 0; for (i = 0; i < m; i++) { if (!ep_is_infty(p[i]) && !ep_is_infty(q[i])) { ep_norm(_p[j], p[i]); ep_norm(_q[j++], q[i]); } } ep_curve_get_ord(n); bn_sub_dig(n, n, 1); fp2_set_dig(r0, 1); fp2_set_dig(r1, 1); if (j > 0) { pp_mil_lit_k2(r0, t0, _p, _q, j, n); pp_mil_k2(r1, t1, _q, _p, j, n); fp2_inv(r1, r1); fp2_mul(r0, r0, r1); fp2_inv(r1, r0); fp2_inv_uni(r0, r0); } fp2_mul(r, r0, r1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(r0); fp2_free(r1); bn_free(n); for (i = 0; i < m; i++) { ep_free(_p[i]); ep_free(_q[i]); ep_free(t0[i]); ep_free(t1[i]); } } }
/** * Adds two points represented in affine coordinates on an ordinary prime * elliptic curve. * * @param r - the result. * @param s - the resulting slope. * @param p - the first point to add. * @param q - the second point to add. */ static void ep2_add_basic_imp(ep2_t r, fp2_t s, ep2_t p, ep2_t q) { 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 = x2 - x1. */ fp2_sub(t0, q->x, p->x); /* t1 = y2 - y1. */ fp2_sub(t1, q->y, p->y); /* If t0 is zero. */ if (fp2_is_zero(t0)) { if (fp2_is_zero(t1)) { /* If t1 is zero, q = p, should have doubled. */ ep2_dbl_basic(r, p); } else { /* If t1 is not zero and t0 is zero, q = -p and r = infty. */ ep2_set_infty(r); } } else { /* t2 = 1/(x2 - x1). */ fp2_inv(t2, t0); /* t2 = lambda = (y2 - y1)/(x2 - x1). */ fp2_mul(t2, t1, t2); /* x3 = lambda^2 - x2 - x1. */ fp2_sqr(t1, t2); fp2_sub(t0, t1, p->x); fp2_sub(t0, t0, q->x); /* y3 = lambda * (x1 - x3) - y1. */ fp2_sub(t1, p->x, t0); fp2_mul(t1, t2, t1); fp2_sub(r->y, t1, p->y); fp2_copy(r->x, t0); fp2_copy(r->z, p->z); if (s != NULL) { fp2_copy(s, t2); } r->norm = 1; } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp2_free(t0); fp2_free(t1); fp2_free(t2); } }
/** * Adds two points represented in projective coordinates on an ordinary prime * elliptic curve. * * @param r - the result. * @param p - the first point to add. * @param q - the second point to add. */ static void ep2_add_projc_imp(ep2_t r, ep2_t p, ep2_t q) { #if defined(EP_MIXED) && defined(STRIP) ep2_add_projc_mix(r, p, q); #else /* General addition. */ fp2_t t0, t1, t2, t3, t4, t5, t6; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); if (q->norm) { ep2_add_projc_mix(r, p, q); } else { /* t0 = z1^2. */ fp2_sqr(t0, p->z); /* t1 = z2^2. */ fp2_sqr(t1, q->z); /* t2 = U1 = x1 * z2^2. */ fp2_mul(t2, p->x, t1); /* t3 = U2 = x2 * z1^2. */ fp2_mul(t3, q->x, t0); /* t6 = z1^2 + z2^2. */ fp2_add(t6, t0, t1); /* t0 = S2 = y2 * z1^3. */ fp2_mul(t0, t0, p->z); fp2_mul(t0, t0, q->y); /* t1 = S1 = y1 * z2^3. */ fp2_mul(t1, t1, q->z); fp2_mul(t1, t1, p->y); /* t3 = H = U2 - U1. */ fp2_sub(t3, t3, t2); /* t0 = R = 2 * (S2 - S1). */ fp2_sub(t0, t0, t1); fp2_dbl(t0, t0); /* If E is zero. */ if (fp2_is_zero(t3)) { if (fp2_is_zero(t0)) { /* If I is zero, p = q, should have doubled. */ ep2_dbl_projc(r, p); } else { /* If I is not zero, q = -p, r = infinity. */ ep2_set_infty(r); } } else { /* t4 = I = (2*H)^2. */ fp2_dbl(t4, t3); fp2_sqr(t4, t4); /* t5 = J = H * I. */ fp2_mul(t5, t3, t4); /* t4 = V = U1 * I. */ fp2_mul(t4, t2, t4); /* x3 = R^2 - J - 2 * V. */ fp2_sqr(r->x, t0); fp2_sub(r->x, r->x, t5); fp2_dbl(t2, t4); fp2_sub(r->x, r->x, t2); /* y3 = R * (V - x3) - 2 * S1 * J. */ fp2_sub(t4, t4, r->x); fp2_mul(t4, t4, t0); fp2_mul(t1, t1, t5); fp2_dbl(t1, t1); fp2_sub(r->y, t4, t1); /* z3 = ((z1 + z2)^2 - z1^2 - z2^2) * H. */ fp2_add(r->z, p->z, q->z); fp2_sqr(r->z, r->z); fp2_sub(r->z, r->z, t6); fp2_mul(r->z, r->z, t3); } } 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); } #endif }
/** * Adds a point represented in affine coordinates to a point represented in * projective coordinates. * * @param r - the result. * @param s - the slope. * @param p - the affine point. * @param q - the projective point. */ static void ep2_add_projc_mix(ep2_t r, ep2_t p, ep2_t q) { fp2_t t0, t1, t2, t3, t4, t5, t6; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); if (!p->norm) { /* t0 = z1^2. */ fp2_sqr(t0, p->z); /* t3 = U2 = x2 * z1^2. */ fp2_mul(t3, q->x, t0); /* t1 = S2 = y2 * z1^3. */ fp2_mul(t1, t0, p->z); fp2_mul(t1, t1, q->y); /* t3 = H = U2 - x1. */ fp2_sub(t3, t3, p->x); /* t1 = R = 2 * (S2 - y1). */ fp2_sub(t1, t1, p->y); } else { /* H = x2 - x1. */ fp2_sub(t3, q->x, p->x); /* t1 = R = 2 * (y2 - y1). */ fp2_sub(t1, q->y, p->y); } /* t2 = HH = H^2. */ fp2_sqr(t2, t3); /* If E is zero. */ if (fp2_is_zero(t3)) { if (fp2_is_zero(t1)) { /* If I is zero, p = q, should have doubled. */ ep2_dbl_projc(r, p); } else { /* If I is not zero, q = -p, r = infinity. */ ep2_set_infty(r); } } else { /* t5 = J = H * HH. */ fp2_mul(t5, t3, t2); /* t4 = V = x1 * HH. */ fp2_mul(t4, p->x, t2); /* x3 = R^2 - J - 2 * V. */ fp2_sqr(r->x, t1); fp2_sub(r->x, r->x, t5); fp2_dbl(t6, t4); fp2_sub(r->x, r->x, t6); /* y3 = R * (V - x3) - Y1 * J. */ fp2_sub(t4, t4, r->x); fp2_mul(t4, t4, t1); fp2_mul(t1, p->y, t5); fp2_sub(r->y, t4, t1); if (!p->norm) { /* z3 = z1 * H. */ fp2_mul(r->z, p->z, t3); } else { /* z3 = H. */ fp2_copy(r->z, t3); } } 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); } }
void fp12_sqr_pck_basic(fp12_t c, fp12_t a) { fp2_t t0, t1, t2, t3, t4, t5, t6; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); fp2_sqr(t0, a[0][1]); fp2_sqr(t1, a[1][2]); fp2_add(t5, a[0][1], a[1][2]); fp2_sqr(t2, t5); fp2_add(t3, t0, t1); fp2_sub(t5, t2, t3); fp2_add(t6, a[1][0], a[0][2]); fp2_sqr(t3, t6); fp2_sqr(t2, a[1][0]); fp2_mul_nor(t6, t5); fp2_add(t5, t6, a[1][0]); fp2_dbl(t5, t5); fp2_add(c[1][0], t5, t6); fp2_mul_nor(t4, t1); fp2_add(t5, t0, t4); fp2_sub(t6, t5, a[0][2]); fp2_sqr(t1, a[0][2]); fp2_dbl(t6, t6); fp2_add(c[0][2], t6, t5); fp2_mul_nor(t4, t1); fp2_add(t5, t2, t4); fp2_sub(t6, t5, a[0][1]); fp2_dbl(t6, t6); fp2_add(c[0][1], t6, t5); fp2_add(t0, t2, t1); fp2_sub(t5, t3, t0); fp2_add(t6, t5, a[1][2]); fp2_dbl(t6, t6); fp2_add(c[1][2], t5, t6); } 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); } }
inline static void fp6_mul_dxs_unr_lazyr(dv6_t c, fp6_t a, fp6_t b) { dv2_t u0, u1, u2, u3; fp2_t t0, t1; dv2_null(u0); dv2_null(u1); dv2_null(u2); dv2_null(u3); fp2_null(t0); fp2_null(t1); TRY { dv2_new(u0); dv2_new(u1); dv2_new(u2); dv2_new(u3); fp2_new(t0); fp2_new(t1); #ifdef RLC_FP_ROOM fp2_mulc_low(u0, a[0], b[0]); fp2_mulc_low(u1, a[1], b[1]); fp2_addn_low(t0, a[0], a[1]); fp2_addn_low(t1, b[0], b[1]); /* c_1 = (a_0 + a_1)(b_0 + b_1) - a_0b_0 - a_1b_1 */ fp2_muln_low(u2, t0, t1); fp2_subc_low(u2, u2, u0); fp2_subc_low(c[1], u2, u1); /* c_0 = a_0b_0 + E a_2b_1 */ fp2_mulc_low(u2, a[2], b[1]); fp2_norh_low(c[0], u2); fp2_addc_low(c[0], u0, c[0]); /* c_2 = a_0b_2 + a_1b_1 */ fp2_mulc_low(u2, a[2], b[0]); fp2_addc_low(c[2], u1, u2); #else fp2_muln_low(u0, a[0], b[0]); fp2_muln_low(u1, a[1], b[1]); fp2_addm_low(t0, a[0], a[1]); fp2_addm_low(t1, b[0], b[1]); /* c_1 = (a_0 + a_1)(b_0 + b_1) - a_0b_0 - a_1b_1 */ fp2_muln_low(u2, t0, t1); fp2_subc_low(u2, u2, u0); fp2_subc_low(c[1], u2, u1); /* c_0 = a_0b_0 + E a_2b_1 */ fp2_muln_low(u2, a[2], b[1]); fp2_nord_low(c[0], u2); fp2_addc_low(c[0], u0, c[0]); /* c_2 = a_0b_2 + a_1b_1 */ fp2_muln_low(u2, a[2], b[0]); fp2_addc_low(c[2], u1, u2); #endif #ifdef RLC_FP_ROOM #else #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv2_free(u0); dv2_free(u1); dv2_free(u2); dv2_free(u3); fp2_free(t0); fp2_free(t1); } }
void ep2_curve_set_twist(int type) { char str[2 * FP_BYTES + 1]; ctx_t *ctx = core_get(); ep2_t g; fp2_t a; fp2_t b; bn_t r; ep2_null(g); fp2_null(a); fp2_null(b); bn_null(r); ctx->ep2_is_twist = 0; if (type == EP_MTYPE || type == EP_DTYPE) { ctx->ep2_is_twist = type; } else { return; } TRY { ep2_new(g); fp2_new(a); fp2_new(b); bn_new(r); switch (ep_param_get()) { #if FP_PRIME == 158 case BN_P158: ASSIGN(BN_P158); break; #elif FP_PRIME == 254 case BN_P254: ASSIGN(BN_P254); break; #elif FP_PRIME == 256 case BN_P256: ASSIGN(BN_P256); break; #elif FP_PRIME == 638 case BN_P638: ASSIGN(BN_P638); break; case B12_P638: ASSIGN(B12_P638); break; #endif default: (void)str; THROW(ERR_NO_VALID); break; } fp2_zero(g->z); fp_set_dig(g->z[0], 1); g->norm = 1; ep2_copy(&(ctx->ep2_g), g); fp_copy(ctx->ep2_a[0], a[0]); fp_copy(ctx->ep2_a[1], a[1]); fp_copy(ctx->ep2_b[0], b[0]); fp_copy(ctx->ep2_b[1], b[1]); bn_copy(&(ctx->ep2_r), r); /* I don't have a better place for this. */ fp_prime_calc(); #if defined(EP_PRECO) ep2_mul_pre((ep2_t *)ep2_curve_get_tab(), &(ctx->ep2_g)); #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { ep2_free(g); fp2_free(a); fp2_free(b); bn_free(r); } }
/** * Computes the constantes required for evaluating Frobenius maps. */ static void fp2_calc() { bn_t e; fp2_t t0; fp2_t t1; ctx_t *ctx = core_get(); bn_null(e); fp2_null(t0); fp2_null(t1); TRY { bn_new(e); fp2_new(t0); fp2_new(t1); fp2_zero(t0); fp_set_dig(t0[0], 1); fp2_mul_nor(t0, t0); e->used = FP_DIGS; dv_copy(e->dp, fp_prime_get(), FP_DIGS); bn_sub_dig(e, e, 1); bn_div_dig(e, e, 6); fp2_exp(t0, t0, e); #if ALLOC == AUTO fp2_copy(ctx->fp2_p[0], t0); fp2_sqr(ctx->fp2_p[1], ctx->fp2_p[0]); fp2_mul(ctx->fp2_p[2], ctx->fp2_p[1], ctx->fp2_p[0]); fp2_sqr(ctx->fp2_p[3], ctx->fp2_p[1]); fp2_mul(ctx->fp2_p[4], ctx->fp2_p[3], ctx->fp2_p[0]); #else fp_copy(ctx->fp2_p[0][0], t0[0]); fp_copy(ctx->fp2_p[0][1], t0[1]); fp2_sqr(t1, t0); fp_copy(ctx->fp2_p[1][0], t1[0]); fp_copy(ctx->fp2_p[1][1], t1[1]); fp2_mul(t1, t1, t0); fp_copy(ctx->fp2_p[2][0], t1[0]); fp_copy(ctx->fp2_p[2][1], t1[1]); fp2_sqr(t1, t0); fp2_sqr(t1, t1); fp_copy(ctx->fp2_p[3][0], t1[0]); fp_copy(ctx->fp2_p[3][1], t1[1]); fp2_mul(t1, t1, t0); fp_copy(ctx->fp2_p[4][0], t1[0]); fp_copy(ctx->fp2_p[4][1], t1[1]); #endif fp2_frb(t1, t0, 1); fp2_mul(t0, t1, t0); fp_copy(ctx->fp2_p2[0], t0[0]); fp_sqr(ctx->fp2_p2[1], ctx->fp2_p2[0]); fp_mul(ctx->fp2_p2[2], ctx->fp2_p2[1], ctx->fp2_p2[0]); fp_sqr(ctx->fp2_p2[3], ctx->fp2_p2[1]); for (int i = 0; i < 5; i++) { fp_mul(ctx->fp2_p3[i][0], ctx->fp2_p2[i % 3], ctx->fp2_p[i][0]); fp_mul(ctx->fp2_p3[i][1], ctx->fp2_p2[i % 3], ctx->fp2_p[i][1]); } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(e); fp2_free(t0); fp2_free(t1); } }
/** * 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_sqr_cyc_basic(fp12_t c, fp12_t a) { fp2_t t0, t1, t2, t3, t4, t5, t6; fp2_null(t0); fp2_null(t1); fp2_null(t2); fp2_null(t3); fp2_null(t4); fp2_null(t5); fp2_null(t6); TRY { fp2_new(t0); fp2_new(t1); fp2_new(t2); fp2_new(t3); fp2_new(t4); fp2_new(t5); fp2_new(t6); /* Define z = sqrt(E) */ /* Now a is seen as (t0,t1) + (t2,t3) * w + (t4,t5) * w^2 */ /* (t0, t1) = (a00 + a11*z)^2. */ fp2_sqr(t2, a[0][0]); fp2_sqr(t3, a[1][1]); fp2_add(t1, a[0][0], a[1][1]); fp2_mul_nor(t0, t3); fp2_add(t0, t0, t2); fp2_sqr(t1, t1); fp2_sub(t1, t1, t2); fp2_sub(t1, t1, t3); fp2_sub(c[0][0], t0, a[0][0]); fp2_add(c[0][0], c[0][0], c[0][0]); fp2_add(c[0][0], t0, c[0][0]); fp2_add(c[1][1], t1, a[1][1]); fp2_add(c[1][1], c[1][1], c[1][1]); fp2_add(c[1][1], t1, c[1][1]); fp2_sqr(t0, a[0][1]); fp2_sqr(t1, a[1][2]); fp2_add(t5, a[0][1], a[1][2]); fp2_sqr(t2, t5); fp2_add(t3, t0, t1); fp2_sub(t5, t2, t3); fp2_add(t6, a[1][0], a[0][2]); fp2_sqr(t3, t6); fp2_sqr(t2, a[1][0]); fp2_mul_nor(t6, t5); fp2_add(t5, t6, a[1][0]); fp2_dbl(t5, t5); fp2_add(c[1][0], t5, t6); fp2_mul_nor(t4, t1); fp2_add(t5, t0, t4); fp2_sub(t6, t5, a[0][2]); fp2_sqr(t1, a[0][2]); fp2_dbl(t6, t6); fp2_add(c[0][2], t6, t5); fp2_mul_nor(t4, t1); fp2_add(t5, t2, t4); fp2_sub(t6, t5, a[0][1]); fp2_dbl(t6, t6); fp2_add(c[0][1], t6, t5); fp2_add(t0, t2, t1); fp2_sub(t5, t3, t0); fp2_add(t6, t5, a[1][2]); fp2_dbl(t6, t6); fp2_add(c[1][2], t5, t6); } 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); } }