Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / freebl / ecl / ec2_aff.c
blob64bb6fecc2290351258c5adb3b1f2b156a6ec887
1 /*
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the elliptic curve math library for binary polynomial field curves.
17 * The Initial Developer of the Original Code is
18 * Sun Microsystems, Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "ec2.h"
40 #include "mplogic.h"
41 #include "mp_gf2m.h"
42 #include <stdlib.h>
44 /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
45 mp_err
46 ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
49 if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
50 return MP_YES;
51 } else {
52 return MP_NO;
57 /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
58 mp_err
59 ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
61 mp_zero(px);
62 mp_zero(py);
63 return MP_OKAY;
66 /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P,
67 * Q, and R can all be identical. Uses affine coordinates. */
68 mp_err
69 ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
70 const mp_int *qy, mp_int *rx, mp_int *ry,
71 const ECGroup *group)
73 mp_err res = MP_OKAY;
74 mp_int lambda, tempx, tempy;
76 MP_DIGITS(&lambda) = 0;
77 MP_DIGITS(&tempx) = 0;
78 MP_DIGITS(&tempy) = 0;
79 MP_CHECKOK(mp_init(&lambda));
80 MP_CHECKOK(mp_init(&tempx));
81 MP_CHECKOK(mp_init(&tempy));
82 /* if P = inf, then R = Q */
83 if (ec_GF2m_pt_is_inf_aff(px, py) == 0) {
84 MP_CHECKOK(mp_copy(qx, rx));
85 MP_CHECKOK(mp_copy(qy, ry));
86 res = MP_OKAY;
87 goto CLEANUP;
89 /* if Q = inf, then R = P */
90 if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) {
91 MP_CHECKOK(mp_copy(px, rx));
92 MP_CHECKOK(mp_copy(py, ry));
93 res = MP_OKAY;
94 goto CLEANUP;
96 /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2
97 * + lambda + px + qx */
98 if (mp_cmp(px, qx) != 0) {
99 MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
100 MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
101 MP_CHECKOK(group->meth->
102 field_div(&tempy, &tempx, &lambda, group->meth));
103 MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
104 MP_CHECKOK(group->meth->
105 field_add(&tempx, &lambda, &tempx, group->meth));
106 MP_CHECKOK(group->meth->
107 field_add(&tempx, &group->curvea, &tempx, group->meth));
108 MP_CHECKOK(group->meth->
109 field_add(&tempx, px, &tempx, group->meth));
110 MP_CHECKOK(group->meth->
111 field_add(&tempx, qx, &tempx, group->meth));
112 } else {
113 /* if py != qy or qx = 0, then R = inf */
114 if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
115 mp_zero(rx);
116 mp_zero(ry);
117 res = MP_OKAY;
118 goto CLEANUP;
120 /* lambda = qx + qy / qx */
121 MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
122 MP_CHECKOK(group->meth->
123 field_add(&lambda, qx, &lambda, group->meth));
124 /* tempx = a + lambda^2 + lambda */
125 MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
126 MP_CHECKOK(group->meth->
127 field_add(&tempx, &lambda, &tempx, group->meth));
128 MP_CHECKOK(group->meth->
129 field_add(&tempx, &group->curvea, &tempx, group->meth));
131 /* ry = (qx + tempx) * lambda + tempx + qy */
132 MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
133 MP_CHECKOK(group->meth->
134 field_mul(&tempy, &lambda, &tempy, group->meth));
135 MP_CHECKOK(group->meth->
136 field_add(&tempy, &tempx, &tempy, group->meth));
137 MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
138 /* rx = tempx */
139 MP_CHECKOK(mp_copy(&tempx, rx));
141 CLEANUP:
142 mp_clear(&lambda);
143 mp_clear(&tempx);
144 mp_clear(&tempy);
145 return res;
148 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
149 * identical. Uses affine coordinates. */
150 mp_err
151 ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
152 const mp_int *qy, mp_int *rx, mp_int *ry,
153 const ECGroup *group)
155 mp_err res = MP_OKAY;
156 mp_int nqy;
158 MP_DIGITS(&nqy) = 0;
159 MP_CHECKOK(mp_init(&nqy));
160 /* nqy = qx+qy */
161 MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
162 MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
163 CLEANUP:
164 mp_clear(&nqy);
165 return res;
168 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
169 * affine coordinates. */
170 mp_err
171 ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
172 mp_int *ry, const ECGroup *group)
174 return group->point_add(px, py, px, py, rx, ry, group);
177 /* by default, this routine is unused and thus doesn't need to be compiled */
178 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
179 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
180 * R can be identical. Uses affine coordinates. */
181 mp_err
182 ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
183 mp_int *rx, mp_int *ry, const ECGroup *group)
185 mp_err res = MP_OKAY;
186 mp_int k, k3, qx, qy, sx, sy;
187 int b1, b3, i, l;
189 MP_DIGITS(&k) = 0;
190 MP_DIGITS(&k3) = 0;
191 MP_DIGITS(&qx) = 0;
192 MP_DIGITS(&qy) = 0;
193 MP_DIGITS(&sx) = 0;
194 MP_DIGITS(&sy) = 0;
195 MP_CHECKOK(mp_init(&k));
196 MP_CHECKOK(mp_init(&k3));
197 MP_CHECKOK(mp_init(&qx));
198 MP_CHECKOK(mp_init(&qy));
199 MP_CHECKOK(mp_init(&sx));
200 MP_CHECKOK(mp_init(&sy));
202 /* if n = 0 then r = inf */
203 if (mp_cmp_z(n) == 0) {
204 mp_zero(rx);
205 mp_zero(ry);
206 res = MP_OKAY;
207 goto CLEANUP;
209 /* Q = P, k = n */
210 MP_CHECKOK(mp_copy(px, &qx));
211 MP_CHECKOK(mp_copy(py, &qy));
212 MP_CHECKOK(mp_copy(n, &k));
213 /* if n < 0 then Q = -Q, k = -k */
214 if (mp_cmp_z(n) < 0) {
215 MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
216 MP_CHECKOK(mp_neg(&k, &k));
218 #ifdef ECL_DEBUG /* basic double and add method */
219 l = mpl_significant_bits(&k) - 1;
220 MP_CHECKOK(mp_copy(&qx, &sx));
221 MP_CHECKOK(mp_copy(&qy, &sy));
222 for (i = l - 1; i >= 0; i--) {
223 /* S = 2S */
224 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
225 /* if k_i = 1, then S = S + Q */
226 if (mpl_get_bit(&k, i) != 0) {
227 MP_CHECKOK(group->
228 point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
231 #else /* double and add/subtract method from
232 * standard */
233 /* k3 = 3 * k */
234 MP_CHECKOK(mp_set_int(&k3, 3));
235 MP_CHECKOK(mp_mul(&k, &k3, &k3));
236 /* S = Q */
237 MP_CHECKOK(mp_copy(&qx, &sx));
238 MP_CHECKOK(mp_copy(&qy, &sy));
239 /* l = index of high order bit in binary representation of 3*k */
240 l = mpl_significant_bits(&k3) - 1;
241 /* for i = l-1 downto 1 */
242 for (i = l - 1; i >= 1; i--) {
243 /* S = 2S */
244 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
245 b3 = MP_GET_BIT(&k3, i);
246 b1 = MP_GET_BIT(&k, i);
247 /* if k3_i = 1 and k_i = 0, then S = S + Q */
248 if ((b3 == 1) && (b1 == 0)) {
249 MP_CHECKOK(group->
250 point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
251 /* if k3_i = 0 and k_i = 1, then S = S - Q */
252 } else if ((b3 == 0) && (b1 == 1)) {
253 MP_CHECKOK(group->
254 point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
257 #endif
258 /* output S */
259 MP_CHECKOK(mp_copy(&sx, rx));
260 MP_CHECKOK(mp_copy(&sy, ry));
262 CLEANUP:
263 mp_clear(&k);
264 mp_clear(&k3);
265 mp_clear(&qx);
266 mp_clear(&qy);
267 mp_clear(&sx);
268 mp_clear(&sy);
269 return res;
271 #endif
273 /* Validates a point on a GF2m curve. */
274 mp_err
275 ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
277 mp_err res = MP_NO;
278 mp_int accl, accr, tmp, pxt, pyt;
280 MP_DIGITS(&accl) = 0;
281 MP_DIGITS(&accr) = 0;
282 MP_DIGITS(&tmp) = 0;
283 MP_DIGITS(&pxt) = 0;
284 MP_DIGITS(&pyt) = 0;
285 MP_CHECKOK(mp_init(&accl));
286 MP_CHECKOK(mp_init(&accr));
287 MP_CHECKOK(mp_init(&tmp));
288 MP_CHECKOK(mp_init(&pxt));
289 MP_CHECKOK(mp_init(&pyt));
291 /* 1: Verify that publicValue is not the point at infinity */
292 if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
293 res = MP_NO;
294 goto CLEANUP;
296 /* 2: Verify that the coordinates of publicValue are elements
297 * of the field.
299 if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
300 (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
301 res = MP_NO;
302 goto CLEANUP;
304 /* 3: Verify that publicValue is on the curve. */
305 if (group->meth->field_enc) {
306 group->meth->field_enc(px, &pxt, group->meth);
307 group->meth->field_enc(py, &pyt, group->meth);
308 } else {
309 mp_copy(px, &pxt);
310 mp_copy(py, &pyt);
312 /* left-hand side: y^2 + x*y */
313 MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
314 MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
315 MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
316 /* right-hand side: x^3 + a*x^2 + b */
317 MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
318 MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
319 MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
320 MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
321 MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
322 /* check LHS - RHS == 0 */
323 MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
324 if (mp_cmp_z(&accr) != 0) {
325 res = MP_NO;
326 goto CLEANUP;
328 /* 4: Verify that the order of the curve times the publicValue
329 * is the point at infinity.
331 MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
332 if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
333 res = MP_NO;
334 goto CLEANUP;
337 res = MP_YES;
339 CLEANUP:
340 mp_clear(&accl);
341 mp_clear(&accr);
342 mp_clear(&tmp);
343 mp_clear(&pxt);
344 mp_clear(&pyt);
345 return res;