b0ab12e6 |
1 | /* -*-c-*- |
2 | * |
391faf42 |
3 | * $Id: ec.h,v 1.6 2004/03/22 02:19:10 mdw Exp $ |
b0ab12e6 |
4 | * |
5 | * Elliptic curve definitions |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: ec.h,v $ |
391faf42 |
33 | * Revision 1.6 2004/03/22 02:19:10 mdw |
34 | * Rationalise the sliding-window threshold. Drop guarantee that right |
35 | * arguments to EC @add@ are canonical, and fix up projective implementations |
36 | * to cope. |
37 | * |
c3caa2fa |
38 | * Revision 1.5 2004/03/21 22:52:06 mdw |
39 | * Merge and close elliptic curve branch. |
40 | * |
ceb3f0c0 |
41 | * Revision 1.4.4.3 2004/03/21 22:39:46 mdw |
42 | * Elliptic curves on binary fields work. |
43 | * |
8823192f |
44 | * Revision 1.4.4.2 2004/03/20 00:13:31 mdw |
45 | * Projective coordinates for prime curves |
46 | * |
dbfee00a |
47 | * Revision 1.4.4.1 2003/06/10 13:43:53 mdw |
48 | * Simple (non-projective) curves over prime fields now seem to work. |
49 | * |
41cb1beb |
50 | * Revision 1.4 2003/05/15 23:25:59 mdw |
51 | * Make elliptic curve stuff build. |
52 | * |
b085fd91 |
53 | * Revision 1.3 2002/01/13 13:48:44 mdw |
54 | * Further progress. |
55 | * |
41a324a7 |
56 | * Revision 1.2 2001/05/07 17:29:44 mdw |
57 | * Treat projective coordinates as an internal representation. Various |
58 | * minor interface changes. |
59 | * |
b0ab12e6 |
60 | * Revision 1.1 2001/04/29 18:12:33 mdw |
61 | * Prototype version. |
62 | * |
63 | */ |
64 | |
65 | #ifndef CATACOMB_EC_H |
66 | #define CATACOMB_EC_H |
67 | |
68 | #ifdef __cplusplus |
69 | extern "C" { |
70 | #endif |
71 | |
72 | /*----- Header files ------------------------------------------------------*/ |
73 | |
74 | #include "field.h" |
75 | #include "mp.h" |
76 | |
77 | /*----- Data structures ---------------------------------------------------*/ |
78 | |
b085fd91 |
79 | /* --- An elliptic curve representation --- */ |
80 | |
b0ab12e6 |
81 | typedef struct ec_curve { |
82 | const struct ec_ops *ops; /* Curve operations */ |
83 | field *f; /* Underlying field structure */ |
84 | } ec_curve; |
85 | |
b085fd91 |
86 | /* --- An elliptic curve point --- */ |
87 | |
b0ab12e6 |
88 | typedef struct ec { |
89 | mp *x, *y; /* Point coordinates */ |
90 | mp *z; /* Common denominator (or null) */ |
91 | } ec; |
92 | |
b085fd91 |
93 | /* --- A factor for simultaneous multiplication --- */ |
94 | |
95 | typedef struct ec_mulfactor { |
96 | ec base; /* The point */ |
41cb1beb |
97 | mp *exp; /* The exponent */ |
b085fd91 |
98 | } ec_mulfactor; |
99 | |
8823192f |
100 | /* --- Elliptic curve operations --- * |
101 | * |
102 | * All operations (apart from @destroy@ and @in@) are guaranteed to be |
391faf42 |
103 | * performed on internal representations of points. |
104 | * |
105 | * (Historical note. We used to guarantee that the second to @add@ and @mul@ |
106 | * was the output of @in@ or @fix@, but this canonification turned out to |
107 | * make the precomputation in @ec_exp@ too slow. Projective implementations |
108 | * must therefore cope with a pair of arbitrary points.) |
8823192f |
109 | */ |
b085fd91 |
110 | |
b0ab12e6 |
111 | typedef struct ec_ops { |
112 | void (*destroy)(ec_curve */*c*/); |
41a324a7 |
113 | ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
114 | ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
8823192f |
115 | ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
41a324a7 |
116 | ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/); |
b085fd91 |
117 | ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
41a324a7 |
118 | ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/); |
b085fd91 |
119 | ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/); |
41a324a7 |
120 | ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
8823192f |
121 | int (*check)(ec_curve */*c*/, const ec */*p*/); |
b0ab12e6 |
122 | } ec_ops; |
123 | |
41a324a7 |
124 | #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p)) |
dbfee00a |
125 | #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p)) |
8823192f |
126 | #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p)) |
41a324a7 |
127 | |
b0ab12e6 |
128 | #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x)) |
b085fd91 |
129 | #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x)) |
b0ab12e6 |
130 | #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q)) |
b085fd91 |
131 | #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q)) |
b0ab12e6 |
132 | #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p)) |
8823192f |
133 | #define EC_CHECK(c, p) (c)->ops->check((c), (p)) |
b0ab12e6 |
134 | |
135 | /*----- Simple memory management things -----------------------------------*/ |
136 | |
137 | /* --- @ec_create@ --- * |
138 | * |
139 | * Arguments: @ec *p@ = pointer to an elliptic-curve point |
140 | * |
41a324a7 |
141 | * Returns: The argument @p@. |
b0ab12e6 |
142 | * |
143 | * Use: Initializes a new point. The initial value is the additive |
144 | * identity (which is universal for all curves). |
145 | */ |
146 | |
147 | #define EC_INIT { MP_NEW, MP_NEW, MP_NEW } |
148 | |
149 | #define EC_CREATE(p) do { \ |
150 | ec *_p = (p); \ |
151 | _p->x = _p->y = _p->z = MP_NEW; \ |
152 | } while (0) |
153 | |
41a324a7 |
154 | extern ec *ec_create(ec */*p*/); |
b0ab12e6 |
155 | |
156 | /* --- @ec_destroy@ --- * |
157 | * |
158 | * Arguments: @ec *p@ = pointer to an elliptic-curve point |
159 | * |
160 | * Returns: --- |
161 | * |
162 | * Use: Destroys a point, making it invalid. |
163 | */ |
164 | |
165 | #define EC_DESTROY(p) do { \ |
166 | ec *_p = (p); \ |
167 | if (!EC_ATINF(_p)) { \ |
168 | MP_DROP(_p->x); \ |
169 | MP_DROP(_p->y); \ |
170 | if (_p->z) MP_DROP(_p->z); \ |
171 | } \ |
172 | } while (0) |
173 | |
174 | extern void ec_destroy(ec */*p*/); |
175 | |
176 | /* --- @ec_atinf@ --- * |
177 | * |
178 | * Arguments: @const ec *p@ = pointer to a point |
179 | * |
180 | * Returns: Nonzero if %$p = O$% is the point at infinity, zero |
181 | * otherwise. |
182 | */ |
183 | |
184 | #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC) |
185 | |
186 | extern int ec_atinf(const ec */*p*/); |
187 | |
188 | /* --- @ec_setinf@ --- * |
189 | * |
190 | * Arguments: @ec *p@ = pointer to a point |
191 | * |
41a324a7 |
192 | * Returns: The argument @p@. |
b0ab12e6 |
193 | * |
194 | * Use: Sets the given point to be the point %$O$% at infinity. |
195 | */ |
196 | |
197 | #define EC_SETINF(p) do { \ |
198 | ec *_p = (p); \ |
199 | if (!EC_ATINF(_p)) { \ |
200 | MP_DROP(_p->x); \ |
201 | MP_DROP(_p->y); \ |
202 | if (_p->z) MP_DROP(_p->z); \ |
203 | _p->x = _p->y = _p->z = MP_NEW; \ |
204 | _p->y = MP_NEW; \ |
205 | _p->z = MP_NEW; \ |
206 | } \ |
207 | } while (0) |
208 | |
41a324a7 |
209 | extern ec *ec_setinf(ec */*p*/); |
b0ab12e6 |
210 | |
211 | /* --- @ec_copy@ --- * |
212 | * |
213 | * Arguments: @ec *d@ = pointer to destination point |
214 | * @const ec *p@ = pointer to source point |
215 | * |
41a324a7 |
216 | * Returns: The destination @d@. |
b0ab12e6 |
217 | * |
218 | * Use: Creates a copy of an elliptic curve point. |
219 | */ |
220 | |
221 | #define EC_COPY(d, p) do { \ |
222 | ec *_d = (d); \ |
223 | const ec *_p = (p); \ |
224 | if (d != p) { \ |
225 | EC_DESTROY(d); \ |
226 | if (EC_ATINF(p)) \ |
227 | _d->x = _d->y = _d->z = MP_NEW; \ |
228 | else { \ |
b085fd91 |
229 | _d->x = MP_COPY(_p->x); \ |
230 | _d->y = MP_COPY(_p->y); \ |
231 | _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \ |
b0ab12e6 |
232 | } \ |
233 | } \ |
234 | } while (0) |
235 | |
41a324a7 |
236 | extern ec *ec_copy(ec */*d*/, const ec */*p*/); |
b0ab12e6 |
237 | |
238 | /*----- Interesting arithmetic --------------------------------------------*/ |
239 | |
b0ab12e6 |
240 | /* --- @ec_find@ --- * |
241 | * |
242 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
243 | * @ec *d@ = pointer to the destination point |
244 | * @mp *x@ = a possible x-coordinate |
245 | * |
b085fd91 |
246 | * Returns: The destination if OK, or null if no point was found. |
b0ab12e6 |
247 | * |
b085fd91 |
248 | * Use: Finds a point on an elliptic curve with a given |
249 | * x-coordinate. If there is no point with the given |
250 | * %$x$%-coordinate, a null pointer is returned and the |
251 | * destination is left invalid. |
b0ab12e6 |
252 | */ |
253 | |
b085fd91 |
254 | extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/); |
255 | |
256 | /* --- @ec_neg@ --- * |
257 | * |
258 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
259 | * @ec *d@ = pointer to the destination point |
260 | * @const ec *p@ = pointer to the operand point |
261 | * |
262 | * Returns: The destination point. |
263 | * |
264 | * Use: Computes the negation of the given point. |
265 | */ |
266 | |
267 | extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
268 | |
269 | /* --- @ec_add@ --- * |
270 | * |
271 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
272 | * @ec *d@ = pointer to the destination point |
273 | * @const ec *p, *q@ = pointers to the operand points |
274 | * |
41a324a7 |
275 | * Returns: The destination @d@. |
b0ab12e6 |
276 | * |
277 | * Use: Adds two points on an elliptic curve. |
278 | */ |
279 | |
41a324a7 |
280 | extern ec *ec_add(ec_curve */*c*/, ec */*d*/, |
281 | const ec */*p*/, const ec */*q*/); |
b0ab12e6 |
282 | |
b085fd91 |
283 | /* --- @ec_sub@ --- * |
284 | * |
285 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
286 | * @ec *d@ = pointer to the destination point |
287 | * @const ec *p, *q@ = pointers to the operand points |
288 | * |
289 | * Returns: The destination @d@. |
290 | * |
291 | * Use: Subtracts one point from another on an elliptic curve. |
292 | */ |
293 | |
294 | extern ec *ec_sub(ec_curve */*c*/, ec */*d*/, |
295 | const ec */*p*/, const ec */*q*/); |
296 | |
b0ab12e6 |
297 | /* --- @ec_dbl@ --- * |
298 | * |
299 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
300 | * @ec *d@ = pointer to the destination point |
301 | * @const ec *p@ = pointer to the operand point |
302 | * |
41a324a7 |
303 | * Returns: The destination @d@. |
b0ab12e6 |
304 | * |
305 | * Use: Doubles a point on an elliptic curve. |
306 | */ |
307 | |
41a324a7 |
308 | extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
309 | |
8823192f |
310 | /* --- @ec_check@ --- * |
311 | * |
312 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
313 | * @const ec *p@ = pointer to the point |
314 | * |
315 | * Returns: Zero if OK, nonzero if this is an invalid point. |
316 | * |
317 | * Use: Checks that a point is actually on an elliptic curve. |
318 | */ |
319 | |
320 | extern int ec_check(ec_curve */*c*/, const ec */*p*/); |
321 | |
b085fd91 |
322 | /* --- @ec_mul@, @ec_imul@ --- * |
b0ab12e6 |
323 | * |
324 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
325 | * @ec *d@ = pointer to the destination point |
326 | * @const ec *p@ = pointer to the generator point |
327 | * @mp *n@ = integer multiplier |
328 | * |
41a324a7 |
329 | * Returns: The destination @d@. |
b0ab12e6 |
330 | * |
b085fd91 |
331 | * Use: Multiplies a point by a scalar, returning %$n p$%. The |
332 | * @imul@ variant uses internal representations for argument |
333 | * and result. |
b0ab12e6 |
334 | */ |
335 | |
41a324a7 |
336 | extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/); |
b085fd91 |
337 | extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/); |
338 | |
339 | /* --- @ec_mmul@, @ec_immul@ --- * |
340 | * |
341 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
342 | * @ec *d@ = pointer to the destination point |
343 | * @const ec_mulfactor *f@ = pointer to vector of factors |
344 | * @size_t n@ = number of factors |
345 | * |
346 | * Returns: The destination @d@. |
347 | * |
348 | * Use: Does simultaneous point multiplication. The @immul@ variant |
349 | * uses internal representations for arguments and result. |
350 | */ |
351 | |
352 | extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/, |
353 | const ec_mulfactor */*f*/, size_t /*n*/); |
354 | extern ec *ec_immul(ec_curve */*c*/, ec */*d*/, |
355 | const ec_mulfactor */*f*/, size_t /*n*/); |
41a324a7 |
356 | |
357 | /*----- Standard curve operations -----------------------------------------*/ |
358 | |
8823192f |
359 | /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- * |
41a324a7 |
360 | * |
361 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
362 | * @ec *d@ = pointer to the destination |
363 | * @const ec *p@ = pointer to a source point |
364 | * |
365 | * Returns: The destination @d@. |
366 | * |
367 | * Use: An identity operation if your curve has no internal |
368 | * representation. (The field internal representation is still |
369 | * used.) |
370 | */ |
371 | |
372 | extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
373 | extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
8823192f |
374 | extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
41a324a7 |
375 | |
8823192f |
376 | /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- * |
41a324a7 |
377 | * |
378 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
379 | * @ec *d@ = pointer to the destination |
380 | * @const ec *p@ = pointer to a source point |
381 | * |
382 | * Returns: The destination @d@. |
383 | * |
384 | * Use: Conversion functions if your curve operations use a |
385 | * projective representation. |
386 | */ |
387 | |
388 | extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
389 | extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
8823192f |
390 | extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
391 | |
b085fd91 |
392 | /* --- @ec_stdsub@ --- * |
393 | * |
394 | * Arguments: @ec_curve *c@ = pointer to an elliptic curve |
395 | * @ec *d@ = pointer to the destination |
41cb1beb |
396 | * @const ec *p, *q@ = the operand points |
b085fd91 |
397 | * |
398 | * Returns: The destination @d@. |
399 | * |
400 | * Use: Standard point subtraction operation, in terms of negation |
401 | * and addition. This isn't as efficient as a ready-made |
402 | * subtraction operator. |
403 | */ |
404 | |
41cb1beb |
405 | extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/, |
406 | const ec */*p*/, const ec */*q*/); |
b085fd91 |
407 | |
b0ab12e6 |
408 | /*----- Creating curves ---------------------------------------------------*/ |
409 | |
b085fd91 |
410 | /* --- @ec_destroycurve@ --- * |
411 | * |
412 | * Arguments: @ec_curve *c@ = pointer to an ellptic curve |
413 | * |
414 | * Returns: --- |
415 | * |
416 | * Use: Destroys a description of an elliptic curve. |
417 | */ |
418 | |
419 | extern void ec_destroycurve(ec_curve */*c*/); |
420 | |
421 | /* --- @ec_prime@, @ec_primeproj@ --- * |
b0ab12e6 |
422 | * |
dbfee00a |
423 | * Arguments: @field *f@ = the underlying field for this elliptic curve |
b0ab12e6 |
424 | * @mp *a, *b@ = the coefficients for this curve |
425 | * |
426 | * Returns: A pointer to the curve. |
427 | * |
428 | * Use: Creates a curve structure for an elliptic curve defined over |
b085fd91 |
429 | * a prime field. The @primeproj@ variant uses projective |
430 | * coordinates, which can be a win. |
b0ab12e6 |
431 | */ |
432 | |
433 | extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/); |
b085fd91 |
434 | extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/); |
b0ab12e6 |
435 | |
ceb3f0c0 |
436 | /* --- @ec_bin@, @ec_binproj@ --- * |
b0ab12e6 |
437 | * |
438 | * Arguments: @field *f@ = the underlying field for this elliptic curve |
439 | * @mp *a, *b@ = the coefficients for this curve |
440 | * |
441 | * Returns: A pointer to the curve. |
442 | * |
ceb3f0c0 |
443 | * Use: Creates a curve structure for an elliptic curve defined over |
444 | * a binary field. The @binproj@ variant uses projective |
445 | * coordinates, which can be a win. |
b0ab12e6 |
446 | */ |
447 | |
448 | extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/); |
ceb3f0c0 |
449 | extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/); |
b0ab12e6 |
450 | |
451 | /*----- That's all, folks -------------------------------------------------*/ |
452 | |
453 | #ifdef __cplusplus |
454 | } |
455 | #endif |
456 | |
457 | #endif |