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