3 * $Id: ec.h,v 1.7 2004/03/23 15:19:32 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.7 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
36 * Revision 1.6 2004/03/22 02:19:10 mdw
37 * Rationalise the sliding-window threshold. Drop guarantee that right
38 * arguments to EC @add@ are canonical, and fix up projective implementations
41 * Revision 1.5 2004/03/21 22:52:06 mdw
42 * Merge and close elliptic curve branch.
44 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
45 * Elliptic curves on binary fields work.
47 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
48 * Projective coordinates for prime curves
50 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
51 * Simple (non-projective) curves over prime fields now seem to work.
53 * Revision 1.4 2003/05/15 23:25:59 mdw
54 * Make elliptic curve stuff build.
56 * Revision 1.3 2002/01/13 13:48:44 mdw
59 * Revision 1.2 2001/05/07 17:29:44 mdw
60 * Treat projective coordinates as an internal representation. Various
61 * minor interface changes.
63 * Revision 1.1 2001/04/29 18:12:33 mdw
75 /*----- Header files ------------------------------------------------------*/
80 /*----- Data structures ---------------------------------------------------*/
82 /* --- An elliptic curve representation --- */
84 typedef struct ec_curve
{
85 const struct ec_ops
*ops
; /* Curve operations */
86 field
*f
; /* Underlying field structure */
89 /* --- An elliptic curve point --- */
92 mp
*x
, *y
; /* Point coordinates */
93 mp
*z
; /* Common denominator (or null) */
96 /* --- A factor for simultaneous multiplication --- */
98 typedef struct ec_mulfactor
{
99 ec base
; /* The point */
100 mp
*exp
; /* The exponent */
103 /* --- Elliptic curve operations --- *
105 * All operations (apart from @destroy@ and @in@) are guaranteed to be
106 * performed on internal representations of points.
108 * (Historical note. We used to guarantee that the second to @add@ and @mul@
109 * was the output of @in@ or @fix@, but this canonification turned out to
110 * make the precomputation in @ec_exp@ too slow. Projective implementations
111 * must therefore cope with a pair of arbitrary points.)
114 typedef struct ec_ops
{
115 void (*destroy
)(ec_curve */
*c*/
);
116 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
117 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
118 ec
*(*fix
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
119 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
120 ec
*(*neg
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
121 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
122 ec
*(*sub
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
123 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
124 int (*check
)(ec_curve */
*c*/
, const ec */
*p*/
);
127 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
128 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
129 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
131 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
132 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
133 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
134 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
135 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
136 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
138 /*----- Simple memory management things -----------------------------------*/
140 /* --- @ec_create@ --- *
142 * Arguments: @ec *p@ = pointer to an elliptic-curve point
144 * Returns: The argument @p@.
146 * Use: Initializes a new point. The initial value is the additive
147 * identity (which is universal for all curves).
150 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
152 #define EC_CREATE(p) do { \
154 _p->x = _p->y = _p->z = MP_NEW; \
157 extern ec
*ec_create(ec */
*p*/
);
159 /* --- @ec_destroy@ --- *
161 * Arguments: @ec *p@ = pointer to an elliptic-curve point
165 * Use: Destroys a point, making it invalid.
168 #define EC_DESTROY(p) do { \
170 if (!EC_ATINF(_p)) { \
173 if (_p->z) MP_DROP(_p->z); \
177 extern void ec_destroy(ec */
*p*/
);
179 /* --- @ec_atinf@ --- *
181 * Arguments: @const ec *p@ = pointer to a point
183 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
187 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
189 extern int ec_atinf(const ec */
*p*/
);
191 /* --- @ec_setinf@ --- *
193 * Arguments: @ec *p@ = pointer to a point
195 * Returns: The argument @p@.
197 * Use: Sets the given point to be the point %$O$% at infinity.
200 #define EC_SETINF(p) do { \
202 if (!EC_ATINF(_p)) { \
205 if (_p->z) MP_DROP(_p->z); \
206 _p->x = _p->y = _p->z = MP_NEW; \
212 extern ec
*ec_setinf(ec */
*p*/
);
214 /* --- @ec_copy@ --- *
216 * Arguments: @ec *d@ = pointer to destination point
217 * @const ec *p@ = pointer to source point
219 * Returns: The destination @d@.
221 * Use: Creates a copy of an elliptic curve point.
224 #define EC_COPY(d, p) do { \
226 const ec *_p = (p); \
230 _d->x = _d->y = _d->z = MP_NEW; \
232 _d->x = MP_COPY(_p->x); \
233 _d->y = MP_COPY(_p->y); \
234 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
239 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
243 * Arguments: @const ec *p, *q@ = two points
245 * Returns: Nonzero if the points are equal. Compares external-format
249 #define EC_EQ(p, q) \
250 ((EC_ATINF(p) && EC_ATINF(q)) || \
251 (!EC_ATINF(p) && !EC_ATINF(q) && \
252 MP_EQ((p)->x, (q)->x) && \
253 MP_EQ((p)->y, (q)->y)))
255 extern int ec_eq(const ec
*p
, const ec
*q
);
257 /*----- Interesting arithmetic --------------------------------------------*/
259 /* --- @ec_find@ --- *
261 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
262 * @ec *d@ = pointer to the destination point
263 * @mp *x@ = a possible x-coordinate
265 * Returns: The destination if OK, or null if no point was found.
267 * Use: Finds a point on an elliptic curve with a given
268 * x-coordinate. If there is no point with the given
269 * %$x$%-coordinate, a null pointer is returned and the
270 * destination is left invalid.
273 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
275 /* --- @ec_rand@ --- *
277 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
278 * @ec *d@ = pointer to the destination point
279 * @grand *r@ = random number source
281 * Returns: The destination @d@.
283 * Use: Finds a random point on the given curve.
286 extern ec
*ec_rand(ec_curve */
*c*/
, ec */
*d*/
, grand */
*r*/
);
288 /* --- @ec_neg@ --- *
290 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
291 * @ec *d@ = pointer to the destination point
292 * @const ec *p@ = pointer to the operand point
294 * Returns: The destination point.
296 * Use: Computes the negation of the given point.
299 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
301 /* --- @ec_add@ --- *
303 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
304 * @ec *d@ = pointer to the destination point
305 * @const ec *p, *q@ = pointers to the operand points
307 * Returns: The destination @d@.
309 * Use: Adds two points on an elliptic curve.
312 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
313 const ec */
*p*/
, const ec */
*q*/
);
315 /* --- @ec_sub@ --- *
317 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
318 * @ec *d@ = pointer to the destination point
319 * @const ec *p, *q@ = pointers to the operand points
321 * Returns: The destination @d@.
323 * Use: Subtracts one point from another on an elliptic curve.
326 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
327 const ec */
*p*/
, const ec */
*q*/
);
329 /* --- @ec_dbl@ --- *
331 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
332 * @ec *d@ = pointer to the destination point
333 * @const ec *p@ = pointer to the operand point
335 * Returns: The destination @d@.
337 * Use: Doubles a point on an elliptic curve.
340 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
342 /* --- @ec_check@ --- *
344 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
345 * @const ec *p@ = pointer to the point
347 * Returns: Zero if OK, nonzero if this is an invalid point.
349 * Use: Checks that a point is actually on an elliptic curve.
352 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
354 /* --- @ec_mul@, @ec_imul@ --- *
356 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
357 * @ec *d@ = pointer to the destination point
358 * @const ec *p@ = pointer to the generator point
359 * @mp *n@ = integer multiplier
361 * Returns: The destination @d@.
363 * Use: Multiplies a point by a scalar, returning %$n p$%. The
364 * @imul@ variant uses internal representations for argument
368 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
369 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
371 /* --- @ec_mmul@, @ec_immul@ --- *
373 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
374 * @ec *d@ = pointer to the destination point
375 * @const ec_mulfactor *f@ = pointer to vector of factors
376 * @size_t n@ = number of factors
378 * Returns: The destination @d@.
380 * Use: Does simultaneous point multiplication. The @immul@ variant
381 * uses internal representations for arguments and result.
384 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
385 const ec_mulfactor */
*f*/
, size_t /*n*/);
386 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
387 const ec_mulfactor */
*f*/
, size_t /*n*/);
389 /*----- Standard curve operations -----------------------------------------*/
391 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
393 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
394 * @ec *d@ = pointer to the destination
395 * @const ec *p@ = pointer to a source point
397 * Returns: The destination @d@.
399 * Use: An identity operation if your curve has no internal
400 * representation. (The field internal representation is still
404 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
405 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
406 extern ec
*ec_idfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
408 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
410 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
411 * @ec *d@ = pointer to the destination
412 * @const ec *p@ = pointer to a source point
414 * Returns: The destination @d@.
416 * Use: Conversion functions if your curve operations use a
417 * projective representation.
420 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
421 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
422 extern ec
*ec_projfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
424 /* --- @ec_stdsub@ --- *
426 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
427 * @ec *d@ = pointer to the destination
428 * @const ec *p, *q@ = the operand points
430 * Returns: The destination @d@.
432 * Use: Standard point subtraction operation, in terms of negation
433 * and addition. This isn't as efficient as a ready-made
434 * subtraction operator.
437 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
438 const ec */
*p*/
, const ec */
*q*/
);
440 /*----- Creating curves ---------------------------------------------------*/
442 /* --- @ec_destroycurve@ --- *
444 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
448 * Use: Destroys a description of an elliptic curve.
451 extern void ec_destroycurve(ec_curve */
*c*/
);
453 /* --- @ec_prime@, @ec_primeproj@ --- *
455 * Arguments: @field *f@ = the underlying field for this elliptic curve
456 * @mp *a, *b@ = the coefficients for this curve
458 * Returns: A pointer to the curve.
460 * Use: Creates a curve structure for an elliptic curve defined over
461 * a prime field. The @primeproj@ variant uses projective
462 * coordinates, which can be a win.
465 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
466 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
468 /* --- @ec_bin@, @ec_binproj@ --- *
470 * Arguments: @field *f@ = the underlying field for this elliptic curve
471 * @mp *a, *b@ = the coefficients for this curve
473 * Returns: A pointer to the curve.
475 * Use: Creates a curve structure for an elliptic curve defined over
476 * a binary field. The @binproj@ variant uses projective
477 * coordinates, which can be a win.
480 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
481 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
483 /*----- That's all, folks -------------------------------------------------*/