andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "ecp.h" andre@0: #include "mplogic.h" andre@0: #include andre@0: #ifdef ECL_DEBUG andre@0: #include andre@0: #endif andre@0: andre@0: /* Converts a point P(px, py) from affine coordinates to Jacobian andre@0: * projective coordinates R(rx, ry, rz). Assumes input is already andre@0: * field-encoded using field_enc, and returns output that is still andre@0: * field-encoded. */ andre@0: mp_err andre@0: ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, mp_int *rz, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: andre@0: if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) { andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); andre@0: } else { andre@0: MP_CHECKOK(mp_copy(px, rx)); andre@0: MP_CHECKOK(mp_copy(py, ry)); andre@0: MP_CHECKOK(mp_set_int(rz, 1)); andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth)); andre@0: } andre@0: } andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Converts a point P(px, py, pz) from Jacobian projective coordinates to andre@0: * affine coordinates R(rx, ry). P and R can share x and y coordinates. andre@0: * Assumes input is already field-encoded using field_enc, and returns andre@0: * output that is still field-encoded. */ andre@0: mp_err andre@0: ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py, const mp_int *pz, andre@0: mp_int *rx, mp_int *ry, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int z1, z2, z3; andre@0: andre@0: MP_DIGITS(&z1) = 0; andre@0: MP_DIGITS(&z2) = 0; andre@0: MP_DIGITS(&z3) = 0; andre@0: MP_CHECKOK(mp_init(&z1)); andre@0: MP_CHECKOK(mp_init(&z2)); andre@0: MP_CHECKOK(mp_init(&z3)); andre@0: andre@0: /* if point at infinity, then set point at infinity and exit */ andre@0: if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_aff(rx, ry)); andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: /* transform (px, py, pz) into (px / pz^2, py / pz^3) */ andre@0: if (mp_cmp_d(pz, 1) == 0) { andre@0: MP_CHECKOK(mp_copy(px, rx)); andre@0: MP_CHECKOK(mp_copy(py, ry)); andre@0: } else { andre@0: MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&z1, &z2, &z3, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(px, &z2, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(py, &z3, ry, group->meth)); andre@0: } andre@0: andre@0: CLEANUP: andre@0: mp_clear(&z1); andre@0: mp_clear(&z2); andre@0: mp_clear(&z3); andre@0: return res; andre@0: } andre@0: andre@0: /* Checks if point P(px, py, pz) is at infinity. Uses Jacobian andre@0: * coordinates. */ andre@0: mp_err andre@0: ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py, const mp_int *pz) andre@0: { andre@0: return mp_cmp_z(pz); andre@0: } andre@0: andre@0: /* Sets P(px, py, pz) to be the point at infinity. Uses Jacobian andre@0: * coordinates. */ andre@0: mp_err andre@0: ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz) andre@0: { andre@0: mp_zero(pz); andre@0: return MP_OKAY; andre@0: } andre@0: andre@0: /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is andre@0: * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical. andre@0: * Uses mixed Jacobian-affine coordinates. Assumes input is already andre@0: * field-encoded using field_enc, and returns output that is still andre@0: * field-encoded. Uses equation (2) from Brown, Hankerson, Lopez, and andre@0: * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime andre@0: * Fields. */ andre@0: mp_err andre@0: ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py, const mp_int *pz, andre@0: const mp_int *qx, const mp_int *qy, mp_int *rx, andre@0: mp_int *ry, mp_int *rz, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int A, B, C, D, C2, C3; andre@0: andre@0: MP_DIGITS(&A) = 0; andre@0: MP_DIGITS(&B) = 0; andre@0: MP_DIGITS(&C) = 0; andre@0: MP_DIGITS(&D) = 0; andre@0: MP_DIGITS(&C2) = 0; andre@0: MP_DIGITS(&C3) = 0; andre@0: MP_CHECKOK(mp_init(&A)); andre@0: MP_CHECKOK(mp_init(&B)); andre@0: MP_CHECKOK(mp_init(&C)); andre@0: MP_CHECKOK(mp_init(&D)); andre@0: MP_CHECKOK(mp_init(&C2)); andre@0: MP_CHECKOK(mp_init(&C3)); andre@0: andre@0: /* If either P or Q is the point at infinity, then return the other andre@0: * point */ andre@0: if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { andre@0: MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group)); andre@0: goto CLEANUP; andre@0: } andre@0: if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) { andre@0: MP_CHECKOK(mp_copy(px, rx)); andre@0: MP_CHECKOK(mp_copy(py, ry)); andre@0: MP_CHECKOK(mp_copy(pz, rz)); andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: /* A = qx * pz^2, B = qy * pz^3 */ andre@0: MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth)); andre@0: andre@0: /* C = A - px, D = B - py */ andre@0: MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth)); andre@0: andre@0: /* C2 = C^2, C3 = C^3 */ andre@0: MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth)); andre@0: andre@0: /* rz = pz * C */ andre@0: MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth)); andre@0: andre@0: /* C = px * C^2 */ andre@0: MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth)); andre@0: /* A = D^2 */ andre@0: MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth)); andre@0: andre@0: /* rx = D^2 - (C^3 + 2 * (px * C^2)) */ andre@0: MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth)); andre@0: andre@0: /* C3 = py * C^3 */ andre@0: MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth)); andre@0: andre@0: /* ry = D * (px * C^2 - rx) - py * C^3 */ andre@0: MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth)); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&A); andre@0: mp_clear(&B); andre@0: mp_clear(&C); andre@0: mp_clear(&D); andre@0: mp_clear(&C2); andre@0: mp_clear(&C3); andre@0: return res; andre@0: } andre@0: andre@0: /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses andre@0: * Jacobian coordinates. andre@0: * andre@0: * Assumes input is already field-encoded using field_enc, and returns andre@0: * output that is still field-encoded. andre@0: * andre@0: * This routine implements Point Doubling in the Jacobian Projective andre@0: * space as described in the paper "Efficient elliptic curve exponentiation andre@0: * using mixed coordinates", by H. Cohen, A Miyaji, T. Ono. andre@0: */ andre@0: mp_err andre@0: ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py, const mp_int *pz, andre@0: mp_int *rx, mp_int *ry, mp_int *rz, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int t0, t1, M, S; andre@0: andre@0: MP_DIGITS(&t0) = 0; andre@0: MP_DIGITS(&t1) = 0; andre@0: MP_DIGITS(&M) = 0; andre@0: MP_DIGITS(&S) = 0; andre@0: MP_CHECKOK(mp_init(&t0)); andre@0: MP_CHECKOK(mp_init(&t1)); andre@0: MP_CHECKOK(mp_init(&M)); andre@0: MP_CHECKOK(mp_init(&S)); andre@0: andre@0: if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: if (mp_cmp_d(pz, 1) == 0) { andre@0: /* M = 3 * px^2 + a */ andre@0: MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_add(&t0, &group->curvea, &M, group->meth)); andre@0: } else if (mp_cmp_int(&group->curvea, -3) == 0) { andre@0: /* M = 3 * (px + pz^2) * (px - pz^2) */ andre@0: MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(px, &M, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(px, &M, &t1, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&t0, &t1, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&M, &M, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&t0, &M, &M, group->meth)); andre@0: } else { andre@0: /* M = 3 * (px^2) + a * (pz^4) */ andre@0: MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(&M, &M, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_mul(&M, &group->curvea, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(&M, &t0, &M, group->meth)); andre@0: } andre@0: andre@0: /* rz = 2 * py * pz */ andre@0: /* t0 = 4 * py^2 */ andre@0: if (mp_cmp_d(pz, 1) == 0) { andre@0: MP_CHECKOK(group->meth->field_add(py, py, rz, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(rz, &t0, group->meth)); andre@0: } else { andre@0: MP_CHECKOK(group->meth->field_add(py, py, &t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&t0, pz, rz, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth)); andre@0: } andre@0: andre@0: /* S = 4 * px * py^2 = px * (2 * py)^2 */ andre@0: MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth)); andre@0: andre@0: /* rx = M^2 - 2 * S */ andre@0: MP_CHECKOK(group->meth->field_add(&S, &S, &t1, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(rx, &t1, rx, group->meth)); andre@0: andre@0: /* ry = M * (S - rx) - 8 * py^4 */ andre@0: MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth)); andre@0: if (mp_isodd(&t1)) { andre@0: MP_CHECKOK(mp_add(&t1, &group->meth->irr, &t1)); andre@0: } andre@0: MP_CHECKOK(mp_div_2(&t1, &t1)); andre@0: MP_CHECKOK(group->meth->field_sub(&S, rx, &S, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(&M, &S, &M, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&M, &t1, ry, group->meth)); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&t0); andre@0: mp_clear(&t1); andre@0: mp_clear(&M); andre@0: mp_clear(&S); andre@0: return res; andre@0: } andre@0: andre@0: /* by default, this routine is unused and thus doesn't need to be compiled */ andre@0: #ifdef ECL_ENABLE_GFP_PT_MUL_JAC andre@0: /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters andre@0: * a, b and p are the elliptic curve coefficients and the prime that andre@0: * determines the field GFp. Elliptic curve points P and R can be andre@0: * identical. Uses mixed Jacobian-affine coordinates. Assumes input is andre@0: * already field-encoded using field_enc, and returns output that is still andre@0: * field-encoded. Uses 4-bit window method. */ andre@0: mp_err andre@0: ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px, const mp_int *py, andre@0: mp_int *rx, mp_int *ry, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int precomp[16][2], rz; andre@0: int i, ni, d; andre@0: andre@0: MP_DIGITS(&rz) = 0; andre@0: for (i = 0; i < 16; i++) { andre@0: MP_DIGITS(&precomp[i][0]) = 0; andre@0: MP_DIGITS(&precomp[i][1]) = 0; andre@0: } andre@0: andre@0: ARGCHK(group != NULL, MP_BADARG); andre@0: ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG); andre@0: andre@0: /* initialize precomputation table */ andre@0: for (i = 0; i < 16; i++) { andre@0: MP_CHECKOK(mp_init(&precomp[i][0])); andre@0: MP_CHECKOK(mp_init(&precomp[i][1])); andre@0: } andre@0: andre@0: /* fill precomputation table */ andre@0: mp_zero(&precomp[0][0]); andre@0: mp_zero(&precomp[0][1]); andre@0: MP_CHECKOK(mp_copy(px, &precomp[1][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[1][1])); andre@0: for (i = 2; i < 16; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[1][0], &precomp[1][1], andre@0: &precomp[i - 1][0], &precomp[i - 1][1], andre@0: &precomp[i][0], &precomp[i][1], group)); andre@0: } andre@0: andre@0: d = (mpl_significant_bits(n) + 3) / 4; andre@0: andre@0: /* R = inf */ andre@0: MP_CHECKOK(mp_init(&rz)); andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); andre@0: andre@0: for (i = d - 1; i >= 0; i--) { andre@0: /* compute window ni */ andre@0: ni = MP_GET_BIT(n, 4 * i + 3); andre@0: ni <<= 1; andre@0: ni |= MP_GET_BIT(n, 4 * i + 2); andre@0: ni <<= 1; andre@0: ni |= MP_GET_BIT(n, 4 * i + 1); andre@0: ni <<= 1; andre@0: ni |= MP_GET_BIT(n, 4 * i); andre@0: /* R = 2^4 * R */ andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: /* R = R + (ni * P) */ andre@0: MP_CHECKOK(ec_GFp_pt_add_jac_aff andre@0: (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry, andre@0: &rz, group)); andre@0: } andre@0: andre@0: /* convert result S to affine coordinates */ andre@0: MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group)); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&rz); andre@0: for (i = 0; i < 16; i++) { andre@0: mp_clear(&precomp[i][0]); andre@0: mp_clear(&precomp[i][1]); andre@0: } andre@0: return res; andre@0: } andre@0: #endif andre@0: andre@0: /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + andre@0: * k2 * P(x, y), where G is the generator (base point) of the group of andre@0: * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. andre@0: * Uses mixed Jacobian-affine coordinates. Input and output values are andre@0: * assumed to be NOT field-encoded. Uses algorithm 15 (simultaneous andre@0: * multiple point multiplication) from Brown, Hankerson, Lopez, Menezes. andre@0: * Software Implementation of the NIST Elliptic Curves over Prime Fields. */ andre@0: mp_err andre@0: ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px, andre@0: const mp_int *py, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int precomp[4][4][2]; andre@0: mp_int rz; andre@0: const mp_int *a, *b; andre@0: int i, j; andre@0: int ai, bi, d; andre@0: andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: MP_DIGITS(&precomp[i][j][0]) = 0; andre@0: MP_DIGITS(&precomp[i][j][1]) = 0; andre@0: } andre@0: } andre@0: MP_DIGITS(&rz) = 0; andre@0: andre@0: ARGCHK(group != NULL, MP_BADARG); andre@0: ARGCHK(!((k1 == NULL) andre@0: && ((k2 == NULL) || (px == NULL) andre@0: || (py == NULL))), MP_BADARG); andre@0: andre@0: /* if some arguments are not defined used ECPoint_mul */ andre@0: if (k1 == NULL) { andre@0: return ECPoint_mul(group, k2, px, py, rx, ry); andre@0: } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { andre@0: return ECPoint_mul(group, k1, NULL, NULL, rx, ry); andre@0: } andre@0: andre@0: /* initialize precomputation table */ andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: MP_CHECKOK(mp_init(&precomp[i][j][0])); andre@0: MP_CHECKOK(mp_init(&precomp[i][j][1])); andre@0: } andre@0: } andre@0: andre@0: /* fill precomputation table */ andre@0: /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */ andre@0: if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) { andre@0: a = k2; andre@0: b = k1; andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(px, &precomp[1][0][0], group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(py, &precomp[1][0][1], group->meth)); andre@0: } else { andre@0: MP_CHECKOK(mp_copy(px, &precomp[1][0][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[1][0][1])); andre@0: } andre@0: MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0])); andre@0: MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1])); andre@0: } else { andre@0: a = k1; andre@0: b = k2; andre@0: MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0])); andre@0: MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1])); andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(px, &precomp[0][1][0], group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(py, &precomp[0][1][1], group->meth)); andre@0: } else { andre@0: MP_CHECKOK(mp_copy(px, &precomp[0][1][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[0][1][1])); andre@0: } andre@0: } andre@0: /* precompute [*][0][*] */ andre@0: mp_zero(&precomp[0][0][0]); andre@0: mp_zero(&precomp[0][0][1]); andre@0: MP_CHECKOK(group-> andre@0: point_dbl(&precomp[1][0][0], &precomp[1][0][1], andre@0: &precomp[2][0][0], &precomp[2][0][1], group)); andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[1][0][0], &precomp[1][0][1], andre@0: &precomp[2][0][0], &precomp[2][0][1], andre@0: &precomp[3][0][0], &precomp[3][0][1], group)); andre@0: /* precompute [*][1][*] */ andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][1][0], &precomp[i][1][1], group)); andre@0: } andre@0: /* precompute [*][2][*] */ andre@0: MP_CHECKOK(group-> andre@0: point_dbl(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[0][2][0], &precomp[0][2][1], group)); andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][2][0], &precomp[0][2][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][2][0], &precomp[i][2][1], group)); andre@0: } andre@0: /* precompute [*][3][*] */ andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[0][2][0], &precomp[0][2][1], andre@0: &precomp[0][3][0], &precomp[0][3][1], group)); andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][3][0], &precomp[0][3][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][3][0], &precomp[i][3][1], group)); andre@0: } andre@0: andre@0: d = (mpl_significant_bits(a) + 1) / 2; andre@0: andre@0: /* R = inf */ andre@0: MP_CHECKOK(mp_init(&rz)); andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); andre@0: andre@0: for (i = d - 1; i >= 0; i--) { andre@0: ai = MP_GET_BIT(a, 2 * i + 1); andre@0: ai <<= 1; andre@0: ai |= MP_GET_BIT(a, 2 * i); andre@0: bi = MP_GET_BIT(b, 2 * i + 1); andre@0: bi <<= 1; andre@0: bi |= MP_GET_BIT(b, 2 * i); andre@0: /* R = 2^2 * R */ andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); andre@0: /* R = R + (ai * A + bi * B) */ andre@0: MP_CHECKOK(ec_GFp_pt_add_jac_aff andre@0: (rx, ry, &rz, &precomp[ai][bi][0], &precomp[ai][bi][1], andre@0: rx, ry, &rz, group)); andre@0: } andre@0: andre@0: MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group)); andre@0: andre@0: if (group->meth->field_dec) { andre@0: MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); andre@0: } andre@0: andre@0: CLEANUP: andre@0: mp_clear(&rz); andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: mp_clear(&precomp[i][j][0]); andre@0: mp_clear(&precomp[i][j][1]); andre@0: } andre@0: } andre@0: return res; andre@0: }