Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "ecp.h" |
michael@0 | 6 | #include "mplogic.h" |
michael@0 | 7 | #include <stdlib.h> |
michael@0 | 8 | #ifdef ECL_DEBUG |
michael@0 | 9 | #include <assert.h> |
michael@0 | 10 | #endif |
michael@0 | 11 | |
michael@0 | 12 | /* Converts a point P(px, py) from affine coordinates to Jacobian |
michael@0 | 13 | * projective coordinates R(rx, ry, rz). Assumes input is already |
michael@0 | 14 | * field-encoded using field_enc, and returns output that is still |
michael@0 | 15 | * field-encoded. */ |
michael@0 | 16 | mp_err |
michael@0 | 17 | ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx, |
michael@0 | 18 | mp_int *ry, mp_int *rz, const ECGroup *group) |
michael@0 | 19 | { |
michael@0 | 20 | mp_err res = MP_OKAY; |
michael@0 | 21 | |
michael@0 | 22 | if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) { |
michael@0 | 23 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); |
michael@0 | 24 | } else { |
michael@0 | 25 | MP_CHECKOK(mp_copy(px, rx)); |
michael@0 | 26 | MP_CHECKOK(mp_copy(py, ry)); |
michael@0 | 27 | MP_CHECKOK(mp_set_int(rz, 1)); |
michael@0 | 28 | if (group->meth->field_enc) { |
michael@0 | 29 | MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth)); |
michael@0 | 30 | } |
michael@0 | 31 | } |
michael@0 | 32 | CLEANUP: |
michael@0 | 33 | return res; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | /* Converts a point P(px, py, pz) from Jacobian projective coordinates to |
michael@0 | 37 | * affine coordinates R(rx, ry). P and R can share x and y coordinates. |
michael@0 | 38 | * Assumes input is already field-encoded using field_enc, and returns |
michael@0 | 39 | * output that is still field-encoded. */ |
michael@0 | 40 | mp_err |
michael@0 | 41 | ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py, const mp_int *pz, |
michael@0 | 42 | mp_int *rx, mp_int *ry, const ECGroup *group) |
michael@0 | 43 | { |
michael@0 | 44 | mp_err res = MP_OKAY; |
michael@0 | 45 | mp_int z1, z2, z3; |
michael@0 | 46 | |
michael@0 | 47 | MP_DIGITS(&z1) = 0; |
michael@0 | 48 | MP_DIGITS(&z2) = 0; |
michael@0 | 49 | MP_DIGITS(&z3) = 0; |
michael@0 | 50 | MP_CHECKOK(mp_init(&z1)); |
michael@0 | 51 | MP_CHECKOK(mp_init(&z2)); |
michael@0 | 52 | MP_CHECKOK(mp_init(&z3)); |
michael@0 | 53 | |
michael@0 | 54 | /* if point at infinity, then set point at infinity and exit */ |
michael@0 | 55 | if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { |
michael@0 | 56 | MP_CHECKOK(ec_GFp_pt_set_inf_aff(rx, ry)); |
michael@0 | 57 | goto CLEANUP; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /* transform (px, py, pz) into (px / pz^2, py / pz^3) */ |
michael@0 | 61 | if (mp_cmp_d(pz, 1) == 0) { |
michael@0 | 62 | MP_CHECKOK(mp_copy(px, rx)); |
michael@0 | 63 | MP_CHECKOK(mp_copy(py, ry)); |
michael@0 | 64 | } else { |
michael@0 | 65 | MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth)); |
michael@0 | 66 | MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth)); |
michael@0 | 67 | MP_CHECKOK(group->meth->field_mul(&z1, &z2, &z3, group->meth)); |
michael@0 | 68 | MP_CHECKOK(group->meth->field_mul(px, &z2, rx, group->meth)); |
michael@0 | 69 | MP_CHECKOK(group->meth->field_mul(py, &z3, ry, group->meth)); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | CLEANUP: |
michael@0 | 73 | mp_clear(&z1); |
michael@0 | 74 | mp_clear(&z2); |
michael@0 | 75 | mp_clear(&z3); |
michael@0 | 76 | return res; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | /* Checks if point P(px, py, pz) is at infinity. Uses Jacobian |
michael@0 | 80 | * coordinates. */ |
michael@0 | 81 | mp_err |
michael@0 | 82 | ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py, const mp_int *pz) |
michael@0 | 83 | { |
michael@0 | 84 | return mp_cmp_z(pz); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /* Sets P(px, py, pz) to be the point at infinity. Uses Jacobian |
michael@0 | 88 | * coordinates. */ |
michael@0 | 89 | mp_err |
michael@0 | 90 | ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz) |
michael@0 | 91 | { |
michael@0 | 92 | mp_zero(pz); |
michael@0 | 93 | return MP_OKAY; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is |
michael@0 | 97 | * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical. |
michael@0 | 98 | * Uses mixed Jacobian-affine coordinates. Assumes input is already |
michael@0 | 99 | * field-encoded using field_enc, and returns output that is still |
michael@0 | 100 | * field-encoded. Uses equation (2) from Brown, Hankerson, Lopez, and |
michael@0 | 101 | * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime |
michael@0 | 102 | * Fields. */ |
michael@0 | 103 | mp_err |
michael@0 | 104 | ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py, const mp_int *pz, |
michael@0 | 105 | const mp_int *qx, const mp_int *qy, mp_int *rx, |
michael@0 | 106 | mp_int *ry, mp_int *rz, const ECGroup *group) |
michael@0 | 107 | { |
michael@0 | 108 | mp_err res = MP_OKAY; |
michael@0 | 109 | mp_int A, B, C, D, C2, C3; |
michael@0 | 110 | |
michael@0 | 111 | MP_DIGITS(&A) = 0; |
michael@0 | 112 | MP_DIGITS(&B) = 0; |
michael@0 | 113 | MP_DIGITS(&C) = 0; |
michael@0 | 114 | MP_DIGITS(&D) = 0; |
michael@0 | 115 | MP_DIGITS(&C2) = 0; |
michael@0 | 116 | MP_DIGITS(&C3) = 0; |
michael@0 | 117 | MP_CHECKOK(mp_init(&A)); |
michael@0 | 118 | MP_CHECKOK(mp_init(&B)); |
michael@0 | 119 | MP_CHECKOK(mp_init(&C)); |
michael@0 | 120 | MP_CHECKOK(mp_init(&D)); |
michael@0 | 121 | MP_CHECKOK(mp_init(&C2)); |
michael@0 | 122 | MP_CHECKOK(mp_init(&C3)); |
michael@0 | 123 | |
michael@0 | 124 | /* If either P or Q is the point at infinity, then return the other |
michael@0 | 125 | * point */ |
michael@0 | 126 | if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { |
michael@0 | 127 | MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group)); |
michael@0 | 128 | goto CLEANUP; |
michael@0 | 129 | } |
michael@0 | 130 | if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) { |
michael@0 | 131 | MP_CHECKOK(mp_copy(px, rx)); |
michael@0 | 132 | MP_CHECKOK(mp_copy(py, ry)); |
michael@0 | 133 | MP_CHECKOK(mp_copy(pz, rz)); |
michael@0 | 134 | goto CLEANUP; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | /* A = qx * pz^2, B = qy * pz^3 */ |
michael@0 | 138 | MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth)); |
michael@0 | 139 | MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth)); |
michael@0 | 140 | MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth)); |
michael@0 | 141 | MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth)); |
michael@0 | 142 | |
michael@0 | 143 | /* C = A - px, D = B - py */ |
michael@0 | 144 | MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth)); |
michael@0 | 145 | MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth)); |
michael@0 | 146 | |
michael@0 | 147 | /* C2 = C^2, C3 = C^3 */ |
michael@0 | 148 | MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth)); |
michael@0 | 149 | MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth)); |
michael@0 | 150 | |
michael@0 | 151 | /* rz = pz * C */ |
michael@0 | 152 | MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth)); |
michael@0 | 153 | |
michael@0 | 154 | /* C = px * C^2 */ |
michael@0 | 155 | MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth)); |
michael@0 | 156 | /* A = D^2 */ |
michael@0 | 157 | MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth)); |
michael@0 | 158 | |
michael@0 | 159 | /* rx = D^2 - (C^3 + 2 * (px * C^2)) */ |
michael@0 | 160 | MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth)); |
michael@0 | 161 | MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth)); |
michael@0 | 162 | MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth)); |
michael@0 | 163 | |
michael@0 | 164 | /* C3 = py * C^3 */ |
michael@0 | 165 | MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth)); |
michael@0 | 166 | |
michael@0 | 167 | /* ry = D * (px * C^2 - rx) - py * C^3 */ |
michael@0 | 168 | MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth)); |
michael@0 | 169 | MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth)); |
michael@0 | 170 | MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth)); |
michael@0 | 171 | |
michael@0 | 172 | CLEANUP: |
michael@0 | 173 | mp_clear(&A); |
michael@0 | 174 | mp_clear(&B); |
michael@0 | 175 | mp_clear(&C); |
michael@0 | 176 | mp_clear(&D); |
michael@0 | 177 | mp_clear(&C2); |
michael@0 | 178 | mp_clear(&C3); |
michael@0 | 179 | return res; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses |
michael@0 | 183 | * Jacobian coordinates. |
michael@0 | 184 | * |
michael@0 | 185 | * Assumes input is already field-encoded using field_enc, and returns |
michael@0 | 186 | * output that is still field-encoded. |
michael@0 | 187 | * |
michael@0 | 188 | * This routine implements Point Doubling in the Jacobian Projective |
michael@0 | 189 | * space as described in the paper "Efficient elliptic curve exponentiation |
michael@0 | 190 | * using mixed coordinates", by H. Cohen, A Miyaji, T. Ono. |
michael@0 | 191 | */ |
michael@0 | 192 | mp_err |
michael@0 | 193 | ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py, const mp_int *pz, |
michael@0 | 194 | mp_int *rx, mp_int *ry, mp_int *rz, const ECGroup *group) |
michael@0 | 195 | { |
michael@0 | 196 | mp_err res = MP_OKAY; |
michael@0 | 197 | mp_int t0, t1, M, S; |
michael@0 | 198 | |
michael@0 | 199 | MP_DIGITS(&t0) = 0; |
michael@0 | 200 | MP_DIGITS(&t1) = 0; |
michael@0 | 201 | MP_DIGITS(&M) = 0; |
michael@0 | 202 | MP_DIGITS(&S) = 0; |
michael@0 | 203 | MP_CHECKOK(mp_init(&t0)); |
michael@0 | 204 | MP_CHECKOK(mp_init(&t1)); |
michael@0 | 205 | MP_CHECKOK(mp_init(&M)); |
michael@0 | 206 | MP_CHECKOK(mp_init(&S)); |
michael@0 | 207 | |
michael@0 | 208 | if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { |
michael@0 | 209 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); |
michael@0 | 210 | goto CLEANUP; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | if (mp_cmp_d(pz, 1) == 0) { |
michael@0 | 214 | /* M = 3 * px^2 + a */ |
michael@0 | 215 | MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth)); |
michael@0 | 216 | MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth)); |
michael@0 | 217 | MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth)); |
michael@0 | 218 | MP_CHECKOK(group->meth-> |
michael@0 | 219 | field_add(&t0, &group->curvea, &M, group->meth)); |
michael@0 | 220 | } else if (mp_cmp_int(&group->curvea, -3) == 0) { |
michael@0 | 221 | /* M = 3 * (px + pz^2) * (px - pz^2) */ |
michael@0 | 222 | MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth)); |
michael@0 | 223 | MP_CHECKOK(group->meth->field_add(px, &M, &t0, group->meth)); |
michael@0 | 224 | MP_CHECKOK(group->meth->field_sub(px, &M, &t1, group->meth)); |
michael@0 | 225 | MP_CHECKOK(group->meth->field_mul(&t0, &t1, &M, group->meth)); |
michael@0 | 226 | MP_CHECKOK(group->meth->field_add(&M, &M, &t0, group->meth)); |
michael@0 | 227 | MP_CHECKOK(group->meth->field_add(&t0, &M, &M, group->meth)); |
michael@0 | 228 | } else { |
michael@0 | 229 | /* M = 3 * (px^2) + a * (pz^4) */ |
michael@0 | 230 | MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth)); |
michael@0 | 231 | MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth)); |
michael@0 | 232 | MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth)); |
michael@0 | 233 | MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth)); |
michael@0 | 234 | MP_CHECKOK(group->meth->field_sqr(&M, &M, group->meth)); |
michael@0 | 235 | MP_CHECKOK(group->meth-> |
michael@0 | 236 | field_mul(&M, &group->curvea, &M, group->meth)); |
michael@0 | 237 | MP_CHECKOK(group->meth->field_add(&M, &t0, &M, group->meth)); |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | /* rz = 2 * py * pz */ |
michael@0 | 241 | /* t0 = 4 * py^2 */ |
michael@0 | 242 | if (mp_cmp_d(pz, 1) == 0) { |
michael@0 | 243 | MP_CHECKOK(group->meth->field_add(py, py, rz, group->meth)); |
michael@0 | 244 | MP_CHECKOK(group->meth->field_sqr(rz, &t0, group->meth)); |
michael@0 | 245 | } else { |
michael@0 | 246 | MP_CHECKOK(group->meth->field_add(py, py, &t0, group->meth)); |
michael@0 | 247 | MP_CHECKOK(group->meth->field_mul(&t0, pz, rz, group->meth)); |
michael@0 | 248 | MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth)); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /* S = 4 * px * py^2 = px * (2 * py)^2 */ |
michael@0 | 252 | MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth)); |
michael@0 | 253 | |
michael@0 | 254 | /* rx = M^2 - 2 * S */ |
michael@0 | 255 | MP_CHECKOK(group->meth->field_add(&S, &S, &t1, group->meth)); |
michael@0 | 256 | MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth)); |
michael@0 | 257 | MP_CHECKOK(group->meth->field_sub(rx, &t1, rx, group->meth)); |
michael@0 | 258 | |
michael@0 | 259 | /* ry = M * (S - rx) - 8 * py^4 */ |
michael@0 | 260 | MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth)); |
michael@0 | 261 | if (mp_isodd(&t1)) { |
michael@0 | 262 | MP_CHECKOK(mp_add(&t1, &group->meth->irr, &t1)); |
michael@0 | 263 | } |
michael@0 | 264 | MP_CHECKOK(mp_div_2(&t1, &t1)); |
michael@0 | 265 | MP_CHECKOK(group->meth->field_sub(&S, rx, &S, group->meth)); |
michael@0 | 266 | MP_CHECKOK(group->meth->field_mul(&M, &S, &M, group->meth)); |
michael@0 | 267 | MP_CHECKOK(group->meth->field_sub(&M, &t1, ry, group->meth)); |
michael@0 | 268 | |
michael@0 | 269 | CLEANUP: |
michael@0 | 270 | mp_clear(&t0); |
michael@0 | 271 | mp_clear(&t1); |
michael@0 | 272 | mp_clear(&M); |
michael@0 | 273 | mp_clear(&S); |
michael@0 | 274 | return res; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | /* by default, this routine is unused and thus doesn't need to be compiled */ |
michael@0 | 278 | #ifdef ECL_ENABLE_GFP_PT_MUL_JAC |
michael@0 | 279 | /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters |
michael@0 | 280 | * a, b and p are the elliptic curve coefficients and the prime that |
michael@0 | 281 | * determines the field GFp. Elliptic curve points P and R can be |
michael@0 | 282 | * identical. Uses mixed Jacobian-affine coordinates. Assumes input is |
michael@0 | 283 | * already field-encoded using field_enc, and returns output that is still |
michael@0 | 284 | * field-encoded. Uses 4-bit window method. */ |
michael@0 | 285 | mp_err |
michael@0 | 286 | ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px, const mp_int *py, |
michael@0 | 287 | mp_int *rx, mp_int *ry, const ECGroup *group) |
michael@0 | 288 | { |
michael@0 | 289 | mp_err res = MP_OKAY; |
michael@0 | 290 | mp_int precomp[16][2], rz; |
michael@0 | 291 | int i, ni, d; |
michael@0 | 292 | |
michael@0 | 293 | MP_DIGITS(&rz) = 0; |
michael@0 | 294 | for (i = 0; i < 16; i++) { |
michael@0 | 295 | MP_DIGITS(&precomp[i][0]) = 0; |
michael@0 | 296 | MP_DIGITS(&precomp[i][1]) = 0; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 300 | ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG); |
michael@0 | 301 | |
michael@0 | 302 | /* initialize precomputation table */ |
michael@0 | 303 | for (i = 0; i < 16; i++) { |
michael@0 | 304 | MP_CHECKOK(mp_init(&precomp[i][0])); |
michael@0 | 305 | MP_CHECKOK(mp_init(&precomp[i][1])); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | /* fill precomputation table */ |
michael@0 | 309 | mp_zero(&precomp[0][0]); |
michael@0 | 310 | mp_zero(&precomp[0][1]); |
michael@0 | 311 | MP_CHECKOK(mp_copy(px, &precomp[1][0])); |
michael@0 | 312 | MP_CHECKOK(mp_copy(py, &precomp[1][1])); |
michael@0 | 313 | for (i = 2; i < 16; i++) { |
michael@0 | 314 | MP_CHECKOK(group-> |
michael@0 | 315 | point_add(&precomp[1][0], &precomp[1][1], |
michael@0 | 316 | &precomp[i - 1][0], &precomp[i - 1][1], |
michael@0 | 317 | &precomp[i][0], &precomp[i][1], group)); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | d = (mpl_significant_bits(n) + 3) / 4; |
michael@0 | 321 | |
michael@0 | 322 | /* R = inf */ |
michael@0 | 323 | MP_CHECKOK(mp_init(&rz)); |
michael@0 | 324 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); |
michael@0 | 325 | |
michael@0 | 326 | for (i = d - 1; i >= 0; i--) { |
michael@0 | 327 | /* compute window ni */ |
michael@0 | 328 | ni = MP_GET_BIT(n, 4 * i + 3); |
michael@0 | 329 | ni <<= 1; |
michael@0 | 330 | ni |= MP_GET_BIT(n, 4 * i + 2); |
michael@0 | 331 | ni <<= 1; |
michael@0 | 332 | ni |= MP_GET_BIT(n, 4 * i + 1); |
michael@0 | 333 | ni <<= 1; |
michael@0 | 334 | ni |= MP_GET_BIT(n, 4 * i); |
michael@0 | 335 | /* R = 2^4 * R */ |
michael@0 | 336 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 337 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 338 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 339 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 340 | /* R = R + (ni * P) */ |
michael@0 | 341 | MP_CHECKOK(ec_GFp_pt_add_jac_aff |
michael@0 | 342 | (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry, |
michael@0 | 343 | &rz, group)); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | /* convert result S to affine coordinates */ |
michael@0 | 347 | MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group)); |
michael@0 | 348 | |
michael@0 | 349 | CLEANUP: |
michael@0 | 350 | mp_clear(&rz); |
michael@0 | 351 | for (i = 0; i < 16; i++) { |
michael@0 | 352 | mp_clear(&precomp[i][0]); |
michael@0 | 353 | mp_clear(&precomp[i][1]); |
michael@0 | 354 | } |
michael@0 | 355 | return res; |
michael@0 | 356 | } |
michael@0 | 357 | #endif |
michael@0 | 358 | |
michael@0 | 359 | /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + |
michael@0 | 360 | * k2 * P(x, y), where G is the generator (base point) of the group of |
michael@0 | 361 | * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. |
michael@0 | 362 | * Uses mixed Jacobian-affine coordinates. Input and output values are |
michael@0 | 363 | * assumed to be NOT field-encoded. Uses algorithm 15 (simultaneous |
michael@0 | 364 | * multiple point multiplication) from Brown, Hankerson, Lopez, Menezes. |
michael@0 | 365 | * Software Implementation of the NIST Elliptic Curves over Prime Fields. */ |
michael@0 | 366 | mp_err |
michael@0 | 367 | ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px, |
michael@0 | 368 | const mp_int *py, mp_int *rx, mp_int *ry, |
michael@0 | 369 | const ECGroup *group) |
michael@0 | 370 | { |
michael@0 | 371 | mp_err res = MP_OKAY; |
michael@0 | 372 | mp_int precomp[4][4][2]; |
michael@0 | 373 | mp_int rz; |
michael@0 | 374 | const mp_int *a, *b; |
michael@0 | 375 | int i, j; |
michael@0 | 376 | int ai, bi, d; |
michael@0 | 377 | |
michael@0 | 378 | for (i = 0; i < 4; i++) { |
michael@0 | 379 | for (j = 0; j < 4; j++) { |
michael@0 | 380 | MP_DIGITS(&precomp[i][j][0]) = 0; |
michael@0 | 381 | MP_DIGITS(&precomp[i][j][1]) = 0; |
michael@0 | 382 | } |
michael@0 | 383 | } |
michael@0 | 384 | MP_DIGITS(&rz) = 0; |
michael@0 | 385 | |
michael@0 | 386 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 387 | ARGCHK(!((k1 == NULL) |
michael@0 | 388 | && ((k2 == NULL) || (px == NULL) |
michael@0 | 389 | || (py == NULL))), MP_BADARG); |
michael@0 | 390 | |
michael@0 | 391 | /* if some arguments are not defined used ECPoint_mul */ |
michael@0 | 392 | if (k1 == NULL) { |
michael@0 | 393 | return ECPoint_mul(group, k2, px, py, rx, ry); |
michael@0 | 394 | } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { |
michael@0 | 395 | return ECPoint_mul(group, k1, NULL, NULL, rx, ry); |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | /* initialize precomputation table */ |
michael@0 | 399 | for (i = 0; i < 4; i++) { |
michael@0 | 400 | for (j = 0; j < 4; j++) { |
michael@0 | 401 | MP_CHECKOK(mp_init(&precomp[i][j][0])); |
michael@0 | 402 | MP_CHECKOK(mp_init(&precomp[i][j][1])); |
michael@0 | 403 | } |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | /* fill precomputation table */ |
michael@0 | 407 | /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */ |
michael@0 | 408 | if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) { |
michael@0 | 409 | a = k2; |
michael@0 | 410 | b = k1; |
michael@0 | 411 | if (group->meth->field_enc) { |
michael@0 | 412 | MP_CHECKOK(group->meth-> |
michael@0 | 413 | field_enc(px, &precomp[1][0][0], group->meth)); |
michael@0 | 414 | MP_CHECKOK(group->meth-> |
michael@0 | 415 | field_enc(py, &precomp[1][0][1], group->meth)); |
michael@0 | 416 | } else { |
michael@0 | 417 | MP_CHECKOK(mp_copy(px, &precomp[1][0][0])); |
michael@0 | 418 | MP_CHECKOK(mp_copy(py, &precomp[1][0][1])); |
michael@0 | 419 | } |
michael@0 | 420 | MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0])); |
michael@0 | 421 | MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1])); |
michael@0 | 422 | } else { |
michael@0 | 423 | a = k1; |
michael@0 | 424 | b = k2; |
michael@0 | 425 | MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0])); |
michael@0 | 426 | MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1])); |
michael@0 | 427 | if (group->meth->field_enc) { |
michael@0 | 428 | MP_CHECKOK(group->meth-> |
michael@0 | 429 | field_enc(px, &precomp[0][1][0], group->meth)); |
michael@0 | 430 | MP_CHECKOK(group->meth-> |
michael@0 | 431 | field_enc(py, &precomp[0][1][1], group->meth)); |
michael@0 | 432 | } else { |
michael@0 | 433 | MP_CHECKOK(mp_copy(px, &precomp[0][1][0])); |
michael@0 | 434 | MP_CHECKOK(mp_copy(py, &precomp[0][1][1])); |
michael@0 | 435 | } |
michael@0 | 436 | } |
michael@0 | 437 | /* precompute [*][0][*] */ |
michael@0 | 438 | mp_zero(&precomp[0][0][0]); |
michael@0 | 439 | mp_zero(&precomp[0][0][1]); |
michael@0 | 440 | MP_CHECKOK(group-> |
michael@0 | 441 | point_dbl(&precomp[1][0][0], &precomp[1][0][1], |
michael@0 | 442 | &precomp[2][0][0], &precomp[2][0][1], group)); |
michael@0 | 443 | MP_CHECKOK(group-> |
michael@0 | 444 | point_add(&precomp[1][0][0], &precomp[1][0][1], |
michael@0 | 445 | &precomp[2][0][0], &precomp[2][0][1], |
michael@0 | 446 | &precomp[3][0][0], &precomp[3][0][1], group)); |
michael@0 | 447 | /* precompute [*][1][*] */ |
michael@0 | 448 | for (i = 1; i < 4; i++) { |
michael@0 | 449 | MP_CHECKOK(group-> |
michael@0 | 450 | point_add(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 451 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 452 | &precomp[i][1][0], &precomp[i][1][1], group)); |
michael@0 | 453 | } |
michael@0 | 454 | /* precompute [*][2][*] */ |
michael@0 | 455 | MP_CHECKOK(group-> |
michael@0 | 456 | point_dbl(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 457 | &precomp[0][2][0], &precomp[0][2][1], group)); |
michael@0 | 458 | for (i = 1; i < 4; i++) { |
michael@0 | 459 | MP_CHECKOK(group-> |
michael@0 | 460 | point_add(&precomp[0][2][0], &precomp[0][2][1], |
michael@0 | 461 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 462 | &precomp[i][2][0], &precomp[i][2][1], group)); |
michael@0 | 463 | } |
michael@0 | 464 | /* precompute [*][3][*] */ |
michael@0 | 465 | MP_CHECKOK(group-> |
michael@0 | 466 | point_add(&precomp[0][1][0], &precomp[0][1][1], |
michael@0 | 467 | &precomp[0][2][0], &precomp[0][2][1], |
michael@0 | 468 | &precomp[0][3][0], &precomp[0][3][1], group)); |
michael@0 | 469 | for (i = 1; i < 4; i++) { |
michael@0 | 470 | MP_CHECKOK(group-> |
michael@0 | 471 | point_add(&precomp[0][3][0], &precomp[0][3][1], |
michael@0 | 472 | &precomp[i][0][0], &precomp[i][0][1], |
michael@0 | 473 | &precomp[i][3][0], &precomp[i][3][1], group)); |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | d = (mpl_significant_bits(a) + 1) / 2; |
michael@0 | 477 | |
michael@0 | 478 | /* R = inf */ |
michael@0 | 479 | MP_CHECKOK(mp_init(&rz)); |
michael@0 | 480 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); |
michael@0 | 481 | |
michael@0 | 482 | for (i = d - 1; i >= 0; i--) { |
michael@0 | 483 | ai = MP_GET_BIT(a, 2 * i + 1); |
michael@0 | 484 | ai <<= 1; |
michael@0 | 485 | ai |= MP_GET_BIT(a, 2 * i); |
michael@0 | 486 | bi = MP_GET_BIT(b, 2 * i + 1); |
michael@0 | 487 | bi <<= 1; |
michael@0 | 488 | bi |= MP_GET_BIT(b, 2 * i); |
michael@0 | 489 | /* R = 2^2 * R */ |
michael@0 | 490 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 491 | MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group)); |
michael@0 | 492 | /* R = R + (ai * A + bi * B) */ |
michael@0 | 493 | MP_CHECKOK(ec_GFp_pt_add_jac_aff |
michael@0 | 494 | (rx, ry, &rz, &precomp[ai][bi][0], &precomp[ai][bi][1], |
michael@0 | 495 | rx, ry, &rz, group)); |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group)); |
michael@0 | 499 | |
michael@0 | 500 | if (group->meth->field_dec) { |
michael@0 | 501 | MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); |
michael@0 | 502 | MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | CLEANUP: |
michael@0 | 506 | mp_clear(&rz); |
michael@0 | 507 | for (i = 0; i < 4; i++) { |
michael@0 | 508 | for (j = 0; j < 4; j++) { |
michael@0 | 509 | mp_clear(&precomp[i][j][0]); |
michael@0 | 510 | mp_clear(&precomp[i][j][1]); |
michael@0 | 511 | } |
michael@0 | 512 | } |
michael@0 | 513 | return res; |
michael@0 | 514 | } |