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