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