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