Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 "ecl-priv.h" |
michael@0 | 7 | #include "mplogic.h" |
michael@0 | 8 | #include <stdlib.h> |
michael@0 | 9 | |
michael@0 | 10 | #define MAX_SCRATCH 6 |
michael@0 | 11 | |
michael@0 | 12 | /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses |
michael@0 | 13 | * Modified Jacobian coordinates. |
michael@0 | 14 | * |
michael@0 | 15 | * Assumes input is already field-encoded using field_enc, and returns |
michael@0 | 16 | * output that is still field-encoded. |
michael@0 | 17 | * |
michael@0 | 18 | */ |
michael@0 | 19 | mp_err |
michael@0 | 20 | ec_GFp_pt_dbl_jm(const mp_int *px, const mp_int *py, const mp_int *pz, |
michael@0 | 21 | const mp_int *paz4, mp_int *rx, mp_int *ry, mp_int *rz, |
michael@0 | 22 | mp_int *raz4, mp_int scratch[], const ECGroup *group) |
michael@0 | 23 | { |
michael@0 | 24 | mp_err res = MP_OKAY; |
michael@0 | 25 | mp_int *t0, *t1, *M, *S; |
michael@0 | 26 | |
michael@0 | 27 | t0 = &scratch[0]; |
michael@0 | 28 | t1 = &scratch[1]; |
michael@0 | 29 | M = &scratch[2]; |
michael@0 | 30 | S = &scratch[3]; |
michael@0 | 31 | |
michael@0 | 32 | #if MAX_SCRATCH < 4 |
michael@0 | 33 | #error "Scratch array defined too small " |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | /* Check for point at infinity */ |
michael@0 | 37 | if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { |
michael@0 | 38 | /* Set r = pt at infinity by setting rz = 0 */ |
michael@0 | 39 | |
michael@0 | 40 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz)); |
michael@0 | 41 | goto CLEANUP; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | /* M = 3 (px^2) + a*(pz^4) */ |
michael@0 | 45 | MP_CHECKOK(group->meth->field_sqr(px, t0, group->meth)); |
michael@0 | 46 | MP_CHECKOK(group->meth->field_add(t0, t0, M, group->meth)); |
michael@0 | 47 | MP_CHECKOK(group->meth->field_add(t0, M, t0, group->meth)); |
michael@0 | 48 | MP_CHECKOK(group->meth->field_add(t0, paz4, M, group->meth)); |
michael@0 | 49 | |
michael@0 | 50 | /* rz = 2 * py * pz */ |
michael@0 | 51 | MP_CHECKOK(group->meth->field_mul(py, pz, S, group->meth)); |
michael@0 | 52 | MP_CHECKOK(group->meth->field_add(S, S, rz, group->meth)); |
michael@0 | 53 | |
michael@0 | 54 | /* t0 = 2y^2 , t1 = 8y^4 */ |
michael@0 | 55 | MP_CHECKOK(group->meth->field_sqr(py, t0, group->meth)); |
michael@0 | 56 | MP_CHECKOK(group->meth->field_add(t0, t0, t0, group->meth)); |
michael@0 | 57 | MP_CHECKOK(group->meth->field_sqr(t0, t1, group->meth)); |
michael@0 | 58 | MP_CHECKOK(group->meth->field_add(t1, t1, t1, group->meth)); |
michael@0 | 59 | |
michael@0 | 60 | /* S = 4 * px * py^2 = 2 * px * t0 */ |
michael@0 | 61 | MP_CHECKOK(group->meth->field_mul(px, t0, S, group->meth)); |
michael@0 | 62 | MP_CHECKOK(group->meth->field_add(S, S, S, group->meth)); |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | /* rx = M^2 - 2S */ |
michael@0 | 66 | MP_CHECKOK(group->meth->field_sqr(M, rx, group->meth)); |
michael@0 | 67 | MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth)); |
michael@0 | 68 | MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth)); |
michael@0 | 69 | |
michael@0 | 70 | /* ry = M * (S - rx) - t1 */ |
michael@0 | 71 | MP_CHECKOK(group->meth->field_sub(S, rx, S, group->meth)); |
michael@0 | 72 | MP_CHECKOK(group->meth->field_mul(S, M, ry, group->meth)); |
michael@0 | 73 | MP_CHECKOK(group->meth->field_sub(ry, t1, ry, group->meth)); |
michael@0 | 74 | |
michael@0 | 75 | /* ra*z^4 = 2*t1*(apz4) */ |
michael@0 | 76 | MP_CHECKOK(group->meth->field_mul(paz4, t1, raz4, group->meth)); |
michael@0 | 77 | MP_CHECKOK(group->meth->field_add(raz4, raz4, raz4, group->meth)); |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | CLEANUP: |
michael@0 | 81 | return res; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is |
michael@0 | 85 | * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical. |
michael@0 | 86 | * Uses mixed Modified_Jacobian-affine coordinates. Assumes input is |
michael@0 | 87 | * already field-encoded using field_enc, and returns output that is still |
michael@0 | 88 | * field-encoded. */ |
michael@0 | 89 | mp_err |
michael@0 | 90 | ec_GFp_pt_add_jm_aff(const mp_int *px, const mp_int *py, const mp_int *pz, |
michael@0 | 91 | const mp_int *paz4, const mp_int *qx, |
michael@0 | 92 | const mp_int *qy, mp_int *rx, mp_int *ry, mp_int *rz, |
michael@0 | 93 | mp_int *raz4, mp_int scratch[], const ECGroup *group) |
michael@0 | 94 | { |
michael@0 | 95 | mp_err res = MP_OKAY; |
michael@0 | 96 | mp_int *A, *B, *C, *D, *C2, *C3; |
michael@0 | 97 | |
michael@0 | 98 | A = &scratch[0]; |
michael@0 | 99 | B = &scratch[1]; |
michael@0 | 100 | C = &scratch[2]; |
michael@0 | 101 | D = &scratch[3]; |
michael@0 | 102 | C2 = &scratch[4]; |
michael@0 | 103 | C3 = &scratch[5]; |
michael@0 | 104 | |
michael@0 | 105 | #if MAX_SCRATCH < 6 |
michael@0 | 106 | #error "Scratch array defined too small " |
michael@0 | 107 | #endif |
michael@0 | 108 | |
michael@0 | 109 | /* If either P or Q is the point at infinity, then return the other |
michael@0 | 110 | * point */ |
michael@0 | 111 | if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) { |
michael@0 | 112 | MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group)); |
michael@0 | 113 | MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth)); |
michael@0 | 114 | MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth)); |
michael@0 | 115 | MP_CHECKOK(group->meth-> |
michael@0 | 116 | field_mul(raz4, &group->curvea, raz4, group->meth)); |
michael@0 | 117 | goto CLEANUP; |
michael@0 | 118 | } |
michael@0 | 119 | if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) { |
michael@0 | 120 | MP_CHECKOK(mp_copy(px, rx)); |
michael@0 | 121 | MP_CHECKOK(mp_copy(py, ry)); |
michael@0 | 122 | MP_CHECKOK(mp_copy(pz, rz)); |
michael@0 | 123 | MP_CHECKOK(mp_copy(paz4, raz4)); |
michael@0 | 124 | goto CLEANUP; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /* A = qx * pz^2, B = qy * pz^3 */ |
michael@0 | 128 | MP_CHECKOK(group->meth->field_sqr(pz, A, group->meth)); |
michael@0 | 129 | MP_CHECKOK(group->meth->field_mul(A, pz, B, group->meth)); |
michael@0 | 130 | MP_CHECKOK(group->meth->field_mul(A, qx, A, group->meth)); |
michael@0 | 131 | MP_CHECKOK(group->meth->field_mul(B, qy, B, group->meth)); |
michael@0 | 132 | |
michael@0 | 133 | /* C = A - px, D = B - py */ |
michael@0 | 134 | MP_CHECKOK(group->meth->field_sub(A, px, C, group->meth)); |
michael@0 | 135 | MP_CHECKOK(group->meth->field_sub(B, py, D, group->meth)); |
michael@0 | 136 | |
michael@0 | 137 | /* C2 = C^2, C3 = C^3 */ |
michael@0 | 138 | MP_CHECKOK(group->meth->field_sqr(C, C2, group->meth)); |
michael@0 | 139 | MP_CHECKOK(group->meth->field_mul(C, C2, C3, group->meth)); |
michael@0 | 140 | |
michael@0 | 141 | /* rz = pz * C */ |
michael@0 | 142 | MP_CHECKOK(group->meth->field_mul(pz, C, rz, group->meth)); |
michael@0 | 143 | |
michael@0 | 144 | /* C = px * C^2 */ |
michael@0 | 145 | MP_CHECKOK(group->meth->field_mul(px, C2, C, group->meth)); |
michael@0 | 146 | /* A = D^2 */ |
michael@0 | 147 | MP_CHECKOK(group->meth->field_sqr(D, A, group->meth)); |
michael@0 | 148 | |
michael@0 | 149 | /* rx = D^2 - (C^3 + 2 * (px * C^2)) */ |
michael@0 | 150 | MP_CHECKOK(group->meth->field_add(C, C, rx, group->meth)); |
michael@0 | 151 | MP_CHECKOK(group->meth->field_add(C3, rx, rx, group->meth)); |
michael@0 | 152 | MP_CHECKOK(group->meth->field_sub(A, rx, rx, group->meth)); |
michael@0 | 153 | |
michael@0 | 154 | /* C3 = py * C^3 */ |
michael@0 | 155 | MP_CHECKOK(group->meth->field_mul(py, C3, C3, group->meth)); |
michael@0 | 156 | |
michael@0 | 157 | /* ry = D * (px * C^2 - rx) - py * C^3 */ |
michael@0 | 158 | MP_CHECKOK(group->meth->field_sub(C, rx, ry, group->meth)); |
michael@0 | 159 | MP_CHECKOK(group->meth->field_mul(D, ry, ry, group->meth)); |
michael@0 | 160 | MP_CHECKOK(group->meth->field_sub(ry, C3, ry, group->meth)); |
michael@0 | 161 | |
michael@0 | 162 | /* raz4 = a * rz^4 */ |
michael@0 | 163 | MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth)); |
michael@0 | 164 | MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth)); |
michael@0 | 165 | MP_CHECKOK(group->meth-> |
michael@0 | 166 | field_mul(raz4, &group->curvea, raz4, group->meth)); |
michael@0 | 167 | CLEANUP: |
michael@0 | 168 | return res; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic |
michael@0 | 172 | * curve points P and R can be identical. Uses mixed Modified-Jacobian |
michael@0 | 173 | * co-ordinates for doubling and Chudnovsky Jacobian coordinates for |
michael@0 | 174 | * additions. Assumes input is already field-encoded using field_enc, and |
michael@0 | 175 | * returns output that is still field-encoded. Uses 5-bit window NAF |
michael@0 | 176 | * method (algorithm 11) for scalar-point multiplication from Brown, |
michael@0 | 177 | * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic |
michael@0 | 178 | * Curves Over Prime Fields. */ |
michael@0 | 179 | mp_err |
michael@0 | 180 | ec_GFp_pt_mul_jm_wNAF(const mp_int *n, const mp_int *px, const mp_int *py, |
michael@0 | 181 | mp_int *rx, mp_int *ry, const ECGroup *group) |
michael@0 | 182 | { |
michael@0 | 183 | mp_err res = MP_OKAY; |
michael@0 | 184 | mp_int precomp[16][2], rz, tpx, tpy; |
michael@0 | 185 | mp_int raz4; |
michael@0 | 186 | mp_int scratch[MAX_SCRATCH]; |
michael@0 | 187 | signed char *naf = NULL; |
michael@0 | 188 | int i, orderBitSize; |
michael@0 | 189 | |
michael@0 | 190 | MP_DIGITS(&rz) = 0; |
michael@0 | 191 | MP_DIGITS(&raz4) = 0; |
michael@0 | 192 | MP_DIGITS(&tpx) = 0; |
michael@0 | 193 | MP_DIGITS(&tpy) = 0; |
michael@0 | 194 | for (i = 0; i < 16; i++) { |
michael@0 | 195 | MP_DIGITS(&precomp[i][0]) = 0; |
michael@0 | 196 | MP_DIGITS(&precomp[i][1]) = 0; |
michael@0 | 197 | } |
michael@0 | 198 | for (i = 0; i < MAX_SCRATCH; i++) { |
michael@0 | 199 | MP_DIGITS(&scratch[i]) = 0; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | ARGCHK(group != NULL, MP_BADARG); |
michael@0 | 203 | ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG); |
michael@0 | 204 | |
michael@0 | 205 | /* initialize precomputation table */ |
michael@0 | 206 | MP_CHECKOK(mp_init(&tpx)); |
michael@0 | 207 | MP_CHECKOK(mp_init(&tpy));; |
michael@0 | 208 | MP_CHECKOK(mp_init(&rz)); |
michael@0 | 209 | MP_CHECKOK(mp_init(&raz4)); |
michael@0 | 210 | |
michael@0 | 211 | for (i = 0; i < 16; i++) { |
michael@0 | 212 | MP_CHECKOK(mp_init(&precomp[i][0])); |
michael@0 | 213 | MP_CHECKOK(mp_init(&precomp[i][1])); |
michael@0 | 214 | } |
michael@0 | 215 | for (i = 0; i < MAX_SCRATCH; i++) { |
michael@0 | 216 | MP_CHECKOK(mp_init(&scratch[i])); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | /* Set out[8] = P */ |
michael@0 | 220 | MP_CHECKOK(mp_copy(px, &precomp[8][0])); |
michael@0 | 221 | MP_CHECKOK(mp_copy(py, &precomp[8][1])); |
michael@0 | 222 | |
michael@0 | 223 | /* Set (tpx, tpy) = 2P */ |
michael@0 | 224 | MP_CHECKOK(group-> |
michael@0 | 225 | point_dbl(&precomp[8][0], &precomp[8][1], &tpx, &tpy, |
michael@0 | 226 | group)); |
michael@0 | 227 | |
michael@0 | 228 | /* Set 3P, 5P, ..., 15P */ |
michael@0 | 229 | for (i = 8; i < 15; i++) { |
michael@0 | 230 | MP_CHECKOK(group-> |
michael@0 | 231 | point_add(&precomp[i][0], &precomp[i][1], &tpx, &tpy, |
michael@0 | 232 | &precomp[i + 1][0], &precomp[i + 1][1], |
michael@0 | 233 | group)); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | /* Set -15P, -13P, ..., -P */ |
michael@0 | 237 | for (i = 0; i < 8; i++) { |
michael@0 | 238 | MP_CHECKOK(mp_copy(&precomp[15 - i][0], &precomp[i][0])); |
michael@0 | 239 | MP_CHECKOK(group->meth-> |
michael@0 | 240 | field_neg(&precomp[15 - i][1], &precomp[i][1], |
michael@0 | 241 | group->meth)); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | /* R = inf */ |
michael@0 | 245 | MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz)); |
michael@0 | 246 | |
michael@0 | 247 | orderBitSize = mpl_significant_bits(&group->order); |
michael@0 | 248 | |
michael@0 | 249 | /* Allocate memory for NAF */ |
michael@0 | 250 | naf = (signed char *) malloc(sizeof(signed char) * (orderBitSize + 1)); |
michael@0 | 251 | if (naf == NULL) { |
michael@0 | 252 | res = MP_MEM; |
michael@0 | 253 | goto CLEANUP; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | /* Compute 5NAF */ |
michael@0 | 257 | ec_compute_wNAF(naf, orderBitSize, n, 5); |
michael@0 | 258 | |
michael@0 | 259 | /* wNAF method */ |
michael@0 | 260 | for (i = orderBitSize; i >= 0; i--) { |
michael@0 | 261 | /* R = 2R */ |
michael@0 | 262 | ec_GFp_pt_dbl_jm(rx, ry, &rz, &raz4, rx, ry, &rz, |
michael@0 | 263 | &raz4, scratch, group); |
michael@0 | 264 | if (naf[i] != 0) { |
michael@0 | 265 | ec_GFp_pt_add_jm_aff(rx, ry, &rz, &raz4, |
michael@0 | 266 | &precomp[(naf[i] + 15) / 2][0], |
michael@0 | 267 | &precomp[(naf[i] + 15) / 2][1], rx, ry, |
michael@0 | 268 | &rz, &raz4, scratch, group); |
michael@0 | 269 | } |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | /* convert result S to affine coordinates */ |
michael@0 | 273 | MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group)); |
michael@0 | 274 | |
michael@0 | 275 | CLEANUP: |
michael@0 | 276 | for (i = 0; i < MAX_SCRATCH; i++) { |
michael@0 | 277 | mp_clear(&scratch[i]); |
michael@0 | 278 | } |
michael@0 | 279 | for (i = 0; i < 16; i++) { |
michael@0 | 280 | mp_clear(&precomp[i][0]); |
michael@0 | 281 | mp_clear(&precomp[i][1]); |
michael@0 | 282 | } |
michael@0 | 283 | mp_clear(&tpx); |
michael@0 | 284 | mp_clear(&tpy); |
michael@0 | 285 | mp_clear(&rz); |
michael@0 | 286 | mp_clear(&raz4); |
michael@0 | 287 | free(naf); |
michael@0 | 288 | return res; |
michael@0 | 289 | } |