b0ab12e6 |
1 | /* -*-c-*- |
2 | * |
b085fd91 |
3 | * $Id: ec.h,v 1.3 2002/01/13 13:48:44 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 $ |
b085fd91 |
33 | * Revision 1.3 2002/01/13 13:48:44 mdw |
34 | * Further progress. |
35 | * |
41a324a7 |
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 | * |
b0ab12e6 |
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 | |
b085fd91 |
59 | /* --- An elliptic curve representation --- */ |
60 | |
b0ab12e6 |
61 | typedef struct ec_curve { |
62 | const struct ec_ops *ops; /* Curve operations */ |
63 | field *f; /* Underlying field structure */ |
64 | } ec_curve; |
65 | |
b085fd91 |
66 | /* --- An elliptic curve point --- */ |
67 | |
b0ab12e6 |
68 | typedef struct ec { |
69 | mp *x, *y; /* Point coordinates */ |
70 | mp *z; /* Common denominator (or null) */ |
71 | } ec; |
72 | |
b085fd91 |
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 | |
b0ab12e6 |
82 | typedef struct ec_ops { |
83 | void (*destroy)(ec_curve */*c*/); |
41a324a7 |
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*/); |
b085fd91 |
87 | ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
41a324a7 |
88 | ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/); |
b085fd91 |
89 | ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/); |
41a324a7 |
90 | ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
91 | } ec_ops; |
92 | |
41a324a7 |
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 | |
b0ab12e6 |
96 | #define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x)) |
b085fd91 |
97 | #define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x)) |
b0ab12e6 |
98 | #define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q)) |
b085fd91 |
99 | #define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q)) |
b0ab12e6 |
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 | * |
41a324a7 |
108 | * Returns: The argument @p@. |
b0ab12e6 |
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 | |
41a324a7 |
121 | extern ec *ec_create(ec */*p*/); |
b0ab12e6 |
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 | * |
41a324a7 |
159 | * Returns: The argument @p@. |
b0ab12e6 |
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 | |
41a324a7 |
176 | extern ec *ec_setinf(ec */*p*/); |
b0ab12e6 |
177 | |
178 | /* --- @ec_copy@ --- * |
179 | * |
180 | * Arguments: @ec *d@ = pointer to destination point |
181 | * @const ec *p@ = pointer to source point |
182 | * |
41a324a7 |
183 | * Returns: The destination @d@. |
b0ab12e6 |
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 { \ |
b085fd91 |
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; \ |
b0ab12e6 |
199 | } \ |
200 | } \ |
201 | } while (0) |
202 | |
41a324a7 |
203 | extern ec *ec_copy(ec */*d*/, const ec */*p*/); |
b0ab12e6 |
204 | |
205 | /*----- Interesting arithmetic --------------------------------------------*/ |
206 | |
b085fd91 |
207 | /* --- @ec_in@ --- * |
b0ab12e6 |
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 | * |
b085fd91 |
213 | * Returns: The destination point. |
b0ab12e6 |
214 | * |
b085fd91 |
215 | * Use: Converts a point to internal representation. |
b0ab12e6 |
216 | */ |
217 | |
b085fd91 |
218 | extern ec *ec_in(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
219 | |
b085fd91 |
220 | /* --- @ec_out@ --- * |
b0ab12e6 |
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 | * |
b085fd91 |
226 | * Returns: The destination point. |
b0ab12e6 |
227 | * |
b085fd91 |
228 | * Use: Converts a point to external representation. |
b0ab12e6 |
229 | */ |
230 | |
b085fd91 |
231 | extern ec *ec_out(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
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 | * |
b085fd91 |
239 | * Returns: The destination if OK, or null if no point was found. |
b0ab12e6 |
240 | * |
b085fd91 |
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. |
b0ab12e6 |
245 | */ |
246 | |
b085fd91 |
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*/); |
b0ab12e6 |
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 | * |
41a324a7 |
268 | * Returns: The destination @d@. |
b0ab12e6 |
269 | * |
270 | * Use: Adds two points on an elliptic curve. |
271 | */ |
272 | |
41a324a7 |
273 | extern ec *ec_add(ec_curve */*c*/, ec */*d*/, |
274 | const ec */*p*/, const ec */*q*/); |
b0ab12e6 |
275 | |
b085fd91 |
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 | |
b0ab12e6 |
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 | * |
41a324a7 |
296 | * Returns: The destination @d@. |
b0ab12e6 |
297 | * |
298 | * Use: Doubles a point on an elliptic curve. |
299 | */ |
300 | |
41a324a7 |
301 | extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/); |
b0ab12e6 |
302 | |
b085fd91 |
303 | /* --- @ec_mul@, @ec_imul@ --- * |
b0ab12e6 |
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 | * |
41a324a7 |
310 | * Returns: The destination @d@. |
b0ab12e6 |
311 | * |
b085fd91 |
312 | * Use: Multiplies a point by a scalar, returning %$n p$%. The |
313 | * @imul@ variant uses internal representations for argument |
314 | * and result. |
b0ab12e6 |
315 | */ |
316 | |
41a324a7 |
317 | extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/); |
b085fd91 |
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*/); |
41a324a7 |
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*/); |
b0ab12e6 |
370 | |
b085fd91 |
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 | |
b0ab12e6 |
386 | /*----- Creating curves ---------------------------------------------------*/ |
387 | |
b085fd91 |
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@ --- * |
b0ab12e6 |
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 |
b085fd91 |
407 | * a prime field. The @primeproj@ variant uses projective |
408 | * coordinates, which can be a win. |
b0ab12e6 |
409 | */ |
410 | |
411 | extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/); |
b085fd91 |
412 | extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/); |
b0ab12e6 |
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 |