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