1 /*
2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
3 * Use is subject to license terms.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* *********************************************************************
25 *
26 * The Original Code is the elliptic curve math library for prime field curves.
27 *
28 * The Initial Developer of the Original Code is
29 * Sun Microsystems, Inc.
30 * Portions created by the Initial Developer are Copyright (C) 2003
31 * the Initial Developer. All Rights Reserved.
32 *
33 * Contributor(s):
34 * Sheueling Chang-Shantz <sheueling.chang@sun.com>,
35 * Stephen Fung <fungstep@hotmail.com>, and
36 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
37 * Bodo Moeller <moeller@cdc.informatik.tu-darmstadt.de>,
38 * Nils Larsch <nla@trustcenter.de>, and
39 * Lenka Fibikova <fibikova@exp-math.uni-essen.de>, the OpenSSL Project
40 *
41 * Last Modified Date from the Original Code: May 2017
42 *********************************************************************** */
43
44 #include "ecp.h"
45 #include "mplogic.h"
46 #ifndef _KERNEL
47 #include <stdlib.h>
48 #endif
49 #ifdef ECL_DEBUG
50 #include <assert.h>
51 #endif
52
53 /* Converts a point P(px, py) from affine coordinates to Jacobian
54 * projective coordinates R(rx, ry, rz). Assumes input is already
55 * field-encoded using field_enc, and returns output that is still
56 * field-encoded. */
57 mp_err
ec_GFp_pt_aff2jac(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,mp_int * rz,const ECGroup * group)58 ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx,
59 mp_int *ry, mp_int *rz, const ECGroup *group)
60 {
61 mp_err res = MP_OKAY;
62
63 if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
64 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
65 } else {
66 MP_CHECKOK(mp_copy(px, rx));
67 MP_CHECKOK(mp_copy(py, ry));
68 MP_CHECKOK(mp_set_int(rz, 1));
69 if (group->meth->field_enc) {
70 MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth));
71 }
72 }
73 CLEANUP:
74 return res;
75 }
76
77 /* Converts a point P(px, py, pz) from Jacobian projective coordinates to
78 * affine coordinates R(rx, ry). P and R can share x and y coordinates.
79 * Assumes input is already field-encoded using field_enc, and returns
80 * output that is still field-encoded. */
81 mp_err
ec_GFp_pt_jac2aff(const mp_int * px,const mp_int * py,const mp_int * pz,mp_int * rx,mp_int * ry,const ECGroup * group)82 ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py, const mp_int *pz,
83 mp_int *rx, mp_int *ry, const ECGroup *group)
84 {
85 mp_err res = MP_OKAY;
86 mp_int z1, z2, z3;
87
88 MP_DIGITS(&z1) = 0;
89 MP_DIGITS(&z2) = 0;
90 MP_DIGITS(&z3) = 0;
91 MP_CHECKOK(mp_init(&z1, FLAG(px)));
92 MP_CHECKOK(mp_init(&z2, FLAG(px)));
93 MP_CHECKOK(mp_init(&z3, FLAG(px)));
94
95 /* if point at infinity, then set point at infinity and exit */
96 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
97 MP_CHECKOK(ec_GFp_pt_set_inf_aff(rx, ry));
98 goto CLEANUP;
99 }
100
101 /* transform (px, py, pz) into (px / pz^2, py / pz^3) */
102 if (mp_cmp_d(pz, 1) == 0) {
103 MP_CHECKOK(mp_copy(px, rx));
104 MP_CHECKOK(mp_copy(py, ry));
105 } else {
106 MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth));
107 MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth));
108 MP_CHECKOK(group->meth->field_mul(&z1, &z2, &z3, group->meth));
109 MP_CHECKOK(group->meth->field_mul(px, &z2, rx, group->meth));
110 MP_CHECKOK(group->meth->field_mul(py, &z3, ry, group->meth));
111 }
112
113 CLEANUP:
114 mp_clear(&z1);
115 mp_clear(&z2);
116 mp_clear(&z3);
117 return res;
118 }
119
120 /* Checks if point P(px, py, pz) is at infinity. Uses Jacobian
121 * coordinates. */
122 mp_err
ec_GFp_pt_is_inf_jac(const mp_int * px,const mp_int * py,const mp_int * pz)123 ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py, const mp_int *pz)
124 {
125 return mp_cmp_z(pz);
126 }
127
128 /* Sets P(px, py, pz) to be the point at infinity. Uses Jacobian
129 * coordinates. */
130 mp_err
ec_GFp_pt_set_inf_jac(mp_int * px,mp_int * py,mp_int * pz)131 ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz)
132 {
133 mp_zero(pz);
134 return MP_OKAY;
135 }
136
137 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
138 * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical.
139 * Uses mixed Jacobian-affine coordinates. Assumes input is already
140 * field-encoded using field_enc, and returns output that is still
141 * field-encoded. Uses equation (2) from Brown, Hankerson, Lopez, and
142 * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime
143 * Fields. */
144 mp_err
ec_GFp_pt_add_jac_aff(const mp_int * px,const mp_int * py,const mp_int * pz,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,mp_int * rz,const ECGroup * group)145 ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
146 const mp_int *qx, const mp_int *qy, mp_int *rx,
147 mp_int *ry, mp_int *rz, const ECGroup *group)
148 {
149 mp_err res = MP_OKAY;
150 mp_int A, B, C, D, C2, C3;
151
152 MP_DIGITS(&A) = 0;
153 MP_DIGITS(&B) = 0;
154 MP_DIGITS(&C) = 0;
155 MP_DIGITS(&D) = 0;
156 MP_DIGITS(&C2) = 0;
157 MP_DIGITS(&C3) = 0;
158 MP_CHECKOK(mp_init(&A, FLAG(px)));
159 MP_CHECKOK(mp_init(&B, FLAG(px)));
160 MP_CHECKOK(mp_init(&C, FLAG(px)));
161 MP_CHECKOK(mp_init(&D, FLAG(px)));
162 MP_CHECKOK(mp_init(&C2, FLAG(px)));
163 MP_CHECKOK(mp_init(&C3, FLAG(px)));
164
165 /* If either P or Q is the point at infinity, then return the other
166 * point */
167 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
168 MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
169 goto CLEANUP;
170 }
171 if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
172 MP_CHECKOK(mp_copy(px, rx));
173 MP_CHECKOK(mp_copy(py, ry));
174 MP_CHECKOK(mp_copy(pz, rz));
175 goto CLEANUP;
176 }
177
178 /* A = qx * pz^2, B = qy * pz^3 */
179 MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth));
180 MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth));
181 MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth));
182 MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth));
183
184 /*
185 * Additional checks for point equality and point at infinity
186 */
187 if (mp_cmp(px, &A) == 0 && mp_cmp(py, &B) == 0) {
188 /* POINT_DOUBLE(P) */
189 MP_CHECKOK(ec_GFp_pt_dbl_jac(px, py, pz, rx, ry, rz, group));
190 goto CLEANUP;
191 }
192
193 /* C = A - px, D = B - py */
194 MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth));
195 MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth));
196
197 /* C2 = C^2, C3 = C^3 */
198 MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth));
199 MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth));
200
201 /* rz = pz * C */
202 MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth));
203
204 /* C = px * C^2 */
205 MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth));
206 /* A = D^2 */
207 MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth));
208
209 /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
210 MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth));
211 MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth));
212 MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth));
213
214 /* C3 = py * C^3 */
215 MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth));
216
217 /* ry = D * (px * C^2 - rx) - py * C^3 */
218 MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth));
219 MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth));
220 MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth));
221
222 CLEANUP:
223 mp_clear(&A);
224 mp_clear(&B);
225 mp_clear(&C);
226 mp_clear(&D);
227 mp_clear(&C2);
228 mp_clear(&C3);
229 return res;
230 }
231
232 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
233 * Jacobian coordinates.
234 *
235 * Assumes input is already field-encoded using field_enc, and returns
236 * output that is still field-encoded.
237 *
238 * This routine implements Point Doubling in the Jacobian Projective
239 * space as described in the paper "Efficient elliptic curve exponentiation
240 * using mixed coordinates", by H. Cohen, A Miyaji, T. Ono.
241 */
242 mp_err
ec_GFp_pt_dbl_jac(const mp_int * px,const mp_int * py,const mp_int * pz,mp_int * rx,mp_int * ry,mp_int * rz,const ECGroup * group)243 ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py, const mp_int *pz,
244 mp_int *rx, mp_int *ry, mp_int *rz, const ECGroup *group)
245 {
246 mp_err res = MP_OKAY;
247 mp_int t0, t1, M, S;
248
249 MP_DIGITS(&t0) = 0;
250 MP_DIGITS(&t1) = 0;
251 MP_DIGITS(&M) = 0;
252 MP_DIGITS(&S) = 0;
253 MP_CHECKOK(mp_init(&t0, FLAG(px)));
254 MP_CHECKOK(mp_init(&t1, FLAG(px)));
255 MP_CHECKOK(mp_init(&M, FLAG(px)));
256 MP_CHECKOK(mp_init(&S, FLAG(px)));
257
258 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
259 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
260 goto CLEANUP;
261 }
262
263 if (mp_cmp_d(pz, 1) == 0) {
264 /* M = 3 * px^2 + a */
265 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
266 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
267 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
268 MP_CHECKOK(group->meth->
269 field_add(&t0, &group->curvea, &M, group->meth));
270 } else if (mp_cmp_int(&group->curvea, -3, FLAG(px)) == 0) {
271 /* M = 3 * (px + pz^2) * (px - pz^2) */
272 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
273 MP_CHECKOK(group->meth->field_add(px, &M, &t0, group->meth));
274 MP_CHECKOK(group->meth->field_sub(px, &M, &t1, group->meth));
275 MP_CHECKOK(group->meth->field_mul(&t0, &t1, &M, group->meth));
276 MP_CHECKOK(group->meth->field_add(&M, &M, &t0, group->meth));
277 MP_CHECKOK(group->meth->field_add(&t0, &M, &M, group->meth));
278 } else {
279 /* M = 3 * (px^2) + a * (pz^4) */
280 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
281 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
282 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
283 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
284 MP_CHECKOK(group->meth->field_sqr(&M, &M, group->meth));
285 MP_CHECKOK(group->meth->
286 field_mul(&M, &group->curvea, &M, group->meth));
287 MP_CHECKOK(group->meth->field_add(&M, &t0, &M, group->meth));
288 }
289
290 /* rz = 2 * py * pz */
291 /* t0 = 4 * py^2 */
292 if (mp_cmp_d(pz, 1) == 0) {
293 MP_CHECKOK(group->meth->field_add(py, py, rz, group->meth));
294 MP_CHECKOK(group->meth->field_sqr(rz, &t0, group->meth));
295 } else {
296 MP_CHECKOK(group->meth->field_add(py, py, &t0, group->meth));
297 MP_CHECKOK(group->meth->field_mul(&t0, pz, rz, group->meth));
298 MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth));
299 }
300
301 /* S = 4 * px * py^2 = px * (2 * py)^2 */
302 MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth));
303
304 /* rx = M^2 - 2 * S */
305 MP_CHECKOK(group->meth->field_add(&S, &S, &t1, group->meth));
306 MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth));
307 MP_CHECKOK(group->meth->field_sub(rx, &t1, rx, group->meth));
308
309 /* ry = M * (S - rx) - 8 * py^4 */
310 MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth));
311 if (mp_isodd(&t1)) {
312 MP_CHECKOK(mp_add(&t1, &group->meth->irr, &t1));
313 }
314 MP_CHECKOK(mp_div_2(&t1, &t1));
315 MP_CHECKOK(group->meth->field_sub(&S, rx, &S, group->meth));
316 MP_CHECKOK(group->meth->field_mul(&M, &S, &M, group->meth));
317 MP_CHECKOK(group->meth->field_sub(&M, &t1, ry, group->meth));
318
319 CLEANUP:
320 mp_clear(&t0);
321 mp_clear(&t1);
322 mp_clear(&M);
323 mp_clear(&S);
324 return res;
325 }
326
327 /* by default, this routine is unused and thus doesn't need to be compiled */
328 #ifdef ECL_ENABLE_GFP_PT_MUL_JAC
329 /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters
330 * a, b and p are the elliptic curve coefficients and the prime that
331 * determines the field GFp. Elliptic curve points P and R can be
332 * identical. Uses mixed Jacobian-affine coordinates. Assumes input is
333 * already field-encoded using field_enc, and returns output that is still
334 * field-encoded. Uses 4-bit window method. */
335 mp_err
ec_GFp_pt_mul_jac(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)336 ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px, const mp_int *py,
337 mp_int *rx, mp_int *ry, const ECGroup *group)
338 {
339 mp_err res = MP_OKAY;
340 mp_int precomp[16][2], rz;
341 int i, ni, d;
342
343 MP_DIGITS(&rz) = 0;
344 for (i = 0; i < 16; i++) {
345 MP_DIGITS(&precomp[i][0]) = 0;
346 MP_DIGITS(&precomp[i][1]) = 0;
347 }
348
349 ARGCHK(group != NULL, MP_BADARG);
350 ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
351
352 /* initialize precomputation table */
353 for (i = 0; i < 16; i++) {
354 MP_CHECKOK(mp_init(&precomp[i][0]));
355 MP_CHECKOK(mp_init(&precomp[i][1]));
356 }
357
358 /* fill precomputation table */
359 mp_zero(&precomp[0][0]);
360 mp_zero(&precomp[0][1]);
361 MP_CHECKOK(mp_copy(px, &precomp[1][0]));
362 MP_CHECKOK(mp_copy(py, &precomp[1][1]));
363 for (i = 2; i < 16; i++) {
364 MP_CHECKOK(group->
365 point_add(&precomp[1][0], &precomp[1][1],
366 &precomp[i - 1][0], &precomp[i - 1][1],
367 &precomp[i][0], &precomp[i][1], group));
368 }
369
370 d = (mpl_significant_bits(n) + 3) / 4;
371
372 /* R = inf */
373 MP_CHECKOK(mp_init(&rz));
374 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
375
376 for (i = d - 1; i >= 0; i--) {
377 /* compute window ni */
378 ni = MP_GET_BIT(n, 4 * i + 3);
379 ni <<= 1;
380 ni |= MP_GET_BIT(n, 4 * i + 2);
381 ni <<= 1;
382 ni |= MP_GET_BIT(n, 4 * i + 1);
383 ni <<= 1;
384 ni |= MP_GET_BIT(n, 4 * i);
385 /* R = 2^4 * R */
386 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
387 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
388 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
389 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
390 /* R = R + (ni * P) */
391 MP_CHECKOK(ec_GFp_pt_add_jac_aff
392 (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry,
393 &rz, group));
394 }
395
396 /* convert result S to affine coordinates */
397 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
398
399 CLEANUP:
400 mp_clear(&rz);
401 for (i = 0; i < 16; i++) {
402 mp_clear(&precomp[i][0]);
403 mp_clear(&precomp[i][1]);
404 }
405 return res;
406 }
407 #endif
408
409 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
410 * k2 * P(x, y), where G is the generator (base point) of the group of
411 * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
412 * Uses mixed Jacobian-affine coordinates. Input and output values are
413 * assumed to be NOT field-encoded. Uses algorithm 15 (simultaneous
414 * multiple point multiplication) from Brown, Hankerson, Lopez, Menezes.
415 * Software Implementation of the NIST Elliptic Curves over Prime Fields. */
416 mp_err
ec_GFp_pts_mul_jac(const mp_int * k1,const mp_int * k2,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group,int timing)417 ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px,
418 const mp_int *py, mp_int *rx, mp_int *ry,
419 const ECGroup *group, int timing)
420 {
421 mp_err res = MP_OKAY;
422 mp_int precomp[4][4][2];
423 mp_int rz;
424 const mp_int *a, *b;
425 int i, j;
426 int ai, bi, d;
427
428 for (i = 0; i < 4; i++) {
429 for (j = 0; j < 4; j++) {
430 MP_DIGITS(&precomp[i][j][0]) = 0;
431 MP_DIGITS(&precomp[i][j][1]) = 0;
432 }
433 }
434 MP_DIGITS(&rz) = 0;
435
436 ARGCHK(group != NULL, MP_BADARG);
437 ARGCHK(!((k1 == NULL)
438 && ((k2 == NULL) || (px == NULL)
439 || (py == NULL))), MP_BADARG);
440
441 /* if some arguments are not defined used ECPoint_mul */
442 if (k1 == NULL) {
443 return ECPoint_mul(group, k2, px, py, rx, ry, timing);
444 } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
445 return ECPoint_mul(group, k1, NULL, NULL, rx, ry, timing);
446 }
447
448 /* initialize precomputation table */
449 for (i = 0; i < 4; i++) {
450 for (j = 0; j < 4; j++) {
451 MP_CHECKOK(mp_init(&precomp[i][j][0], FLAG(k1)));
452 MP_CHECKOK(mp_init(&precomp[i][j][1], FLAG(k1)));
453 }
454 }
455
456 /* fill precomputation table */
457 /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */
458 if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) {
459 a = k2;
460 b = k1;
461 if (group->meth->field_enc) {
462 MP_CHECKOK(group->meth->
463 field_enc(px, &precomp[1][0][0], group->meth));
464 MP_CHECKOK(group->meth->
465 field_enc(py, &precomp[1][0][1], group->meth));
466 } else {
467 MP_CHECKOK(mp_copy(px, &precomp[1][0][0]));
468 MP_CHECKOK(mp_copy(py, &precomp[1][0][1]));
469 }
470 MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0]));
471 MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1]));
472 } else {
473 a = k1;
474 b = k2;
475 MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0]));
476 MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1]));
477 if (group->meth->field_enc) {
478 MP_CHECKOK(group->meth->
479 field_enc(px, &precomp[0][1][0], group->meth));
480 MP_CHECKOK(group->meth->
481 field_enc(py, &precomp[0][1][1], group->meth));
482 } else {
483 MP_CHECKOK(mp_copy(px, &precomp[0][1][0]));
484 MP_CHECKOK(mp_copy(py, &precomp[0][1][1]));
485 }
486 }
487 /* precompute [*][0][*] */
488 mp_zero(&precomp[0][0][0]);
489 mp_zero(&precomp[0][0][1]);
490 MP_CHECKOK(group->
491 point_dbl(&precomp[1][0][0], &precomp[1][0][1],
492 &precomp[2][0][0], &precomp[2][0][1], group));
493 MP_CHECKOK(group->
494 point_add(&precomp[1][0][0], &precomp[1][0][1],
495 &precomp[2][0][0], &precomp[2][0][1],
496 &precomp[3][0][0], &precomp[3][0][1], group));
497 /* precompute [*][1][*] */
498 for (i = 1; i < 4; i++) {
499 MP_CHECKOK(group->
500 point_add(&precomp[0][1][0], &precomp[0][1][1],
501 &precomp[i][0][0], &precomp[i][0][1],
502 &precomp[i][1][0], &precomp[i][1][1], group));
503 }
504 /* precompute [*][2][*] */
505 MP_CHECKOK(group->
506 point_dbl(&precomp[0][1][0], &precomp[0][1][1],
507 &precomp[0][2][0], &precomp[0][2][1], group));
508 for (i = 1; i < 4; i++) {
509 MP_CHECKOK(group->
510 point_add(&precomp[0][2][0], &precomp[0][2][1],
511 &precomp[i][0][0], &precomp[i][0][1],
512 &precomp[i][2][0], &precomp[i][2][1], group));
513 }
514 /* precompute [*][3][*] */
515 MP_CHECKOK(group->
516 point_add(&precomp[0][1][0], &precomp[0][1][1],
517 &precomp[0][2][0], &precomp[0][2][1],
518 &precomp[0][3][0], &precomp[0][3][1], group));
519 for (i = 1; i < 4; i++) {
520 MP_CHECKOK(group->
521 point_add(&precomp[0][3][0], &precomp[0][3][1],
522 &precomp[i][0][0], &precomp[i][0][1],
523 &precomp[i][3][0], &precomp[i][3][1], group));
524 }
525
526 d = (mpl_significant_bits(a) + 1) / 2;
527
528 /* R = inf */
529 MP_CHECKOK(mp_init(&rz, FLAG(k1)));
530 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
531
532 for (i = d - 1; i >= 0; i--) {
533 ai = MP_GET_BIT(a, 2 * i + 1);
534 ai <<= 1;
535 ai |= MP_GET_BIT(a, 2 * i);
536 bi = MP_GET_BIT(b, 2 * i + 1);
537 bi <<= 1;
538 bi |= MP_GET_BIT(b, 2 * i);
539 /* R = 2^2 * R */
540 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
541 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
542 /* R = R + (ai * A + bi * B) */
543 MP_CHECKOK(ec_GFp_pt_add_jac_aff
544 (rx, ry, &rz, &precomp[ai][bi][0], &precomp[ai][bi][1],
545 rx, ry, &rz, group));
546 }
547
548 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
549
550 if (group->meth->field_dec) {
551 MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
552 MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
553 }
554
555 CLEANUP:
556 mp_clear(&rz);
557 for (i = 0; i < 4; i++) {
558 for (j = 0; j < 4; j++) {
559 mp_clear(&precomp[i][j][0]);
560 mp_clear(&precomp[i][j][1]);
561 }
562 }
563 return res;
564 }
565