Unleashed v1.4
[unleashed.git] / usr / src / common / crypto / ecc / ecp_jac.c
blob4c6b8280c2c1708121e1ae7518f2b0bb00d0fc46
1 /*
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is the elliptic curve math library for prime field curves.
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.
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
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.
42 * ***** END LICENSE BLOCK ***** */
44 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
45 * Use is subject to license terms.
47 * Sun elects to use this software under the MPL license.
50 #pragma ident "%Z%%M% %I% %E% SMI"
52 #include "ecp.h"
53 #include "mplogic.h"
54 #ifndef _KERNEL
55 #include <stdlib.h>
56 #endif
57 #ifdef ECL_DEBUG
58 #include <assert.h>
59 #endif
61 /* Converts a point P(px, py) from affine coordinates to Jacobian
62 * projective coordinates R(rx, ry, rz). Assumes input is already
63 * field-encoded using field_enc, and returns output that is still
64 * field-encoded. */
65 mp_err
66 ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx,
67 mp_int *ry, mp_int *rz, const ECGroup *group)
69 mp_err res = MP_OKAY;
71 if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
72 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
73 } else {
74 MP_CHECKOK(mp_copy(px, rx));
75 MP_CHECKOK(mp_copy(py, ry));
76 MP_CHECKOK(mp_set_int(rz, 1));
77 if (group->meth->field_enc) {
78 MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth));
81 CLEANUP:
82 return res;
85 /* Converts a point P(px, py, pz) from Jacobian projective coordinates to
86 * affine coordinates R(rx, ry). P and R can share x and y coordinates.
87 * Assumes input is already field-encoded using field_enc, and returns
88 * output that is still field-encoded. */
89 mp_err
90 ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py, const mp_int *pz,
91 mp_int *rx, mp_int *ry, const ECGroup *group)
93 mp_err res = MP_OKAY;
94 mp_int z1, z2, z3;
96 MP_DIGITS(&z1) = 0;
97 MP_DIGITS(&z2) = 0;
98 MP_DIGITS(&z3) = 0;
99 MP_CHECKOK(mp_init(&z1, FLAG(px)));
100 MP_CHECKOK(mp_init(&z2, FLAG(px)));
101 MP_CHECKOK(mp_init(&z3, FLAG(px)));
103 /* if point at infinity, then set point at infinity and exit */
104 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
105 MP_CHECKOK(ec_GFp_pt_set_inf_aff(rx, ry));
106 goto CLEANUP;
109 /* transform (px, py, pz) into (px / pz^2, py / pz^3) */
110 if (mp_cmp_d(pz, 1) == 0) {
111 MP_CHECKOK(mp_copy(px, rx));
112 MP_CHECKOK(mp_copy(py, ry));
113 } else {
114 MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth));
115 MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth));
116 MP_CHECKOK(group->meth->field_mul(&z1, &z2, &z3, group->meth));
117 MP_CHECKOK(group->meth->field_mul(px, &z2, rx, group->meth));
118 MP_CHECKOK(group->meth->field_mul(py, &z3, ry, group->meth));
121 CLEANUP:
122 mp_clear(&z1);
123 mp_clear(&z2);
124 mp_clear(&z3);
125 return res;
128 /* Checks if point P(px, py, pz) is at infinity. Uses Jacobian
129 * coordinates. */
130 mp_err
131 ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py, const mp_int *pz)
133 return mp_cmp_z(pz);
136 /* Sets P(px, py, pz) to be the point at infinity. Uses Jacobian
137 * coordinates. */
138 mp_err
139 ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz)
141 mp_zero(pz);
142 return MP_OKAY;
145 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
146 * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical.
147 * Uses mixed Jacobian-affine coordinates. Assumes input is already
148 * field-encoded using field_enc, and returns output that is still
149 * field-encoded. Uses equation (2) from Brown, Hankerson, Lopez, and
150 * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime
151 * Fields. */
152 mp_err
153 ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
154 const mp_int *qx, const mp_int *qy, mp_int *rx,
155 mp_int *ry, mp_int *rz, const ECGroup *group)
157 mp_err res = MP_OKAY;
158 mp_int A, B, C, D, C2, C3;
160 MP_DIGITS(&A) = 0;
161 MP_DIGITS(&B) = 0;
162 MP_DIGITS(&C) = 0;
163 MP_DIGITS(&D) = 0;
164 MP_DIGITS(&C2) = 0;
165 MP_DIGITS(&C3) = 0;
166 MP_CHECKOK(mp_init(&A, FLAG(px)));
167 MP_CHECKOK(mp_init(&B, FLAG(px)));
168 MP_CHECKOK(mp_init(&C, FLAG(px)));
169 MP_CHECKOK(mp_init(&D, FLAG(px)));
170 MP_CHECKOK(mp_init(&C2, FLAG(px)));
171 MP_CHECKOK(mp_init(&C3, FLAG(px)));
173 /* If either P or Q is the point at infinity, then return the other
174 * point */
175 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
176 MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
177 goto CLEANUP;
179 if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
180 MP_CHECKOK(mp_copy(px, rx));
181 MP_CHECKOK(mp_copy(py, ry));
182 MP_CHECKOK(mp_copy(pz, rz));
183 goto CLEANUP;
186 /* A = qx * pz^2, B = qy * pz^3 */
187 MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth));
188 MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth));
189 MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth));
190 MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth));
192 /* C = A - px, D = B - py */
193 MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth));
194 MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth));
196 /* C2 = C^2, C3 = C^3 */
197 MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth));
198 MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth));
200 /* rz = pz * C */
201 MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth));
203 /* C = px * C^2 */
204 MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth));
205 /* A = D^2 */
206 MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth));
208 /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
209 MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth));
210 MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth));
211 MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth));
213 /* C3 = py * C^3 */
214 MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth));
216 /* ry = D * (px * C^2 - rx) - py * C^3 */
217 MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth));
218 MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth));
219 MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth));
221 CLEANUP:
222 mp_clear(&A);
223 mp_clear(&B);
224 mp_clear(&C);
225 mp_clear(&D);
226 mp_clear(&C2);
227 mp_clear(&C3);
228 return res;
231 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
232 * Jacobian coordinates.
234 * Assumes input is already field-encoded using field_enc, and returns
235 * output that is still field-encoded.
237 * This routine implements Point Doubling in the Jacobian Projective
238 * space as described in the paper "Efficient elliptic curve exponentiation
239 * using mixed coordinates", by H. Cohen, A Miyaji, T. Ono.
241 mp_err
242 ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py, const mp_int *pz,
243 mp_int *rx, mp_int *ry, mp_int *rz, const ECGroup *group)
245 mp_err res = MP_OKAY;
246 mp_int t0, t1, M, S;
248 MP_DIGITS(&t0) = 0;
249 MP_DIGITS(&t1) = 0;
250 MP_DIGITS(&M) = 0;
251 MP_DIGITS(&S) = 0;
252 MP_CHECKOK(mp_init(&t0, FLAG(px)));
253 MP_CHECKOK(mp_init(&t1, FLAG(px)));
254 MP_CHECKOK(mp_init(&M, FLAG(px)));
255 MP_CHECKOK(mp_init(&S, FLAG(px)));
257 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
258 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
259 goto CLEANUP;
262 if (mp_cmp_d(pz, 1) == 0) {
263 /* M = 3 * px^2 + a */
264 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
265 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
266 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
267 MP_CHECKOK(group->meth->
268 field_add(&t0, &group->curvea, &M, group->meth));
269 } else if (mp_cmp_int(&group->curvea, -3, FLAG(px)) == 0) {
270 /* M = 3 * (px + pz^2) * (px - pz^2) */
271 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
272 MP_CHECKOK(group->meth->field_add(px, &M, &t0, group->meth));
273 MP_CHECKOK(group->meth->field_sub(px, &M, &t1, group->meth));
274 MP_CHECKOK(group->meth->field_mul(&t0, &t1, &M, group->meth));
275 MP_CHECKOK(group->meth->field_add(&M, &M, &t0, group->meth));
276 MP_CHECKOK(group->meth->field_add(&t0, &M, &M, group->meth));
277 } else {
278 /* M = 3 * (px^2) + a * (pz^4) */
279 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
280 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
281 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
282 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
283 MP_CHECKOK(group->meth->field_sqr(&M, &M, group->meth));
284 MP_CHECKOK(group->meth->
285 field_mul(&M, &group->curvea, &M, group->meth));
286 MP_CHECKOK(group->meth->field_add(&M, &t0, &M, group->meth));
289 /* rz = 2 * py * pz */
290 /* t0 = 4 * py^2 */
291 if (mp_cmp_d(pz, 1) == 0) {
292 MP_CHECKOK(group->meth->field_add(py, py, rz, group->meth));
293 MP_CHECKOK(group->meth->field_sqr(rz, &t0, group->meth));
294 } else {
295 MP_CHECKOK(group->meth->field_add(py, py, &t0, group->meth));
296 MP_CHECKOK(group->meth->field_mul(&t0, pz, rz, group->meth));
297 MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth));
300 /* S = 4 * px * py^2 = px * (2 * py)^2 */
301 MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth));
303 /* rx = M^2 - 2 * S */
304 MP_CHECKOK(group->meth->field_add(&S, &S, &t1, group->meth));
305 MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth));
306 MP_CHECKOK(group->meth->field_sub(rx, &t1, rx, group->meth));
308 /* ry = M * (S - rx) - 8 * py^4 */
309 MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth));
310 if (mp_isodd(&t1)) {
311 MP_CHECKOK(mp_add(&t1, &group->meth->irr, &t1));
313 MP_CHECKOK(mp_div_2(&t1, &t1));
314 MP_CHECKOK(group->meth->field_sub(&S, rx, &S, group->meth));
315 MP_CHECKOK(group->meth->field_mul(&M, &S, &M, group->meth));
316 MP_CHECKOK(group->meth->field_sub(&M, &t1, ry, group->meth));
318 CLEANUP:
319 mp_clear(&t0);
320 mp_clear(&t1);
321 mp_clear(&M);
322 mp_clear(&S);
323 return res;
326 /* by default, this routine is unused and thus doesn't need to be compiled */
327 #ifdef ECL_ENABLE_GFP_PT_MUL_JAC
328 /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters
329 * a, b and p are the elliptic curve coefficients and the prime that
330 * determines the field GFp. Elliptic curve points P and R can be
331 * identical. Uses mixed Jacobian-affine coordinates. Assumes input is
332 * already field-encoded using field_enc, and returns output that is still
333 * field-encoded. Uses 4-bit window method. */
334 mp_err
335 ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px, const mp_int *py,
336 mp_int *rx, mp_int *ry, const ECGroup *group)
338 mp_err res = MP_OKAY;
339 mp_int precomp[16][2], rz;
340 int i, ni, d;
342 MP_DIGITS(&rz) = 0;
343 for (i = 0; i < 16; i++) {
344 MP_DIGITS(&precomp[i][0]) = 0;
345 MP_DIGITS(&precomp[i][1]) = 0;
348 ARGCHK(group != NULL, MP_BADARG);
349 ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
351 /* initialize precomputation table */
352 for (i = 0; i < 16; i++) {
353 MP_CHECKOK(mp_init(&precomp[i][0]));
354 MP_CHECKOK(mp_init(&precomp[i][1]));
357 /* fill precomputation table */
358 mp_zero(&precomp[0][0]);
359 mp_zero(&precomp[0][1]);
360 MP_CHECKOK(mp_copy(px, &precomp[1][0]));
361 MP_CHECKOK(mp_copy(py, &precomp[1][1]));
362 for (i = 2; i < 16; i++) {
363 MP_CHECKOK(group->
364 point_add(&precomp[1][0], &precomp[1][1],
365 &precomp[i - 1][0], &precomp[i - 1][1],
366 &precomp[i][0], &precomp[i][1], group));
369 d = (mpl_significant_bits(n) + 3) / 4;
371 /* R = inf */
372 MP_CHECKOK(mp_init(&rz));
373 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
375 for (i = d - 1; i >= 0; i--) {
376 /* compute window ni */
377 ni = MP_GET_BIT(n, 4 * i + 3);
378 ni <<= 1;
379 ni |= MP_GET_BIT(n, 4 * i + 2);
380 ni <<= 1;
381 ni |= MP_GET_BIT(n, 4 * i + 1);
382 ni <<= 1;
383 ni |= MP_GET_BIT(n, 4 * i);
384 /* R = 2^4 * R */
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 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 /* R = R + (ni * P) */
390 MP_CHECKOK(ec_GFp_pt_add_jac_aff
391 (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry,
392 &rz, group));
395 /* convert result S to affine coordinates */
396 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
398 CLEANUP:
399 mp_clear(&rz);
400 for (i = 0; i < 16; i++) {
401 mp_clear(&precomp[i][0]);
402 mp_clear(&precomp[i][1]);
404 return res;
406 #endif
408 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
409 * k2 * P(x, y), where G is the generator (base point) of the group of
410 * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
411 * Uses mixed Jacobian-affine coordinates. Input and output values are
412 * assumed to be NOT field-encoded. Uses algorithm 15 (simultaneous
413 * multiple point multiplication) from Brown, Hankerson, Lopez, Menezes.
414 * Software Implementation of the NIST Elliptic Curves over Prime Fields. */
415 mp_err
416 ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px,
417 const mp_int *py, mp_int *rx, mp_int *ry,
418 const ECGroup *group)
420 mp_err res = MP_OKAY;
421 mp_int precomp[4][4][2];
422 mp_int rz;
423 const mp_int *a, *b;
424 int i, j;
425 int ai, bi, d;
427 for (i = 0; i < 4; i++) {
428 for (j = 0; j < 4; j++) {
429 MP_DIGITS(&precomp[i][j][0]) = 0;
430 MP_DIGITS(&precomp[i][j][1]) = 0;
433 MP_DIGITS(&rz) = 0;
435 ARGCHK(group != NULL, MP_BADARG);
436 ARGCHK(!((k1 == NULL)
437 && ((k2 == NULL) || (px == NULL)
438 || (py == NULL))), MP_BADARG);
440 /* if some arguments are not defined used ECPoint_mul */
441 if (k1 == NULL) {
442 return ECPoint_mul(group, k2, px, py, rx, ry);
443 } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
444 return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
447 /* initialize precomputation table */
448 for (i = 0; i < 4; i++) {
449 for (j = 0; j < 4; j++) {
450 MP_CHECKOK(mp_init(&precomp[i][j][0], FLAG(k1)));
451 MP_CHECKOK(mp_init(&precomp[i][j][1], FLAG(k1)));
455 /* fill precomputation table */
456 /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */
457 if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) {
458 a = k2;
459 b = k1;
460 if (group->meth->field_enc) {
461 MP_CHECKOK(group->meth->
462 field_enc(px, &precomp[1][0][0], group->meth));
463 MP_CHECKOK(group->meth->
464 field_enc(py, &precomp[1][0][1], group->meth));
465 } else {
466 MP_CHECKOK(mp_copy(px, &precomp[1][0][0]));
467 MP_CHECKOK(mp_copy(py, &precomp[1][0][1]));
469 MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0]));
470 MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1]));
471 } else {
472 a = k1;
473 b = k2;
474 MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0]));
475 MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1]));
476 if (group->meth->field_enc) {
477 MP_CHECKOK(group->meth->
478 field_enc(px, &precomp[0][1][0], group->meth));
479 MP_CHECKOK(group->meth->
480 field_enc(py, &precomp[0][1][1], group->meth));
481 } else {
482 MP_CHECKOK(mp_copy(px, &precomp[0][1][0]));
483 MP_CHECKOK(mp_copy(py, &precomp[0][1][1]));
486 /* precompute [*][0][*] */
487 mp_zero(&precomp[0][0][0]);
488 mp_zero(&precomp[0][0][1]);
489 MP_CHECKOK(group->
490 point_dbl(&precomp[1][0][0], &precomp[1][0][1],
491 &precomp[2][0][0], &precomp[2][0][1], group));
492 MP_CHECKOK(group->
493 point_add(&precomp[1][0][0], &precomp[1][0][1],
494 &precomp[2][0][0], &precomp[2][0][1],
495 &precomp[3][0][0], &precomp[3][0][1], group));
496 /* precompute [*][1][*] */
497 for (i = 1; i < 4; i++) {
498 MP_CHECKOK(group->
499 point_add(&precomp[0][1][0], &precomp[0][1][1],
500 &precomp[i][0][0], &precomp[i][0][1],
501 &precomp[i][1][0], &precomp[i][1][1], group));
503 /* precompute [*][2][*] */
504 MP_CHECKOK(group->
505 point_dbl(&precomp[0][1][0], &precomp[0][1][1],
506 &precomp[0][2][0], &precomp[0][2][1], group));
507 for (i = 1; i < 4; i++) {
508 MP_CHECKOK(group->
509 point_add(&precomp[0][2][0], &precomp[0][2][1],
510 &precomp[i][0][0], &precomp[i][0][1],
511 &precomp[i][2][0], &precomp[i][2][1], group));
513 /* precompute [*][3][*] */
514 MP_CHECKOK(group->
515 point_add(&precomp[0][1][0], &precomp[0][1][1],
516 &precomp[0][2][0], &precomp[0][2][1],
517 &precomp[0][3][0], &precomp[0][3][1], group));
518 for (i = 1; i < 4; i++) {
519 MP_CHECKOK(group->
520 point_add(&precomp[0][3][0], &precomp[0][3][1],
521 &precomp[i][0][0], &precomp[i][0][1],
522 &precomp[i][3][0], &precomp[i][3][1], group));
525 d = (mpl_significant_bits(a) + 1) / 2;
527 /* R = inf */
528 MP_CHECKOK(mp_init(&rz, FLAG(k1)));
529 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
531 for (i = d - 1; i >= 0; i--) {
532 ai = MP_GET_BIT(a, 2 * i + 1);
533 ai <<= 1;
534 ai |= MP_GET_BIT(a, 2 * i);
535 bi = MP_GET_BIT(b, 2 * i + 1);
536 bi <<= 1;
537 bi |= MP_GET_BIT(b, 2 * i);
538 /* R = 2^2 * R */
539 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
540 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
541 /* R = R + (ai * A + bi * B) */
542 MP_CHECKOK(ec_GFp_pt_add_jac_aff
543 (rx, ry, &rz, &precomp[ai][bi][0], &precomp[ai][bi][1],
544 rx, ry, &rz, group));
547 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
549 if (group->meth->field_dec) {
550 MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
551 MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
554 CLEANUP:
555 mp_clear(&rz);
556 for (i = 0; i < 4; i++) {
557 for (j = 0; j < 4; j++) {
558 mp_clear(&precomp[i][j][0]);
559 mp_clear(&precomp[i][j][1]);
562 return res;