void eb_hlv(eb_t r, const eb_t p) { fb_t l, t; fb_null(l); fb_null(t); TRY { fb_new(l); fb_new(t); /* Solve l^2 + l = u + a. */ switch (eb_curve_opt_a()) { case RLC_ZERO: fb_copy(t, p->x); break; case RLC_ONE: fb_add_dig(t, p->x, (dig_t)1); break; case RLC_TINY: fb_add_dig(t, p->x, eb_curve_get_a()[0]); break; default: fb_add(t, p->x, eb_curve_get_a()); break; } fb_slv(l, t); if (p->norm == 1) { /* Compute t = v + u * lambda. */ fb_mul(t, l, p->x); fb_add(t, t, p->y); } else { /* Compute t = u * (u + lambda_P + lambda). */ fb_add(t, l, p->y); fb_add(t, t, p->x); fb_mul(t, t, p->x); } /* If Tr(t) = 0 then lambda_P = lambda, u = sqrt(t + u). */ if (fb_trc(t) == 0) { fb_copy(r->y, l); fb_add(t, t, p->x); fb_srt(r->x, t); } else { /* Else lambda_P = lambda + 1, u = sqrt(t). */ fb_add_dig(r->y, l, 1); fb_srt(r->x, t); } fb_set_dig(r->z, 1); r->norm = 2; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(l); fb_free(t); } }
int eb_upk(eb_t r, const eb_t p) { fb_t t0, t1; int res = 0; fb_null(t0); fb_null(t1); TRY { fb_new(t0); fb_new(t1); eb_rhs(t1, p); if (eb_curve_is_super()) { /* t0 = c^2. */ fb_sqr(t0, eb_curve_get_c()); /* t0 = 1/c^2. */ fb_inv(t0, t0); /* t0 = t1/c^2. */ fb_mul(t0, t0, t1); res = (fb_trc(t0) == 0); /* Solve t1^2 + t1 = t0. */ fb_slv(t1, t0); /* If this is not the correct solution, try the other. */ if (fb_get_bit(t1, 0) != fb_get_bit(p->y, 0)) { fb_add_dig(t1, t1, 1); } /* x3 = x1, y3 = t1 * c, z3 = 1. */ fb_mul(r->y, t1, eb_curve_get_c()); } else { fb_sqr(t0, p->x); /* t0 = 1/x1^2. */ fb_inv(t0, t0); /* t0 = t1/x1^2. */ fb_mul(t0, t0, t1); res = (fb_trc(t0) == 0); /* Solve t1^2 + t1 = t0. */ fb_slv(t1, t0); /* If this is not the correct solution, try the other. */ if (fb_get_bit(t1, 0) != fb_get_bit(p->y, 0)) { fb_add_dig(t1, t1, 1); } /* x3 = x1, y3 = t1 * x1, z3 = 1. */ fb_mul(r->y, t1, p->x); } fb_copy(r->x, p->x); fb_set_dig(r->z, 1); r->norm = 1; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t0); fb_free(t1); } return res; }
void eb_rhs(fb_t rhs, const eb_t p) { fb_t t0, t1; fb_null(t0); fb_null(t1); TRY { fb_new(t0); fb_new(t1); /* t0 = x1^2. */ fb_sqr(t0, p->x); /* t1 = x1^3. */ fb_mul(t1, t0, p->x); /* t1 = x1^3 + a * x1^2 + b. */ switch (eb_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fb_add(t1, t1, t0); break; case OPT_DIGIT: fb_mul_dig(t0, t0, eb_curve_get_a()[0]); fb_add(t1, t1, t0); break; default: fb_mul(t0, t0, eb_curve_get_a()); fb_add(t1, t1, t0); break; } switch (eb_curve_opt_b()) { case OPT_ZERO: break; case OPT_ONE: fb_add_dig(t1, t1, 1); break; case OPT_DIGIT: fb_add_dig(t1, t1, eb_curve_get_b()[0]); break; default: fb_add(t1, t1, eb_curve_get_b()); break; } fb_copy(rhs, t1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t0); fb_free(t1); } }
void fb_slv_basic(fb_t c, const fb_t a) { int i; fb_t t0; fb_null(t0); TRY { fb_new(t0); fb_copy(t0, a); fb_copy(c, a); for (i = 0; i < (FB_BITS - 1) / 2; i++) { fb_sqr(c, c); fb_sqr(c, c); fb_add(c, c, t0); } fb_add_dig(c, c, fb_trc(c)); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t0); } }
void eb_neg_basic(eb_t r, const eb_t p) { if (eb_is_infty(p)) { eb_set_infty(r); return; } if (r != p) { fb_copy(r->x, p->x); fb_copy(r->z, p->z); } #if defined(EB_SUPER) if (eb_curve_is_super()) { switch (eb_curve_opt_c()) { case OPT_ZERO: fb_copy(r->y, p->y); break; case OPT_ONE: fb_add_dig(r->y, p->y, (dig_t)1); break; case OPT_DIGIT: fb_add_dig(r->y, p->y, eb_curve_get_c()[0]); break; default: fb_add(r->y, p->y, eb_curve_get_c()); break; } r->norm = 1; return; } #endif fb_add(r->y, p->x, p->y); r->norm = 1; }
void fb_slv_quick(fb_t c, const fb_t a) { fb_slvn_low(c, a); fb_add_dig(c, c, fb_trc(c)); }
void eb_neg_projc(eb_t r, const eb_t p) { fb_t t; fb_null(t); if (eb_is_infty(p)) { eb_set_infty(r); return; } if (p->norm) { if (r != p) { fb_copy(r->x, p->x); fb_copy(r->z, p->z); } #if defined(EB_SUPER) if (eb_curve_is_super()) { switch (eb_curve_opt_c()) { case OPT_ZERO: fb_copy(r->y, p->y); break; case OPT_ONE: fb_add_dig(r->y, p->y, (dig_t)1); break; case OPT_DIGIT: fb_add_dig(r->y, p->y, eb_curve_get_c()[0]); break; default: fb_add(r->y, p->y, eb_curve_get_c()); break; } r->norm = 1; return; } #endif fb_add(r->y, p->x, p->y); r->norm = 1; return; } #if defined(EB_SUPER) if (eb_curve_is_super()) { fb_add(r->y, p->y, p->z); fb_copy(r->z, p->z); fb_copy(r->x, p->x); r->norm = 0; return; } #endif TRY { fb_new(t); fb_mul(t, p->x, p->z); fb_add(r->y, p->y, t); if (r != p) { fb_copy(r->z, p->z); fb_copy(r->x, p->x); } r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t); } }
/** * Doubles a point represented in affine coordinates on an ordinary binary * elliptic curve. * * @param[out] r - the result. * @param[in] p - the point to double. */ static void eb_dbl_basic_imp(eb_t r, const eb_t p) { fb_t t0, t1, t2; fb_null(t0); fb_null(t1); fb_null(t2); TRY { fb_new(t0); fb_new(t1); fb_new(t2); /* t0 = 1/x1. */ fb_inv(t0, p->x); /* t0 = y1/x1. */ fb_mul(t0, t0, p->y); /* t0 = lambda = x1 + y1/x1. */ fb_add(t0, t0, p->x); /* t1 = lambda^2. */ fb_sqr(t1, t0); /* t2 = lambda^2 + lambda. */ fb_add(t2, t1, t0); /* t2 = lambda^2 + lambda + a2. */ switch (eb_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fb_add_dig(t2, t2, (dig_t)1); break; case OPT_DIGIT: fb_add_dig(t2, t2, eb_curve_get_a()[0]); break; default: fb_add(t2, t2, eb_curve_get_a()); break; } /* t1 = x1 + x3. */ fb_add(t1, t2, p->x); /* t1 = lambda * (x1 + x3). */ fb_mul(t1, t0, t1); fb_copy(r->x, t2); /* y3 = lambda * (x1 + x3) + x3 + y1. */ fb_add(t1, t1, r->x); fb_add(r->y, t1, p->y); fb_copy(r->z, p->z); r->norm = 1; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t0); fb_free(t1); fb_free(t2); } }
/** * Adds two points represented in affine coordinates on an ordinary binary * elliptic curve. * * @param[out] r - the result. * @param[in] p - the first point to add. * @param[in] q - the second point to add. */ static void eb_add_basic_imp(eb_t r, const eb_t p, const eb_t q) { fb_t t0, t1, t2; fb_null(t0); fb_null(t1); fb_null(t2); TRY { fb_new(t0); fb_new(t1); fb_new(t2); /* t0 = (y1 + y2). */ fb_add(t0, p->y, q->y); /* t1 = (x1 + x2). */ fb_add(t1, p->x, q->x); if (fb_is_zero(t1)) { if (fb_is_zero(t0)) { /* If t1 is zero and t0 is zero, p = q, should have doubled. */ eb_dbl_basic(r, p); } else { /* If t0 is not zero and t1 is zero, q = -p and r = infinity. */ eb_set_infty(r); } } else { /* t2 = 1/(x1 + x2). */ fb_inv(t2, t1); /* t0 = lambda = (y1 + y2)/(x1 + x2). */ fb_mul(t0, t0, t2); /* t2 = lambda^2. */ fb_sqr(t2, t0); /* t2 = lambda^2 + lambda + x1 + x2 + a. */ fb_add(t2, t2, t0); fb_add(t2, t2, t1); switch (eb_curve_opt_a()) { case OPT_ZERO: break; case OPT_ONE: fb_add_dig(t2, t2, (dig_t)1); break; case OPT_DIGIT: fb_add_dig(t2, t2, eb_curve_get_a()[0]); break; default: fb_add(t2, t2, eb_curve_get_a()); break; } /* y3 = lambda*(x3 + x1) + x3 + y1. */ fb_add(t1, t2, p->x); fb_mul(t1, t1, t0); fb_add(t1, t1, t2); fb_add(r->y, p->y, t1); /* x3 = lambda^2 + lambda + x1 + x2 + a. */ fb_copy(r->x, t2); fb_copy(r->z, p->z); r->norm = 1; } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fb_free(t0); fb_free(t1); fb_free(t2); } }