3 * $Id: ec.h,v 1.9 2004/04/01 12:50:09 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.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.)
40 * Revision 1.8 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
43 * Revision 1.7 2004/03/23 15:19:32 mdw
44 * Test elliptic curves more thoroughly.
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
51 * Revision 1.5 2004/03/21 22:52:06 mdw
52 * Merge and close elliptic curve branch.
54 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
55 * Elliptic curves on binary fields work.
57 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
58 * Projective coordinates for prime curves
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.
63 * Revision 1.4 2003/05/15 23:25:59 mdw
64 * Make elliptic curve stuff build.
66 * Revision 1.3 2002/01/13 13:48:44 mdw
69 * Revision 1.2 2001/05/07 17:29:44 mdw
70 * Treat projective coordinates as an internal representation. Various
71 * minor interface changes.
73 * Revision 1.1 2001/04/29 18:12:33 mdw
85 /*----- Header files ------------------------------------------------------*/
87 #ifndef CATACOMB_FIELD_H
95 #ifndef CATACOMB_QDPARSE_H
99 /*----- Data structures ---------------------------------------------------*/
101 /* --- An elliptic curve representation --- */
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) */
109 /* --- An elliptic curve point --- */
112 mp
*x
, *y
; /* Point coordinates */
113 mp
*z
; /* Common denominator (or null) */
116 /* --- A factor for simultaneous multiplication --- */
118 typedef struct ec_mulfactor
{
119 ec base
; /* The point */
120 mp
*exp
; /* The exponent */
123 /* --- Elliptic curve operations --- *
125 * All operations (apart from @destroy@ and @in@) are guaranteed to be
126 * performed on internal representations of points.
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.)
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*/
);
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))
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))
160 /* --- Elliptic curve parameters --- */
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$% */
169 /*----- Simple memory management things -----------------------------------*/
171 /* --- @ec_create@ --- *
173 * Arguments: @ec *p@ = pointer to an elliptic-curve point
175 * Returns: The argument @p@.
177 * Use: Initializes a new point. The initial value is the additive
178 * identity (which is universal for all curves).
181 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
183 #define EC_CREATE(p) do { \
185 _p->x = _p->y = _p->z = MP_NEW; \
188 extern ec
*ec_create(ec */
*p*/
);
190 /* --- @ec_destroy@ --- *
192 * Arguments: @ec *p@ = pointer to an elliptic-curve point
196 * Use: Destroys a point, making it invalid.
199 #define EC_DESTROY(p) do { \
201 if (!EC_ATINF(_p)) { \
204 if (_p->z) MP_DROP(_p->z); \
208 extern void ec_destroy(ec */
*p*/
);
210 /* --- @ec_atinf@ --- *
212 * Arguments: @const ec *p@ = pointer to a point
214 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
218 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
220 extern int ec_atinf(const ec */
*p*/
);
222 /* --- @ec_setinf@ --- *
224 * Arguments: @ec *p@ = pointer to a point
226 * Returns: The argument @p@.
228 * Use: Sets the given point to be the point %$O$% at infinity.
231 #define EC_SETINF(p) do { \
233 if (!EC_ATINF(_p)) { \
236 if (_p->z) MP_DROP(_p->z); \
237 _p->x = _p->y = _p->z = MP_NEW; \
243 extern ec
*ec_setinf(ec */
*p*/
);
245 /* --- @ec_copy@ --- *
247 * Arguments: @ec *d@ = pointer to destination point
248 * @const ec *p@ = pointer to source point
250 * Returns: The destination @d@.
252 * Use: Creates a copy of an elliptic curve point.
255 #define EC_COPY(d, p) do { \
257 const ec *_p = (p); \
261 _d->x = _d->y = _d->z = MP_NEW; \
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; \
270 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
274 * Arguments: @const ec *p, *q@ = two points
276 * Returns: Nonzero if the points are equal. Compares external-format
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)))
286 extern int ec_eq(const ec
*p
, const ec
*q
);
288 /*----- Interesting arithmetic --------------------------------------------*/
290 /* --- @ec_samep@ --- *
292 * Arguments: @ec_curve *c, *d@ = two elliptic curves
294 * Returns: Nonzero if the curves are identical (not just isomorphic).
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.
301 extern int ec_samep(ec_curve */
*c*/
, ec_curve */
*d*/
);
303 /* --- @ec_find@ --- *
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
309 * Returns: The destination if OK, or null if no point was found.
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.
317 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
319 /* --- @ec_rand@ --- *
321 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
322 * @ec *d@ = pointer to the destination point
323 * @grand *r@ = random number source
325 * Returns: The destination @d@.
327 * Use: Finds a random point on the given curve.
330 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
332 /* --- @ec_neg@ --- *
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
338 * Returns: The destination point.
340 * Use: Computes the negation of the given point.
343 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
345 /* --- @ec_add@ --- *
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
351 * Returns: The destination @d@.
353 * Use: Adds two points on an elliptic curve.
356 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
357 const ec */
*p*/
, const ec */
*q*/
);
359 /* --- @ec_sub@ --- *
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
365 * Returns: The destination @d@.
367 * Use: Subtracts one point from another on an elliptic curve.
370 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
371 const ec */
*p*/
, const ec */
*q*/
);
373 /* --- @ec_dbl@ --- *
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
379 * Returns: The destination @d@.
381 * Use: Doubles a point on an elliptic curve.
384 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
386 /* --- @ec_check@ --- *
388 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
389 * @const ec *p@ = pointer to the point
391 * Returns: Zero if OK, nonzero if this is an invalid point.
393 * Use: Checks that a point is actually on an elliptic curve.
396 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
398 /* --- @ec_mul@, @ec_imul@ --- *
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
405 * Returns: The destination @d@.
407 * Use: Multiplies a point by a scalar, returning %$n p$%. The
408 * @imul@ variant uses internal representations for argument
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*/
);
415 /* --- @ec_mmul@, @ec_immul@ --- *
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
422 * Returns: The destination @d@.
424 * Use: Does simultaneous point multiplication. The @immul@ variant
425 * uses internal representations for arguments and result.
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*/);
433 /*----- Standard curve operations -----------------------------------------*/
435 /* --- @ec_stdsamep@ --- *
437 * Arguments: @ec_curve *c, *d@ = two elliptic curves
439 * Returns: Nonzero if the curves are identical (not just isomorphic).
441 * Use: Simple sameness check on @a@ and @b@ curve members.
444 extern int ec_stdsamep(ec_curve */
*c*/
, ec_curve */
*d*/
);
446 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
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
452 * Returns: The destination @d@.
454 * Use: An identity operation if your curve has no internal
455 * representation. (The field internal representation is still
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*/
);
463 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
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
469 * Returns: The destination @d@.
471 * Use: Conversion functions if your curve operations use a
472 * projective representation.
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*/
);
479 /* --- @ec_stdsub@ --- *
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
485 * Returns: The destination @d@.
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.
492 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
493 const ec */
*p*/
, const ec */
*q*/
);
495 /*----- Creating curves ---------------------------------------------------*/
497 /* --- @ec_destroycurve@ --- *
499 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
503 * Use: Destroys a description of an elliptic curve.
506 extern void ec_destroycurve(ec_curve */
*c*/
);
508 /* --- @ec_prime@, @ec_primeproj@ --- *
510 * Arguments: @field *f@ = the underlying field for this elliptic curve
511 * @mp *a, *b@ = the coefficients for this curve
513 * Returns: A pointer to the curve.
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.
520 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
521 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
523 /* --- @ec_bin@, @ec_binproj@ --- *
525 * Arguments: @field *f@ = the underlying field for this elliptic curve
526 * @mp *a, *b@ = the coefficients for this curve
528 * Returns: A pointer to the curve.
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.
535 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
536 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
538 /*----- Curve parameter sets ----------------------------------------------*/
540 /* --- @ec_curveparse@ --- *
542 * Arguments: @qd_parse *qd@ = parser context
544 * Returns: Elliptic curve pointer if OK, or null.
546 * Use: Parses an elliptic curve description, which has the form
548 * * a field description
550 * * `prime', `primeproj', `bin', or `binproj'
552 * * the %$a$% parameter
554 * * the %$b$% parameter
557 extern ec_curve
*ec_curveparse(qd_parse */
*qd*/
);
559 /* --- @ec_ptparse@ --- *
561 * Arguments: @qd_parse *qd@ = parser context
562 * @ec *p@ = where to put the point
564 * Returns: The point address, or null.
566 * Use: Parses an elliptic curve point. This has the form
573 extern ec
*ec_ptparse(qd_parse */
*qd*/
, ec */
*p*/
);
575 /* --- @ec_infoparse@ --- *
577 * Arguments: @qd_parse *qd@ = parser context
578 * @ec_info *ei@ = curve information block, currently
581 * Returns: Zero on success, nonzero on failure.
583 * Use: Parses an elliptic curve information string, and stores the
584 * information in @ei@. This has the form
586 * * elliptic curve description
595 extern int ec_infoparse(qd_parse */
*qd*/
, ec_info */
*ei*/
);
597 /* --- @ec_getinfo@ --- *
599 * Arguments: @ec_info *ei@ = where to write the information
600 * @const char *p@ = string describing a curve
602 * Returns: Null on success, or a pointer to an error message.
604 * Use: Parses out information about a curve. The string is either a
605 * standard curve name, or a curve info string.
608 extern const char *ec_getinfo(ec_info */
*ei*/
, const char */
*p*/
);
610 /* --- @ec_sameinfop@ --- *
612 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
614 * Returns: Nonzero if the curves are identical (not just isomorphic).
616 * Use: Checks for sameness of curve parameters.
619 extern int ec_sameinfop(ec_info */
*ei*/
, ec_info */
*ej*/
);
621 /* --- @ec_freeinfo@ --- *
623 * Arguments: @ec_info *ei@ = elliptic curve information block to free
627 * Use: Frees the information block.
630 extern void ec_freeinfo(ec_info */
*ei*/
);
632 /* --- @ec_checkinfo@ --- *
634 * Arguments: @const ec_info *ei@ = elliptic curve information block
636 * Returns: Null if OK, or pointer to error message.
638 * Use: Checks an elliptic curve according to the rules in SEC1.
641 extern const char *ec_checkinfo(const ec_info */
*ei*/
, grand */
*gr*/
);
643 /*----- That's all, folks -------------------------------------------------*/