3 * $Id: ec.h,v 1.4.4.1 2003/06/10 13:43:53 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.4.4.1 2003/06/10 13:43:53 mdw
34 * Simple (non-projective) curves over prime fields now seem to work.
36 * Revision 1.4 2003/05/15 23:25:59 mdw
37 * Make elliptic curve stuff build.
39 * Revision 1.3 2002/01/13 13:48:44 mdw
42 * Revision 1.2 2001/05/07 17:29:44 mdw
43 * Treat projective coordinates as an internal representation. Various
44 * minor interface changes.
46 * Revision 1.1 2001/04/29 18:12:33 mdw
58 /*----- Header files ------------------------------------------------------*/
63 /*----- Data structures ---------------------------------------------------*/
65 /* --- An elliptic curve representation --- */
67 typedef struct ec_curve
{
68 const struct ec_ops
*ops
; /* Curve operations */
69 field
*f
; /* Underlying field structure */
72 /* --- An elliptic curve point --- */
75 mp
*x
, *y
; /* Point coordinates */
76 mp
*z
; /* Common denominator (or null) */
79 /* --- A factor for simultaneous multiplication --- */
81 typedef struct ec_mulfactor
{
82 ec base
; /* The point */
83 mp
*exp
; /* The exponent */
86 /* --- Elliptic curve operations --- */
88 typedef struct ec_ops
{
89 void (*destroy
)(ec_curve */
*c*/
);
90 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
91 ec
*(*out
)(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*/
);
99 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
100 #define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
102 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
103 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
104 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
105 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
106 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
108 /*----- Simple memory management things -----------------------------------*/
110 /* --- @ec_create@ --- *
112 * Arguments: @ec *p@ = pointer to an elliptic-curve point
114 * Returns: The argument @p@.
116 * Use: Initializes a new point. The initial value is the additive
117 * identity (which is universal for all curves).
120 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
122 #define EC_CREATE(p) do { \
124 _p->x = _p->y = _p->z = MP_NEW; \
127 extern ec
*ec_create(ec */
*p*/
);
129 /* --- @ec_destroy@ --- *
131 * Arguments: @ec *p@ = pointer to an elliptic-curve point
135 * Use: Destroys a point, making it invalid.
138 #define EC_DESTROY(p) do { \
140 if (!EC_ATINF(_p)) { \
143 if (_p->z) MP_DROP(_p->z); \
147 extern void ec_destroy(ec */
*p*/
);
149 /* --- @ec_atinf@ --- *
151 * Arguments: @const ec *p@ = pointer to a point
153 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
157 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
159 extern int ec_atinf(const ec */
*p*/
);
161 /* --- @ec_setinf@ --- *
163 * Arguments: @ec *p@ = pointer to a point
165 * Returns: The argument @p@.
167 * Use: Sets the given point to be the point %$O$% at infinity.
170 #define EC_SETINF(p) do { \
172 if (!EC_ATINF(_p)) { \
175 if (_p->z) MP_DROP(_p->z); \
176 _p->x = _p->y = _p->z = MP_NEW; \
182 extern ec
*ec_setinf(ec */
*p*/
);
184 /* --- @ec_copy@ --- *
186 * Arguments: @ec *d@ = pointer to destination point
187 * @const ec *p@ = pointer to source point
189 * Returns: The destination @d@.
191 * Use: Creates a copy of an elliptic curve point.
194 #define EC_COPY(d, p) do { \
196 const ec *_p = (p); \
200 _d->x = _d->y = _d->z = MP_NEW; \
202 _d->x = MP_COPY(_p->x); \
203 _d->y = MP_COPY(_p->y); \
204 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
209 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
211 /*----- Interesting arithmetic --------------------------------------------*/
215 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
216 * @ec *d@ = pointer to the destination point
217 * @const ec *p@ = pointer to the source point
219 * Returns: The destination point.
221 * Use: Converts a point to internal representation.
224 extern ec
*ec_in(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
226 /* --- @ec_out@ --- *
228 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
229 * @ec *d@ = pointer to the destination point
230 * @const ec *p@ = pointer to the source point
232 * Returns: The destination point.
234 * Use: Converts a point to external representation.
237 extern ec
*ec_out(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
239 /* --- @ec_find@ --- *
241 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
242 * @ec *d@ = pointer to the destination point
243 * @mp *x@ = a possible x-coordinate
245 * Returns: The destination if OK, or null if no point was found.
247 * Use: Finds a point on an elliptic curve with a given
248 * x-coordinate. If there is no point with the given
249 * %$x$%-coordinate, a null pointer is returned and the
250 * destination is left invalid.
253 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
255 /* --- @ec_neg@ --- *
257 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
258 * @ec *d@ = pointer to the destination point
259 * @const ec *p@ = pointer to the operand point
261 * Returns: The destination point.
263 * Use: Computes the negation of the given point.
266 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
268 /* --- @ec_add@ --- *
270 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
271 * @ec *d@ = pointer to the destination point
272 * @const ec *p, *q@ = pointers to the operand points
274 * Returns: The destination @d@.
276 * Use: Adds two points on an elliptic curve.
279 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
280 const ec */
*p*/
, const ec */
*q*/
);
282 /* --- @ec_sub@ --- *
284 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
285 * @ec *d@ = pointer to the destination point
286 * @const ec *p, *q@ = pointers to the operand points
288 * Returns: The destination @d@.
290 * Use: Subtracts one point from another on an elliptic curve.
293 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
294 const ec */
*p*/
, const ec */
*q*/
);
296 /* --- @ec_dbl@ --- *
298 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
299 * @ec *d@ = pointer to the destination point
300 * @const ec *p@ = pointer to the operand point
302 * Returns: The destination @d@.
304 * Use: Doubles a point on an elliptic curve.
307 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
309 /* --- @ec_mul@, @ec_imul@ --- *
311 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
312 * @ec *d@ = pointer to the destination point
313 * @const ec *p@ = pointer to the generator point
314 * @mp *n@ = integer multiplier
316 * Returns: The destination @d@.
318 * Use: Multiplies a point by a scalar, returning %$n p$%. The
319 * @imul@ variant uses internal representations for argument
323 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
324 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
326 /* --- @ec_mmul@, @ec_immul@ --- *
328 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
329 * @ec *d@ = pointer to the destination point
330 * @const ec_mulfactor *f@ = pointer to vector of factors
331 * @size_t n@ = number of factors
333 * Returns: The destination @d@.
335 * Use: Does simultaneous point multiplication. The @immul@ variant
336 * uses internal representations for arguments and result.
339 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
340 const ec_mulfactor */
*f*/
, size_t /*n*/);
341 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
342 const ec_mulfactor */
*f*/
, size_t /*n*/);
344 /*----- Standard curve operations -----------------------------------------*/
346 /* --- @ec_idin@, @ec_idout@ --- *
348 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
349 * @ec *d@ = pointer to the destination
350 * @const ec *p@ = pointer to a source point
352 * Returns: The destination @d@.
354 * Use: An identity operation if your curve has no internal
355 * representation. (The field internal representation is still
359 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
360 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
362 /* --- @ec_projin@, @ec_projout@ --- *
364 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
365 * @ec *d@ = pointer to the destination
366 * @const ec *p@ = pointer to a source point
368 * Returns: The destination @d@.
370 * Use: Conversion functions if your curve operations use a
371 * projective representation.
374 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
375 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
377 /* --- @ec_stdsub@ --- *
379 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
380 * @ec *d@ = pointer to the destination
381 * @const ec *p, *q@ = the operand points
383 * Returns: The destination @d@.
385 * Use: Standard point subtraction operation, in terms of negation
386 * and addition. This isn't as efficient as a ready-made
387 * subtraction operator.
390 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
391 const ec */
*p*/
, const ec */
*q*/
);
393 /*----- Creating curves ---------------------------------------------------*/
395 /* --- @ec_destroycurve@ --- *
397 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
401 * Use: Destroys a description of an elliptic curve.
404 extern void ec_destroycurve(ec_curve */
*c*/
);
406 /* --- @ec_prime@, @ec_primeproj@ --- *
408 * Arguments: @field *f@ = the underlying field for this elliptic curve
409 * @mp *a, *b@ = the coefficients for this curve
411 * Returns: A pointer to the curve.
413 * Use: Creates a curve structure for an elliptic curve defined over
414 * a prime field. The @primeproj@ variant uses projective
415 * coordinates, which can be a win.
418 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
419 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
421 /* --- @ec_bin@ --- *
423 * Arguments: @field *f@ = the underlying field for this elliptic curve
424 * @mp *a, *b@ = the coefficients for this curve
426 * Returns: A pointer to the curve.
428 * Use: Creates a curve structure for a non-supersingular elliptic
429 * curve defined over a binary field.
432 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
434 /*----- That's all, folks -------------------------------------------------*/