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