General robustification.
[u/mdw/catacomb] / ec.h
1 /* -*-c-*-
2 *
3 * $Id: ec.h,v 1.10 2004/04/03 03:32:05 mdw Exp $
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 $
33 * Revision 1.10 2004/04/03 03:32:05 mdw
34 * General robustification.
35 *
36 * Revision 1.9 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
42 *
43 * Revision 1.8 2004/03/27 17:54:11 mdw
44 * Standard curves and curve checking.
45 *
46 * Revision 1.7 2004/03/23 15:19:32 mdw
47 * Test elliptic curves more thoroughly.
48 *
49 * Revision 1.6 2004/03/22 02:19:10 mdw
50 * Rationalise the sliding-window threshold. Drop guarantee that right
51 * arguments to EC @add@ are canonical, and fix up projective implementations
52 * to cope.
53 *
54 * Revision 1.5 2004/03/21 22:52:06 mdw
55 * Merge and close elliptic curve branch.
56 *
57 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
58 * Elliptic curves on binary fields work.
59 *
60 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
61 * Projective coordinates for prime curves
62 *
63 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
64 * Simple (non-projective) curves over prime fields now seem to work.
65 *
66 * Revision 1.4 2003/05/15 23:25:59 mdw
67 * Make elliptic curve stuff build.
68 *
69 * Revision 1.3 2002/01/13 13:48:44 mdw
70 * Further progress.
71 *
72 * Revision 1.2 2001/05/07 17:29:44 mdw
73 * Treat projective coordinates as an internal representation. Various
74 * minor interface changes.
75 *
76 * Revision 1.1 2001/04/29 18:12:33 mdw
77 * Prototype version.
78 *
79 */
80
81 #ifndef CATACOMB_EC_H
82 #define CATACOMB_EC_H
83
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87
88 /*----- Header files ------------------------------------------------------*/
89
90 #ifndef CATACOMB_FIELD_H
91 # include "field.h"
92 #endif
93
94 #ifndef CATACOMB_MP_H
95 # include "mp.h"
96 #endif
97
98 #ifndef CATACOMB_QDPARSE_H
99 # include "qdparse.h"
100 #endif
101
102 /*----- Data structures ---------------------------------------------------*/
103
104 /* --- An elliptic curve representation --- */
105
106 typedef struct ec_curve {
107 const struct ec_ops *ops; /* Curve operations */
108 field *f; /* Underlying field structure */
109 mp *a, *b; /* Standard params (internal form) */
110 } ec_curve;
111
112 /* --- An elliptic curve point --- */
113
114 typedef struct ec {
115 mp *x, *y; /* Point coordinates */
116 mp *z; /* Common denominator (or null) */
117 } ec;
118
119 /* --- A factor for simultaneous multiplication --- */
120
121 typedef struct ec_mulfactor {
122 ec base; /* The point */
123 mp *exp; /* The exponent */
124 } ec_mulfactor;
125
126 /* --- Elliptic curve operations --- *
127 *
128 * All operations (apart from @destroy@ and @in@) are guaranteed to be
129 * performed on internal representations of points.
130 *
131 * (Historical note. We used to guarantee that the second to @add@ and @mul@
132 * was the output of @in@ or @fix@, but this canonification turned out to
133 * make the precomputation in @ec_exp@ too slow. Projective implementations
134 * must therefore cope with a pair of arbitrary points.)
135 */
136
137 typedef struct ec_ops {
138 void (*destroy)(ec_curve */*c*/);
139 int (*samep)(ec_curve */*c*/, ec_curve */*d*/);
140 ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
141 ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
142 ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
143 ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
144 ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
145 ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
146 ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
147 ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
148 int (*check)(ec_curve */*c*/, const ec */*p*/);
149 } ec_ops;
150
151 #define EC_SAMEP(c, d) (c)->ops->samep((c), (d))
152 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
153 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
154 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
155
156 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
157 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
158 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
159 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
160 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
161 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
162
163 /* --- Elliptic curve parameters --- */
164
165 typedef struct ec_info {
166 ec_curve *c; /* The actual curve */
167 ec g; /* The common point */
168 mp *r; /* Order of %$g$% */
169 mp *h; /* Cofactor %$h = \#E/r$% */
170 } ec_info;
171
172 /*----- Simple memory management things -----------------------------------*/
173
174 /* --- @ec_create@ --- *
175 *
176 * Arguments: @ec *p@ = pointer to an elliptic-curve point
177 *
178 * Returns: The argument @p@.
179 *
180 * Use: Initializes a new point. The initial value is the additive
181 * identity (which is universal for all curves).
182 */
183
184 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
185
186 #define EC_CREATE(p) do { \
187 ec *_p = (p); \
188 _p->x = _p->y = _p->z = MP_NEW; \
189 } while (0)
190
191 extern ec *ec_create(ec */*p*/);
192
193 /* --- @ec_destroy@ --- *
194 *
195 * Arguments: @ec *p@ = pointer to an elliptic-curve point
196 *
197 * Returns: ---
198 *
199 * Use: Destroys a point, making it invalid.
200 */
201
202 #define EC_DESTROY(p) do { \
203 ec *_p = (p); \
204 if (!EC_ATINF(_p)) { \
205 MP_DROP(_p->x); \
206 MP_DROP(_p->y); \
207 if (_p->z) MP_DROP(_p->z); \
208 } \
209 } while (0)
210
211 extern void ec_destroy(ec */*p*/);
212
213 /* --- @ec_atinf@ --- *
214 *
215 * Arguments: @const ec *p@ = pointer to a point
216 *
217 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
218 * otherwise.
219 */
220
221 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
222
223 extern int ec_atinf(const ec */*p*/);
224
225 /* --- @ec_setinf@ --- *
226 *
227 * Arguments: @ec *p@ = pointer to a point
228 *
229 * Returns: The argument @p@.
230 *
231 * Use: Sets the given point to be the point %$O$% at infinity.
232 */
233
234 #define EC_SETINF(p) do { \
235 ec *_p = (p); \
236 if (!EC_ATINF(_p)) { \
237 MP_DROP(_p->x); \
238 MP_DROP(_p->y); \
239 if (_p->z) MP_DROP(_p->z); \
240 _p->x = _p->y = _p->z = MP_NEW; \
241 _p->y = MP_NEW; \
242 _p->z = MP_NEW; \
243 } \
244 } while (0)
245
246 extern ec *ec_setinf(ec */*p*/);
247
248 /* --- @ec_copy@ --- *
249 *
250 * Arguments: @ec *d@ = pointer to destination point
251 * @const ec *p@ = pointer to source point
252 *
253 * Returns: The destination @d@.
254 *
255 * Use: Creates a copy of an elliptic curve point.
256 */
257
258 #define EC_COPY(d, p) do { \
259 ec *_d = (d); \
260 const ec *_p = (p); \
261 if (d != p) { \
262 EC_DESTROY(d); \
263 if (EC_ATINF(p)) \
264 _d->x = _d->y = _d->z = MP_NEW; \
265 else { \
266 _d->x = MP_COPY(_p->x); \
267 _d->y = MP_COPY(_p->y); \
268 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
269 } \
270 } \
271 } while (0)
272
273 extern ec *ec_copy(ec */*d*/, const ec */*p*/);
274
275 /* --- @ec_eq@ --- *
276 *
277 * Arguments: @const ec *p, *q@ = two points
278 *
279 * Returns: Nonzero if the points are equal. Compares external-format
280 * points.
281 */
282
283 #define EC_EQ(p, q) \
284 ((EC_ATINF(p) && EC_ATINF(q)) || \
285 (!EC_ATINF(p) && !EC_ATINF(q) && \
286 MP_EQ((p)->x, (q)->x) && \
287 MP_EQ((p)->y, (q)->y)))
288
289 extern int ec_eq(const ec *p, const ec *q);
290
291 /*----- Interesting arithmetic --------------------------------------------*/
292
293 /* --- @ec_samep@ --- *
294 *
295 * Arguments: @ec_curve *c, *d@ = two elliptic curves
296 *
297 * Returns: Nonzero if the curves are identical (not just isomorphic).
298 *
299 * Use: Checks for sameness of curves. This function does the full
300 * check, not just the curve-type-specific check done by the
301 * @sampep@ field operation.
302 */
303
304 extern int ec_samep(ec_curve */*c*/, ec_curve */*d*/);
305
306 /* --- @ec_find@ --- *
307 *
308 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
309 * @ec *d@ = pointer to the destination point
310 * @mp *x@ = a possible x-coordinate
311 *
312 * Returns: The destination if OK, or null if no point was found.
313 *
314 * Use: Finds a point on an elliptic curve with a given
315 * x-coordinate. If there is no point with the given
316 * %$x$%-coordinate, a null pointer is returned and the
317 * destination is left invalid.
318 */
319
320 extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
321
322 /* --- @ec_rand@ --- *
323 *
324 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
325 * @ec *d@ = pointer to the destination point
326 * @grand *r@ = random number source
327 *
328 * Returns: The destination @d@.
329 *
330 * Use: Finds a random point on the given curve.
331 */
332
333 extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
334
335 /* --- @ec_neg@ --- *
336 *
337 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
338 * @ec *d@ = pointer to the destination point
339 * @const ec *p@ = pointer to the operand point
340 *
341 * Returns: The destination point.
342 *
343 * Use: Computes the negation of the given point.
344 */
345
346 extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
347
348 /* --- @ec_add@ --- *
349 *
350 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
351 * @ec *d@ = pointer to the destination point
352 * @const ec *p, *q@ = pointers to the operand points
353 *
354 * Returns: The destination @d@.
355 *
356 * Use: Adds two points on an elliptic curve.
357 */
358
359 extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
360 const ec */*p*/, const ec */*q*/);
361
362 /* --- @ec_sub@ --- *
363 *
364 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
365 * @ec *d@ = pointer to the destination point
366 * @const ec *p, *q@ = pointers to the operand points
367 *
368 * Returns: The destination @d@.
369 *
370 * Use: Subtracts one point from another on an elliptic curve.
371 */
372
373 extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
374 const ec */*p*/, const ec */*q*/);
375
376 /* --- @ec_dbl@ --- *
377 *
378 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
379 * @ec *d@ = pointer to the destination point
380 * @const ec *p@ = pointer to the operand point
381 *
382 * Returns: The destination @d@.
383 *
384 * Use: Doubles a point on an elliptic curve.
385 */
386
387 extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
388
389 /* --- @ec_check@ --- *
390 *
391 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
392 * @const ec *p@ = pointer to the point
393 *
394 * Returns: Zero if OK, nonzero if this is an invalid point.
395 *
396 * Use: Checks that a point is actually on an elliptic curve.
397 */
398
399 extern int ec_check(ec_curve */*c*/, const ec */*p*/);
400
401 /* --- @ec_mul@, @ec_imul@ --- *
402 *
403 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
404 * @ec *d@ = pointer to the destination point
405 * @const ec *p@ = pointer to the generator point
406 * @mp *n@ = integer multiplier
407 *
408 * Returns: The destination @d@.
409 *
410 * Use: Multiplies a point by a scalar, returning %$n p$%. The
411 * @imul@ variant uses internal representations for argument
412 * and result.
413 */
414
415 extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
416 extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
417
418 /* --- @ec_mmul@, @ec_immul@ --- *
419 *
420 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
421 * @ec *d@ = pointer to the destination point
422 * @const ec_mulfactor *f@ = pointer to vector of factors
423 * @size_t n@ = number of factors
424 *
425 * Returns: The destination @d@.
426 *
427 * Use: Does simultaneous point multiplication. The @immul@ variant
428 * uses internal representations for arguments and result.
429 */
430
431 extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/,
432 const ec_mulfactor */*f*/, size_t /*n*/);
433 extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
434 const ec_mulfactor */*f*/, size_t /*n*/);
435
436 /*----- Standard curve operations -----------------------------------------*/
437
438 /* --- @ec_stdsamep@ --- *
439 *
440 * Arguments: @ec_curve *c, *d@ = two elliptic curves
441 *
442 * Returns: Nonzero if the curves are identical (not just isomorphic).
443 *
444 * Use: Simple sameness check on @a@ and @b@ curve members.
445 */
446
447 extern int ec_stdsamep(ec_curve */*c*/, ec_curve */*d*/);
448
449 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
450 *
451 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
452 * @ec *d@ = pointer to the destination
453 * @const ec *p@ = pointer to a source point
454 *
455 * Returns: The destination @d@.
456 *
457 * Use: An identity operation if your curve has no internal
458 * representation. (The field internal representation is still
459 * used.)
460 */
461
462 extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
463 extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
464 extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
465
466 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
467 *
468 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
469 * @ec *d@ = pointer to the destination
470 * @const ec *p@ = pointer to a source point
471 *
472 * Returns: The destination @d@.
473 *
474 * Use: Conversion functions if your curve operations use a
475 * projective representation.
476 */
477
478 extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
479 extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
480 extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
481
482 /* --- @ec_stdsub@ --- *
483 *
484 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
485 * @ec *d@ = pointer to the destination
486 * @const ec *p, *q@ = the operand points
487 *
488 * Returns: The destination @d@.
489 *
490 * Use: Standard point subtraction operation, in terms of negation
491 * and addition. This isn't as efficient as a ready-made
492 * subtraction operator.
493 */
494
495 extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
496 const ec */*p*/, const ec */*q*/);
497
498 /*----- Creating curves ---------------------------------------------------*/
499
500 /* --- @ec_destroycurve@ --- *
501 *
502 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
503 *
504 * Returns: ---
505 *
506 * Use: Destroys a description of an elliptic curve.
507 */
508
509 extern void ec_destroycurve(ec_curve */*c*/);
510
511 /* --- @ec_prime@, @ec_primeproj@ --- *
512 *
513 * Arguments: @field *f@ = the underlying field for this elliptic curve
514 * @mp *a, *b@ = the coefficients for this curve
515 *
516 * Returns: A pointer to the curve, or null.
517 *
518 * Use: Creates a curve structure for an elliptic curve defined over
519 * a prime field. The @primeproj@ variant uses projective
520 * coordinates, which can be a win.
521 */
522
523 extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
524 extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
525
526 /* --- @ec_bin@, @ec_binproj@ --- *
527 *
528 * Arguments: @field *f@ = the underlying field for this elliptic curve
529 * @mp *a, *b@ = the coefficients for this curve
530 *
531 * Returns: A pointer to the curve, or null.
532 *
533 * Use: Creates a curve structure for an elliptic curve defined over
534 * a binary field. The @binproj@ variant uses projective
535 * coordinates, which can be a win.
536 */
537
538 extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
539 extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
540
541 /*----- Curve parameter sets ----------------------------------------------*/
542
543 /* --- @ec_curveparse@ --- *
544 *
545 * Arguments: @qd_parse *qd@ = parser context
546 *
547 * Returns: Elliptic curve pointer if OK, or null.
548 *
549 * Use: Parses an elliptic curve description, which has the form
550 *
551 * * a field description
552 * * an optional `/'
553 * * `prime', `primeproj', `bin', or `binproj'
554 * * an optional `:'
555 * * the %$a$% parameter
556 * * an optional `,'
557 * * the %$b$% parameter
558 */
559
560 extern ec_curve *ec_curveparse(qd_parse */*qd*/);
561
562 /* --- @ec_ptparse@ --- *
563 *
564 * Arguments: @qd_parse *qd@ = parser context
565 * @ec *p@ = where to put the point
566 *
567 * Returns: The point address, or null.
568 *
569 * Use: Parses an elliptic curve point. This has the form
570 *
571 * * %$x$%-coordinate
572 * * optional `,'
573 * * %$y$%-coordinate
574 */
575
576 extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
577
578 /* --- @ec_infoparse@ --- *
579 *
580 * Arguments: @qd_parse *qd@ = parser context
581 * @ec_info *ei@ = curve information block, currently
582 * uninitialized
583 *
584 * Returns: Zero on success, nonzero on failure.
585 *
586 * Use: Parses an elliptic curve information string, and stores the
587 * information in @ei@. This has the form
588 *
589 * * elliptic curve description
590 * * optional `/'
591 * * common point
592 * * optional `:'
593 * * group order
594 * * optional `*'
595 * * cofactor
596 */
597
598 extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
599
600 /* --- @ec_getinfo@ --- *
601 *
602 * Arguments: @ec_info *ei@ = where to write the information
603 * @const char *p@ = string describing a curve
604 *
605 * Returns: Null on success, or a pointer to an error message.
606 *
607 * Use: Parses out information about a curve. The string is either a
608 * standard curve name, or a curve info string.
609 */
610
611 extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
612
613 /* --- @ec_sameinfop@ --- *
614 *
615 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
616 *
617 * Returns: Nonzero if the curves are identical (not just isomorphic).
618 *
619 * Use: Checks for sameness of curve parameters.
620 */
621
622 extern int ec_sameinfop(ec_info */*ei*/, ec_info */*ej*/);
623
624 /* --- @ec_freeinfo@ --- *
625 *
626 * Arguments: @ec_info *ei@ = elliptic curve information block to free
627 *
628 * Returns: ---
629 *
630 * Use: Frees the information block.
631 */
632
633 extern void ec_freeinfo(ec_info */*ei*/);
634
635 /* --- @ec_checkinfo@ --- *
636 *
637 * Arguments: @const ec_info *ei@ = elliptic curve information block
638 *
639 * Returns: Null if OK, or pointer to error message.
640 *
641 * Use: Checks an elliptic curve according to the rules in SEC1.
642 */
643
644 extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
645
646 /*----- That's all, folks -------------------------------------------------*/
647
648 #ifdef __cplusplus
649 }
650 #endif
651
652 #endif