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@ and @in@) are guaranteed to be
76 * performed on internal representations of points.
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.)
84 typedef struct ec_ops
{
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*/
);
99 #define EC_NAME(c) (c)->ops->name
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))
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))
113 /* --- Elliptic curve parameters --- */
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$% */
122 /*----- Simple memory management things -----------------------------------*/
124 /* --- @ec_create@ --- *
126 * Arguments: @ec *p@ = pointer to an elliptic-curve point
128 * Returns: The argument @p@.
130 * Use: Initializes a new point. The initial value is the additive
131 * identity (which is universal for all curves).
134 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
136 #define EC_CREATE(p) do { \
138 _p->x = _p->y = _p->z = MP_NEW; \
141 extern ec
*ec_create(ec */
*p*/
);
143 /* --- @ec_destroy@ --- *
145 * Arguments: @ec *p@ = pointer to an elliptic-curve point
149 * Use: Destroys a point, making it invalid.
152 #define EC_DESTROY(p) do { \
154 if (!EC_ATINF(_p)) { \
157 if (_p->z) MP_DROP(_p->z); \
161 extern void ec_destroy(ec */
*p*/
);
163 /* --- @ec_atinf@ --- *
165 * Arguments: @const ec *p@ = pointer to a point
167 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
171 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
173 extern int ec_atinf(const ec */
*p*/
);
175 /* --- @ec_setinf@ --- *
177 * Arguments: @ec *p@ = pointer to a point
179 * Returns: The argument @p@.
181 * Use: Sets the given point to be the point %$O$% at infinity.
184 #define EC_SETINF(p) do { \
186 if (!EC_ATINF(_p)) { \
189 if (_p->z) MP_DROP(_p->z); \
190 _p->x = _p->y = _p->z = MP_NEW; \
196 extern ec
*ec_setinf(ec */
*p*/
);
198 /* --- @ec_copy@ --- *
200 * Arguments: @ec *d@ = pointer to destination point
201 * @const ec *p@ = pointer to source point
203 * Returns: The destination @d@.
205 * Use: Creates a copy of an elliptic curve point.
208 #define EC_COPY(d, p) do { \
210 const ec *_p = (p); \
214 _d->x = _d->y = _d->z = MP_NEW; \
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; \
223 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
227 * Arguments: @const ec *p, *q@ = two points
229 * Returns: Nonzero if the points are equal. Compares external-format
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)))
239 extern int ec_eq(const ec
*p
, const ec
*q
);
241 /*----- Interesting arithmetic --------------------------------------------*/
243 /* --- @ec_samep@ --- *
245 * Arguments: @ec_curve *c, *d@ = two elliptic curves
247 * Returns: Nonzero if the curves are identical (not just isomorphic).
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.
254 extern int ec_samep(ec_curve */
*c*/
, ec_curve */
*d*/
);
256 /* --- @ec_find@ --- *
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
262 * Returns: The destination if OK, or null if no point was found.
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.
270 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
272 /* --- @ec_rand@ --- *
274 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
275 * @ec *d@ = pointer to the destination point
276 * @grand *r@ = random number source
278 * Returns: The destination @d@.
280 * Use: Finds a random point on the given curve.
283 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
285 /* --- @ec_neg@ --- *
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
291 * Returns: The destination point.
293 * Use: Computes the negation of the given point.
296 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
298 /* --- @ec_add@ --- *
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
304 * Returns: The destination @d@.
306 * Use: Adds two points on an elliptic curve.
309 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
310 const ec */
*p*/
, const ec */
*q*/
);
312 /* --- @ec_sub@ --- *
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
318 * Returns: The destination @d@.
320 * Use: Subtracts one point from another on an elliptic curve.
323 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
324 const ec */
*p*/
, const ec */
*q*/
);
326 /* --- @ec_dbl@ --- *
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
332 * Returns: The destination @d@.
334 * Use: Doubles a point on an elliptic curve.
337 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
339 /* --- @ec_check@ --- *
341 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
342 * @const ec *p@ = pointer to the point
344 * Returns: Zero if OK, nonzero if this is an invalid point.
346 * Use: Checks that a point is actually on an elliptic curve.
349 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
351 /* --- @ec_mul@, @ec_imul@ --- *
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
358 * Returns: The destination @d@.
360 * Use: Multiplies a point by a scalar, returning %$n p$%. The
361 * @imul@ variant uses internal representations for argument
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*/
);
368 /* --- @ec_mmul@, @ec_immul@ --- *
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
375 * Returns: The destination @d@.
377 * Use: Does simultaneous point multiplication. The @immul@ variant
378 * uses internal representations for arguments and result.
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*/);
386 /*----- Standard curve operations -----------------------------------------*/
388 /* --- @ec_stdsamep@ --- *
390 * Arguments: @ec_curve *c, *d@ = two elliptic curves
392 * Returns: Nonzero if the curves are identical (not just isomorphic).
394 * Use: Simple sameness check on @a@ and @b@ curve members.
397 extern int ec_stdsamep(ec_curve */
*c*/
, ec_curve */
*d*/
);
399 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
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
405 * Returns: The destination @d@.
407 * Use: An identity operation if your curve has no internal
408 * representation. (The field internal representation is still
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*/
);
416 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
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
422 * Returns: The destination @d@.
424 * Use: Conversion functions if your curve operations use a
425 * projective representation.
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*/
);
432 /* --- @ec_stdsub@ --- *
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
438 * Returns: The destination @d@.
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.
445 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
446 const ec */
*p*/
, const ec */
*q*/
);
448 /*----- Creating curves ---------------------------------------------------*/
450 /* --- @ec_destroycurve@ --- *
452 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
456 * Use: Destroys a description of an elliptic curve.
459 extern void ec_destroycurve(ec_curve */
*c*/
);
461 /* --- @ec_prime@, @ec_primeproj@ --- *
463 * Arguments: @field *f@ = the underlying field for this elliptic curve
464 * @mp *a, *b@ = the coefficients for this curve
466 * Returns: A pointer to the curve, or null.
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.
473 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
474 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
476 /* --- @ec_bin@, @ec_binproj@ --- *
478 * Arguments: @field *f@ = the underlying field for this elliptic curve
479 * @mp *a, *b@ = the coefficients for this curve
481 * Returns: A pointer to the curve, or null.
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.
488 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
489 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
491 /*----- Curve parameter sets ----------------------------------------------*/
493 /* --- @ec_curveparse@ --- *
495 * Arguments: @qd_parse *qd@ = parser context
497 * Returns: Elliptic curve pointer if OK, or null.
499 * Use: Parses an elliptic curve description, which has the form
501 * * a field description
503 * * `prime', `primeproj', `bin', or `binproj'
505 * * the %$a$% parameter
507 * * the %$b$% parameter
510 extern ec_curve
*ec_curveparse(qd_parse */
*qd*/
);
512 /* --- @ec_ptparse@ --- *
514 * Arguments: @qd_parse *qd@ = parser context
515 * @ec *p@ = where to put the point
517 * Returns: The point address, or null.
519 * Use: Parses an elliptic curve point. This has the form
526 extern ec
*ec_ptparse(qd_parse */
*qd*/
, ec */
*p*/
);
528 /* --- @ec_infoparse@ --- *
530 * Arguments: @qd_parse *qd@ = parser context
531 * @ec_info *ei@ = curve information block, currently
534 * Returns: Zero on success, nonzero on failure.
536 * Use: Parses an elliptic curve information string, and stores the
537 * information in @ei@. This has the form
539 * * elliptic curve description
548 extern int ec_infoparse(qd_parse */
*qd*/
, ec_info */
*ei*/
);
550 /* --- @ec_infofromdata@ --- *
552 * Arguments: @ec_info *ei@ = where to write the information
553 * @ecdata *ed@ = raw data
557 * Use: Loads elliptic curve information about one of the standard
562 extern void ec_infofromdata(ec_info */
*ei*/
, struct ecdata */
*ed*/
);
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_sameinfop@ --- *
579 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
581 * Returns: Nonzero if the curves are identical (not just isomorphic).
583 * Use: Checks for sameness of curve parameters.
586 extern int ec_sameinfop(ec_info */
*ei*/
, ec_info */
*ej*/
);
588 /* --- @ec_freeinfo@ --- *
590 * Arguments: @ec_info *ei@ = elliptic curve information block to free
594 * Use: Frees the information block.
597 extern void ec_freeinfo(ec_info */
*ei*/
);
599 /* --- @ec_checkinfo@ --- *
601 * Arguments: @const ec_info *ei@ = elliptic curve information block
603 * Returns: Null if OK, or pointer to error message.
605 * Use: Checks an elliptic curve according to the rules in SEC1.
608 extern const char *ec_checkinfo(const ec_info */
*ei*/
, grand */
*gr*/
);
610 /*----- That's all, folks -------------------------------------------------*/