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