xref: /onnv-gate/usr/src/common/crypto/ecc/ecp_aff.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 
58*5697Smcpowers /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
59*5697Smcpowers mp_err
ec_GFp_pt_is_inf_aff(const mp_int * px,const mp_int * py)60*5697Smcpowers ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
61*5697Smcpowers {
62*5697Smcpowers 
63*5697Smcpowers 	if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
64*5697Smcpowers 		return MP_YES;
65*5697Smcpowers 	} else {
66*5697Smcpowers 		return MP_NO;
67*5697Smcpowers 	}
68*5697Smcpowers 
69*5697Smcpowers }
70*5697Smcpowers 
71*5697Smcpowers /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
72*5697Smcpowers mp_err
ec_GFp_pt_set_inf_aff(mp_int * px,mp_int * py)73*5697Smcpowers ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
74*5697Smcpowers {
75*5697Smcpowers 	mp_zero(px);
76*5697Smcpowers 	mp_zero(py);
77*5697Smcpowers 	return MP_OKAY;
78*5697Smcpowers }
79*5697Smcpowers 
80*5697Smcpowers /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P,
81*5697Smcpowers  * Q, and R can all be identical. Uses affine coordinates. Assumes input
82*5697Smcpowers  * is already field-encoded using field_enc, and returns output that is
83*5697Smcpowers  * still field-encoded. */
84*5697Smcpowers mp_err
ec_GFp_pt_add_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)85*5697Smcpowers ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
86*5697Smcpowers 				  const mp_int *qy, mp_int *rx, mp_int *ry,
87*5697Smcpowers 				  const ECGroup *group)
88*5697Smcpowers {
89*5697Smcpowers 	mp_err res = MP_OKAY;
90*5697Smcpowers 	mp_int lambda, temp, tempx, tempy;
91*5697Smcpowers 
92*5697Smcpowers 	MP_DIGITS(&lambda) = 0;
93*5697Smcpowers 	MP_DIGITS(&temp) = 0;
94*5697Smcpowers 	MP_DIGITS(&tempx) = 0;
95*5697Smcpowers 	MP_DIGITS(&tempy) = 0;
96*5697Smcpowers 	MP_CHECKOK(mp_init(&lambda, FLAG(px)));
97*5697Smcpowers 	MP_CHECKOK(mp_init(&temp, FLAG(px)));
98*5697Smcpowers 	MP_CHECKOK(mp_init(&tempx, FLAG(px)));
99*5697Smcpowers 	MP_CHECKOK(mp_init(&tempy, FLAG(px)));
100*5697Smcpowers 	/* if P = inf, then R = Q */
101*5697Smcpowers 	if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
102*5697Smcpowers 		MP_CHECKOK(mp_copy(qx, rx));
103*5697Smcpowers 		MP_CHECKOK(mp_copy(qy, ry));
104*5697Smcpowers 		res = MP_OKAY;
105*5697Smcpowers 		goto CLEANUP;
106*5697Smcpowers 	}
107*5697Smcpowers 	/* if Q = inf, then R = P */
108*5697Smcpowers 	if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
109*5697Smcpowers 		MP_CHECKOK(mp_copy(px, rx));
110*5697Smcpowers 		MP_CHECKOK(mp_copy(py, ry));
111*5697Smcpowers 		res = MP_OKAY;
112*5697Smcpowers 		goto CLEANUP;
113*5697Smcpowers 	}
114*5697Smcpowers 	/* if px != qx, then lambda = (py-qy) / (px-qx) */
115*5697Smcpowers 	if (mp_cmp(px, qx) != 0) {
116*5697Smcpowers 		MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
117*5697Smcpowers 		MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
118*5697Smcpowers 		MP_CHECKOK(group->meth->
119*5697Smcpowers 				   field_div(&tempy, &tempx, &lambda, group->meth));
120*5697Smcpowers 	} else {
121*5697Smcpowers 		/* if py != qy or qy = 0, then R = inf */
122*5697Smcpowers 		if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
123*5697Smcpowers 			mp_zero(rx);
124*5697Smcpowers 			mp_zero(ry);
125*5697Smcpowers 			res = MP_OKAY;
126*5697Smcpowers 			goto CLEANUP;
127*5697Smcpowers 		}
128*5697Smcpowers 		/* lambda = (3qx^2+a) / (2qy) */
129*5697Smcpowers 		MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
130*5697Smcpowers 		MP_CHECKOK(mp_set_int(&temp, 3));
131*5697Smcpowers 		if (group->meth->field_enc) {
132*5697Smcpowers 			MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
133*5697Smcpowers 		}
134*5697Smcpowers 		MP_CHECKOK(group->meth->
135*5697Smcpowers 				   field_mul(&tempx, &temp, &tempx, group->meth));
136*5697Smcpowers 		MP_CHECKOK(group->meth->
137*5697Smcpowers 				   field_add(&tempx, &group->curvea, &tempx, group->meth));
138*5697Smcpowers 		MP_CHECKOK(mp_set_int(&temp, 2));
139*5697Smcpowers 		if (group->meth->field_enc) {
140*5697Smcpowers 			MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
141*5697Smcpowers 		}
142*5697Smcpowers 		MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
143*5697Smcpowers 		MP_CHECKOK(group->meth->
144*5697Smcpowers 				   field_div(&tempx, &tempy, &lambda, group->meth));
145*5697Smcpowers 	}
146*5697Smcpowers 	/* rx = lambda^2 - px - qx */
147*5697Smcpowers 	MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
148*5697Smcpowers 	MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
149*5697Smcpowers 	MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
150*5697Smcpowers 	/* ry = (x1-x2) * lambda - y1 */
151*5697Smcpowers 	MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
152*5697Smcpowers 	MP_CHECKOK(group->meth->
153*5697Smcpowers 			   field_mul(&tempy, &lambda, &tempy, group->meth));
154*5697Smcpowers 	MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
155*5697Smcpowers 	MP_CHECKOK(mp_copy(&tempx, rx));
156*5697Smcpowers 	MP_CHECKOK(mp_copy(&tempy, ry));
157*5697Smcpowers 
158*5697Smcpowers   CLEANUP:
159*5697Smcpowers 	mp_clear(&lambda);
160*5697Smcpowers 	mp_clear(&temp);
161*5697Smcpowers 	mp_clear(&tempx);
162*5697Smcpowers 	mp_clear(&tempy);
163*5697Smcpowers 	return res;
164*5697Smcpowers }
165*5697Smcpowers 
166*5697Smcpowers /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
167*5697Smcpowers  * identical. Uses affine coordinates. Assumes input is already
168*5697Smcpowers  * field-encoded using field_enc, and returns output that is still
169*5697Smcpowers  * field-encoded. */
170*5697Smcpowers mp_err
ec_GFp_pt_sub_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)171*5697Smcpowers ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
172*5697Smcpowers 				  const mp_int *qy, mp_int *rx, mp_int *ry,
173*5697Smcpowers 				  const ECGroup *group)
174*5697Smcpowers {
175*5697Smcpowers 	mp_err res = MP_OKAY;
176*5697Smcpowers 	mp_int nqy;
177*5697Smcpowers 
178*5697Smcpowers 	MP_DIGITS(&nqy) = 0;
179*5697Smcpowers 	MP_CHECKOK(mp_init(&nqy, FLAG(px)));
180*5697Smcpowers 	/* nqy = -qy */
181*5697Smcpowers 	MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
182*5697Smcpowers 	res = group->point_add(px, py, qx, &nqy, rx, ry, group);
183*5697Smcpowers   CLEANUP:
184*5697Smcpowers 	mp_clear(&nqy);
185*5697Smcpowers 	return res;
186*5697Smcpowers }
187*5697Smcpowers 
188*5697Smcpowers /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
189*5697Smcpowers  * affine coordinates. Assumes input is already field-encoded using
190*5697Smcpowers  * field_enc, and returns output that is still field-encoded. */
191*5697Smcpowers mp_err
ec_GFp_pt_dbl_aff(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)192*5697Smcpowers ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
193*5697Smcpowers 				  mp_int *ry, const ECGroup *group)
194*5697Smcpowers {
195*5697Smcpowers 	return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
196*5697Smcpowers }
197*5697Smcpowers 
198*5697Smcpowers /* by default, this routine is unused and thus doesn't need to be compiled */
199*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
200*5697Smcpowers /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
201*5697Smcpowers  * R can be identical. Uses affine coordinates. Assumes input is already
202*5697Smcpowers  * field-encoded using field_enc, and returns output that is still
203*5697Smcpowers  * field-encoded. */
204*5697Smcpowers mp_err
ec_GFp_pt_mul_aff(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)205*5697Smcpowers ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
206*5697Smcpowers 				  mp_int *rx, mp_int *ry, const ECGroup *group)
207*5697Smcpowers {
208*5697Smcpowers 	mp_err res = MP_OKAY;
209*5697Smcpowers 	mp_int k, k3, qx, qy, sx, sy;
210*5697Smcpowers 	int b1, b3, i, l;
211*5697Smcpowers 
212*5697Smcpowers 	MP_DIGITS(&k) = 0;
213*5697Smcpowers 	MP_DIGITS(&k3) = 0;
214*5697Smcpowers 	MP_DIGITS(&qx) = 0;
215*5697Smcpowers 	MP_DIGITS(&qy) = 0;
216*5697Smcpowers 	MP_DIGITS(&sx) = 0;
217*5697Smcpowers 	MP_DIGITS(&sy) = 0;
218*5697Smcpowers 	MP_CHECKOK(mp_init(&k));
219*5697Smcpowers 	MP_CHECKOK(mp_init(&k3));
220*5697Smcpowers 	MP_CHECKOK(mp_init(&qx));
221*5697Smcpowers 	MP_CHECKOK(mp_init(&qy));
222*5697Smcpowers 	MP_CHECKOK(mp_init(&sx));
223*5697Smcpowers 	MP_CHECKOK(mp_init(&sy));
224*5697Smcpowers 
225*5697Smcpowers 	/* if n = 0 then r = inf */
226*5697Smcpowers 	if (mp_cmp_z(n) == 0) {
227*5697Smcpowers 		mp_zero(rx);
228*5697Smcpowers 		mp_zero(ry);
229*5697Smcpowers 		res = MP_OKAY;
230*5697Smcpowers 		goto CLEANUP;
231*5697Smcpowers 	}
232*5697Smcpowers 	/* Q = P, k = n */
233*5697Smcpowers 	MP_CHECKOK(mp_copy(px, &qx));
234*5697Smcpowers 	MP_CHECKOK(mp_copy(py, &qy));
235*5697Smcpowers 	MP_CHECKOK(mp_copy(n, &k));
236*5697Smcpowers 	/* if n < 0 then Q = -Q, k = -k */
237*5697Smcpowers 	if (mp_cmp_z(n) < 0) {
238*5697Smcpowers 		MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
239*5697Smcpowers 		MP_CHECKOK(mp_neg(&k, &k));
240*5697Smcpowers 	}
241*5697Smcpowers #ifdef ECL_DEBUG				/* basic double and add method */
242*5697Smcpowers 	l = mpl_significant_bits(&k) - 1;
243*5697Smcpowers 	MP_CHECKOK(mp_copy(&qx, &sx));
244*5697Smcpowers 	MP_CHECKOK(mp_copy(&qy, &sy));
245*5697Smcpowers 	for (i = l - 1; i >= 0; i--) {
246*5697Smcpowers 		/* S = 2S */
247*5697Smcpowers 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
248*5697Smcpowers 		/* if k_i = 1, then S = S + Q */
249*5697Smcpowers 		if (mpl_get_bit(&k, i) != 0) {
250*5697Smcpowers 			MP_CHECKOK(group->
251*5697Smcpowers 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
252*5697Smcpowers 		}
253*5697Smcpowers 	}
254*5697Smcpowers #else							/* double and add/subtract method from
255*5697Smcpowers 								 * standard */
256*5697Smcpowers 	/* k3 = 3 * k */
257*5697Smcpowers 	MP_CHECKOK(mp_set_int(&k3, 3));
258*5697Smcpowers 	MP_CHECKOK(mp_mul(&k, &k3, &k3));
259*5697Smcpowers 	/* S = Q */
260*5697Smcpowers 	MP_CHECKOK(mp_copy(&qx, &sx));
261*5697Smcpowers 	MP_CHECKOK(mp_copy(&qy, &sy));
262*5697Smcpowers 	/* l = index of high order bit in binary representation of 3*k */
263*5697Smcpowers 	l = mpl_significant_bits(&k3) - 1;
264*5697Smcpowers 	/* for i = l-1 downto 1 */
265*5697Smcpowers 	for (i = l - 1; i >= 1; i--) {
266*5697Smcpowers 		/* S = 2S */
267*5697Smcpowers 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
268*5697Smcpowers 		b3 = MP_GET_BIT(&k3, i);
269*5697Smcpowers 		b1 = MP_GET_BIT(&k, i);
270*5697Smcpowers 		/* if k3_i = 1 and k_i = 0, then S = S + Q */
271*5697Smcpowers 		if ((b3 == 1) && (b1 == 0)) {
272*5697Smcpowers 			MP_CHECKOK(group->
273*5697Smcpowers 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
274*5697Smcpowers 			/* if k3_i = 0 and k_i = 1, then S = S - Q */
275*5697Smcpowers 		} else if ((b3 == 0) && (b1 == 1)) {
276*5697Smcpowers 			MP_CHECKOK(group->
277*5697Smcpowers 					   point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
278*5697Smcpowers 		}
279*5697Smcpowers 	}
280*5697Smcpowers #endif
281*5697Smcpowers 	/* output S */
282*5697Smcpowers 	MP_CHECKOK(mp_copy(&sx, rx));
283*5697Smcpowers 	MP_CHECKOK(mp_copy(&sy, ry));
284*5697Smcpowers 
285*5697Smcpowers   CLEANUP:
286*5697Smcpowers 	mp_clear(&k);
287*5697Smcpowers 	mp_clear(&k3);
288*5697Smcpowers 	mp_clear(&qx);
289*5697Smcpowers 	mp_clear(&qy);
290*5697Smcpowers 	mp_clear(&sx);
291*5697Smcpowers 	mp_clear(&sy);
292*5697Smcpowers 	return res;
293*5697Smcpowers }
294*5697Smcpowers #endif
295*5697Smcpowers 
296*5697Smcpowers /* Validates a point on a GFp curve. */
297*5697Smcpowers mp_err
ec_GFp_validate_point(const mp_int * px,const mp_int * py,const ECGroup * group)298*5697Smcpowers ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
299*5697Smcpowers {
300*5697Smcpowers 	mp_err res = MP_NO;
301*5697Smcpowers 	mp_int accl, accr, tmp, pxt, pyt;
302*5697Smcpowers 
303*5697Smcpowers 	MP_DIGITS(&accl) = 0;
304*5697Smcpowers 	MP_DIGITS(&accr) = 0;
305*5697Smcpowers 	MP_DIGITS(&tmp) = 0;
306*5697Smcpowers 	MP_DIGITS(&pxt) = 0;
307*5697Smcpowers 	MP_DIGITS(&pyt) = 0;
308*5697Smcpowers 	MP_CHECKOK(mp_init(&accl, FLAG(px)));
309*5697Smcpowers 	MP_CHECKOK(mp_init(&accr, FLAG(px)));
310*5697Smcpowers 	MP_CHECKOK(mp_init(&tmp, FLAG(px)));
311*5697Smcpowers 	MP_CHECKOK(mp_init(&pxt, FLAG(px)));
312*5697Smcpowers 	MP_CHECKOK(mp_init(&pyt, FLAG(px)));
313*5697Smcpowers 
314*5697Smcpowers     /* 1: Verify that publicValue is not the point at infinity */
315*5697Smcpowers 	if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
316*5697Smcpowers 		res = MP_NO;
317*5697Smcpowers 		goto CLEANUP;
318*5697Smcpowers 	}
319*5697Smcpowers     /* 2: Verify that the coordinates of publicValue are elements
320*5697Smcpowers      *    of the field.
321*5697Smcpowers      */
322*5697Smcpowers 	if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
323*5697Smcpowers 		(MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
324*5697Smcpowers 		res = MP_NO;
325*5697Smcpowers 		goto CLEANUP;
326*5697Smcpowers 	}
327*5697Smcpowers     /* 3: Verify that publicValue is on the curve. */
328*5697Smcpowers 	if (group->meth->field_enc) {
329*5697Smcpowers 		group->meth->field_enc(px, &pxt, group->meth);
330*5697Smcpowers 		group->meth->field_enc(py, &pyt, group->meth);
331*5697Smcpowers 	} else {
332*5697Smcpowers 		mp_copy(px, &pxt);
333*5697Smcpowers 		mp_copy(py, &pyt);
334*5697Smcpowers 	}
335*5697Smcpowers 	/* left-hand side: y^2  */
336*5697Smcpowers 	MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
337*5697Smcpowers 	/* right-hand side: x^3 + a*x + b */
338*5697Smcpowers 	MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
339*5697Smcpowers 	MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
340*5697Smcpowers 	MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
341*5697Smcpowers 	MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
342*5697Smcpowers 	MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
343*5697Smcpowers 	/* check LHS - RHS == 0 */
344*5697Smcpowers 	MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
345*5697Smcpowers 	if (mp_cmp_z(&accr) != 0) {
346*5697Smcpowers 		res = MP_NO;
347*5697Smcpowers 		goto CLEANUP;
348*5697Smcpowers 	}
349*5697Smcpowers     /* 4: Verify that the order of the curve times the publicValue
350*5697Smcpowers      *    is the point at infinity.
351*5697Smcpowers      */
352*5697Smcpowers 	MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
353*5697Smcpowers 	if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
354*5697Smcpowers 		res = MP_NO;
355*5697Smcpowers 		goto CLEANUP;
356*5697Smcpowers 	}
357*5697Smcpowers 
358*5697Smcpowers 	res = MP_YES;
359*5697Smcpowers 
360*5697Smcpowers CLEANUP:
361*5697Smcpowers 	mp_clear(&accl);
362*5697Smcpowers 	mp_clear(&accr);
363*5697Smcpowers 	mp_clear(&tmp);
364*5697Smcpowers 	mp_clear(&pxt);
365*5697Smcpowers 	mp_clear(&pyt);
366*5697Smcpowers 	return res;
367*5697Smcpowers }
368