3 * Elliptic curve definitions
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
35 /*----- Header files ------------------------------------------------------*/
37 #ifndef CATACOMB_FIELD_H
45 #ifndef CATACOMB_QDPARSE_H
49 /*----- Data structures ---------------------------------------------------*/
51 /* --- An elliptic curve representation --- */
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) */
59 /* --- An elliptic curve point --- */
62 mp *x, *y; /* Point coordinates */
63 mp *z; /* Common denominator (or null) */
66 /* --- A factor for simultaneous multiplication --- */
68 typedef struct ec_mulfactor {
69 ec base; /* The point */
70 mp *exp; /* The exponent */
73 /* --- Elliptic curve operations --- *
75 * All operations apart from @destroy@, @in@, and @compr@ are guaranteed to
76 * be performed on internal representations of points; @in@ and @compr@ will
77 * always be performed on external representations; @destroy@ might be
78 * performed on either.
80 * (Historical note. We used to guarantee that the second to @add@ and @mul@
81 * was the output of @in@ or @fix@, but this canonification turned out to
82 * make the precomputation in @ec_exp@ too slow. Projective implementations
83 * must therefore cope with a pair of arbitrary points.)
86 typedef struct ec_ops {
88 void (*destroy)(ec_curve */*c*/);
89 int (*samep)(ec_curve */*c*/, ec_curve */*d*/);
90 ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
91 ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
92 ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
93 ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
94 ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
95 ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
96 ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
97 ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
98 int (*check)(ec_curve */*c*/, const ec */*p*/);
99 int (*compr)(ec_curve */*c*/, const ec */*p*/);
102 #define EC_NAME(c) (c)->ops->name
104 #define EC_SAMEP(c, d) (c)->ops->samep((c), (d))
105 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
106 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
107 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
109 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
110 #define EC_COMPR(c, d) (c)->ops->compr((c), (d))
111 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
112 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
113 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
114 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
115 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
117 /* --- Elliptic curve parameters --- */
119 typedef struct ec_info {
120 ec_curve *c; /* The actual curve */
121 ec g; /* The common point */
122 mp *r; /* Order of %$g$% */
123 mp *h; /* Cofactor %$h = \#E/r$% */
126 /*----- Simple memory management things -----------------------------------*/
128 /* --- @ec_create@ --- *
130 * Arguments: @ec *p@ = pointer to an elliptic-curve point
132 * Returns: The argument @p@.
134 * Use: Initializes a new point. The initial value is the additive
135 * identity (which is universal for all curves).
138 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
140 #define EC_CREATE(p) do { \
142 _p->x = _p->y = _p->z = MP_NEW; \
145 extern ec *ec_create(ec */*p*/);
147 /* --- @ec_destroy@ --- *
149 * Arguments: @ec *p@ = pointer to an elliptic-curve point
153 * Use: Destroys a point, making it invalid.
156 #define EC_DESTROY(p) do { \
158 if (!EC_ATINF(_p)) { \
161 if (_p->z) MP_DROP(_p->z); \
165 extern void ec_destroy(ec */*p*/);
167 /* --- @ec_atinf@ --- *
169 * Arguments: @const ec *p@ = pointer to a point
171 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
175 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
177 extern int ec_atinf(const ec */*p*/);
179 /* --- @ec_setinf@ --- *
181 * Arguments: @ec *p@ = pointer to a point
183 * Returns: The argument @p@.
185 * Use: Sets the given point to be the point %$O$% at infinity.
188 #define EC_SETINF(p) do { \
190 if (!EC_ATINF(_p)) { \
193 if (_p->z) MP_DROP(_p->z); \
194 _p->x = _p->y = _p->z = MP_NEW; \
200 extern ec *ec_setinf(ec */*p*/);
202 /* --- @ec_copy@ --- *
204 * Arguments: @ec *d@ = pointer to destination point
205 * @const ec *p@ = pointer to source point
207 * Returns: The destination @d@.
209 * Use: Creates a copy of an elliptic curve point.
212 #define EC_COPY(d, p) do { \
214 const ec *_p = (p); \
218 _d->x = _d->y = _d->z = MP_NEW; \
220 _d->x = MP_COPY(_p->x); \
221 _d->y = MP_COPY(_p->y); \
222 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
227 extern ec *ec_copy(ec */*d*/, const ec */*p*/);
231 * Arguments: @const ec *p, *q@ = two points
233 * Returns: Nonzero if the points are equal. Compares external-format
237 #define EC_EQ(p, q) \
238 ((EC_ATINF(p) && EC_ATINF(q)) || \
239 (!EC_ATINF(p) && !EC_ATINF(q) && \
240 MP_EQ((p)->x, (q)->x) && \
241 MP_EQ((p)->y, (q)->y)))
243 extern int ec_eq(const ec *p, const ec *q);
245 /*----- Interesting arithmetic --------------------------------------------*/
247 /* --- @ec_samep@ --- *
249 * Arguments: @ec_curve *c, *d@ = two elliptic curves
251 * Returns: Nonzero if the curves are identical (not just isomorphic).
253 * Use: Checks for sameness of curves. This function does the full
254 * check, not just the curve-type-specific check done by the
255 * @sampep@ field operation.
258 extern int ec_samep(ec_curve */*c*/, ec_curve */*d*/);
260 /* --- @ec_find@ --- *
262 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
263 * @ec *d@ = pointer to the destination point
264 * @mp *x@ = a possible x-coordinate
266 * Returns: The destination if OK, or null if no point was found.
268 * Use: Finds a point on an elliptic curve with a given
269 * x-coordinate. If there is no point with the given
270 * %$x$%-coordinate, a null pointer is returned and the
271 * destination is left invalid.
274 extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
276 /* --- @ec_rand@ --- *
278 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
279 * @ec *d@ = pointer to the destination point
280 * @grand *r@ = random number source
282 * Returns: The destination @d@.
284 * Use: Finds a random point on the given curve.
287 extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
289 /* --- @ec_neg@ --- *
291 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
292 * @ec *d@ = pointer to the destination point
293 * @const ec *p@ = pointer to the operand point
295 * Returns: The destination point.
297 * Use: Computes the negation of the given point.
300 extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
302 /* --- @ec_add@ --- *
304 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
305 * @ec *d@ = pointer to the destination point
306 * @const ec *p, *q@ = pointers to the operand points
308 * Returns: The destination @d@.
310 * Use: Adds two points on an elliptic curve.
313 extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
314 const ec */*p*/, const ec */*q*/);
316 /* --- @ec_sub@ --- *
318 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
319 * @ec *d@ = pointer to the destination point
320 * @const ec *p, *q@ = pointers to the operand points
322 * Returns: The destination @d@.
324 * Use: Subtracts one point from another on an elliptic curve.
327 extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
328 const ec */*p*/, const ec */*q*/);
330 /* --- @ec_dbl@ --- *
332 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
333 * @ec *d@ = pointer to the destination point
334 * @const ec *p@ = pointer to the operand point
336 * Returns: The destination @d@.
338 * Use: Doubles a point on an elliptic curve.
341 extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
343 /* --- @ec_check@ --- *
345 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
346 * @const ec *p@ = pointer to the point
348 * Returns: Zero if OK, nonzero if this is an invalid point.
350 * Use: Checks that a point is actually on an elliptic curve.
353 extern int ec_check(ec_curve */*c*/, const ec */*p*/);
355 /* --- @ec_mul@, @ec_imul@ --- *
357 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
358 * @ec *d@ = pointer to the destination point
359 * @const ec *p@ = pointer to the generator point
360 * @mp *n@ = integer multiplier
362 * Returns: The destination @d@.
364 * Use: Multiplies a point by a scalar, returning %$n p$%. The
365 * @imul@ variant uses internal representations for argument
369 extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
370 extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
372 /* --- @ec_mmul@, @ec_immul@ --- *
374 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
375 * @ec *d@ = pointer to the destination point
376 * @const ec_mulfactor *f@ = pointer to vector of factors
377 * @size_t n@ = number of factors
379 * Returns: The destination @d@.
381 * Use: Does simultaneous point multiplication. The @immul@ variant
382 * uses internal representations for arguments and result.
385 extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/,
386 const ec_mulfactor */*f*/, size_t /*n*/);
387 extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
388 const ec_mulfactor */*f*/, size_t /*n*/);
390 /*----- Standard curve operations -----------------------------------------*/
392 /* --- @ec_stdsamep@ --- *
394 * Arguments: @ec_curve *c, *d@ = two elliptic curves
396 * Returns: Nonzero if the curves are identical (not just isomorphic).
398 * Use: Simple sameness check on @a@ and @b@ curve members.
401 extern int ec_stdsamep(ec_curve */*c*/, ec_curve */*d*/);
403 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
405 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
406 * @ec *d@ = pointer to the destination
407 * @const ec *p@ = pointer to a source point
409 * Returns: The destination @d@.
411 * Use: An identity operation if your curve has no internal
412 * representation. (The field internal representation is still
416 extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
417 extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
418 extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
420 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
422 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
423 * @ec *d@ = pointer to the destination
424 * @const ec *p@ = pointer to a source point
426 * Returns: The destination @d@.
428 * Use: Conversion functions if your curve operations use a
429 * projective representation.
432 extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
433 extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
434 extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
436 /* --- @ec_stdsub@ --- *
438 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
439 * @ec *d@ = pointer to the destination
440 * @const ec *p, *q@ = the operand points
442 * Returns: The destination @d@.
444 * Use: Standard point subtraction operation, in terms of negation
445 * and addition. This isn't as efficient as a ready-made
446 * subtraction operator.
449 extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
450 const ec */*p*/, const ec */*q*/);
452 /*----- Creating curves ---------------------------------------------------*/
454 /* --- @ec_destroycurve@ --- *
456 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
460 * Use: Destroys a description of an elliptic curve.
463 extern void ec_destroycurve(ec_curve */*c*/);
465 /* --- @ec_prime@, @ec_primeproj@ --- *
467 * Arguments: @field *f@ = the underlying field for this elliptic curve
468 * @mp *a, *b@ = the coefficients for this curve
470 * Returns: A pointer to the curve, or null.
472 * Use: Creates a curve structure for an elliptic curve defined over
473 * a prime field. The @primeproj@ variant uses projective
474 * coordinates, which can be a win.
477 extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
478 extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
480 /* --- @ec_bin@, @ec_binproj@ --- *
482 * Arguments: @field *f@ = the underlying field for this elliptic curve
483 * @mp *a, *b@ = the coefficients for this curve
485 * Returns: A pointer to the curve, or null.
487 * Use: Creates a curve structure for an elliptic curve defined over
488 * a binary field. The @binproj@ variant uses projective
489 * coordinates, which can be a win.
492 extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
493 extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
495 /*----- Curve parameter sets ----------------------------------------------*/
497 /* --- @ec_curveparse@ --- *
499 * Arguments: @qd_parse *qd@ = parser context
501 * Returns: Elliptic curve pointer if OK, or null.
503 * Use: Parses an elliptic curve description, which has the form
505 * * a field description
507 * * `prime', `primeproj', `bin', or `binproj'
509 * * the %$a$% parameter
511 * * the %$b$% parameter
514 extern ec_curve *ec_curveparse(qd_parse */*qd*/);
516 /* --- @ec_ptparse@ --- *
518 * Arguments: @qd_parse *qd@ = parser context
519 * @ec *p@ = where to put the point
521 * Returns: The point address, or null.
523 * Use: Parses an elliptic curve point. This has the form
530 extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
532 /* --- @ec_infoparse@ --- *
534 * Arguments: @qd_parse *qd@ = parser context
535 * @ec_info *ei@ = curve information block, currently
538 * Returns: Zero on success, nonzero on failure.
540 * Use: Parses an elliptic curve information string, and stores the
541 * information in @ei@. This has the form
543 * * elliptic curve description
552 extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
554 /* --- @ec_infofromdata@ --- *
556 * Arguments: @ec_info *ei@ = where to write the information
557 * @ecdata *ed@ = raw data
561 * Use: Loads elliptic curve information about one of the standard
566 extern void ec_infofromdata(ec_info */*ei*/, struct ecdata */*ed*/);
568 /* --- @ec_getinfo@ --- *
570 * Arguments: @ec_info *ei@ = where to write the information
571 * @const char *p@ = string describing a curve
573 * Returns: Null on success, or a pointer to an error message.
575 * Use: Parses out information about a curve. The string is either a
576 * standard curve name, or a curve info string.
579 extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
581 /* --- @ec_sameinfop@ --- *
583 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
585 * Returns: Nonzero if the curves are identical (not just isomorphic).
587 * Use: Checks for sameness of curve parameters.
590 extern int ec_sameinfop(ec_info */*ei*/, ec_info */*ej*/);
592 /* --- @ec_freeinfo@ --- *
594 * Arguments: @ec_info *ei@ = elliptic curve information block to free
598 * Use: Frees the information block.
601 extern void ec_freeinfo(ec_info */*ei*/);
603 /* --- @ec_checkinfo@ --- *
605 * Arguments: @const ec_info *ei@ = elliptic curve information block
607 * Returns: Null if OK, or pointer to error message.
609 * Use: Checks an elliptic curve according to the rules in SEC1.
612 extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
614 /*----- That's all, folks -------------------------------------------------*/