Elliptic curves on binary fields work.
[u/mdw/catacomb] / ec.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
ceb3f0c0 3 * $Id: ec.h,v 1.4.4.3 2004/03/21 22:39:46 mdw Exp $
b0ab12e6 4 *
5 * Elliptic curve definitions
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: ec.h,v $
ceb3f0c0 33 * Revision 1.4.4.3 2004/03/21 22:39:46 mdw
34 * Elliptic curves on binary fields work.
35 *
8823192f 36 * Revision 1.4.4.2 2004/03/20 00:13:31 mdw
37 * Projective coordinates for prime curves
38 *
dbfee00a 39 * Revision 1.4.4.1 2003/06/10 13:43:53 mdw
40 * Simple (non-projective) curves over prime fields now seem to work.
41 *
41cb1beb 42 * Revision 1.4 2003/05/15 23:25:59 mdw
43 * Make elliptic curve stuff build.
44 *
b085fd91 45 * Revision 1.3 2002/01/13 13:48:44 mdw
46 * Further progress.
47 *
41a324a7 48 * Revision 1.2 2001/05/07 17:29:44 mdw
49 * Treat projective coordinates as an internal representation. Various
50 * minor interface changes.
51 *
b0ab12e6 52 * Revision 1.1 2001/04/29 18:12:33 mdw
53 * Prototype version.
54 *
55 */
56
57#ifndef CATACOMB_EC_H
58#define CATACOMB_EC_H
59
60#ifdef __cplusplus
61 extern "C" {
62#endif
63
64/*----- Header files ------------------------------------------------------*/
65
66#include "field.h"
67#include "mp.h"
68
69/*----- Data structures ---------------------------------------------------*/
70
b085fd91 71/* --- An elliptic curve representation --- */
72
b0ab12e6 73typedef struct ec_curve {
74 const struct ec_ops *ops; /* Curve operations */
75 field *f; /* Underlying field structure */
76} ec_curve;
77
b085fd91 78/* --- An elliptic curve point --- */
79
b0ab12e6 80typedef struct ec {
81 mp *x, *y; /* Point coordinates */
82 mp *z; /* Common denominator (or null) */
83} ec;
84
b085fd91 85/* --- A factor for simultaneous multiplication --- */
86
87typedef struct ec_mulfactor {
88 ec base; /* The point */
41cb1beb 89 mp *exp; /* The exponent */
b085fd91 90} ec_mulfactor;
91
8823192f 92/* --- Elliptic curve operations --- *
93 *
94 * All operations (apart from @destroy@ and @in@) are guaranteed to be
95 * performed on internal representations of points. Moreover, the second
96 * argument to @add@ and @mul@ is guaranteed to be the output of @in@ or
97 * @fix@.
98 */
b085fd91 99
b0ab12e6 100typedef struct ec_ops {
101 void (*destroy)(ec_curve */*c*/);
41a324a7 102 ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
103 ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 104 ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 105 ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
b085fd91 106 ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 107 ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
b085fd91 108 ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
41a324a7 109 ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 110 int (*check)(ec_curve */*c*/, const ec */*p*/);
b0ab12e6 111} ec_ops;
112
41a324a7 113#define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
dbfee00a 114#define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
8823192f 115#define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
41a324a7 116
b0ab12e6 117#define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
b085fd91 118#define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
b0ab12e6 119#define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
b085fd91 120#define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
b0ab12e6 121#define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
8823192f 122#define EC_CHECK(c, p) (c)->ops->check((c), (p))
b0ab12e6 123
124/*----- Simple memory management things -----------------------------------*/
125
126/* --- @ec_create@ --- *
127 *
128 * Arguments: @ec *p@ = pointer to an elliptic-curve point
129 *
41a324a7 130 * Returns: The argument @p@.
b0ab12e6 131 *
132 * Use: Initializes a new point. The initial value is the additive
133 * identity (which is universal for all curves).
134 */
135
136#define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
137
138#define EC_CREATE(p) do { \
139 ec *_p = (p); \
140 _p->x = _p->y = _p->z = MP_NEW; \
141} while (0)
142
41a324a7 143extern ec *ec_create(ec */*p*/);
b0ab12e6 144
145/* --- @ec_destroy@ --- *
146 *
147 * Arguments: @ec *p@ = pointer to an elliptic-curve point
148 *
149 * Returns: ---
150 *
151 * Use: Destroys a point, making it invalid.
152 */
153
154#define EC_DESTROY(p) do { \
155 ec *_p = (p); \
156 if (!EC_ATINF(_p)) { \
157 MP_DROP(_p->x); \
158 MP_DROP(_p->y); \
159 if (_p->z) MP_DROP(_p->z); \
160 } \
161} while (0)
162
163extern void ec_destroy(ec */*p*/);
164
165/* --- @ec_atinf@ --- *
166 *
167 * Arguments: @const ec *p@ = pointer to a point
168 *
169 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
170 * otherwise.
171 */
172
173#define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
174
175extern int ec_atinf(const ec */*p*/);
176
177/* --- @ec_setinf@ --- *
178 *
179 * Arguments: @ec *p@ = pointer to a point
180 *
41a324a7 181 * Returns: The argument @p@.
b0ab12e6 182 *
183 * Use: Sets the given point to be the point %$O$% at infinity.
184 */
185
186#define EC_SETINF(p) do { \
187 ec *_p = (p); \
188 if (!EC_ATINF(_p)) { \
189 MP_DROP(_p->x); \
190 MP_DROP(_p->y); \
191 if (_p->z) MP_DROP(_p->z); \
192 _p->x = _p->y = _p->z = MP_NEW; \
193 _p->y = MP_NEW; \
194 _p->z = MP_NEW; \
195 } \
196} while (0)
197
41a324a7 198extern ec *ec_setinf(ec */*p*/);
b0ab12e6 199
200/* --- @ec_copy@ --- *
201 *
202 * Arguments: @ec *d@ = pointer to destination point
203 * @const ec *p@ = pointer to source point
204 *
41a324a7 205 * Returns: The destination @d@.
b0ab12e6 206 *
207 * Use: Creates a copy of an elliptic curve point.
208 */
209
210#define EC_COPY(d, p) do { \
211 ec *_d = (d); \
212 const ec *_p = (p); \
213 if (d != p) { \
214 EC_DESTROY(d); \
215 if (EC_ATINF(p)) \
216 _d->x = _d->y = _d->z = MP_NEW; \
217 else { \
b085fd91 218 _d->x = MP_COPY(_p->x); \
219 _d->y = MP_COPY(_p->y); \
220 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
b0ab12e6 221 } \
222 } \
223} while (0)
224
41a324a7 225extern ec *ec_copy(ec */*d*/, const ec */*p*/);
b0ab12e6 226
227/*----- Interesting arithmetic --------------------------------------------*/
228
b0ab12e6 229/* --- @ec_find@ --- *
230 *
231 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
232 * @ec *d@ = pointer to the destination point
233 * @mp *x@ = a possible x-coordinate
234 *
b085fd91 235 * Returns: The destination if OK, or null if no point was found.
b0ab12e6 236 *
b085fd91 237 * Use: Finds a point on an elliptic curve with a given
238 * x-coordinate. If there is no point with the given
239 * %$x$%-coordinate, a null pointer is returned and the
240 * destination is left invalid.
b0ab12e6 241 */
242
b085fd91 243extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
244
245/* --- @ec_neg@ --- *
246 *
247 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
248 * @ec *d@ = pointer to the destination point
249 * @const ec *p@ = pointer to the operand point
250 *
251 * Returns: The destination point.
252 *
253 * Use: Computes the negation of the given point.
254 */
255
256extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 257
258/* --- @ec_add@ --- *
259 *
260 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
261 * @ec *d@ = pointer to the destination point
262 * @const ec *p, *q@ = pointers to the operand points
263 *
41a324a7 264 * Returns: The destination @d@.
b0ab12e6 265 *
266 * Use: Adds two points on an elliptic curve.
267 */
268
41a324a7 269extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
270 const ec */*p*/, const ec */*q*/);
b0ab12e6 271
b085fd91 272/* --- @ec_sub@ --- *
273 *
274 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
275 * @ec *d@ = pointer to the destination point
276 * @const ec *p, *q@ = pointers to the operand points
277 *
278 * Returns: The destination @d@.
279 *
280 * Use: Subtracts one point from another on an elliptic curve.
281 */
282
283extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
284 const ec */*p*/, const ec */*q*/);
285
b0ab12e6 286/* --- @ec_dbl@ --- *
287 *
288 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
289 * @ec *d@ = pointer to the destination point
290 * @const ec *p@ = pointer to the operand point
291 *
41a324a7 292 * Returns: The destination @d@.
b0ab12e6 293 *
294 * Use: Doubles a point on an elliptic curve.
295 */
296
41a324a7 297extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 298
8823192f 299/* --- @ec_check@ --- *
300 *
301 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
302 * @const ec *p@ = pointer to the point
303 *
304 * Returns: Zero if OK, nonzero if this is an invalid point.
305 *
306 * Use: Checks that a point is actually on an elliptic curve.
307 */
308
309extern int ec_check(ec_curve */*c*/, const ec */*p*/);
310
b085fd91 311/* --- @ec_mul@, @ec_imul@ --- *
b0ab12e6 312 *
313 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
314 * @ec *d@ = pointer to the destination point
315 * @const ec *p@ = pointer to the generator point
316 * @mp *n@ = integer multiplier
317 *
41a324a7 318 * Returns: The destination @d@.
b0ab12e6 319 *
b085fd91 320 * Use: Multiplies a point by a scalar, returning %$n p$%. The
321 * @imul@ variant uses internal representations for argument
322 * and result.
b0ab12e6 323 */
324
41a324a7 325extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
b085fd91 326extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
327
328/* --- @ec_mmul@, @ec_immul@ --- *
329 *
330 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
331 * @ec *d@ = pointer to the destination point
332 * @const ec_mulfactor *f@ = pointer to vector of factors
333 * @size_t n@ = number of factors
334 *
335 * Returns: The destination @d@.
336 *
337 * Use: Does simultaneous point multiplication. The @immul@ variant
338 * uses internal representations for arguments and result.
339 */
340
341extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/,
342 const ec_mulfactor */*f*/, size_t /*n*/);
343extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
344 const ec_mulfactor */*f*/, size_t /*n*/);
41a324a7 345
346/*----- Standard curve operations -----------------------------------------*/
347
8823192f 348/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
41a324a7 349 *
350 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
351 * @ec *d@ = pointer to the destination
352 * @const ec *p@ = pointer to a source point
353 *
354 * Returns: The destination @d@.
355 *
356 * Use: An identity operation if your curve has no internal
357 * representation. (The field internal representation is still
358 * used.)
359 */
360
361extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
362extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 363extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 364
8823192f 365/* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
41a324a7 366 *
367 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
368 * @ec *d@ = pointer to the destination
369 * @const ec *p@ = pointer to a source point
370 *
371 * Returns: The destination @d@.
372 *
373 * Use: Conversion functions if your curve operations use a
374 * projective representation.
375 */
376
377extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
378extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 379extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 380
b085fd91 381/* --- @ec_stdsub@ --- *
382 *
383 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
384 * @ec *d@ = pointer to the destination
41cb1beb 385 * @const ec *p, *q@ = the operand points
b085fd91 386 *
387 * Returns: The destination @d@.
388 *
389 * Use: Standard point subtraction operation, in terms of negation
390 * and addition. This isn't as efficient as a ready-made
391 * subtraction operator.
392 */
393
41cb1beb 394extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
395 const ec */*p*/, const ec */*q*/);
b085fd91 396
b0ab12e6 397/*----- Creating curves ---------------------------------------------------*/
398
b085fd91 399/* --- @ec_destroycurve@ --- *
400 *
401 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
402 *
403 * Returns: ---
404 *
405 * Use: Destroys a description of an elliptic curve.
406 */
407
408extern void ec_destroycurve(ec_curve */*c*/);
409
410/* --- @ec_prime@, @ec_primeproj@ --- *
b0ab12e6 411 *
dbfee00a 412 * Arguments: @field *f@ = the underlying field for this elliptic curve
b0ab12e6 413 * @mp *a, *b@ = the coefficients for this curve
414 *
415 * Returns: A pointer to the curve.
416 *
417 * Use: Creates a curve structure for an elliptic curve defined over
b085fd91 418 * a prime field. The @primeproj@ variant uses projective
419 * coordinates, which can be a win.
b0ab12e6 420 */
421
422extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
b085fd91 423extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 424
ceb3f0c0 425/* --- @ec_bin@, @ec_binproj@ --- *
b0ab12e6 426 *
427 * Arguments: @field *f@ = the underlying field for this elliptic curve
428 * @mp *a, *b@ = the coefficients for this curve
429 *
430 * Returns: A pointer to the curve.
431 *
ceb3f0c0 432 * Use: Creates a curve structure for an elliptic curve defined over
433 * a binary field. The @binproj@ variant uses projective
434 * coordinates, which can be a win.
b0ab12e6 435 */
436
437extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
ceb3f0c0 438extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 439
440/*----- That's all, folks -------------------------------------------------*/
441
442#ifdef __cplusplus
443 }
444#endif
445
446#endif