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 binary polynomial 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 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24*f9fbec18Smcpowers *
25*f9fbec18Smcpowers * Alternatively, the contents of this file may be used under the terms of
26*f9fbec18Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27*f9fbec18Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28*f9fbec18Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29*f9fbec18Smcpowers * of those above. If you wish to allow use of your version of this file only
30*f9fbec18Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31*f9fbec18Smcpowers * use your version of this file under the terms of the MPL, indicate your
32*f9fbec18Smcpowers * decision by deleting the provisions above and replace them with the notice
33*f9fbec18Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34*f9fbec18Smcpowers * the provisions above, a recipient may use your version of this file under
35*f9fbec18Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36*f9fbec18Smcpowers *
37*f9fbec18Smcpowers * ***** END LICENSE BLOCK ***** */
38*f9fbec18Smcpowers /*
39*f9fbec18Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40*f9fbec18Smcpowers * Use is subject to license terms.
41*f9fbec18Smcpowers *
42*f9fbec18Smcpowers * Sun elects to use this software under the MPL license.
43*f9fbec18Smcpowers */
44*f9fbec18Smcpowers
45*f9fbec18Smcpowers #pragma ident "%Z%%M% %I% %E% SMI"
46*f9fbec18Smcpowers
47*f9fbec18Smcpowers #include "ec2.h"
48*f9fbec18Smcpowers #include "mplogic.h"
49*f9fbec18Smcpowers #include "mp_gf2m.h"
50*f9fbec18Smcpowers #ifndef _KERNEL
51*f9fbec18Smcpowers #include <stdlib.h>
52*f9fbec18Smcpowers #endif
53*f9fbec18Smcpowers
54*f9fbec18Smcpowers /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
55*f9fbec18Smcpowers mp_err
ec_GF2m_pt_is_inf_aff(const mp_int * px,const mp_int * py)56*f9fbec18Smcpowers ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
57*f9fbec18Smcpowers {
58*f9fbec18Smcpowers
59*f9fbec18Smcpowers if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
60*f9fbec18Smcpowers return MP_YES;
61*f9fbec18Smcpowers } else {
62*f9fbec18Smcpowers return MP_NO;
63*f9fbec18Smcpowers }
64*f9fbec18Smcpowers
65*f9fbec18Smcpowers }
66*f9fbec18Smcpowers
67*f9fbec18Smcpowers /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
68*f9fbec18Smcpowers mp_err
ec_GF2m_pt_set_inf_aff(mp_int * px,mp_int * py)69*f9fbec18Smcpowers ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
70*f9fbec18Smcpowers {
71*f9fbec18Smcpowers mp_zero(px);
72*f9fbec18Smcpowers mp_zero(py);
73*f9fbec18Smcpowers return MP_OKAY;
74*f9fbec18Smcpowers }
75*f9fbec18Smcpowers
76*f9fbec18Smcpowers /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P,
77*f9fbec18Smcpowers * Q, and R can all be identical. Uses affine coordinates. */
78*f9fbec18Smcpowers mp_err
ec_GF2m_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)79*f9fbec18Smcpowers ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
80*f9fbec18Smcpowers const mp_int *qy, mp_int *rx, mp_int *ry,
81*f9fbec18Smcpowers const ECGroup *group)
82*f9fbec18Smcpowers {
83*f9fbec18Smcpowers mp_err res = MP_OKAY;
84*f9fbec18Smcpowers mp_int lambda, tempx, tempy;
85*f9fbec18Smcpowers
86*f9fbec18Smcpowers MP_DIGITS(&lambda) = 0;
87*f9fbec18Smcpowers MP_DIGITS(&tempx) = 0;
88*f9fbec18Smcpowers MP_DIGITS(&tempy) = 0;
89*f9fbec18Smcpowers MP_CHECKOK(mp_init(&lambda, FLAG(px)));
90*f9fbec18Smcpowers MP_CHECKOK(mp_init(&tempx, FLAG(px)));
91*f9fbec18Smcpowers MP_CHECKOK(mp_init(&tempy, FLAG(px)));
92*f9fbec18Smcpowers /* if P = inf, then R = Q */
93*f9fbec18Smcpowers if (ec_GF2m_pt_is_inf_aff(px, py) == 0) {
94*f9fbec18Smcpowers MP_CHECKOK(mp_copy(qx, rx));
95*f9fbec18Smcpowers MP_CHECKOK(mp_copy(qy, ry));
96*f9fbec18Smcpowers res = MP_OKAY;
97*f9fbec18Smcpowers goto CLEANUP;
98*f9fbec18Smcpowers }
99*f9fbec18Smcpowers /* if Q = inf, then R = P */
100*f9fbec18Smcpowers if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) {
101*f9fbec18Smcpowers MP_CHECKOK(mp_copy(px, rx));
102*f9fbec18Smcpowers MP_CHECKOK(mp_copy(py, ry));
103*f9fbec18Smcpowers res = MP_OKAY;
104*f9fbec18Smcpowers goto CLEANUP;
105*f9fbec18Smcpowers }
106*f9fbec18Smcpowers /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2
107*f9fbec18Smcpowers * + lambda + px + qx */
108*f9fbec18Smcpowers if (mp_cmp(px, qx) != 0) {
109*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
110*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
111*f9fbec18Smcpowers MP_CHECKOK(group->meth->
112*f9fbec18Smcpowers field_div(&tempy, &tempx, &lambda, group->meth));
113*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
114*f9fbec18Smcpowers MP_CHECKOK(group->meth->
115*f9fbec18Smcpowers field_add(&tempx, &lambda, &tempx, group->meth));
116*f9fbec18Smcpowers MP_CHECKOK(group->meth->
117*f9fbec18Smcpowers field_add(&tempx, &group->curvea, &tempx, group->meth));
118*f9fbec18Smcpowers MP_CHECKOK(group->meth->
119*f9fbec18Smcpowers field_add(&tempx, px, &tempx, group->meth));
120*f9fbec18Smcpowers MP_CHECKOK(group->meth->
121*f9fbec18Smcpowers field_add(&tempx, qx, &tempx, group->meth));
122*f9fbec18Smcpowers } else {
123*f9fbec18Smcpowers /* if py != qy or qx = 0, then R = inf */
124*f9fbec18Smcpowers if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
125*f9fbec18Smcpowers mp_zero(rx);
126*f9fbec18Smcpowers mp_zero(ry);
127*f9fbec18Smcpowers res = MP_OKAY;
128*f9fbec18Smcpowers goto CLEANUP;
129*f9fbec18Smcpowers }
130*f9fbec18Smcpowers /* lambda = qx + qy / qx */
131*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
132*f9fbec18Smcpowers MP_CHECKOK(group->meth->
133*f9fbec18Smcpowers field_add(&lambda, qx, &lambda, group->meth));
134*f9fbec18Smcpowers /* tempx = a + lambda^2 + lambda */
135*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
136*f9fbec18Smcpowers MP_CHECKOK(group->meth->
137*f9fbec18Smcpowers field_add(&tempx, &lambda, &tempx, group->meth));
138*f9fbec18Smcpowers MP_CHECKOK(group->meth->
139*f9fbec18Smcpowers field_add(&tempx, &group->curvea, &tempx, group->meth));
140*f9fbec18Smcpowers }
141*f9fbec18Smcpowers /* ry = (qx + tempx) * lambda + tempx + qy */
142*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
143*f9fbec18Smcpowers MP_CHECKOK(group->meth->
144*f9fbec18Smcpowers field_mul(&tempy, &lambda, &tempy, group->meth));
145*f9fbec18Smcpowers MP_CHECKOK(group->meth->
146*f9fbec18Smcpowers field_add(&tempy, &tempx, &tempy, group->meth));
147*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
148*f9fbec18Smcpowers /* rx = tempx */
149*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&tempx, rx));
150*f9fbec18Smcpowers
151*f9fbec18Smcpowers CLEANUP:
152*f9fbec18Smcpowers mp_clear(&lambda);
153*f9fbec18Smcpowers mp_clear(&tempx);
154*f9fbec18Smcpowers mp_clear(&tempy);
155*f9fbec18Smcpowers return res;
156*f9fbec18Smcpowers }
157*f9fbec18Smcpowers
158*f9fbec18Smcpowers /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
159*f9fbec18Smcpowers * identical. Uses affine coordinates. */
160*f9fbec18Smcpowers mp_err
ec_GF2m_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)161*f9fbec18Smcpowers ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
162*f9fbec18Smcpowers const mp_int *qy, mp_int *rx, mp_int *ry,
163*f9fbec18Smcpowers const ECGroup *group)
164*f9fbec18Smcpowers {
165*f9fbec18Smcpowers mp_err res = MP_OKAY;
166*f9fbec18Smcpowers mp_int nqy;
167*f9fbec18Smcpowers
168*f9fbec18Smcpowers MP_DIGITS(&nqy) = 0;
169*f9fbec18Smcpowers MP_CHECKOK(mp_init(&nqy, FLAG(px)));
170*f9fbec18Smcpowers /* nqy = qx+qy */
171*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
172*f9fbec18Smcpowers MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
173*f9fbec18Smcpowers CLEANUP:
174*f9fbec18Smcpowers mp_clear(&nqy);
175*f9fbec18Smcpowers return res;
176*f9fbec18Smcpowers }
177*f9fbec18Smcpowers
178*f9fbec18Smcpowers /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
179*f9fbec18Smcpowers * affine coordinates. */
180*f9fbec18Smcpowers mp_err
ec_GF2m_pt_dbl_aff(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)181*f9fbec18Smcpowers ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
182*f9fbec18Smcpowers mp_int *ry, const ECGroup *group)
183*f9fbec18Smcpowers {
184*f9fbec18Smcpowers return group->point_add(px, py, px, py, rx, ry, group);
185*f9fbec18Smcpowers }
186*f9fbec18Smcpowers
187*f9fbec18Smcpowers /* by default, this routine is unused and thus doesn't need to be compiled */
188*f9fbec18Smcpowers #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
189*f9fbec18Smcpowers /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
190*f9fbec18Smcpowers * R can be identical. Uses affine coordinates. */
191*f9fbec18Smcpowers mp_err
ec_GF2m_pt_mul_aff(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)192*f9fbec18Smcpowers ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
193*f9fbec18Smcpowers mp_int *rx, mp_int *ry, const ECGroup *group)
194*f9fbec18Smcpowers {
195*f9fbec18Smcpowers mp_err res = MP_OKAY;
196*f9fbec18Smcpowers mp_int k, k3, qx, qy, sx, sy;
197*f9fbec18Smcpowers int b1, b3, i, l;
198*f9fbec18Smcpowers
199*f9fbec18Smcpowers MP_DIGITS(&k) = 0;
200*f9fbec18Smcpowers MP_DIGITS(&k3) = 0;
201*f9fbec18Smcpowers MP_DIGITS(&qx) = 0;
202*f9fbec18Smcpowers MP_DIGITS(&qy) = 0;
203*f9fbec18Smcpowers MP_DIGITS(&sx) = 0;
204*f9fbec18Smcpowers MP_DIGITS(&sy) = 0;
205*f9fbec18Smcpowers MP_CHECKOK(mp_init(&k));
206*f9fbec18Smcpowers MP_CHECKOK(mp_init(&k3));
207*f9fbec18Smcpowers MP_CHECKOK(mp_init(&qx));
208*f9fbec18Smcpowers MP_CHECKOK(mp_init(&qy));
209*f9fbec18Smcpowers MP_CHECKOK(mp_init(&sx));
210*f9fbec18Smcpowers MP_CHECKOK(mp_init(&sy));
211*f9fbec18Smcpowers
212*f9fbec18Smcpowers /* if n = 0 then r = inf */
213*f9fbec18Smcpowers if (mp_cmp_z(n) == 0) {
214*f9fbec18Smcpowers mp_zero(rx);
215*f9fbec18Smcpowers mp_zero(ry);
216*f9fbec18Smcpowers res = MP_OKAY;
217*f9fbec18Smcpowers goto CLEANUP;
218*f9fbec18Smcpowers }
219*f9fbec18Smcpowers /* Q = P, k = n */
220*f9fbec18Smcpowers MP_CHECKOK(mp_copy(px, &qx));
221*f9fbec18Smcpowers MP_CHECKOK(mp_copy(py, &qy));
222*f9fbec18Smcpowers MP_CHECKOK(mp_copy(n, &k));
223*f9fbec18Smcpowers /* if n < 0 then Q = -Q, k = -k */
224*f9fbec18Smcpowers if (mp_cmp_z(n) < 0) {
225*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
226*f9fbec18Smcpowers MP_CHECKOK(mp_neg(&k, &k));
227*f9fbec18Smcpowers }
228*f9fbec18Smcpowers #ifdef ECL_DEBUG /* basic double and add method */
229*f9fbec18Smcpowers l = mpl_significant_bits(&k) - 1;
230*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&qx, &sx));
231*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&qy, &sy));
232*f9fbec18Smcpowers for (i = l - 1; i >= 0; i--) {
233*f9fbec18Smcpowers /* S = 2S */
234*f9fbec18Smcpowers MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
235*f9fbec18Smcpowers /* if k_i = 1, then S = S + Q */
236*f9fbec18Smcpowers if (mpl_get_bit(&k, i) != 0) {
237*f9fbec18Smcpowers MP_CHECKOK(group->
238*f9fbec18Smcpowers point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
239*f9fbec18Smcpowers }
240*f9fbec18Smcpowers }
241*f9fbec18Smcpowers #else /* double and add/subtract method from
242*f9fbec18Smcpowers * standard */
243*f9fbec18Smcpowers /* k3 = 3 * k */
244*f9fbec18Smcpowers MP_CHECKOK(mp_set_int(&k3, 3));
245*f9fbec18Smcpowers MP_CHECKOK(mp_mul(&k, &k3, &k3));
246*f9fbec18Smcpowers /* S = Q */
247*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&qx, &sx));
248*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&qy, &sy));
249*f9fbec18Smcpowers /* l = index of high order bit in binary representation of 3*k */
250*f9fbec18Smcpowers l = mpl_significant_bits(&k3) - 1;
251*f9fbec18Smcpowers /* for i = l-1 downto 1 */
252*f9fbec18Smcpowers for (i = l - 1; i >= 1; i--) {
253*f9fbec18Smcpowers /* S = 2S */
254*f9fbec18Smcpowers MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
255*f9fbec18Smcpowers b3 = MP_GET_BIT(&k3, i);
256*f9fbec18Smcpowers b1 = MP_GET_BIT(&k, i);
257*f9fbec18Smcpowers /* if k3_i = 1 and k_i = 0, then S = S + Q */
258*f9fbec18Smcpowers if ((b3 == 1) && (b1 == 0)) {
259*f9fbec18Smcpowers MP_CHECKOK(group->
260*f9fbec18Smcpowers point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
261*f9fbec18Smcpowers /* if k3_i = 0 and k_i = 1, then S = S - Q */
262*f9fbec18Smcpowers } else if ((b3 == 0) && (b1 == 1)) {
263*f9fbec18Smcpowers MP_CHECKOK(group->
264*f9fbec18Smcpowers point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
265*f9fbec18Smcpowers }
266*f9fbec18Smcpowers }
267*f9fbec18Smcpowers #endif
268*f9fbec18Smcpowers /* output S */
269*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&sx, rx));
270*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&sy, ry));
271*f9fbec18Smcpowers
272*f9fbec18Smcpowers CLEANUP:
273*f9fbec18Smcpowers mp_clear(&k);
274*f9fbec18Smcpowers mp_clear(&k3);
275*f9fbec18Smcpowers mp_clear(&qx);
276*f9fbec18Smcpowers mp_clear(&qy);
277*f9fbec18Smcpowers mp_clear(&sx);
278*f9fbec18Smcpowers mp_clear(&sy);
279*f9fbec18Smcpowers return res;
280*f9fbec18Smcpowers }
281*f9fbec18Smcpowers #endif
282*f9fbec18Smcpowers
283*f9fbec18Smcpowers /* Validates a point on a GF2m curve. */
284*f9fbec18Smcpowers mp_err
ec_GF2m_validate_point(const mp_int * px,const mp_int * py,const ECGroup * group)285*f9fbec18Smcpowers ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
286*f9fbec18Smcpowers {
287*f9fbec18Smcpowers mp_err res = MP_NO;
288*f9fbec18Smcpowers mp_int accl, accr, tmp, pxt, pyt;
289*f9fbec18Smcpowers
290*f9fbec18Smcpowers MP_DIGITS(&accl) = 0;
291*f9fbec18Smcpowers MP_DIGITS(&accr) = 0;
292*f9fbec18Smcpowers MP_DIGITS(&tmp) = 0;
293*f9fbec18Smcpowers MP_DIGITS(&pxt) = 0;
294*f9fbec18Smcpowers MP_DIGITS(&pyt) = 0;
295*f9fbec18Smcpowers MP_CHECKOK(mp_init(&accl, FLAG(px)));
296*f9fbec18Smcpowers MP_CHECKOK(mp_init(&accr, FLAG(px)));
297*f9fbec18Smcpowers MP_CHECKOK(mp_init(&tmp, FLAG(px)));
298*f9fbec18Smcpowers MP_CHECKOK(mp_init(&pxt, FLAG(px)));
299*f9fbec18Smcpowers MP_CHECKOK(mp_init(&pyt, FLAG(px)));
300*f9fbec18Smcpowers
301*f9fbec18Smcpowers /* 1: Verify that publicValue is not the point at infinity */
302*f9fbec18Smcpowers if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
303*f9fbec18Smcpowers res = MP_NO;
304*f9fbec18Smcpowers goto CLEANUP;
305*f9fbec18Smcpowers }
306*f9fbec18Smcpowers /* 2: Verify that the coordinates of publicValue are elements
307*f9fbec18Smcpowers * of the field.
308*f9fbec18Smcpowers */
309*f9fbec18Smcpowers if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
310*f9fbec18Smcpowers (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
311*f9fbec18Smcpowers res = MP_NO;
312*f9fbec18Smcpowers goto CLEANUP;
313*f9fbec18Smcpowers }
314*f9fbec18Smcpowers /* 3: Verify that publicValue is on the curve. */
315*f9fbec18Smcpowers if (group->meth->field_enc) {
316*f9fbec18Smcpowers group->meth->field_enc(px, &pxt, group->meth);
317*f9fbec18Smcpowers group->meth->field_enc(py, &pyt, group->meth);
318*f9fbec18Smcpowers } else {
319*f9fbec18Smcpowers mp_copy(px, &pxt);
320*f9fbec18Smcpowers mp_copy(py, &pyt);
321*f9fbec18Smcpowers }
322*f9fbec18Smcpowers /* left-hand side: y^2 + x*y */
323*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
324*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
325*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
326*f9fbec18Smcpowers /* right-hand side: x^3 + a*x^2 + b */
327*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
328*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
329*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
330*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
331*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
332*f9fbec18Smcpowers /* check LHS - RHS == 0 */
333*f9fbec18Smcpowers MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
334*f9fbec18Smcpowers if (mp_cmp_z(&accr) != 0) {
335*f9fbec18Smcpowers res = MP_NO;
336*f9fbec18Smcpowers goto CLEANUP;
337*f9fbec18Smcpowers }
338*f9fbec18Smcpowers /* 4: Verify that the order of the curve times the publicValue
339*f9fbec18Smcpowers * is the point at infinity.
340*f9fbec18Smcpowers */
341*f9fbec18Smcpowers MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
342*f9fbec18Smcpowers if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
343*f9fbec18Smcpowers res = MP_NO;
344*f9fbec18Smcpowers goto CLEANUP;
345*f9fbec18Smcpowers }
346*f9fbec18Smcpowers
347*f9fbec18Smcpowers res = MP_YES;
348*f9fbec18Smcpowers
349*f9fbec18Smcpowers CLEANUP:
350*f9fbec18Smcpowers mp_clear(&accl);
351*f9fbec18Smcpowers mp_clear(&accr);
352*f9fbec18Smcpowers mp_clear(&tmp);
353*f9fbec18Smcpowers mp_clear(&pxt);
354*f9fbec18Smcpowers mp_clear(&pyt);
355*f9fbec18Smcpowers return res;
356*f9fbec18Smcpowers }
357