3 * $Id: ec.h,v 1.10 2004/04/03 03:32:05 mdw Exp $
5 * Elliptic curve definitions
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.10 2004/04/03 03:32:05 mdw
34 * General robustification.
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.)
43 * Revision 1.8 2004/03/27 17:54:11 mdw
44 * Standard curves and curve checking.
46 * Revision 1.7 2004/03/23 15:19:32 mdw
47 * Test elliptic curves more thoroughly.
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
54 * Revision 1.5 2004/03/21 22:52:06 mdw
55 * Merge and close elliptic curve branch.
57 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
58 * Elliptic curves on binary fields work.
60 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
61 * Projective coordinates for prime curves
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.
66 * Revision 1.4 2003/05/15 23:25:59 mdw
67 * Make elliptic curve stuff build.
69 * Revision 1.3 2002/01/13 13:48:44 mdw
72 * Revision 1.2 2001/05/07 17:29:44 mdw
73 * Treat projective coordinates as an internal representation. Various
74 * minor interface changes.
76 * Revision 1.1 2001/04/29 18:12:33 mdw
88 /*----- Header files ------------------------------------------------------*/
90 #ifndef CATACOMB_FIELD_H
98 #ifndef CATACOMB_QDPARSE_H
102 /*----- Data structures ---------------------------------------------------*/
104 /* --- An elliptic curve representation --- */
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) */
112 /* --- An elliptic curve point --- */
115 mp *x, *y; /* Point coordinates */
116 mp *z; /* Common denominator (or null) */
119 /* --- A factor for simultaneous multiplication --- */
121 typedef struct ec_mulfactor {
122 ec base; /* The point */
123 mp *exp; /* The exponent */
126 /* --- Elliptic curve operations --- *
128 * All operations (apart from @destroy@ and @in@) are guaranteed to be
129 * performed on internal representations of points.
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.)
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*/);
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))
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))
163 /* --- Elliptic curve parameters --- */
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$% */
172 /*----- Simple memory management things -----------------------------------*/
174 /* --- @ec_create@ --- *
176 * Arguments: @ec *p@ = pointer to an elliptic-curve point
178 * Returns: The argument @p@.
180 * Use: Initializes a new point. The initial value is the additive
181 * identity (which is universal for all curves).
184 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
186 #define EC_CREATE(p) do { \
188 _p->x = _p->y = _p->z = MP_NEW; \
191 extern ec *ec_create(ec */*p*/);
193 /* --- @ec_destroy@ --- *
195 * Arguments: @ec *p@ = pointer to an elliptic-curve point
199 * Use: Destroys a point, making it invalid.
202 #define EC_DESTROY(p) do { \
204 if (!EC_ATINF(_p)) { \
207 if (_p->z) MP_DROP(_p->z); \
211 extern void ec_destroy(ec */*p*/);
213 /* --- @ec_atinf@ --- *
215 * Arguments: @const ec *p@ = pointer to a point
217 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
221 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
223 extern int ec_atinf(const ec */*p*/);
225 /* --- @ec_setinf@ --- *
227 * Arguments: @ec *p@ = pointer to a point
229 * Returns: The argument @p@.
231 * Use: Sets the given point to be the point %$O$% at infinity.
234 #define EC_SETINF(p) do { \
236 if (!EC_ATINF(_p)) { \
239 if (_p->z) MP_DROP(_p->z); \
240 _p->x = _p->y = _p->z = MP_NEW; \
246 extern ec *ec_setinf(ec */*p*/);
248 /* --- @ec_copy@ --- *
250 * Arguments: @ec *d@ = pointer to destination point
251 * @const ec *p@ = pointer to source point
253 * Returns: The destination @d@.
255 * Use: Creates a copy of an elliptic curve point.
258 #define EC_COPY(d, p) do { \
260 const ec *_p = (p); \
264 _d->x = _d->y = _d->z = MP_NEW; \
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; \
273 extern ec *ec_copy(ec */*d*/, const ec */*p*/);
277 * Arguments: @const ec *p, *q@ = two points
279 * Returns: Nonzero if the points are equal. Compares external-format
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)))
289 extern int ec_eq(const ec *p, const ec *q);
291 /*----- Interesting arithmetic --------------------------------------------*/
293 /* --- @ec_samep@ --- *
295 * Arguments: @ec_curve *c, *d@ = two elliptic curves
297 * Returns: Nonzero if the curves are identical (not just isomorphic).
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.
304 extern int ec_samep(ec_curve */*c*/, ec_curve */*d*/);
306 /* --- @ec_find@ --- *
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
312 * Returns: The destination if OK, or null if no point was found.
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.
320 extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
322 /* --- @ec_rand@ --- *
324 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
325 * @ec *d@ = pointer to the destination point
326 * @grand *r@ = random number source
328 * Returns: The destination @d@.
330 * Use: Finds a random point on the given curve.
333 extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
335 /* --- @ec_neg@ --- *
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
341 * Returns: The destination point.
343 * Use: Computes the negation of the given point.
346 extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
348 /* --- @ec_add@ --- *
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
354 * Returns: The destination @d@.
356 * Use: Adds two points on an elliptic curve.
359 extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
360 const ec */*p*/, const ec */*q*/);
362 /* --- @ec_sub@ --- *
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
368 * Returns: The destination @d@.
370 * Use: Subtracts one point from another on an elliptic curve.
373 extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
374 const ec */*p*/, const ec */*q*/);
376 /* --- @ec_dbl@ --- *
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
382 * Returns: The destination @d@.
384 * Use: Doubles a point on an elliptic curve.
387 extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
389 /* --- @ec_check@ --- *
391 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
392 * @const ec *p@ = pointer to the point
394 * Returns: Zero if OK, nonzero if this is an invalid point.
396 * Use: Checks that a point is actually on an elliptic curve.
399 extern int ec_check(ec_curve */*c*/, const ec */*p*/);
401 /* --- @ec_mul@, @ec_imul@ --- *
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
408 * Returns: The destination @d@.
410 * Use: Multiplies a point by a scalar, returning %$n p$%. The
411 * @imul@ variant uses internal representations for argument
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*/);
418 /* --- @ec_mmul@, @ec_immul@ --- *
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
425 * Returns: The destination @d@.
427 * Use: Does simultaneous point multiplication. The @immul@ variant
428 * uses internal representations for arguments and result.
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*/);
436 /*----- Standard curve operations -----------------------------------------*/
438 /* --- @ec_stdsamep@ --- *
440 * Arguments: @ec_curve *c, *d@ = two elliptic curves
442 * Returns: Nonzero if the curves are identical (not just isomorphic).
444 * Use: Simple sameness check on @a@ and @b@ curve members.
447 extern int ec_stdsamep(ec_curve */*c*/, ec_curve */*d*/);
449 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
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
455 * Returns: The destination @d@.
457 * Use: An identity operation if your curve has no internal
458 * representation. (The field internal representation is still
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*/);
466 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
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
472 * Returns: The destination @d@.
474 * Use: Conversion functions if your curve operations use a
475 * projective representation.
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*/);
482 /* --- @ec_stdsub@ --- *
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
488 * Returns: The destination @d@.
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.
495 extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
496 const ec */*p*/, const ec */*q*/);
498 /*----- Creating curves ---------------------------------------------------*/
500 /* --- @ec_destroycurve@ --- *
502 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
506 * Use: Destroys a description of an elliptic curve.
509 extern void ec_destroycurve(ec_curve */*c*/);
511 /* --- @ec_prime@, @ec_primeproj@ --- *
513 * Arguments: @field *f@ = the underlying field for this elliptic curve
514 * @mp *a, *b@ = the coefficients for this curve
516 * Returns: A pointer to the curve, or null.
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.
523 extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
524 extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
526 /* --- @ec_bin@, @ec_binproj@ --- *
528 * Arguments: @field *f@ = the underlying field for this elliptic curve
529 * @mp *a, *b@ = the coefficients for this curve
531 * Returns: A pointer to the curve, or null.
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.
538 extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
539 extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
541 /*----- Curve parameter sets ----------------------------------------------*/
543 /* --- @ec_curveparse@ --- *
545 * Arguments: @qd_parse *qd@ = parser context
547 * Returns: Elliptic curve pointer if OK, or null.
549 * Use: Parses an elliptic curve description, which has the form
551 * * a field description
553 * * `prime', `primeproj', `bin', or `binproj'
555 * * the %$a$% parameter
557 * * the %$b$% parameter
560 extern ec_curve *ec_curveparse(qd_parse */*qd*/);
562 /* --- @ec_ptparse@ --- *
564 * Arguments: @qd_parse *qd@ = parser context
565 * @ec *p@ = where to put the point
567 * Returns: The point address, or null.
569 * Use: Parses an elliptic curve point. This has the form
576 extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
578 /* --- @ec_infoparse@ --- *
580 * Arguments: @qd_parse *qd@ = parser context
581 * @ec_info *ei@ = curve information block, currently
584 * Returns: Zero on success, nonzero on failure.
586 * Use: Parses an elliptic curve information string, and stores the
587 * information in @ei@. This has the form
589 * * elliptic curve description
598 extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
600 /* --- @ec_getinfo@ --- *
602 * Arguments: @ec_info *ei@ = where to write the information
603 * @const char *p@ = string describing a curve
605 * Returns: Null on success, or a pointer to an error message.
607 * Use: Parses out information about a curve. The string is either a
608 * standard curve name, or a curve info string.
611 extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
613 /* --- @ec_sameinfop@ --- *
615 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
617 * Returns: Nonzero if the curves are identical (not just isomorphic).
619 * Use: Checks for sameness of curve parameters.
622 extern int ec_sameinfop(ec_info */*ei*/, ec_info */*ej*/);
624 /* --- @ec_freeinfo@ --- *
626 * Arguments: @ec_info *ei@ = elliptic curve information block to free
630 * Use: Frees the information block.
633 extern void ec_freeinfo(ec_info */*ei*/);
635 /* --- @ec_checkinfo@ --- *
637 * Arguments: @const ec_info *ei@ = elliptic curve information block
639 * Returns: Null if OK, or pointer to error message.
641 * Use: Checks an elliptic curve according to the rules in SEC1.
644 extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
646 /*----- That's all, folks -------------------------------------------------*/