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,
37 /*----- Header files ------------------------------------------------------*/
39 #ifndef CATACOMB_FIELD_H
47 #ifndef CATACOMB_QDPARSE_H
51 /*----- Data structures ---------------------------------------------------*/
53 /* --- An elliptic curve representation --- */
55 typedef struct ec_curve
{
56 const struct ec_ops
*ops
; /* Curve operations */
57 field
*f
; /* Underlying field structure */
58 mp
*a
, *b
; /* Standard params (internal form) */
61 /* --- An elliptic curve point --- */
64 mp
*x
, *y
; /* Point coordinates */
65 mp
*z
; /* Common denominator (or null) */
68 /* --- A factor for simultaneous multiplication --- */
70 typedef struct ec_mulfactor
{
71 ec base
; /* The point */
72 mp
*exp
; /* The exponent */
75 /* --- Elliptic curve operations --- *
77 * All operations (apart from @destroy@ and @in@) are guaranteed to be
78 * performed on internal representations of points.
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*/
);
101 #define EC_NAME(c) (c)->ops->name
103 #define EC_SAMEP(c, d) (c)->ops->samep((c), (d))
104 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
105 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
106 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
108 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
109 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
110 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
111 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
112 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
113 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
115 /* --- Elliptic curve parameters --- */
117 typedef struct ec_info
{
118 ec_curve
*c
; /* The actual curve */
119 ec g
; /* The common point */
120 mp
*r
; /* Order of %$g$% */
121 mp
*h
; /* Cofactor %$h = \#E/r$% */
124 /*----- Simple memory management things -----------------------------------*/
126 /* --- @ec_create@ --- *
128 * Arguments: @ec *p@ = pointer to an elliptic-curve point
130 * Returns: The argument @p@.
132 * Use: Initializes a new point. The initial value is the additive
133 * identity (which is universal for all curves).
136 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
138 #define EC_CREATE(p) do { \
140 _p->x = _p->y = _p->z = MP_NEW; \
143 extern ec
*ec_create(ec */
*p*/
);
145 /* --- @ec_destroy@ --- *
147 * Arguments: @ec *p@ = pointer to an elliptic-curve point
151 * Use: Destroys a point, making it invalid.
154 #define EC_DESTROY(p) do { \
156 if (!EC_ATINF(_p)) { \
159 if (_p->z) MP_DROP(_p->z); \
163 extern void ec_destroy(ec */
*p*/
);
165 /* --- @ec_atinf@ --- *
167 * Arguments: @const ec *p@ = pointer to a point
169 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
173 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
175 extern int ec_atinf(const ec */
*p*/
);
177 /* --- @ec_setinf@ --- *
179 * Arguments: @ec *p@ = pointer to a point
181 * Returns: The argument @p@.
183 * Use: Sets the given point to be the point %$O$% at infinity.
186 #define EC_SETINF(p) do { \
188 if (!EC_ATINF(_p)) { \
191 if (_p->z) MP_DROP(_p->z); \
192 _p->x = _p->y = _p->z = MP_NEW; \
198 extern ec
*ec_setinf(ec */
*p*/
);
200 /* --- @ec_copy@ --- *
202 * Arguments: @ec *d@ = pointer to destination point
203 * @const ec *p@ = pointer to source point
205 * Returns: The destination @d@.
207 * Use: Creates a copy of an elliptic curve point.
210 #define EC_COPY(d, p) do { \
212 const ec *_p = (p); \
216 _d->x = _d->y = _d->z = MP_NEW; \
218 _d->x = MP_COPY(_p->x); \
219 _d->y = MP_COPY(_p->y); \
220 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
225 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
229 * Arguments: @const ec *p, *q@ = two points
231 * Returns: Nonzero if the points are equal. Compares external-format
235 #define EC_EQ(p, q) \
236 ((EC_ATINF(p) && EC_ATINF(q)) || \
237 (!EC_ATINF(p) && !EC_ATINF(q) && \
238 MP_EQ((p)->x, (q)->x) && \
239 MP_EQ((p)->y, (q)->y)))
241 extern int ec_eq(const ec
*p
, const ec
*q
);
243 /*----- Interesting arithmetic --------------------------------------------*/
245 /* --- @ec_samep@ --- *
247 * Arguments: @ec_curve *c, *d@ = two elliptic curves
249 * Returns: Nonzero if the curves are identical (not just isomorphic).
251 * Use: Checks for sameness of curves. This function does the full
252 * check, not just the curve-type-specific check done by the
253 * @sampep@ field operation.
256 extern int ec_samep(ec_curve */
*c*/
, ec_curve */
*d*/
);
258 /* --- @ec_find@ --- *
260 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
261 * @ec *d@ = pointer to the destination point
262 * @mp *x@ = a possible x-coordinate
264 * Returns: The destination if OK, or null if no point was found.
266 * Use: Finds a point on an elliptic curve with a given
267 * x-coordinate. If there is no point with the given
268 * %$x$%-coordinate, a null pointer is returned and the
269 * destination is left invalid.
272 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
274 /* --- @ec_rand@ --- *
276 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
277 * @ec *d@ = pointer to the destination point
278 * @grand *r@ = random number source
280 * Returns: The destination @d@.
282 * Use: Finds a random point on the given curve.
285 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
287 /* --- @ec_neg@ --- *
289 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
290 * @ec *d@ = pointer to the destination point
291 * @const ec *p@ = pointer to the operand point
293 * Returns: The destination point.
295 * Use: Computes the negation of the given point.
298 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
300 /* --- @ec_add@ --- *
302 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
303 * @ec *d@ = pointer to the destination point
304 * @const ec *p, *q@ = pointers to the operand points
306 * Returns: The destination @d@.
308 * Use: Adds two points on an elliptic curve.
311 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
312 const ec */
*p*/
, const ec */
*q*/
);
314 /* --- @ec_sub@ --- *
316 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
317 * @ec *d@ = pointer to the destination point
318 * @const ec *p, *q@ = pointers to the operand points
320 * Returns: The destination @d@.
322 * Use: Subtracts one point from another on an elliptic curve.
325 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
326 const ec */
*p*/
, const ec */
*q*/
);
328 /* --- @ec_dbl@ --- *
330 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
331 * @ec *d@ = pointer to the destination point
332 * @const ec *p@ = pointer to the operand point
334 * Returns: The destination @d@.
336 * Use: Doubles a point on an elliptic curve.
339 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
341 /* --- @ec_check@ --- *
343 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
344 * @const ec *p@ = pointer to the point
346 * Returns: Zero if OK, nonzero if this is an invalid point.
348 * Use: Checks that a point is actually on an elliptic curve.
351 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
353 /* --- @ec_mul@, @ec_imul@ --- *
355 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
356 * @ec *d@ = pointer to the destination point
357 * @const ec *p@ = pointer to the generator point
358 * @mp *n@ = integer multiplier
360 * Returns: The destination @d@.
362 * Use: Multiplies a point by a scalar, returning %$n p$%. The
363 * @imul@ variant uses internal representations for argument
367 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
368 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
370 /* --- @ec_mmul@, @ec_immul@ --- *
372 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
373 * @ec *d@ = pointer to the destination point
374 * @const ec_mulfactor *f@ = pointer to vector of factors
375 * @size_t n@ = number of factors
377 * Returns: The destination @d@.
379 * Use: Does simultaneous point multiplication. The @immul@ variant
380 * uses internal representations for arguments and result.
383 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
384 const ec_mulfactor */
*f*/
, size_t /*n*/);
385 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
386 const ec_mulfactor */
*f*/
, size_t /*n*/);
388 /*----- Standard curve operations -----------------------------------------*/
390 /* --- @ec_stdsamep@ --- *
392 * Arguments: @ec_curve *c, *d@ = two elliptic curves
394 * Returns: Nonzero if the curves are identical (not just isomorphic).
396 * Use: Simple sameness check on @a@ and @b@ curve members.
399 extern int ec_stdsamep(ec_curve */
*c*/
, ec_curve */
*d*/
);
401 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
403 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
404 * @ec *d@ = pointer to the destination
405 * @const ec *p@ = pointer to a source point
407 * Returns: The destination @d@.
409 * Use: An identity operation if your curve has no internal
410 * representation. (The field internal representation is still
414 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
415 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
416 extern ec
*ec_idfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
418 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
420 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
421 * @ec *d@ = pointer to the destination
422 * @const ec *p@ = pointer to a source point
424 * Returns: The destination @d@.
426 * Use: Conversion functions if your curve operations use a
427 * projective representation.
430 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
431 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
432 extern ec
*ec_projfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
434 /* --- @ec_stdsub@ --- *
436 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
437 * @ec *d@ = pointer to the destination
438 * @const ec *p, *q@ = the operand points
440 * Returns: The destination @d@.
442 * Use: Standard point subtraction operation, in terms of negation
443 * and addition. This isn't as efficient as a ready-made
444 * subtraction operator.
447 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
448 const ec */
*p*/
, const ec */
*q*/
);
450 /*----- Creating curves ---------------------------------------------------*/
452 /* --- @ec_destroycurve@ --- *
454 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
458 * Use: Destroys a description of an elliptic curve.
461 extern void ec_destroycurve(ec_curve */
*c*/
);
463 /* --- @ec_prime@, @ec_primeproj@ --- *
465 * Arguments: @field *f@ = the underlying field for this elliptic curve
466 * @mp *a, *b@ = the coefficients for this curve
468 * Returns: A pointer to the curve, or null.
470 * Use: Creates a curve structure for an elliptic curve defined over
471 * a prime field. The @primeproj@ variant uses projective
472 * coordinates, which can be a win.
475 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
476 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
478 /* --- @ec_bin@, @ec_binproj@ --- *
480 * Arguments: @field *f@ = the underlying field for this elliptic curve
481 * @mp *a, *b@ = the coefficients for this curve
483 * Returns: A pointer to the curve, or null.
485 * Use: Creates a curve structure for an elliptic curve defined over
486 * a binary field. The @binproj@ variant uses projective
487 * coordinates, which can be a win.
490 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
491 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
493 /*----- Curve parameter sets ----------------------------------------------*/
495 /* --- @ec_curveparse@ --- *
497 * Arguments: @qd_parse *qd@ = parser context
499 * Returns: Elliptic curve pointer if OK, or null.
501 * Use: Parses an elliptic curve description, which has the form
503 * * a field description
505 * * `prime', `primeproj', `bin', or `binproj'
507 * * the %$a$% parameter
509 * * the %$b$% parameter
512 extern ec_curve
*ec_curveparse(qd_parse */
*qd*/
);
514 /* --- @ec_ptparse@ --- *
516 * Arguments: @qd_parse *qd@ = parser context
517 * @ec *p@ = where to put the point
519 * Returns: The point address, or null.
521 * Use: Parses an elliptic curve point. This has the form
528 extern ec
*ec_ptparse(qd_parse */
*qd*/
, ec */
*p*/
);
530 /* --- @ec_infoparse@ --- *
532 * Arguments: @qd_parse *qd@ = parser context
533 * @ec_info *ei@ = curve information block, currently
536 * Returns: Zero on success, nonzero on failure.
538 * Use: Parses an elliptic curve information string, and stores the
539 * information in @ei@. This has the form
541 * * elliptic curve description
550 extern int ec_infoparse(qd_parse */
*qd*/
, ec_info */
*ei*/
);
552 /* --- @ec_infofromdata@ --- *
554 * Arguments: @ec_info *ei@ = where to write the information
555 * @ecdata *ed@ = raw data
559 * Use: Loads elliptic curve information about one of the standard
564 extern void ec_infofromdata(ec_info */
*ei*/
, struct ecdata */
*ed*/
);
566 /* --- @ec_getinfo@ --- *
568 * Arguments: @ec_info *ei@ = where to write the information
569 * @const char *p@ = string describing a curve
571 * Returns: Null on success, or a pointer to an error message.
573 * Use: Parses out information about a curve. The string is either a
574 * standard curve name, or a curve info string.
577 extern const char *ec_getinfo(ec_info */
*ei*/
, const char */
*p*/
);
579 /* --- @ec_sameinfop@ --- *
581 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
583 * Returns: Nonzero if the curves are identical (not just isomorphic).
585 * Use: Checks for sameness of curve parameters.
588 extern int ec_sameinfop(ec_info */
*ei*/
, ec_info */
*ej*/
);
590 /* --- @ec_freeinfo@ --- *
592 * Arguments: @ec_info *ei@ = elliptic curve information block to free
596 * Use: Frees the information block.
599 extern void ec_freeinfo(ec_info */
*ei*/
);
601 /* --- @ec_checkinfo@ --- *
603 * Arguments: @const ec_info *ei@ = elliptic curve information block
605 * Returns: Null if OK, or pointer to error message.
607 * Use: Checks an elliptic curve according to the rules in SEC1.
610 extern const char *ec_checkinfo(const ec_info */
*ei*/
, grand */
*gr*/
);
612 /*----- That's all, folks -------------------------------------------------*/