void fp2_nord_low(dv2_t c, dv2_t a) { dv2_t t; bn_t b; dv2_null(t); bn_null(b); TRY { dv2_new(t); bn_new(b); #ifdef FP_QNRES /* If p = 3 mod 8, (1 + i) is a QNR/CNR. */ /* (a_0 + a_1 * i) * (1 + i) = (a_0 - a_1) + (a_0 + a_1) * u. */ dv_copy(t[0], a[1], 2 * FP_DIGS); fp_addc_low(c[1], a[0], a[1]); fp_subc_low(c[0], a[0], t[0]); #else switch (fp_prime_get_mod8()) { case 3: /* If p = 3 mod 8, (1 + u) is a QNR, u^2 = -1. */ /* (a_0 + a_1 * u) * (1 + u) = (a_0 - a_1) + (a_0 + a_1) * u. */ dv_copy(t[0], a[1], 2 * FP_DIGS); fp_addc_low(c[1], a[0], a[1]); fp_subc_low(c[0], a[0], t[0]); break; case 1: case 5: /* If p = 1,5 mod 8, (u) is a QNR. */ dv_copy(t[0], a[0], 2 * FP_DIGS); dv_zero(t[1], FP_DIGS); dv_copy(t[1] + FP_DIGS, fp_prime_get(), FP_DIGS); fp_subc_low(c[0], t[1], a[1]); for (int i = -1; i > fp_prime_get_qnr(); i--) { fp_subc_low(c[0], c[0], a[1]); } dv_copy(c[1], t[0], 2 * FP_DIGS); break; case 7: /* If p = 7 mod 8, (2 + u) is a QNR/CNR. */ fp2_addc_low(t, a, a); fp_subc_low(c[0], t[0], a[1]); fp_addc_low(c[1], t[1], a[0]); break; default: THROW(ERR_NO_VALID); break; } #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv2_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_mulm_low(fp2_t c, fp2_t a, fp2_t b) { align dv2_t t; dv2_null(t); TRY { dv2_new(t); fp2_muln_low(t, a, b); fp2_rdcn_low(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv2_free(t); } }
void fp2_sqrm_low(fp2_t c, fp2_t a) { align dv2_t t; dv2_null(t); TRY { dv2_new(t); fp2_sqrn_low(t, a); fp2_rdcn_low(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv2_free(t); } }
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 fp2_nord_low(dv2_t c, dv2_t a) { dv2_t t; bn_t b; dv2_null(t); bn_null(b); TRY { dv2_new(t); bn_new(b); #if FP_PRIME == 158 fp_addc_low(t[0], a[0], a[0]); fp_addc_low(t[0], t[0], t[0]); fp_subc_low(t[0], t[0], a[1]); fp_addc_low(t[1], a[1], a[1]); fp_addc_low(t[1], t[1], t[1]); fp_addc_low(c[1], a[0], t[1]); dv_copy(c[0], t[0], 2 * FP_DIGS); #elif defined(FP_QNRES) /* If p = 3 mod 8, (1 + i) is a QNR/CNR. */ /* (a_0 + a_1 * i) * (1 + i) = (a_0 - a_1) + (a_0 + a_1) * u. */ dv_copy(t[0], a[1], 2 * FP_DIGS); fp_addc_low(c[1], a[0], a[1]); fp_subc_low(c[0], a[0], t[0]); #else switch (fp_prime_get_mod8()) { case 3: /* If p = 3 mod 8, (1 + u) is a QNR, u^2 = -1. */ /* (a_0 + a_1 * u) * (1 + u) = (a_0 - a_1) + (a_0 + a_1) * u. */ dv_copy(t[0], a[1], 2 * FP_DIGS); fp_addc_low(c[1], a[0], a[1]); fp_subc_low(c[0], a[0], t[0]); break; case 5: /* If p = 5 mod 8, (u) is a QNR. */ dv_copy(t[0], a[0], 2 * FP_DIGS); dv_zero(t[1], FP_DIGS); dv_copy(t[1] + FP_DIGS, fp_prime_get(), FP_DIGS); fp_subc_low(c[0], t[1], a[1]); for (int i = -1; i > fp_prime_get_qnr(); i--) { fp_subc_low(c[0], c[0], a[1]); } dv_copy(c[1], t[0], 2 * FP_DIGS); break; case 7: /* If p = 7 mod 8, (2^lg_4(b-1) + u) is a QNR/CNR. */ /* (a_0 + a_1 * u)(2^lg_4(b-1) + u) = * (2^lg_4(b-1)a_0 - a_1) + (a_0 + 2^lg_4(b-1)a_1 * u. */ fp2_addc_low(t, a, a); fp_prime_back(b, ep_curve_get_b()); for (int i = 1; i < bn_bits(b) / 2; i++) { fp2_addc_low(t, t, t); } fp_subc_low(c[0], t[0], a[1]); fp_addc_low(c[1], t[1], a[0]); break; default: THROW(ERR_NO_VALID); break; } #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv2_free(t); bn_free(b); } }
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 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 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); } }
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); } }