79e36549cfd4dcacb74d0df0e1bc21755785c0a7
3 * $Id: ec.h,v 1.8 2004/03/27 17:54:11 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.8 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
36 * Revision 1.7 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
39 * Revision 1.6 2004/03/22 02:19:10 mdw
40 * Rationalise the sliding-window threshold. Drop guarantee that right
41 * arguments to EC @add@ are canonical, and fix up projective implementations
44 * Revision 1.5 2004/03/21 22:52:06 mdw
45 * Merge and close elliptic curve branch.
47 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
48 * Elliptic curves on binary fields work.
50 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
51 * Projective coordinates for prime curves
53 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
54 * Simple (non-projective) curves over prime fields now seem to work.
56 * Revision 1.4 2003/05/15 23:25:59 mdw
57 * Make elliptic curve stuff build.
59 * Revision 1.3 2002/01/13 13:48:44 mdw
62 * Revision 1.2 2001/05/07 17:29:44 mdw
63 * Treat projective coordinates as an internal representation. Various
64 * minor interface changes.
66 * Revision 1.1 2001/04/29 18:12:33 mdw
78 /*----- Header files ------------------------------------------------------*/
80 #ifndef CATACOMB_FIELD_H
88 #ifndef CATACOMB_QDPARSE_H
92 /*----- Data structures ---------------------------------------------------*/
94 /* --- An elliptic curve representation --- */
96 typedef struct ec_curve
{
97 const struct ec_ops
*ops
; /* Curve operations */
98 field
*f
; /* Underlying field structure */
99 mp
*a
, *b
; /* Standard params (internal form) */
102 /* --- An elliptic curve point --- */
105 mp
*x
, *y
; /* Point coordinates */
106 mp
*z
; /* Common denominator (or null) */
109 /* --- A factor for simultaneous multiplication --- */
111 typedef struct ec_mulfactor
{
112 ec base
; /* The point */
113 mp
*exp
; /* The exponent */
116 /* --- Elliptic curve operations --- *
118 * All operations (apart from @destroy@ and @in@) are guaranteed to be
119 * performed on internal representations of points.
121 * (Historical note. We used to guarantee that the second to @add@ and @mul@
122 * was the output of @in@ or @fix@, but this canonification turned out to
123 * make the precomputation in @ec_exp@ too slow. Projective implementations
124 * must therefore cope with a pair of arbitrary points.)
127 typedef struct ec_ops
{
128 void (*destroy
)(ec_curve */
*c*/
);
129 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
130 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
131 ec
*(*fix
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
132 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
133 ec
*(*neg
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
134 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
135 ec
*(*sub
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
136 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
137 int (*check
)(ec_curve */
*c*/
, const ec */
*p*/
);
140 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
141 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
142 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
144 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
145 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
146 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
147 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
148 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
149 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
151 /* --- Elliptic curve parameters --- */
153 typedef struct ec_info
{
154 ec_curve
*c
; /* The actual curve */
155 ec g
; /* The common point */
156 mp
*r
; /* Order of %$g$% */
157 mp
*h
; /* Cofactor %$h = \#E/r$% */
160 /*----- Simple memory management things -----------------------------------*/
162 /* --- @ec_create@ --- *
164 * Arguments: @ec *p@ = pointer to an elliptic-curve point
166 * Returns: The argument @p@.
168 * Use: Initializes a new point. The initial value is the additive
169 * identity (which is universal for all curves).
172 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
174 #define EC_CREATE(p) do { \
176 _p->x = _p->y = _p->z = MP_NEW; \
179 extern ec
*ec_create(ec */
*p*/
);
181 /* --- @ec_destroy@ --- *
183 * Arguments: @ec *p@ = pointer to an elliptic-curve point
187 * Use: Destroys a point, making it invalid.
190 #define EC_DESTROY(p) do { \
192 if (!EC_ATINF(_p)) { \
195 if (_p->z) MP_DROP(_p->z); \
199 extern void ec_destroy(ec */
*p*/
);
201 /* --- @ec_atinf@ --- *
203 * Arguments: @const ec *p@ = pointer to a point
205 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
209 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
211 extern int ec_atinf(const ec */
*p*/
);
213 /* --- @ec_setinf@ --- *
215 * Arguments: @ec *p@ = pointer to a point
217 * Returns: The argument @p@.
219 * Use: Sets the given point to be the point %$O$% at infinity.
222 #define EC_SETINF(p) do { \
224 if (!EC_ATINF(_p)) { \
227 if (_p->z) MP_DROP(_p->z); \
228 _p->x = _p->y = _p->z = MP_NEW; \
234 extern ec
*ec_setinf(ec */
*p*/
);
236 /* --- @ec_copy@ --- *
238 * Arguments: @ec *d@ = pointer to destination point
239 * @const ec *p@ = pointer to source point
241 * Returns: The destination @d@.
243 * Use: Creates a copy of an elliptic curve point.
246 #define EC_COPY(d, p) do { \
248 const ec *_p = (p); \
252 _d->x = _d->y = _d->z = MP_NEW; \
254 _d->x = MP_COPY(_p->x); \
255 _d->y = MP_COPY(_p->y); \
256 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
261 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
265 * Arguments: @const ec *p, *q@ = two points
267 * Returns: Nonzero if the points are equal. Compares external-format
271 #define EC_EQ(p, q) \
272 ((EC_ATINF(p) && EC_ATINF(q)) || \
273 (!EC_ATINF(p) && !EC_ATINF(q) && \
274 MP_EQ((p)->x, (q)->x) && \
275 MP_EQ((p)->y, (q)->y)))
277 extern int ec_eq(const ec
*p
, const ec
*q
);
279 /*----- Interesting arithmetic --------------------------------------------*/
281 /* --- @ec_find@ --- *
283 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
284 * @ec *d@ = pointer to the destination point
285 * @mp *x@ = a possible x-coordinate
287 * Returns: The destination if OK, or null if no point was found.
289 * Use: Finds a point on an elliptic curve with a given
290 * x-coordinate. If there is no point with the given
291 * %$x$%-coordinate, a null pointer is returned and the
292 * destination is left invalid.
295 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
297 /* --- @ec_rand@ --- *
299 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
300 * @ec *d@ = pointer to the destination point
301 * @grand *r@ = random number source
303 * Returns: The destination @d@.
305 * Use: Finds a random point on the given curve.
308 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
310 /* --- @ec_neg@ --- *
312 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
313 * @ec *d@ = pointer to the destination point
314 * @const ec *p@ = pointer to the operand point
316 * Returns: The destination point.
318 * Use: Computes the negation of the given point.
321 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
323 /* --- @ec_add@ --- *
325 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
326 * @ec *d@ = pointer to the destination point
327 * @const ec *p, *q@ = pointers to the operand points
329 * Returns: The destination @d@.
331 * Use: Adds two points on an elliptic curve.
334 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
335 const ec */
*p*/
, const ec */
*q*/
);
337 /* --- @ec_sub@ --- *
339 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
340 * @ec *d@ = pointer to the destination point
341 * @const ec *p, *q@ = pointers to the operand points
343 * Returns: The destination @d@.
345 * Use: Subtracts one point from another on an elliptic curve.
348 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
349 const ec */
*p*/
, const ec */
*q*/
);
351 /* --- @ec_dbl@ --- *
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 operand point
357 * Returns: The destination @d@.
359 * Use: Doubles a point on an elliptic curve.
362 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
364 /* --- @ec_check@ --- *
366 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
367 * @const ec *p@ = pointer to the point
369 * Returns: Zero if OK, nonzero if this is an invalid point.
371 * Use: Checks that a point is actually on an elliptic curve.
374 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
376 /* --- @ec_mul@, @ec_imul@ --- *
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 generator point
381 * @mp *n@ = integer multiplier
383 * Returns: The destination @d@.
385 * Use: Multiplies a point by a scalar, returning %$n p$%. The
386 * @imul@ variant uses internal representations for argument
390 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
391 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
393 /* --- @ec_mmul@, @ec_immul@ --- *
395 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
396 * @ec *d@ = pointer to the destination point
397 * @const ec_mulfactor *f@ = pointer to vector of factors
398 * @size_t n@ = number of factors
400 * Returns: The destination @d@.
402 * Use: Does simultaneous point multiplication. The @immul@ variant
403 * uses internal representations for arguments and result.
406 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
407 const ec_mulfactor */
*f*/
, size_t /*n*/);
408 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
409 const ec_mulfactor */
*f*/
, size_t /*n*/);
411 /*----- Standard curve operations -----------------------------------------*/
413 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
415 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
416 * @ec *d@ = pointer to the destination
417 * @const ec *p@ = pointer to a source point
419 * Returns: The destination @d@.
421 * Use: An identity operation if your curve has no internal
422 * representation. (The field internal representation is still
426 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
427 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
428 extern ec
*ec_idfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
430 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
432 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
433 * @ec *d@ = pointer to the destination
434 * @const ec *p@ = pointer to a source point
436 * Returns: The destination @d@.
438 * Use: Conversion functions if your curve operations use a
439 * projective representation.
442 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
443 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
444 extern ec
*ec_projfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
446 /* --- @ec_stdsub@ --- *
448 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
449 * @ec *d@ = pointer to the destination
450 * @const ec *p, *q@ = the operand points
452 * Returns: The destination @d@.
454 * Use: Standard point subtraction operation, in terms of negation
455 * and addition. This isn't as efficient as a ready-made
456 * subtraction operator.
459 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
460 const ec */
*p*/
, const ec */
*q*/
);
462 /*----- Creating curves ---------------------------------------------------*/
464 /* --- @ec_destroycurve@ --- *
466 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
470 * Use: Destroys a description of an elliptic curve.
473 extern void ec_destroycurve(ec_curve */
*c*/
);
475 /* --- @ec_prime@, @ec_primeproj@ --- *
477 * Arguments: @field *f@ = the underlying field for this elliptic curve
478 * @mp *a, *b@ = the coefficients for this curve
480 * Returns: A pointer to the curve.
482 * Use: Creates a curve structure for an elliptic curve defined over
483 * a prime field. The @primeproj@ variant uses projective
484 * coordinates, which can be a win.
487 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
488 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
490 /* --- @ec_bin@, @ec_binproj@ --- *
492 * Arguments: @field *f@ = the underlying field for this elliptic curve
493 * @mp *a, *b@ = the coefficients for this curve
495 * Returns: A pointer to the curve.
497 * Use: Creates a curve structure for an elliptic curve defined over
498 * a binary field. The @binproj@ variant uses projective
499 * coordinates, which can be a win.
502 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
503 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
505 /*----- Curve parameter sets ----------------------------------------------*/
507 /* --- @ec_curveparse@ --- *
509 * Arguments: @qd_parse *qd@ = parser context
511 * Returns: Elliptic curve pointer if OK, or null.
513 * Use: Parses an elliptic curve description, which has the form
515 * * a field description
517 * * `prime', `primeproj', `bin', or `binproj'
519 * * the %$a$% parameter
521 * * the %$b$% parameter
524 extern ec_curve
*ec_curveparse(qd_parse */
*qd*/
);
526 /* --- @ec_ptparse@ --- *
528 * Arguments: @qd_parse *qd@ = parser context
529 * @ec *p@ = where to put the point
531 * Returns: The point address, or null.
533 * Use: Parses an elliptic curve point. This has the form
540 extern ec
*ec_ptparse(qd_parse */
*qd*/
, ec */
*p*/
);
542 /* --- @ec_infoparse@ --- *
544 * Arguments: @qd_parse *qd@ = parser context
545 * @ec_info *ei@ = curve information block, currently
548 * Returns: Zero on success, nonzero on failure.
550 * Use: Parses an elliptic curve information string, and stores the
551 * information in @ei@. This has the form
553 * * elliptic curve description
562 extern int ec_infoparse(qd_parse */
*qd*/
, ec_info */
*ei*/
);
564 /* --- @ec_getinfo@ --- *
566 * Arguments: @ec_info *ei@ = where to write the information
567 * @const char *p@ = string describing a curve
569 * Returns: Null on success, or a pointer to an error message.
571 * Use: Parses out information about a curve. The string is either a
572 * standard curve name, or a curve info string.
575 extern const char *ec_getinfo(ec_info */
*ei*/
, const char */
*p*/
);
577 /* --- @ec_freeinfo@ --- *
579 * Arguments: @ec_info *ei@ = elliptic curve information block to free
583 * Use: Frees the information block.
586 extern void ec_freeinfo(ec_info */
*ei*/
);
588 /* --- @ec_checkinfo@ --- *
590 * Arguments: @const ec_info *ei@ = elliptic curve information block
592 * Returns: Null if OK, or pointer to error message.
594 * Use: Checks an elliptic curve according to the rules in SEC1.
597 extern const char *ec_checkinfo(const ec_info */
*ei*/
, grand */
*gr*/
);
599 /*----- That's all, folks -------------------------------------------------*/