3 * $Id: ec.h,v 1.4 2003/05/15 23:25:59 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 2003/05/15 23:25:59 mdw
34 * Make elliptic curve stuff build.
36 * Revision 1.3 2002/01/13 13:48:44 mdw
39 * Revision 1.2 2001/05/07 17:29:44 mdw
40 * Treat projective coordinates as an internal representation. Various
41 * minor interface changes.
43 * Revision 1.1 2001/04/29 18:12:33 mdw
55 /*----- Header files ------------------------------------------------------*/
60 /*----- Data structures ---------------------------------------------------*/
62 /* --- An elliptic curve representation --- */
64 typedef struct ec_curve
{
65 const struct ec_ops
*ops
; /* Curve operations */
66 field
*f
; /* Underlying field structure */
69 /* --- An elliptic curve point --- */
72 mp
*x
, *y
; /* Point coordinates */
73 mp
*z
; /* Common denominator (or null) */
76 /* --- A factor for simultaneous multiplication --- */
78 typedef struct ec_mulfactor
{
79 ec base
; /* The point */
80 mp
*exp
; /* The exponent */
83 /* --- Elliptic curve operations --- */
85 typedef struct ec_ops
{
86 void (*destroy
)(ec_curve */
*c*/
);
87 ec
*(*in
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
88 ec
*(*out
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
89 ec
*(*find
)(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
90 ec
*(*neg
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
91 ec
*(*add
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
92 ec
*(*sub
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, const ec */
*q*/
);
93 ec
*(*dbl
)(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
96 #define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
97 #define EC_OUT(c, d, p) (c)->ops->in((c), (d), (p))
99 #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
100 #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
101 #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
102 #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
103 #define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
105 /*----- Simple memory management things -----------------------------------*/
107 /* --- @ec_create@ --- *
109 * Arguments: @ec *p@ = pointer to an elliptic-curve point
111 * Returns: The argument @p@.
113 * Use: Initializes a new point. The initial value is the additive
114 * identity (which is universal for all curves).
117 #define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
119 #define EC_CREATE(p) do { \
121 _p->x = _p->y = _p->z = MP_NEW; \
124 extern ec
*ec_create(ec */
*p*/
);
126 /* --- @ec_destroy@ --- *
128 * Arguments: @ec *p@ = pointer to an elliptic-curve point
132 * Use: Destroys a point, making it invalid.
135 #define EC_DESTROY(p) do { \
137 if (!EC_ATINF(_p)) { \
140 if (_p->z) MP_DROP(_p->z); \
144 extern void ec_destroy(ec */
*p*/
);
146 /* --- @ec_atinf@ --- *
148 * Arguments: @const ec *p@ = pointer to a point
150 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
154 #define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
156 extern int ec_atinf(const ec */
*p*/
);
158 /* --- @ec_setinf@ --- *
160 * Arguments: @ec *p@ = pointer to a point
162 * Returns: The argument @p@.
164 * Use: Sets the given point to be the point %$O$% at infinity.
167 #define EC_SETINF(p) do { \
169 if (!EC_ATINF(_p)) { \
172 if (_p->z) MP_DROP(_p->z); \
173 _p->x = _p->y = _p->z = MP_NEW; \
179 extern ec
*ec_setinf(ec */
*p*/
);
181 /* --- @ec_copy@ --- *
183 * Arguments: @ec *d@ = pointer to destination point
184 * @const ec *p@ = pointer to source point
186 * Returns: The destination @d@.
188 * Use: Creates a copy of an elliptic curve point.
191 #define EC_COPY(d, p) do { \
193 const ec *_p = (p); \
197 _d->x = _d->y = _d->z = MP_NEW; \
199 _d->x = MP_COPY(_p->x); \
200 _d->y = MP_COPY(_p->y); \
201 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
206 extern ec
*ec_copy(ec */
*d*/
, const ec */
*p*/
);
208 /*----- Interesting arithmetic --------------------------------------------*/
212 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
213 * @ec *d@ = pointer to the destination point
214 * @const ec *p@ = pointer to the source point
216 * Returns: The destination point.
218 * Use: Converts a point to internal representation.
221 extern ec
*ec_in(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
223 /* --- @ec_out@ --- *
225 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
226 * @ec *d@ = pointer to the destination point
227 * @const ec *p@ = pointer to the source point
229 * Returns: The destination point.
231 * Use: Converts a point to external representation.
234 extern ec
*ec_out(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
236 /* --- @ec_find@ --- *
238 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
239 * @ec *d@ = pointer to the destination point
240 * @mp *x@ = a possible x-coordinate
242 * Returns: The destination if OK, or null if no point was found.
244 * Use: Finds a point on an elliptic curve with a given
245 * x-coordinate. If there is no point with the given
246 * %$x$%-coordinate, a null pointer is returned and the
247 * destination is left invalid.
250 extern ec
*ec_find(ec_curve */
*c*/
, ec */
*d*/
, mp */
*x*/
);
252 /* --- @ec_neg@ --- *
254 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
255 * @ec *d@ = pointer to the destination point
256 * @const ec *p@ = pointer to the operand point
258 * Returns: The destination point.
260 * Use: Computes the negation of the given point.
263 extern ec
*ec_neg(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
265 /* --- @ec_add@ --- *
267 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
268 * @ec *d@ = pointer to the destination point
269 * @const ec *p, *q@ = pointers to the operand points
271 * Returns: The destination @d@.
273 * Use: Adds two points on an elliptic curve.
276 extern ec
*ec_add(ec_curve */
*c*/
, ec */
*d*/
,
277 const ec */
*p*/
, const ec */
*q*/
);
279 /* --- @ec_sub@ --- *
281 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
282 * @ec *d@ = pointer to the destination point
283 * @const ec *p, *q@ = pointers to the operand points
285 * Returns: The destination @d@.
287 * Use: Subtracts one point from another on an elliptic curve.
290 extern ec
*ec_sub(ec_curve */
*c*/
, ec */
*d*/
,
291 const ec */
*p*/
, const ec */
*q*/
);
293 /* --- @ec_dbl@ --- *
295 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
296 * @ec *d@ = pointer to the destination point
297 * @const ec *p@ = pointer to the operand point
299 * Returns: The destination @d@.
301 * Use: Doubles a point on an elliptic curve.
304 extern ec
*ec_dbl(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
306 /* --- @ec_mul@, @ec_imul@ --- *
308 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
309 * @ec *d@ = pointer to the destination point
310 * @const ec *p@ = pointer to the generator point
311 * @mp *n@ = integer multiplier
313 * Returns: The destination @d@.
315 * Use: Multiplies a point by a scalar, returning %$n p$%. The
316 * @imul@ variant uses internal representations for argument
320 extern ec
*ec_mul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
321 extern ec
*ec_imul(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
, mp */
*n*/
);
323 /* --- @ec_mmul@, @ec_immul@ --- *
325 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
326 * @ec *d@ = pointer to the destination point
327 * @const ec_mulfactor *f@ = pointer to vector of factors
328 * @size_t n@ = number of factors
330 * Returns: The destination @d@.
332 * Use: Does simultaneous point multiplication. The @immul@ variant
333 * uses internal representations for arguments and result.
336 extern ec
*ec_mmul(ec_curve */
*c*/
, ec */
*d*/
,
337 const ec_mulfactor */
*f*/
, size_t /*n*/);
338 extern ec
*ec_immul(ec_curve */
*c*/
, ec */
*d*/
,
339 const ec_mulfactor */
*f*/
, size_t /*n*/);
341 /*----- Standard curve operations -----------------------------------------*/
343 /* --- @ec_idin@, @ec_idout@ --- *
345 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
346 * @ec *d@ = pointer to the destination
347 * @const ec *p@ = pointer to a source point
349 * Returns: The destination @d@.
351 * Use: An identity operation if your curve has no internal
352 * representation. (The field internal representation is still
356 extern ec
*ec_idin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
357 extern ec
*ec_idout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
359 /* --- @ec_projin@, @ec_projout@ --- *
361 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
362 * @ec *d@ = pointer to the destination
363 * @const ec *p@ = pointer to a source point
365 * Returns: The destination @d@.
367 * Use: Conversion functions if your curve operations use a
368 * projective representation.
371 extern ec
*ec_projin(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
372 extern ec
*ec_projout(ec_curve */
*c*/
, ec */
*d*/
, const ec */
*p*/
);
374 /* --- @ec_stdsub@ --- *
376 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
377 * @ec *d@ = pointer to the destination
378 * @const ec *p, *q@ = the operand points
380 * Returns: The destination @d@.
382 * Use: Standard point subtraction operation, in terms of negation
383 * and addition. This isn't as efficient as a ready-made
384 * subtraction operator.
387 extern ec
*ec_stdsub(ec_curve */
*c*/
, ec */
*d*/
,
388 const ec */
*p*/
, const ec */
*q*/
);
390 /*----- Creating curves ---------------------------------------------------*/
392 /* --- @ec_destroycurve@ --- *
394 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
398 * Use: Destroys a description of an elliptic curve.
401 extern void ec_destroycurve(ec_curve */
*c*/
);
403 /* --- @ec_prime@, @ec_primeproj@ --- *
405 * Arguments: @field *f@ = the underyling field for this elliptic curve
406 * @mp *a, *b@ = the coefficients for this curve
408 * Returns: A pointer to the curve.
410 * Use: Creates a curve structure for an elliptic curve defined over
411 * a prime field. The @primeproj@ variant uses projective
412 * coordinates, which can be a win.
415 extern ec_curve
*ec_prime(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
416 extern ec_curve
*ec_primeproj(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
418 /* --- @ec_bin@ --- *
420 * Arguments: @field *f@ = the underlying field for this elliptic curve
421 * @mp *a, *b@ = the coefficients for this curve
423 * Returns: A pointer to the curve.
425 * Use: Creates a curve structure for a non-supersingular elliptic
426 * curve defined over a binary field.
429 extern ec_curve
*ec_bin(field */
*f*/
, mp */
*a*/
, mp */
*b*/
);
431 /*----- That's all, folks -------------------------------------------------*/