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