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 "ecl-priv.h" andre@0: #include "mplogic.h" andre@0: #include andre@0: andre@0: #define MAX_SCRATCH 6 andre@0: andre@0: /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses andre@0: * Modified 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: */ andre@0: mp_err andre@0: ec_GFp_pt_dbl_jm(const mp_int *px, const mp_int *py, const mp_int *pz, andre@0: const mp_int *paz4, mp_int *rx, mp_int *ry, mp_int *rz, andre@0: mp_int *raz4, mp_int scratch[], const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int *t0, *t1, *M, *S; andre@0: andre@0: t0 = &scratch[0]; andre@0: t1 = &scratch[1]; andre@0: M = &scratch[2]; andre@0: S = &scratch[3]; andre@0: andre@0: #if MAX_SCRATCH < 4 andre@0: #error "Scratch array defined too small " andre@0: #endif andre@0: andre@0: /* Check for point at infinity */ andre@0: if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { andre@0: /* Set r = pt at infinity by setting rz = 0 */ andre@0: andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); andre@0: goto CLEANUP; andre@0: } andre@0: 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_add(t0, paz4, M, group->meth)); andre@0: andre@0: /* rz = 2 * py * pz */ andre@0: MP_CHECKOK(group->meth->field_mul(py, pz, S, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(S, S, rz, group->meth)); andre@0: andre@0: /* t0 = 2y^2 , t1 = 8y^4 */ andre@0: MP_CHECKOK(group->meth->field_sqr(py, t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(t0, t0, t0, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(t0, t1, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(t1, t1, t1, group->meth)); andre@0: andre@0: /* S = 4 * px * py^2 = 2 * px * t0 */ andre@0: MP_CHECKOK(group->meth->field_mul(px, t0, S, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(S, S, S, group->meth)); andre@0: andre@0: andre@0: /* rx = M^2 - 2S */ andre@0: MP_CHECKOK(group->meth->field_sqr(M, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth)); andre@0: andre@0: /* ry = M * (S - rx) - t1 */ andre@0: MP_CHECKOK(group->meth->field_sub(S, rx, S, group->meth)); andre@0: MP_CHECKOK(group->meth->field_mul(S, M, ry, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(ry, t1, ry, group->meth)); andre@0: andre@0: /* ra*z^4 = 2*t1*(apz4) */ andre@0: MP_CHECKOK(group->meth->field_mul(paz4, t1, raz4, group->meth)); andre@0: MP_CHECKOK(group->meth->field_add(raz4, raz4, raz4, group->meth)); andre@0: andre@0: andre@0: CLEANUP: andre@0: return res; 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 Modified_Jacobian-affine coordinates. Assumes input is andre@0: * already field-encoded using field_enc, and returns output that is still andre@0: * field-encoded. */ andre@0: mp_err andre@0: ec_GFp_pt_add_jm_aff(const mp_int *px, const mp_int *py, const mp_int *pz, andre@0: const mp_int *paz4, const mp_int *qx, andre@0: const mp_int *qy, mp_int *rx, mp_int *ry, mp_int *rz, andre@0: mp_int *raz4, mp_int scratch[], 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: A = &scratch[0]; andre@0: B = &scratch[1]; andre@0: C = &scratch[2]; andre@0: D = &scratch[3]; andre@0: C2 = &scratch[4]; andre@0: C3 = &scratch[5]; andre@0: andre@0: #if MAX_SCRATCH < 6 andre@0: #error "Scratch array defined too small " andre@0: #endif 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: MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_mul(raz4, &group->curvea, raz4, group->meth)); 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: MP_CHECKOK(mp_copy(paz4, raz4)); 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: /* raz4 = a * rz^4 */ andre@0: MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_mul(raz4, &group->curvea, raz4, group->meth)); andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic andre@0: * curve points P and R can be identical. Uses mixed Modified-Jacobian andre@0: * co-ordinates for doubling and Chudnovsky Jacobian coordinates for andre@0: * additions. Assumes input is already field-encoded using field_enc, and andre@0: * returns output that is still field-encoded. Uses 5-bit window NAF andre@0: * method (algorithm 11) for scalar-point multiplication from Brown, andre@0: * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic andre@0: * Curves Over Prime Fields. */ andre@0: mp_err andre@0: ec_GFp_pt_mul_jm_wNAF(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, tpx, tpy; andre@0: mp_int raz4; andre@0: mp_int scratch[MAX_SCRATCH]; andre@0: signed char *naf = NULL; andre@0: int i, orderBitSize; andre@0: andre@0: MP_DIGITS(&rz) = 0; andre@0: MP_DIGITS(&raz4) = 0; andre@0: MP_DIGITS(&tpx) = 0; andre@0: MP_DIGITS(&tpy) = 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: for (i = 0; i < MAX_SCRATCH; i++) { andre@0: MP_DIGITS(&scratch[i]) = 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: MP_CHECKOK(mp_init(&tpx)); andre@0: MP_CHECKOK(mp_init(&tpy));; andre@0: MP_CHECKOK(mp_init(&rz)); andre@0: MP_CHECKOK(mp_init(&raz4)); andre@0: 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: for (i = 0; i < MAX_SCRATCH; i++) { andre@0: MP_CHECKOK(mp_init(&scratch[i])); andre@0: } andre@0: andre@0: /* Set out[8] = P */ andre@0: MP_CHECKOK(mp_copy(px, &precomp[8][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[8][1])); andre@0: andre@0: /* Set (tpx, tpy) = 2P */ andre@0: MP_CHECKOK(group-> andre@0: point_dbl(&precomp[8][0], &precomp[8][1], &tpx, &tpy, andre@0: group)); andre@0: andre@0: /* Set 3P, 5P, ..., 15P */ andre@0: for (i = 8; i < 15; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[i][0], &precomp[i][1], &tpx, &tpy, andre@0: &precomp[i + 1][0], &precomp[i + 1][1], andre@0: group)); andre@0: } andre@0: andre@0: /* Set -15P, -13P, ..., -P */ andre@0: for (i = 0; i < 8; i++) { andre@0: MP_CHECKOK(mp_copy(&precomp[15 - i][0], &precomp[i][0])); andre@0: MP_CHECKOK(group->meth-> andre@0: field_neg(&precomp[15 - i][1], &precomp[i][1], andre@0: group->meth)); andre@0: } andre@0: andre@0: /* R = inf */ andre@0: MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); andre@0: andre@0: orderBitSize = mpl_significant_bits(&group->order); andre@0: andre@0: /* Allocate memory for NAF */ andre@0: naf = (signed char *) malloc(sizeof(signed char) * (orderBitSize + 1)); andre@0: if (naf == NULL) { andre@0: res = MP_MEM; andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: /* Compute 5NAF */ andre@0: ec_compute_wNAF(naf, orderBitSize, n, 5); andre@0: andre@0: /* wNAF method */ andre@0: for (i = orderBitSize; i >= 0; i--) { andre@0: /* R = 2R */ andre@0: ec_GFp_pt_dbl_jm(rx, ry, &rz, &raz4, rx, ry, &rz, andre@0: &raz4, scratch, group); andre@0: if (naf[i] != 0) { andre@0: ec_GFp_pt_add_jm_aff(rx, ry, &rz, &raz4, andre@0: &precomp[(naf[i] + 15) / 2][0], andre@0: &precomp[(naf[i] + 15) / 2][1], rx, ry, andre@0: &rz, &raz4, scratch, group); andre@0: } 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: for (i = 0; i < MAX_SCRATCH; i++) { andre@0: mp_clear(&scratch[i]); andre@0: } 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: mp_clear(&tpx); andre@0: mp_clear(&tpy); andre@0: mp_clear(&rz); andre@0: mp_clear(&raz4); andre@0: free(naf); andre@0: return res; andre@0: }