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: andre@0: /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */ andre@0: mp_err andre@0: ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py) andre@0: { andre@0: andre@0: if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) { andre@0: return MP_YES; andre@0: } else { andre@0: return MP_NO; andre@0: } andre@0: andre@0: } andre@0: andre@0: /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */ andre@0: mp_err andre@0: ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py) andre@0: { andre@0: mp_zero(px); andre@0: mp_zero(py); andre@0: return MP_OKAY; andre@0: } andre@0: andre@0: /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P, andre@0: * Q, and R can all be identical. Uses affine coordinates. Assumes input andre@0: * is already field-encoded using field_enc, and returns output that is andre@0: * still field-encoded. */ andre@0: mp_err andre@0: ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx, andre@0: const mp_int *qy, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int lambda, temp, tempx, tempy; andre@0: andre@0: MP_DIGITS(&lambda) = 0; andre@0: MP_DIGITS(&temp) = 0; andre@0: MP_DIGITS(&tempx) = 0; andre@0: MP_DIGITS(&tempy) = 0; andre@0: MP_CHECKOK(mp_init(&lambda)); andre@0: MP_CHECKOK(mp_init(&temp)); andre@0: MP_CHECKOK(mp_init(&tempx)); andre@0: MP_CHECKOK(mp_init(&tempy)); andre@0: /* if P = inf, then R = Q */ andre@0: if (ec_GFp_pt_is_inf_aff(px, py) == 0) { andre@0: MP_CHECKOK(mp_copy(qx, rx)); andre@0: MP_CHECKOK(mp_copy(qy, ry)); andre@0: res = MP_OKAY; andre@0: goto CLEANUP; andre@0: } andre@0: /* if Q = inf, then R = P */ andre@0: if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) { andre@0: MP_CHECKOK(mp_copy(px, rx)); andre@0: MP_CHECKOK(mp_copy(py, ry)); andre@0: res = MP_OKAY; andre@0: goto CLEANUP; andre@0: } andre@0: /* if px != qx, then lambda = (py-qy) / (px-qx) */ andre@0: if (mp_cmp(px, qx) != 0) { andre@0: MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_div(&tempy, &tempx, &lambda, group->meth)); andre@0: } else { andre@0: /* if py != qy or qy = 0, then R = inf */ andre@0: if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) { andre@0: mp_zero(rx); andre@0: mp_zero(ry); andre@0: res = MP_OKAY; andre@0: goto CLEANUP; andre@0: } andre@0: /* lambda = (3qx^2+a) / (2qy) */ andre@0: MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth)); andre@0: MP_CHECKOK(mp_set_int(&temp, 3)); andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth)); andre@0: } andre@0: MP_CHECKOK(group->meth-> andre@0: field_mul(&tempx, &temp, &tempx, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_add(&tempx, &group->curvea, &tempx, group->meth)); andre@0: MP_CHECKOK(mp_set_int(&temp, 2)); andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth)); andre@0: } andre@0: MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_div(&tempx, &tempy, &lambda, group->meth)); andre@0: } andre@0: /* rx = lambda^2 - px - qx */ andre@0: MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth)); andre@0: /* ry = (x1-x2) * lambda - y1 */ andre@0: MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_mul(&tempy, &lambda, &tempy, group->meth)); andre@0: MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth)); andre@0: MP_CHECKOK(mp_copy(&tempx, rx)); andre@0: MP_CHECKOK(mp_copy(&tempy, ry)); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&lambda); andre@0: mp_clear(&temp); andre@0: mp_clear(&tempx); andre@0: mp_clear(&tempy); andre@0: return res; andre@0: } andre@0: andre@0: /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be andre@0: * identical. Uses affine coordinates. 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_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx, andre@0: const mp_int *qy, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int nqy; andre@0: andre@0: MP_DIGITS(&nqy) = 0; andre@0: MP_CHECKOK(mp_init(&nqy)); andre@0: /* nqy = -qy */ andre@0: MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth)); andre@0: res = group->point_add(px, py, qx, &nqy, rx, ry, group); andre@0: CLEANUP: andre@0: mp_clear(&nqy); 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: * affine coordinates. Assumes input is already field-encoded using andre@0: * field_enc, and returns output that is still field-encoded. */ andre@0: mp_err andre@0: ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group) andre@0: { andre@0: return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group); 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_AFF andre@0: /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and andre@0: * R can be identical. Uses affine coordinates. 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_mul_aff(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 k, k3, qx, qy, sx, sy; andre@0: int b1, b3, i, l; andre@0: andre@0: MP_DIGITS(&k) = 0; andre@0: MP_DIGITS(&k3) = 0; andre@0: MP_DIGITS(&qx) = 0; andre@0: MP_DIGITS(&qy) = 0; andre@0: MP_DIGITS(&sx) = 0; andre@0: MP_DIGITS(&sy) = 0; andre@0: MP_CHECKOK(mp_init(&k)); andre@0: MP_CHECKOK(mp_init(&k3)); andre@0: MP_CHECKOK(mp_init(&qx)); andre@0: MP_CHECKOK(mp_init(&qy)); andre@0: MP_CHECKOK(mp_init(&sx)); andre@0: MP_CHECKOK(mp_init(&sy)); andre@0: andre@0: /* if n = 0 then r = inf */ andre@0: if (mp_cmp_z(n) == 0) { andre@0: mp_zero(rx); andre@0: mp_zero(ry); andre@0: res = MP_OKAY; andre@0: goto CLEANUP; andre@0: } andre@0: /* Q = P, k = n */ andre@0: MP_CHECKOK(mp_copy(px, &qx)); andre@0: MP_CHECKOK(mp_copy(py, &qy)); andre@0: MP_CHECKOK(mp_copy(n, &k)); andre@0: /* if n < 0 then Q = -Q, k = -k */ andre@0: if (mp_cmp_z(n) < 0) { andre@0: MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth)); andre@0: MP_CHECKOK(mp_neg(&k, &k)); andre@0: } andre@0: #ifdef ECL_DEBUG /* basic double and add method */ andre@0: l = mpl_significant_bits(&k) - 1; andre@0: MP_CHECKOK(mp_copy(&qx, &sx)); andre@0: MP_CHECKOK(mp_copy(&qy, &sy)); andre@0: for (i = l - 1; i >= 0; i--) { andre@0: /* S = 2S */ andre@0: MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group)); andre@0: /* if k_i = 1, then S = S + Q */ andre@0: if (mpl_get_bit(&k, i) != 0) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&sx, &sy, &qx, &qy, &sx, &sy, group)); andre@0: } andre@0: } andre@0: #else /* double and add/subtract method from andre@0: * standard */ andre@0: /* k3 = 3 * k */ andre@0: MP_CHECKOK(mp_set_int(&k3, 3)); andre@0: MP_CHECKOK(mp_mul(&k, &k3, &k3)); andre@0: /* S = Q */ andre@0: MP_CHECKOK(mp_copy(&qx, &sx)); andre@0: MP_CHECKOK(mp_copy(&qy, &sy)); andre@0: /* l = index of high order bit in binary representation of 3*k */ andre@0: l = mpl_significant_bits(&k3) - 1; andre@0: /* for i = l-1 downto 1 */ andre@0: for (i = l - 1; i >= 1; i--) { andre@0: /* S = 2S */ andre@0: MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group)); andre@0: b3 = MP_GET_BIT(&k3, i); andre@0: b1 = MP_GET_BIT(&k, i); andre@0: /* if k3_i = 1 and k_i = 0, then S = S + Q */ andre@0: if ((b3 == 1) && (b1 == 0)) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&sx, &sy, &qx, &qy, &sx, &sy, group)); andre@0: /* if k3_i = 0 and k_i = 1, then S = S - Q */ andre@0: } else if ((b3 == 0) && (b1 == 1)) { andre@0: MP_CHECKOK(group-> andre@0: point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group)); andre@0: } andre@0: } andre@0: #endif andre@0: /* output S */ andre@0: MP_CHECKOK(mp_copy(&sx, rx)); andre@0: MP_CHECKOK(mp_copy(&sy, ry)); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&k); andre@0: mp_clear(&k3); andre@0: mp_clear(&qx); andre@0: mp_clear(&qy); andre@0: mp_clear(&sx); andre@0: mp_clear(&sy); andre@0: return res; andre@0: } andre@0: #endif andre@0: andre@0: /* Validates a point on a GFp curve. */ andre@0: mp_err andre@0: ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group) andre@0: { andre@0: mp_err res = MP_NO; andre@0: mp_int accl, accr, tmp, pxt, pyt; andre@0: andre@0: MP_DIGITS(&accl) = 0; andre@0: MP_DIGITS(&accr) = 0; andre@0: MP_DIGITS(&tmp) = 0; andre@0: MP_DIGITS(&pxt) = 0; andre@0: MP_DIGITS(&pyt) = 0; andre@0: MP_CHECKOK(mp_init(&accl)); andre@0: MP_CHECKOK(mp_init(&accr)); andre@0: MP_CHECKOK(mp_init(&tmp)); andre@0: MP_CHECKOK(mp_init(&pxt)); andre@0: MP_CHECKOK(mp_init(&pyt)); andre@0: andre@0: /* 1: Verify that publicValue is not the point at infinity */ andre@0: if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) { andre@0: res = MP_NO; andre@0: goto CLEANUP; andre@0: } andre@0: /* 2: Verify that the coordinates of publicValue are elements andre@0: * of the field. andre@0: */ andre@0: if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) || andre@0: (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) { andre@0: res = MP_NO; andre@0: goto CLEANUP; andre@0: } andre@0: /* 3: Verify that publicValue is on the curve. */ andre@0: if (group->meth->field_enc) { andre@0: group->meth->field_enc(px, &pxt, group->meth); andre@0: group->meth->field_enc(py, &pyt, group->meth); andre@0: } else { andre@0: mp_copy(px, &pxt); andre@0: mp_copy(py, &pyt); andre@0: } andre@0: /* left-hand side: y^2 */ andre@0: MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) ); andre@0: /* right-hand side: x^3 + a*x + b = (x^2 + a)*x + b by Horner's rule */ andre@0: MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) ); andre@0: MP_CHECKOK( group->meth->field_add(&tmp, &group->curvea, &tmp, group->meth) ); andre@0: MP_CHECKOK( group->meth->field_mul(&tmp, &pxt, &accr, group->meth) ); andre@0: MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) ); andre@0: /* check LHS - RHS == 0 */ andre@0: MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) ); andre@0: if (mp_cmp_z(&accr) != 0) { andre@0: res = MP_NO; andre@0: goto CLEANUP; andre@0: } andre@0: /* 4: Verify that the order of the curve times the publicValue andre@0: * is the point at infinity. andre@0: */ andre@0: MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) ); andre@0: if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) { andre@0: res = MP_NO; andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: res = MP_YES; andre@0: andre@0: CLEANUP: andre@0: mp_clear(&accl); andre@0: mp_clear(&accr); andre@0: mp_clear(&tmp); andre@0: mp_clear(&pxt); andre@0: mp_clear(&pyt); andre@0: return res; andre@0: }