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