72ee7a9295b1eb164932650445308d214e491757
[u/mdw/catacomb] / ec.h
1 /* -*-c-*-
2 *
3 * $Id: ec.h,v 1.4.4.1 2003/06/10 13:43:53 mdw Exp $
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 $
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 *
36 * Revision 1.4 2003/05/15 23:25:59 mdw
37 * Make elliptic curve stuff build.
38 *
39 * Revision 1.3 2002/01/13 13:48:44 mdw
40 * Further progress.
41 *
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 *
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
65 /* --- An elliptic curve representation --- */
66
67 typedef struct ec_curve {
68 const struct ec_ops *ops; /* Curve operations */
69 field *f; /* Underlying field structure */
70 } ec_curve;
71
72 /* --- An elliptic curve point --- */
73
74 typedef struct ec {
75 mp *x, *y; /* Point coordinates */
76 mp *z; /* Common denominator (or null) */
77 } ec;
78
79 /* --- A factor for simultaneous multiplication --- */
80
81 typedef struct ec_mulfactor {
82 ec base; /* The point */
83 mp *exp; /* The exponent */
84 } ec_mulfactor;
85
86 /* --- Elliptic curve operations --- */
87
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*/);
97 } ec_ops;
98
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))
101
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))
107
108 /*----- Simple memory management things -----------------------------------*/
109
110 /* --- @ec_create@ --- *
111 *
112 * Arguments: @ec *p@ = pointer to an elliptic-curve point
113 *
114 * Returns: The argument @p@.
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
127 extern ec *ec_create(ec */*p*/);
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
147 extern 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
159 extern int ec_atinf(const ec */*p*/);
160
161 /* --- @ec_setinf@ --- *
162 *
163 * Arguments: @ec *p@ = pointer to a point
164 *
165 * Returns: The argument @p@.
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
182 extern ec *ec_setinf(ec */*p*/);
183
184 /* --- @ec_copy@ --- *
185 *
186 * Arguments: @ec *d@ = pointer to destination point
187 * @const ec *p@ = pointer to source point
188 *
189 * Returns: The destination @d@.
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 { \
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; \
205 } \
206 } \
207 } while (0)
208
209 extern ec *ec_copy(ec */*d*/, const ec */*p*/);
210
211 /*----- Interesting arithmetic --------------------------------------------*/
212
213 /* --- @ec_in@ --- *
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 *
219 * Returns: The destination point.
220 *
221 * Use: Converts a point to internal representation.
222 */
223
224 extern ec *ec_in(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
225
226 /* --- @ec_out@ --- *
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 *
232 * Returns: The destination point.
233 *
234 * Use: Converts a point to external representation.
235 */
236
237 extern ec *ec_out(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
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 *
245 * Returns: The destination if OK, or null if no point was found.
246 *
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.
251 */
252
253 extern 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
266 extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
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 *
274 * Returns: The destination @d@.
275 *
276 * Use: Adds two points on an elliptic curve.
277 */
278
279 extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
280 const ec */*p*/, const ec */*q*/);
281
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
293 extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
294 const ec */*p*/, const ec */*q*/);
295
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 *
302 * Returns: The destination @d@.
303 *
304 * Use: Doubles a point on an elliptic curve.
305 */
306
307 extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
308
309 /* --- @ec_mul@, @ec_imul@ --- *
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 *
316 * Returns: The destination @d@.
317 *
318 * Use: Multiplies a point by a scalar, returning %$n p$%. The
319 * @imul@ variant uses internal representations for argument
320 * and result.
321 */
322
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*/);
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
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*/);
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
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*/);
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
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*/);
376
377 /* --- @ec_stdsub@ --- *
378 *
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
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
390 extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
391 const ec */*p*/, const ec */*q*/);
392
393 /*----- Creating curves ---------------------------------------------------*/
394
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
404 extern void ec_destroycurve(ec_curve */*c*/);
405
406 /* --- @ec_prime@, @ec_primeproj@ --- *
407 *
408 * Arguments: @field *f@ = the underlying field for this elliptic curve
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
414 * a prime field. The @primeproj@ variant uses projective
415 * coordinates, which can be a win.
416 */
417
418 extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
419 extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
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
432 extern 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