3 * $Id: ec.h,v 1.5 2004/03/21 22:52:06 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.5 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
39 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
40 * Projective coordinates for prime curves
42 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
43 * Simple (non-projective) curves over prime fields now seem to work.
45 * Revision 1.4 2003/05/15 23:25:59 mdw
46 * Make elliptic curve stuff build.
48 * Revision 1.3 2002/01/13 13:48:44 mdw
51 * Revision 1.2 2001/05/07 17:29:44 mdw
52 * Treat projective coordinates as an internal representation. Various
53 * minor interface changes.
55 * Revision 1.1 2001/04/29 18:12:33 mdw
67 /*----- Header files ------------------------------------------------------*/
72 /*----- Data structures ---------------------------------------------------*/
74 /* --- An elliptic curve representation --- */
76 typedef struct ec_curve
{
77 const struct ec_ops
*ops
; /* Curve operations */
78 field
*f
; /* Underlying field structure */
81 /* --- An elliptic curve point --- */
84 mp
*x
, *y
; /* Point coordinates */
85 mp
*z
; /* Common denominator (or null) */
88 /* --- A factor for simultaneous multiplication --- */
90 typedef struct ec_mulfactor
{
91 ec base
; /* The point */
92 mp
*exp
; /* The exponent */
95 /* --- Elliptic curve operations --- *
97 * All operations (apart from @destroy@ and @in@) are guaranteed to be
98 * performed on internal representations of points. Moreover, the second
99 * argument to @add@ and @mul@ is guaranteed to be the output of @in@ or
103 typedef struct ec_ops
{
104 void (*destroy
)(ec_curve */
*c*/
);
105 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
106 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
107 ec
*(*fix
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
108 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
109 ec
*(*neg
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
110 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
111 ec
*(*sub
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
112 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
113 int (*check
)(ec_curve */
*c*/
, const ec */
*p*/
);
116 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
117 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
118 #define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
120 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
121 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
122 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
123 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
124 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
125 #define EC_CHECK(c, p) (c)->ops->check((c), (p))
127 /*----- Simple memory management things -----------------------------------*/
129 /* --- @ec_create@ --- *
131 * Arguments: @ec *p@ = pointer to an elliptic-curve point
133 * Returns: The argument @p@.
135 * Use: Initializes a new point. The initial value is the additive
136 * identity (which is universal for all curves).
139 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
141 #define EC_CREATE(p) do { \
143 _p->x = _p->y = _p->z = MP_NEW; \
146 extern ec
*ec_create(ec */
*p*/
);
148 /* --- @ec_destroy@ --- *
150 * Arguments: @ec *p@ = pointer to an elliptic-curve point
154 * Use: Destroys a point, making it invalid.
157 #define EC_DESTROY(p) do { \
159 if (!EC_ATINF(_p)) { \
162 if (_p->z) MP_DROP(_p->z); \
166 extern void ec_destroy(ec */
*p*/
);
168 /* --- @ec_atinf@ --- *
170 * Arguments: @const ec *p@ = pointer to a point
172 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
176 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
178 extern int ec_atinf(const ec */
*p*/
);
180 /* --- @ec_setinf@ --- *
182 * Arguments: @ec *p@ = pointer to a point
184 * Returns: The argument @p@.
186 * Use: Sets the given point to be the point %$O$% at infinity.
189 #define EC_SETINF(p) do { \
191 if (!EC_ATINF(_p)) { \
194 if (_p->z) MP_DROP(_p->z); \
195 _p->x = _p->y = _p->z = MP_NEW; \
201 extern ec
*ec_setinf(ec */
*p*/
);
203 /* --- @ec_copy@ --- *
205 * Arguments: @ec *d@ = pointer to destination point
206 * @const ec *p@ = pointer to source point
208 * Returns: The destination @d@.
210 * Use: Creates a copy of an elliptic curve point.
213 #define EC_COPY(d, p) do { \
215 const ec *_p = (p); \
219 _d->x = _d->y = _d->z = MP_NEW; \
221 _d->x = MP_COPY(_p->x); \
222 _d->y = MP_COPY(_p->y); \
223 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
228 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
230 /*----- Interesting arithmetic --------------------------------------------*/
232 /* --- @ec_find@ --- *
234 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
235 * @ec *d@ = pointer to the destination point
236 * @mp *x@ = a possible x-coordinate
238 * Returns: The destination if OK, or null if no point was found.
240 * Use: Finds a point on an elliptic curve with a given
241 * x-coordinate. If there is no point with the given
242 * %$x$%-coordinate, a null pointer is returned and the
243 * destination is left invalid.
246 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
248 /* --- @ec_neg@ --- *
250 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
251 * @ec *d@ = pointer to the destination point
252 * @const ec *p@ = pointer to the operand point
254 * Returns: The destination point.
256 * Use: Computes the negation of the given point.
259 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
261 /* --- @ec_add@ --- *
263 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
264 * @ec *d@ = pointer to the destination point
265 * @const ec *p, *q@ = pointers to the operand points
267 * Returns: The destination @d@.
269 * Use: Adds two points on an elliptic curve.
272 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
273 const ec */
*p*/
, const ec */
*q*/
);
275 /* --- @ec_sub@ --- *
277 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
278 * @ec *d@ = pointer to the destination point
279 * @const ec *p, *q@ = pointers to the operand points
281 * Returns: The destination @d@.
283 * Use: Subtracts one point from another on an elliptic curve.
286 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
287 const ec */
*p*/
, const ec */
*q*/
);
289 /* --- @ec_dbl@ --- *
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 @d@.
297 * Use: Doubles a point on an elliptic curve.
300 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
302 /* --- @ec_check@ --- *
304 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
305 * @const ec *p@ = pointer to the point
307 * Returns: Zero if OK, nonzero if this is an invalid point.
309 * Use: Checks that a point is actually on an elliptic curve.
312 extern int ec_check(ec_curve */
*c*/
, const ec */
*p*/
);
314 /* --- @ec_mul@, @ec_imul@ --- *
316 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
317 * @ec *d@ = pointer to the destination point
318 * @const ec *p@ = pointer to the generator point
319 * @mp *n@ = integer multiplier
321 * Returns: The destination @d@.
323 * Use: Multiplies a point by a scalar, returning %$n p$%. The
324 * @imul@ variant uses internal representations for argument
328 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
329 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
331 /* --- @ec_mmul@, @ec_immul@ --- *
333 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
334 * @ec *d@ = pointer to the destination point
335 * @const ec_mulfactor *f@ = pointer to vector of factors
336 * @size_t n@ = number of factors
338 * Returns: The destination @d@.
340 * Use: Does simultaneous point multiplication. The @immul@ variant
341 * uses internal representations for arguments and result.
344 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
345 const ec_mulfactor */
*f*/
, size_t /*n*/);
346 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
347 const ec_mulfactor */
*f*/
, size_t /*n*/);
349 /*----- Standard curve operations -----------------------------------------*/
351 /* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
353 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
354 * @ec *d@ = pointer to the destination
355 * @const ec *p@ = pointer to a source point
357 * Returns: The destination @d@.
359 * Use: An identity operation if your curve has no internal
360 * representation. (The field internal representation is still
364 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
365 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
366 extern ec
*ec_idfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
368 /* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
370 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
371 * @ec *d@ = pointer to the destination
372 * @const ec *p@ = pointer to a source point
374 * Returns: The destination @d@.
376 * Use: Conversion functions if your curve operations use a
377 * projective representation.
380 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
381 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
382 extern ec
*ec_projfix(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
384 /* --- @ec_stdsub@ --- *
386 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
387 * @ec *d@ = pointer to the destination
388 * @const ec *p, *q@ = the operand points
390 * Returns: The destination @d@.
392 * Use: Standard point subtraction operation, in terms of negation
393 * and addition. This isn't as efficient as a ready-made
394 * subtraction operator.
397 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
398 const ec */
*p*/
, const ec */
*q*/
);
400 /*----- Creating curves ---------------------------------------------------*/
402 /* --- @ec_destroycurve@ --- *
404 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
408 * Use: Destroys a description of an elliptic curve.
411 extern void ec_destroycurve(ec_curve */
*c*/
);
413 /* --- @ec_prime@, @ec_primeproj@ --- *
415 * Arguments: @field *f@ = the underlying field for this elliptic curve
416 * @mp *a, *b@ = the coefficients for this curve
418 * Returns: A pointer to the curve.
420 * Use: Creates a curve structure for an elliptic curve defined over
421 * a prime field. The @primeproj@ variant uses projective
422 * coordinates, which can be a win.
425 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
426 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
428 /* --- @ec_bin@, @ec_binproj@ --- *
430 * Arguments: @field *f@ = the underlying field for this elliptic curve
431 * @mp *a, *b@ = the coefficients for this curve
433 * Returns: A pointer to the curve.
435 * Use: Creates a curve structure for an elliptic curve defined over
436 * a binary field. The @binproj@ variant uses projective
437 * coordinates, which can be a win.
440 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
441 extern ec_curve
*ec_binproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
443 /*----- That's all, folks -------------------------------------------------*/