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
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.
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 ***** */
44 /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
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)) {
57 /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
59 ec_GF2m_pt_set_inf_aff(mp_int
*px
, mp_int
*py
)
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. */
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
,
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
));
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
));
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
));
113 /* if py != qy or qx = 0, then R = inf */
114 if (((mp_cmp(py
, qy
) != 0)) || (mp_cmp_z(qx
) == 0)) {
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
));
139 MP_CHECKOK(mp_copy(&tempx
, rx
));
148 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
149 * identical. Uses affine coordinates. */
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
;
159 MP_CHECKOK(mp_init(&nqy
));
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
));
168 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
169 * affine coordinates. */
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. */
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
;
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) {
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
--) {
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) {
228 point_add(&sx
, &sy
, &qx
, &qy
, &sx
, &sy
, group
));
231 #else /* double and add/subtract method from
234 MP_CHECKOK(mp_set_int(&k3
, 3));
235 MP_CHECKOK(mp_mul(&k
, &k3
, &k3
));
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
--) {
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)) {
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)) {
254 point_sub(&sx
, &sy
, &qx
, &qy
, &sx
, &sy
, group
));
259 MP_CHECKOK(mp_copy(&sx
, rx
));
260 MP_CHECKOK(mp_copy(&sy
, ry
));
273 /* Validates a point on a GF2m curve. */
275 ec_GF2m_validate_point(const mp_int
*px
, const mp_int
*py
, const ECGroup
*group
)
278 mp_int accl
, accr
, tmp
, pxt
, pyt
;
280 MP_DIGITS(&accl
) = 0;
281 MP_DIGITS(&accr
) = 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
) {
296 /* 2: Verify that the coordinates of publicValue are elements
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)) {
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
);
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) {
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
) {