bd71810c1d3d369f428d8e471f15705e9f333d51
3 * $Id: ec.h,v 1.11 2004/04/08 01:36:15 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,
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
{
87 void (*destroy
)(ec_curve */
*c*/
);
88 int (*samep
)(ec_curve */
*c*/
, ec_curve */
*d*/
);
89 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
90 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
91 ec
*(*fix
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
92 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
93 ec
*(*neg
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
94 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
95 ec
*(*sub
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
96 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
97 int (*check
)(ec_curve */
*c*/
, const ec */
*p*/
);
100 #define EC_SAMEP(c, d) (c)->ops->samep((c), (d))
101 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
102 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
103 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
105 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
106 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
107 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
108 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
109 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
110 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
112 /* --- Elliptic curve parameters --- */
114 typedef struct ec_info
{
115 ec_curve
*c
; /* The actual curve */
116 ec g
; /* The common point */
117 mp
*r
; /* Order of %$g$% */
118 mp
*h
; /* Cofactor %$h = \#E/r$% */
121 /*----- Simple memory management things -----------------------------------*/
123 /* --- @ec_create@ --- *
125 * Arguments: @ec *p@ = pointer to an elliptic-curve point
127 * Returns: The argument @p@.
129 * Use: Initializes a new point. The initial value is the additive
130 * identity (which is universal for all curves).
133 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
135 #define EC_CREATE(p) do { \
137 _p->x = _p->y = _p->z = MP_NEW; \
140 extern ec
*ec_create(ec */
*p*/
);
142 /* --- @ec_destroy@ --- *
144 * Arguments: @ec *p@ = pointer to an elliptic-curve point
148 * Use: Destroys a point, making it invalid.
151 #define EC_DESTROY(p) do { \
153 if (!EC_ATINF(_p)) { \
156 if (_p->z) MP_DROP(_p->z); \
160 extern void ec_destroy(ec */
*p*/
);
162 /* --- @ec_atinf@ --- *
164 * Arguments: @const ec *p@ = pointer to a point
166 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
170 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
172 extern int ec_atinf(const ec */
*p*/
);
174 /* --- @ec_setinf@ --- *
176 * Arguments: @ec *p@ = pointer to a point
178 * Returns: The argument @p@.
180 * Use: Sets the given point to be the point %$O$% at infinity.
183 #define EC_SETINF(p) do { \
185 if (!EC_ATINF(_p)) { \
188 if (_p->z) MP_DROP(_p->z); \
189 _p->x = _p->y = _p->z = MP_NEW; \
195 extern ec
*ec_setinf(ec */
*p*/
);
197 /* --- @ec_copy@ --- *
199 * Arguments: @ec *d@ = pointer to destination point
200 * @const ec *p@ = pointer to source point
202 * Returns: The destination @d@.
204 * Use: Creates a copy of an elliptic curve point.
207 #define EC_COPY(d, p) do { \
209 const ec *_p = (p); \
213 _d->x = _d->y = _d->z = MP_NEW; \
215 _d->x = MP_COPY(_p->x); \
216 _d->y = MP_COPY(_p->y); \
217 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
222 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
226 * Arguments: @const ec *p, *q@ = two points
228 * Returns: Nonzero if the points are equal. Compares external-format
232 #define EC_EQ(p, q) \
233 ((EC_ATINF(p) && EC_ATINF(q)) || \
234 (!EC_ATINF(p) && !EC_ATINF(q) && \
235 MP_EQ((p)->x, (q)->x) && \
236 MP_EQ((p)->y, (q)->y)))
238 extern int ec_eq(const ec
*p
, const ec
*q
);
240 /*----- Interesting arithmetic --------------------------------------------*/
242 /* --- @ec_samep@ --- *
244 * Arguments: @ec_curve *c, *d@ = two elliptic curves
246 * Returns: Nonzero if the curves are identical (not just isomorphic).
248 * Use: Checks for sameness of curves. This function does the full
249 * check, not just the curve-type-specific check done by the
250 * @sampep@ field operation.
253 extern int ec_samep(ec_curve */
*c*/
, ec_curve */
*d*/
);
255 /* --- @ec_find@ --- *
257 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
258 * @ec *d@ = pointer to the destination point
259 * @mp *x@ = a possible x-coordinate
261 * Returns: The destination if OK, or null if no point was found.
263 * Use: Finds a point on an elliptic curve with a given
264 * x-coordinate. If there is no point with the given
265 * %$x$%-coordinate, a null pointer is returned and the
266 * destination is left invalid.
269 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
271 /* --- @ec_rand@ --- *
273 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
274 * @ec *d@ = pointer to the destination point
275 * @grand *r@ = random number source
277 * Returns: The destination @d@.
279 * Use: Finds a random point on the given curve.
282 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
284 /* --- @ec_neg@ --- *
286 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
287 * @ec *d@ = pointer to the destination point
288 * @const ec *p@ = pointer to the operand point
290 * Returns: The destination point.
292 * Use: Computes the negation of the given point.
295 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
297 /* --- @ec_add@ --- *
299 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
300 * @ec *d@ = pointer to the destination point
301 * @const ec *p, *q@ = pointers to the operand points
303 * Returns: The destination @d@.
305 * Use: Adds two points on an elliptic curve.
308 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
309 const ec */
*p*/
, const ec */
*q*/
);
311 /* --- @ec_sub@ --- *
313 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
314 * @ec *d@ = pointer to the destination point
315 * @const ec *p, *q@ = pointers to the operand points
317 * Returns: The destination @d@.
319 * Use: Subtracts one point from another on an elliptic curve.
322 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
323 const ec */
*p*/
, const ec */
*q*/
);
325 /* --- @ec_dbl@ --- *
327 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
328 * @ec *d@ = pointer to the destination point
329 * @const ec *p@ = pointer to the operand point
331 * Returns: The destination @d@.
333 * Use: Doubles a point on an elliptic curve.
336 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
338 /* --- @ec_check@ --- *
340 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
341 * @const ec *p@ = pointer to the point
343 * Returns: Zero if OK, nonzero if this is an invalid point.
345 * Use: Checks that a point is actually on an elliptic curve.
348 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
350 /* --- @ec_mul@, @ec_imul@ --- *
352 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
353 * @ec *d@ = pointer to the destination point
354 * @const ec *p@ = pointer to the generator point
355 * @mp *n@ = integer multiplier
357 * Returns: The destination @d@.
359 * Use: Multiplies a point by a scalar, returning %$n p$%. The
360 * @imul@ variant uses internal representations for argument
364 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
365 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
367 /* --- @ec_mmul@, @ec_immul@ --- *
369 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
370 * @ec *d@ = pointer to the destination point
371 * @const ec_mulfactor *f@ = pointer to vector of factors
372 * @size_t n@ = number of factors
374 * Returns: The destination @d@.
376 * Use: Does simultaneous point multiplication. The @immul@ variant
377 * uses internal representations for arguments and result.
380 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
381 const ec_mulfactor */
*f*/
, size_t /*n*/);
382 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
383 const ec_mulfactor */
*f*/
, size_t /*n*/);
385 /*----- Standard curve operations -----------------------------------------*/
387 /* --- @ec_stdsamep@ --- *
389 * Arguments: @ec_curve *c, *d@ = two elliptic curves
391 * Returns: Nonzero if the curves are identical (not just isomorphic).
393 * Use: Simple sameness check on @a@ and @b@ curve members.
396 extern int ec_stdsamep(ec_curve */
*c*/
, ec_curve */
*d*/
);
398 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
400 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
401 * @ec *d@ = pointer to the destination
402 * @const ec *p@ = pointer to a source point
404 * Returns: The destination @d@.
406 * Use: An identity operation if your curve has no internal
407 * representation. (The field internal representation is still
411 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
412 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
413 extern ec
*ec_idfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
415 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
417 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
418 * @ec *d@ = pointer to the destination
419 * @const ec *p@ = pointer to a source point
421 * Returns: The destination @d@.
423 * Use: Conversion functions if your curve operations use a
424 * projective representation.
427 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
428 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
429 extern ec
*ec_projfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
431 /* --- @ec_stdsub@ --- *
433 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
434 * @ec *d@ = pointer to the destination
435 * @const ec *p, *q@ = the operand points
437 * Returns: The destination @d@.
439 * Use: Standard point subtraction operation, in terms of negation
440 * and addition. This isn't as efficient as a ready-made
441 * subtraction operator.
444 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
445 const ec */
*p*/
, const ec */
*q*/
);
447 /*----- Creating curves ---------------------------------------------------*/
449 /* --- @ec_destroycurve@ --- *
451 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
455 * Use: Destroys a description of an elliptic curve.
458 extern void ec_destroycurve(ec_curve */
*c*/
);
460 /* --- @ec_prime@, @ec_primeproj@ --- *
462 * Arguments: @field *f@ = the underlying field for this elliptic curve
463 * @mp *a, *b@ = the coefficients for this curve
465 * Returns: A pointer to the curve, or null.
467 * Use: Creates a curve structure for an elliptic curve defined over
468 * a prime field. The @primeproj@ variant uses projective
469 * coordinates, which can be a win.
472 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
473 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
475 /* --- @ec_bin@, @ec_binproj@ --- *
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, or null.
482 * Use: Creates a curve structure for an elliptic curve defined over
483 * a binary field. The @binproj@ variant uses projective
484 * coordinates, which can be a win.
487 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
488 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
490 /*----- Curve parameter sets ----------------------------------------------*/
492 /* --- @ec_curveparse@ --- *
494 * Arguments: @qd_parse *qd@ = parser context
496 * Returns: Elliptic curve pointer if OK, or null.
498 * Use: Parses an elliptic curve description, which has the form
500 * * a field description
502 * * `prime', `primeproj', `bin', or `binproj'
504 * * the %$a$% parameter
506 * * the %$b$% parameter
509 extern ec_curve
*ec_curveparse(qd_parse */
*qd*/
);
511 /* --- @ec_ptparse@ --- *
513 * Arguments: @qd_parse *qd@ = parser context
514 * @ec *p@ = where to put the point
516 * Returns: The point address, or null.
518 * Use: Parses an elliptic curve point. This has the form
525 extern ec
*ec_ptparse(qd_parse */
*qd*/
, ec */
*p*/
);
527 /* --- @ec_infoparse@ --- *
529 * Arguments: @qd_parse *qd@ = parser context
530 * @ec_info *ei@ = curve information block, currently
533 * Returns: Zero on success, nonzero on failure.
535 * Use: Parses an elliptic curve information string, and stores the
536 * information in @ei@. This has the form
538 * * elliptic curve description
547 extern int ec_infoparse(qd_parse */
*qd*/
, ec_info */
*ei*/
);
549 /* --- @ec_getinfo@ --- *
551 * Arguments: @ec_info *ei@ = where to write the information
552 * @const char *p@ = string describing a curve
554 * Returns: Null on success, or a pointer to an error message.
556 * Use: Parses out information about a curve. The string is either a
557 * standard curve name, or a curve info string.
560 extern const char *ec_getinfo(ec_info */
*ei*/
, const char */
*p*/
);
562 /* --- @ec_sameinfop@ --- *
564 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
566 * Returns: Nonzero if the curves are identical (not just isomorphic).
568 * Use: Checks for sameness of curve parameters.
571 extern int ec_sameinfop(ec_info */
*ei*/
, ec_info */
*ej*/
);
573 /* --- @ec_freeinfo@ --- *
575 * Arguments: @ec_info *ei@ = elliptic curve information block to free
579 * Use: Frees the information block.
582 extern void ec_freeinfo(ec_info */
*ei*/
);
584 /* --- @ec_checkinfo@ --- *
586 * Arguments: @const ec_info *ei@ = elliptic curve information block
588 * Returns: Null if OK, or pointer to error message.
590 * Use: Checks an elliptic curve according to the rules in SEC1.
593 extern const char *ec_checkinfo(const ec_info */
*ei*/
, grand */
*gr*/
);
595 /*----- That's all, folks -------------------------------------------------*/