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