michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ec2.h" michael@0: #include "mplogic.h" michael@0: #include "mp_gf2m.h" michael@0: #include michael@0: #ifdef ECL_DEBUG michael@0: #include michael@0: #endif michael@0: michael@0: /* by default, these routines are unused and thus don't need to be compiled */ michael@0: #ifdef ECL_ENABLE_GF2M_PROJ michael@0: /* Converts a point P(px, py) from affine coordinates to projective michael@0: * coordinates R(rx, ry, rz). Assumes input is already field-encoded using michael@0: * field_enc, and returns output that is still field-encoded. */ michael@0: mp_err michael@0: ec_GF2m_pt_aff2proj(const mp_int *px, const mp_int *py, mp_int *rx, michael@0: mp_int *ry, mp_int *rz, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: michael@0: MP_CHECKOK(mp_copy(px, rx)); michael@0: MP_CHECKOK(mp_copy(py, ry)); michael@0: MP_CHECKOK(mp_set_int(rz, 1)); michael@0: if (group->meth->field_enc) { michael@0: MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth)); michael@0: } michael@0: CLEANUP: michael@0: return res; michael@0: } michael@0: michael@0: /* Converts a point P(px, py, pz) from projective coordinates to affine michael@0: * coordinates R(rx, ry). P and R can share x and y coordinates. Assumes michael@0: * input is already field-encoded using field_enc, and returns output that michael@0: * is still field-encoded. */ michael@0: mp_err michael@0: ec_GF2m_pt_proj2aff(const mp_int *px, const mp_int *py, const mp_int *pz, michael@0: mp_int *rx, mp_int *ry, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int z1, z2; michael@0: michael@0: MP_DIGITS(&z1) = 0; michael@0: MP_DIGITS(&z2) = 0; michael@0: MP_CHECKOK(mp_init(&z1)); michael@0: MP_CHECKOK(mp_init(&z2)); michael@0: michael@0: /* if point at infinity, then set point at infinity and exit */ michael@0: if (ec_GF2m_pt_is_inf_proj(px, py, pz) == MP_YES) { michael@0: MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry)); michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: /* transform (px, py, pz) into (px / pz, py / pz^2) */ michael@0: if (mp_cmp_d(pz, 1) == 0) { michael@0: MP_CHECKOK(mp_copy(px, rx)); michael@0: MP_CHECKOK(mp_copy(py, ry)); michael@0: } else { michael@0: MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth)); michael@0: MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth)); michael@0: MP_CHECKOK(group->meth->field_mul(px, &z1, rx, group->meth)); michael@0: MP_CHECKOK(group->meth->field_mul(py, &z2, ry, group->meth)); michael@0: } michael@0: michael@0: CLEANUP: michael@0: mp_clear(&z1); michael@0: mp_clear(&z2); michael@0: return res; michael@0: } michael@0: michael@0: /* Checks if point P(px, py, pz) is at infinity. Uses projective michael@0: * coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_is_inf_proj(const mp_int *px, const mp_int *py, michael@0: const mp_int *pz) michael@0: { michael@0: return mp_cmp_z(pz); michael@0: } michael@0: michael@0: /* Sets P(px, py, pz) to be the point at infinity. Uses projective michael@0: * coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_set_inf_proj(mp_int *px, mp_int *py, mp_int *pz) michael@0: { michael@0: mp_zero(pz); michael@0: return MP_OKAY; michael@0: } michael@0: michael@0: /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is michael@0: * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical. michael@0: * Uses mixed projective-affine coordinates. Assumes input is already michael@0: * field-encoded using field_enc, and returns output that is still michael@0: * field-encoded. Uses equation (3) from Hankerson, Hernandez, Menezes. michael@0: * Software Implementation of Elliptic Curve Cryptography Over Binary michael@0: * Fields. */ michael@0: mp_err michael@0: ec_GF2m_pt_add_proj(const mp_int *px, const mp_int *py, const mp_int *pz, michael@0: const mp_int *qx, const mp_int *qy, mp_int *rx, michael@0: mp_int *ry, mp_int *rz, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int A, B, C, D, E, F, G; michael@0: michael@0: /* If either P or Q is the point at infinity, then return the other michael@0: * point */ michael@0: if (ec_GF2m_pt_is_inf_proj(px, py, pz) == MP_YES) { michael@0: return ec_GF2m_pt_aff2proj(qx, qy, rx, ry, rz, group); michael@0: } michael@0: if (ec_GF2m_pt_is_inf_aff(qx, qy) == MP_YES) { michael@0: MP_CHECKOK(mp_copy(px, rx)); michael@0: MP_CHECKOK(mp_copy(py, ry)); michael@0: return mp_copy(pz, rz); michael@0: } michael@0: michael@0: MP_DIGITS(&A) = 0; michael@0: MP_DIGITS(&B) = 0; michael@0: MP_DIGITS(&C) = 0; michael@0: MP_DIGITS(&D) = 0; michael@0: MP_DIGITS(&E) = 0; michael@0: MP_DIGITS(&F) = 0; michael@0: MP_DIGITS(&G) = 0; michael@0: MP_CHECKOK(mp_init(&A)); michael@0: MP_CHECKOK(mp_init(&B)); michael@0: MP_CHECKOK(mp_init(&C)); michael@0: MP_CHECKOK(mp_init(&D)); michael@0: MP_CHECKOK(mp_init(&E)); michael@0: MP_CHECKOK(mp_init(&F)); michael@0: MP_CHECKOK(mp_init(&G)); michael@0: michael@0: /* D = pz^2 */ michael@0: MP_CHECKOK(group->meth->field_sqr(pz, &D, group->meth)); michael@0: michael@0: /* A = qy * pz^2 + py */ michael@0: MP_CHECKOK(group->meth->field_mul(qy, &D, &A, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&A, py, &A, group->meth)); michael@0: michael@0: /* B = qx * pz + px */ michael@0: MP_CHECKOK(group->meth->field_mul(qx, pz, &B, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&B, px, &B, group->meth)); michael@0: michael@0: /* C = pz * B */ michael@0: MP_CHECKOK(group->meth->field_mul(pz, &B, &C, group->meth)); michael@0: michael@0: /* D = B^2 * (C + a * pz^2) (using E as a temporary variable) */ michael@0: MP_CHECKOK(group->meth-> michael@0: field_mul(&group->curvea, &D, &D, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&C, &D, &D, group->meth)); michael@0: MP_CHECKOK(group->meth->field_sqr(&B, &E, group->meth)); michael@0: MP_CHECKOK(group->meth->field_mul(&E, &D, &D, group->meth)); michael@0: michael@0: /* rz = C^2 */ michael@0: MP_CHECKOK(group->meth->field_sqr(&C, rz, group->meth)); michael@0: michael@0: /* E = A * C */ michael@0: MP_CHECKOK(group->meth->field_mul(&A, &C, &E, group->meth)); michael@0: michael@0: /* rx = A^2 + D + E */ michael@0: MP_CHECKOK(group->meth->field_sqr(&A, rx, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(rx, &D, rx, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(rx, &E, rx, group->meth)); michael@0: michael@0: /* F = rx + qx * rz */ michael@0: MP_CHECKOK(group->meth->field_mul(qx, rz, &F, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(rx, &F, &F, group->meth)); michael@0: michael@0: /* G = rx + qy * rz */ michael@0: MP_CHECKOK(group->meth->field_mul(qy, rz, &G, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(rx, &G, &G, group->meth)); michael@0: michael@0: /* ry = E * F + rz * G (using G as a temporary variable) */ michael@0: MP_CHECKOK(group->meth->field_mul(rz, &G, &G, group->meth)); michael@0: MP_CHECKOK(group->meth->field_mul(&E, &F, ry, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(ry, &G, ry, group->meth)); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&A); michael@0: mp_clear(&B); michael@0: mp_clear(&C); michael@0: mp_clear(&D); michael@0: mp_clear(&E); michael@0: mp_clear(&F); michael@0: mp_clear(&G); michael@0: return res; michael@0: } michael@0: michael@0: /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses michael@0: * projective coordinates. michael@0: * michael@0: * Assumes input is already field-encoded using field_enc, and returns michael@0: * output that is still field-encoded. michael@0: * michael@0: * Uses equation (3) from Hankerson, Hernandez, Menezes. Software michael@0: * Implementation of Elliptic Curve Cryptography Over Binary Fields. michael@0: */ michael@0: mp_err michael@0: ec_GF2m_pt_dbl_proj(const mp_int *px, const mp_int *py, const mp_int *pz, michael@0: mp_int *rx, mp_int *ry, mp_int *rz, michael@0: const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int t0, t1; michael@0: michael@0: if (ec_GF2m_pt_is_inf_proj(px, py, pz) == MP_YES) { michael@0: return ec_GF2m_pt_set_inf_proj(rx, ry, rz); michael@0: } michael@0: michael@0: MP_DIGITS(&t0) = 0; michael@0: MP_DIGITS(&t1) = 0; michael@0: MP_CHECKOK(mp_init(&t0)); michael@0: MP_CHECKOK(mp_init(&t1)); michael@0: michael@0: /* t0 = px^2 */ michael@0: /* t1 = pz^2 */ michael@0: MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth)); michael@0: MP_CHECKOK(group->meth->field_sqr(pz, &t1, group->meth)); michael@0: michael@0: /* rz = px^2 * pz^2 */ michael@0: MP_CHECKOK(group->meth->field_mul(&t0, &t1, rz, group->meth)); michael@0: michael@0: /* t0 = px^4 */ michael@0: /* t1 = b * pz^4 */ michael@0: MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth)); michael@0: MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_mul(&group->curveb, &t1, &t1, group->meth)); michael@0: michael@0: /* rx = px^4 + b * pz^4 */ michael@0: MP_CHECKOK(group->meth->field_add(&t0, &t1, rx, group->meth)); michael@0: michael@0: /* ry = b * pz^4 * rz + rx * (a * rz + py^2 + b * pz^4) */ michael@0: MP_CHECKOK(group->meth->field_sqr(py, ry, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(ry, &t1, ry, group->meth)); michael@0: /* t0 = a * rz */ michael@0: MP_CHECKOK(group->meth-> michael@0: field_mul(&group->curvea, rz, &t0, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&t0, ry, ry, group->meth)); michael@0: MP_CHECKOK(group->meth->field_mul(rx, ry, ry, group->meth)); michael@0: /* t1 = b * pz^4 * rz */ michael@0: MP_CHECKOK(group->meth->field_mul(&t1, rz, &t1, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&t1, ry, ry, group->meth)); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&t0); michael@0: mp_clear(&t1); michael@0: return res; michael@0: } michael@0: michael@0: /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters michael@0: * a, b and p are the elliptic curve coefficients and the prime that michael@0: * determines the field GF2m. Elliptic curve points P and R can be michael@0: * identical. Uses mixed projective-affine coordinates. Assumes input is michael@0: * already field-encoded using field_enc, and returns output that is still michael@0: * field-encoded. Uses 4-bit window method. */ michael@0: mp_err michael@0: ec_GF2m_pt_mul_proj(const mp_int *n, const mp_int *px, const mp_int *py, michael@0: mp_int *rx, mp_int *ry, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int precomp[16][2], rz; michael@0: mp_digit precomp_arr[ECL_MAX_FIELD_SIZE_DIGITS * 16 * 2], *t; michael@0: int i, ni, d; michael@0: michael@0: ARGCHK(group != NULL, MP_BADARG); michael@0: ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG); michael@0: michael@0: /* initialize precomputation table */ michael@0: t = precomp_arr; michael@0: for (i = 0; i < 16; i++) { michael@0: /* x co-ord */ michael@0: MP_SIGN(&precomp[i][0]) = MP_ZPOS; michael@0: MP_ALLOC(&precomp[i][0]) = ECL_MAX_FIELD_SIZE_DIGITS; michael@0: MP_USED(&precomp[i][0]) = 1; michael@0: *t = 0; michael@0: MP_DIGITS(&precomp[i][0]) = t; michael@0: t += ECL_MAX_FIELD_SIZE_DIGITS; michael@0: /* y co-ord */ michael@0: MP_SIGN(&precomp[i][1]) = MP_ZPOS; michael@0: MP_ALLOC(&precomp[i][1]) = ECL_MAX_FIELD_SIZE_DIGITS; michael@0: MP_USED(&precomp[i][1]) = 1; michael@0: *t = 0; michael@0: MP_DIGITS(&precomp[i][1]) = t; michael@0: t += ECL_MAX_FIELD_SIZE_DIGITS; michael@0: } michael@0: michael@0: /* fill precomputation table */ michael@0: mp_zero(&precomp[0][0]); michael@0: mp_zero(&precomp[0][1]); michael@0: MP_CHECKOK(mp_copy(px, &precomp[1][0])); michael@0: MP_CHECKOK(mp_copy(py, &precomp[1][1])); michael@0: for (i = 2; i < 16; i++) { michael@0: MP_CHECKOK(group-> michael@0: point_add(&precomp[1][0], &precomp[1][1], michael@0: &precomp[i - 1][0], &precomp[i - 1][1], michael@0: &precomp[i][0], &precomp[i][1], group)); michael@0: } michael@0: michael@0: d = (mpl_significant_bits(n) + 3) / 4; michael@0: michael@0: /* R = inf */ michael@0: MP_DIGITS(&rz) = 0; michael@0: MP_CHECKOK(mp_init(&rz)); michael@0: MP_CHECKOK(ec_GF2m_pt_set_inf_proj(rx, ry, &rz)); michael@0: michael@0: for (i = d - 1; i >= 0; i--) { michael@0: /* compute window ni */ michael@0: ni = MP_GET_BIT(n, 4 * i + 3); michael@0: ni <<= 1; michael@0: ni |= MP_GET_BIT(n, 4 * i + 2); michael@0: ni <<= 1; michael@0: ni |= MP_GET_BIT(n, 4 * i + 1); michael@0: ni <<= 1; michael@0: ni |= MP_GET_BIT(n, 4 * i); michael@0: /* R = 2^4 * R */ michael@0: MP_CHECKOK(ec_GF2m_pt_dbl_proj(rx, ry, &rz, rx, ry, &rz, group)); michael@0: MP_CHECKOK(ec_GF2m_pt_dbl_proj(rx, ry, &rz, rx, ry, &rz, group)); michael@0: MP_CHECKOK(ec_GF2m_pt_dbl_proj(rx, ry, &rz, rx, ry, &rz, group)); michael@0: MP_CHECKOK(ec_GF2m_pt_dbl_proj(rx, ry, &rz, rx, ry, &rz, group)); michael@0: /* R = R + (ni * P) */ michael@0: MP_CHECKOK(ec_GF2m_pt_add_proj michael@0: (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry, michael@0: &rz, group)); michael@0: } michael@0: michael@0: /* convert result S to affine coordinates */ michael@0: MP_CHECKOK(ec_GF2m_pt_proj2aff(rx, ry, &rz, rx, ry, group)); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&rz); michael@0: return res; michael@0: } michael@0: #endif