math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / ec.h
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
b0ab12e6 3 * Elliptic curve definitions
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
b0ab12e6 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
b0ab12e6 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
b0ab12e6 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
b0ab12e6 28#ifndef CATACOMB_EC_H
29#define CATACOMB_EC_H
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
432c4e18 37#ifndef CATACOMB_FIELD_H
38# include "field.h"
39#endif
40
41#ifndef CATACOMB_MP_H
42# include "mp.h"
43#endif
44
45#ifndef CATACOMB_QDPARSE_H
46# include "qdparse.h"
47#endif
b0ab12e6 48
49/*----- Data structures ---------------------------------------------------*/
50
b085fd91 51/* --- An elliptic curve representation --- */
52
b0ab12e6 53typedef struct ec_curve {
54 const struct ec_ops *ops; /* Curve operations */
55 field *f; /* Underlying field structure */
432c4e18 56 mp *a, *b; /* Standard params (internal form) */
b0ab12e6 57} ec_curve;
58
b085fd91 59/* --- An elliptic curve point --- */
60
b0ab12e6 61typedef struct ec {
62 mp *x, *y; /* Point coordinates */
63 mp *z; /* Common denominator (or null) */
64} ec;
65
b085fd91 66/* --- A factor for simultaneous multiplication --- */
67
68typedef struct ec_mulfactor {
69 ec base; /* The point */
41cb1beb 70 mp *exp; /* The exponent */
b085fd91 71} ec_mulfactor;
72
8823192f 73/* --- Elliptic curve operations --- *
74 *
75 * All operations (apart from @destroy@ and @in@) are guaranteed to be
391faf42 76 * performed on internal representations of points.
77 *
78 * (Historical note. We used to guarantee that the second to @add@ and @mul@
79 * was the output of @in@ or @fix@, but this canonification turned out to
80 * make the precomputation in @ec_exp@ too slow. Projective implementations
81 * must therefore cope with a pair of arbitrary points.)
8823192f 82 */
b085fd91 83
b0ab12e6 84typedef struct ec_ops {
f94b972d 85 const char *name;
b0ab12e6 86 void (*destroy)(ec_curve */*c*/);
34e4f738 87 int (*samep)(ec_curve */*c*/, ec_curve */*d*/);
41a324a7 88 ec *(*in)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
89 ec *(*out)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 90 ec *(*fix)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 91 ec *(*find)(ec_curve */*c*/, ec */*d*/, mp */*x*/);
b085fd91 92 ec *(*neg)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 93 ec *(*add)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
b085fd91 94 ec *(*sub)(ec_curve */*c*/, ec */*d*/, const ec */*p*/, const ec */*q*/);
41a324a7 95 ec *(*dbl)(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 96 int (*check)(ec_curve */*c*/, const ec */*p*/);
b0ab12e6 97} ec_ops;
98
f94b972d 99#define EC_NAME(c) (c)->ops->name
100
34e4f738 101#define EC_SAMEP(c, d) (c)->ops->samep((c), (d))
41a324a7 102#define EC_IN(c, d, p) (c)->ops->in((c), (d), (p))
dbfee00a 103#define EC_OUT(c, d, p) (c)->ops->out((c), (d), (p))
8823192f 104#define EC_FIX(c, d, p) (c)->ops->fix((c), (d), (p))
41a324a7 105
b0ab12e6 106#define EC_FIND(c, d, x) (c)->ops->find((c), (d), (x))
b085fd91 107#define EC_NEG(c, d, x) (c)->ops->neg((c), (d), (x))
b0ab12e6 108#define EC_ADD(c, d, p, q) (c)->ops->add((c), (d), (p), (q))
b085fd91 109#define EC_SUB(c, d, p, q) (c)->ops->sub((c), (d), (p), (q))
b0ab12e6 110#define EC_DBL(c, d, p) (c)->ops->dbl((c), (d), (p))
8823192f 111#define EC_CHECK(c, p) (c)->ops->check((c), (p))
b0ab12e6 112
432c4e18 113/* --- Elliptic curve parameters --- */
114
115typedef struct ec_info {
116 ec_curve *c; /* The actual curve */
117 ec g; /* The common point */
118 mp *r; /* Order of %$g$% */
119 mp *h; /* Cofactor %$h = \#E/r$% */
120} ec_info;
121
b0ab12e6 122/*----- Simple memory management things -----------------------------------*/
123
124/* --- @ec_create@ --- *
125 *
126 * Arguments: @ec *p@ = pointer to an elliptic-curve point
127 *
41a324a7 128 * Returns: The argument @p@.
b0ab12e6 129 *
130 * Use: Initializes a new point. The initial value is the additive
131 * identity (which is universal for all curves).
132 */
133
134#define EC_INIT { MP_NEW, MP_NEW, MP_NEW }
135
136#define EC_CREATE(p) do { \
137 ec *_p = (p); \
138 _p->x = _p->y = _p->z = MP_NEW; \
139} while (0)
140
41a324a7 141extern ec *ec_create(ec */*p*/);
b0ab12e6 142
143/* --- @ec_destroy@ --- *
144 *
145 * Arguments: @ec *p@ = pointer to an elliptic-curve point
146 *
147 * Returns: ---
148 *
149 * Use: Destroys a point, making it invalid.
150 */
151
152#define EC_DESTROY(p) do { \
153 ec *_p = (p); \
154 if (!EC_ATINF(_p)) { \
155 MP_DROP(_p->x); \
156 MP_DROP(_p->y); \
157 if (_p->z) MP_DROP(_p->z); \
158 } \
159} while (0)
160
161extern void ec_destroy(ec */*p*/);
162
163/* --- @ec_atinf@ --- *
164 *
165 * Arguments: @const ec *p@ = pointer to a point
166 *
167 * Returns: Nonzero if %$p = O$% is the point at infinity, zero
168 * otherwise.
169 */
170
171#define EC_ATINF(p) ((p)->x == MP_NEW || (p)->x == MP_NEWSEC)
172
173extern int ec_atinf(const ec */*p*/);
174
175/* --- @ec_setinf@ --- *
176 *
177 * Arguments: @ec *p@ = pointer to a point
178 *
41a324a7 179 * Returns: The argument @p@.
b0ab12e6 180 *
181 * Use: Sets the given point to be the point %$O$% at infinity.
182 */
183
184#define EC_SETINF(p) do { \
185 ec *_p = (p); \
186 if (!EC_ATINF(_p)) { \
187 MP_DROP(_p->x); \
188 MP_DROP(_p->y); \
189 if (_p->z) MP_DROP(_p->z); \
190 _p->x = _p->y = _p->z = MP_NEW; \
191 _p->y = MP_NEW; \
192 _p->z = MP_NEW; \
193 } \
194} while (0)
195
41a324a7 196extern ec *ec_setinf(ec */*p*/);
b0ab12e6 197
198/* --- @ec_copy@ --- *
199 *
200 * Arguments: @ec *d@ = pointer to destination point
201 * @const ec *p@ = pointer to source point
202 *
41a324a7 203 * Returns: The destination @d@.
b0ab12e6 204 *
205 * Use: Creates a copy of an elliptic curve point.
206 */
207
208#define EC_COPY(d, p) do { \
209 ec *_d = (d); \
210 const ec *_p = (p); \
211 if (d != p) { \
212 EC_DESTROY(d); \
213 if (EC_ATINF(p)) \
214 _d->x = _d->y = _d->z = MP_NEW; \
215 else { \
b085fd91 216 _d->x = MP_COPY(_p->x); \
217 _d->y = MP_COPY(_p->y); \
218 _d->z = _p->z ? MP_COPY(_p->z) : MP_NEW; \
b0ab12e6 219 } \
220 } \
221} while (0)
222
41a324a7 223extern ec *ec_copy(ec */*d*/, const ec */*p*/);
b0ab12e6 224
bc985cef 225/* --- @ec_eq@ --- *
226 *
227 * Arguments: @const ec *p, *q@ = two points
228 *
229 * Returns: Nonzero if the points are equal. Compares external-format
230 * points.
231 */
232
233#define EC_EQ(p, q) \
234 ((EC_ATINF(p) && EC_ATINF(q)) || \
235 (!EC_ATINF(p) && !EC_ATINF(q) && \
236 MP_EQ((p)->x, (q)->x) && \
237 MP_EQ((p)->y, (q)->y)))
238
239extern int ec_eq(const ec *p, const ec *q);
240
b0ab12e6 241/*----- Interesting arithmetic --------------------------------------------*/
242
34e4f738 243/* --- @ec_samep@ --- *
244 *
245 * Arguments: @ec_curve *c, *d@ = two elliptic curves
246 *
247 * Returns: Nonzero if the curves are identical (not just isomorphic).
248 *
249 * Use: Checks for sameness of curves. This function does the full
250 * check, not just the curve-type-specific check done by the
251 * @sampep@ field operation.
252 */
253
254extern int ec_samep(ec_curve */*c*/, ec_curve */*d*/);
255
b0ab12e6 256/* --- @ec_find@ --- *
257 *
258 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
259 * @ec *d@ = pointer to the destination point
260 * @mp *x@ = a possible x-coordinate
261 *
b085fd91 262 * Returns: The destination if OK, or null if no point was found.
b0ab12e6 263 *
b085fd91 264 * Use: Finds a point on an elliptic curve with a given
265 * x-coordinate. If there is no point with the given
266 * %$x$%-coordinate, a null pointer is returned and the
267 * destination is left invalid.
b0ab12e6 268 */
269
b085fd91 270extern ec *ec_find(ec_curve */*c*/, ec */*d*/, mp */*x*/);
271
bc985cef 272/* --- @ec_rand@ --- *
273 *
274 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
275 * @ec *d@ = pointer to the destination point
276 * @grand *r@ = random number source
277 *
278 * Returns: The destination @d@.
279 *
280 * Use: Finds a random point on the given curve.
281 */
282
283extern ec *ec_rand(ec_curve */*c*/, ec */*d*/, grand */*r*/);
284
b085fd91 285/* --- @ec_neg@ --- *
286 *
287 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
288 * @ec *d@ = pointer to the destination point
289 * @const ec *p@ = pointer to the operand point
290 *
291 * Returns: The destination point.
292 *
293 * Use: Computes the negation of the given point.
294 */
295
296extern ec *ec_neg(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 297
298/* --- @ec_add@ --- *
299 *
300 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
301 * @ec *d@ = pointer to the destination point
302 * @const ec *p, *q@ = pointers to the operand points
303 *
41a324a7 304 * Returns: The destination @d@.
b0ab12e6 305 *
306 * Use: Adds two points on an elliptic curve.
307 */
308
41a324a7 309extern ec *ec_add(ec_curve */*c*/, ec */*d*/,
310 const ec */*p*/, const ec */*q*/);
b0ab12e6 311
b085fd91 312/* --- @ec_sub@ --- *
313 *
314 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
315 * @ec *d@ = pointer to the destination point
316 * @const ec *p, *q@ = pointers to the operand points
317 *
318 * Returns: The destination @d@.
319 *
320 * Use: Subtracts one point from another on an elliptic curve.
321 */
322
323extern ec *ec_sub(ec_curve */*c*/, ec */*d*/,
324 const ec */*p*/, const ec */*q*/);
325
b0ab12e6 326/* --- @ec_dbl@ --- *
327 *
328 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
329 * @ec *d@ = pointer to the destination point
330 * @const ec *p@ = pointer to the operand point
331 *
41a324a7 332 * Returns: The destination @d@.
b0ab12e6 333 *
334 * Use: Doubles a point on an elliptic curve.
335 */
336
41a324a7 337extern ec *ec_dbl(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 338
8823192f 339/* --- @ec_check@ --- *
340 *
341 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
342 * @const ec *p@ = pointer to the point
343 *
344 * Returns: Zero if OK, nonzero if this is an invalid point.
345 *
346 * Use: Checks that a point is actually on an elliptic curve.
347 */
348
349extern int ec_check(ec_curve */*c*/, const ec */*p*/);
350
b085fd91 351/* --- @ec_mul@, @ec_imul@ --- *
b0ab12e6 352 *
353 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
354 * @ec *d@ = pointer to the destination point
355 * @const ec *p@ = pointer to the generator point
356 * @mp *n@ = integer multiplier
357 *
41a324a7 358 * Returns: The destination @d@.
b0ab12e6 359 *
b085fd91 360 * Use: Multiplies a point by a scalar, returning %$n p$%. The
361 * @imul@ variant uses internal representations for argument
362 * and result.
b0ab12e6 363 */
364
41a324a7 365extern ec *ec_mul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
b085fd91 366extern ec *ec_imul(ec_curve */*c*/, ec */*d*/, const ec */*p*/, mp */*n*/);
367
368/* --- @ec_mmul@, @ec_immul@ --- *
369 *
370 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
371 * @ec *d@ = pointer to the destination point
372 * @const ec_mulfactor *f@ = pointer to vector of factors
373 * @size_t n@ = number of factors
374 *
375 * Returns: The destination @d@.
376 *
377 * Use: Does simultaneous point multiplication. The @immul@ variant
378 * uses internal representations for arguments and result.
379 */
380
381extern ec *ec_mmul(ec_curve */*c*/, ec */*d*/,
382 const ec_mulfactor */*f*/, size_t /*n*/);
383extern ec *ec_immul(ec_curve */*c*/, ec */*d*/,
384 const ec_mulfactor */*f*/, size_t /*n*/);
41a324a7 385
386/*----- Standard curve operations -----------------------------------------*/
387
34e4f738 388/* --- @ec_stdsamep@ --- *
389 *
390 * Arguments: @ec_curve *c, *d@ = two elliptic curves
391 *
392 * Returns: Nonzero if the curves are identical (not just isomorphic).
393 *
394 * Use: Simple sameness check on @a@ and @b@ curve members.
395 */
396
397extern int ec_stdsamep(ec_curve */*c*/, ec_curve */*d*/);
398
8823192f 399/* --- @ec_idin@, @ec_idout@, @ec_idfix@ --- *
41a324a7 400 *
401 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
402 * @ec *d@ = pointer to the destination
403 * @const ec *p@ = pointer to a source point
404 *
405 * Returns: The destination @d@.
406 *
407 * Use: An identity operation if your curve has no internal
408 * representation. (The field internal representation is still
409 * used.)
410 */
411
412extern ec *ec_idin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
413extern ec *ec_idout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 414extern ec *ec_idfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
41a324a7 415
8823192f 416/* --- @ec_projin@, @ec_projout@, @ec_projfix@ --- *
41a324a7 417 *
418 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
419 * @ec *d@ = pointer to the destination
420 * @const ec *p@ = pointer to a source point
421 *
422 * Returns: The destination @d@.
423 *
424 * Use: Conversion functions if your curve operations use a
425 * projective representation.
426 */
427
428extern ec *ec_projin(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
429extern ec *ec_projout(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
8823192f 430extern ec *ec_projfix(ec_curve */*c*/, ec */*d*/, const ec */*p*/);
b0ab12e6 431
b085fd91 432/* --- @ec_stdsub@ --- *
433 *
434 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
435 * @ec *d@ = pointer to the destination
41cb1beb 436 * @const ec *p, *q@ = the operand points
b085fd91 437 *
438 * Returns: The destination @d@.
439 *
440 * Use: Standard point subtraction operation, in terms of negation
441 * and addition. This isn't as efficient as a ready-made
442 * subtraction operator.
443 */
444
41cb1beb 445extern ec *ec_stdsub(ec_curve */*c*/, ec */*d*/,
446 const ec */*p*/, const ec */*q*/);
b085fd91 447
b0ab12e6 448/*----- Creating curves ---------------------------------------------------*/
449
b085fd91 450/* --- @ec_destroycurve@ --- *
451 *
452 * Arguments: @ec_curve *c@ = pointer to an ellptic curve
453 *
454 * Returns: ---
455 *
456 * Use: Destroys a description of an elliptic curve.
457 */
458
459extern void ec_destroycurve(ec_curve */*c*/);
460
461/* --- @ec_prime@, @ec_primeproj@ --- *
b0ab12e6 462 *
dbfee00a 463 * Arguments: @field *f@ = the underlying field for this elliptic curve
b0ab12e6 464 * @mp *a, *b@ = the coefficients for this curve
465 *
02d7884d 466 * Returns: A pointer to the curve, or null.
b0ab12e6 467 *
468 * Use: Creates a curve structure for an elliptic curve defined over
b085fd91 469 * a prime field. The @primeproj@ variant uses projective
470 * coordinates, which can be a win.
b0ab12e6 471 */
472
473extern ec_curve *ec_prime(field */*f*/, mp */*a*/, mp */*b*/);
b085fd91 474extern ec_curve *ec_primeproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 475
ceb3f0c0 476/* --- @ec_bin@, @ec_binproj@ --- *
b0ab12e6 477 *
478 * Arguments: @field *f@ = the underlying field for this elliptic curve
479 * @mp *a, *b@ = the coefficients for this curve
480 *
02d7884d 481 * Returns: A pointer to the curve, or null.
b0ab12e6 482 *
ceb3f0c0 483 * Use: Creates a curve structure for an elliptic curve defined over
484 * a binary field. The @binproj@ variant uses projective
485 * coordinates, which can be a win.
b0ab12e6 486 */
487
488extern ec_curve *ec_bin(field */*f*/, mp */*a*/, mp */*b*/);
ceb3f0c0 489extern ec_curve *ec_binproj(field */*f*/, mp */*a*/, mp */*b*/);
b0ab12e6 490
432c4e18 491/*----- Curve parameter sets ----------------------------------------------*/
492
493/* --- @ec_curveparse@ --- *
494 *
495 * Arguments: @qd_parse *qd@ = parser context
496 *
497 * Returns: Elliptic curve pointer if OK, or null.
498 *
499 * Use: Parses an elliptic curve description, which has the form
500 *
501 * * a field description
20095408 502 * * an optional `;'
432c4e18 503 * * `prime', `primeproj', `bin', or `binproj'
504 * * an optional `:'
505 * * the %$a$% parameter
506 * * an optional `,'
507 * * the %$b$% parameter
508 */
509
510extern ec_curve *ec_curveparse(qd_parse */*qd*/);
511
512/* --- @ec_ptparse@ --- *
513 *
514 * Arguments: @qd_parse *qd@ = parser context
515 * @ec *p@ = where to put the point
516 *
517 * Returns: The point address, or null.
518 *
519 * Use: Parses an elliptic curve point. This has the form
520 *
521 * * %$x$%-coordinate
522 * * optional `,'
523 * * %$y$%-coordinate
524 */
525
526extern ec *ec_ptparse(qd_parse */*qd*/, ec */*p*/);
527
528/* --- @ec_infoparse@ --- *
529 *
530 * Arguments: @qd_parse *qd@ = parser context
531 * @ec_info *ei@ = curve information block, currently
532 * uninitialized
533 *
534 * Returns: Zero on success, nonzero on failure.
535 *
536 * Use: Parses an elliptic curve information string, and stores the
537 * information in @ei@. This has the form
538 *
539 * * elliptic curve description
20095408 540 * * optional `;'
432c4e18 541 * * common point
542 * * optional `:'
543 * * group order
544 * * optional `*'
545 * * cofactor
546 */
547
548extern int ec_infoparse(qd_parse */*qd*/, ec_info */*ei*/);
549
7b6d64f1 550/* --- @ec_infofromdata@ --- *
551 *
552 * Arguments: @ec_info *ei@ = where to write the information
553 * @ecdata *ed@ = raw data
554 *
555 * Returns: ---
556 *
557 * Use: Loads elliptic curve information about one of the standard
558 * curves.
559 */
560
561struct ecdata;
562extern void ec_infofromdata(ec_info */*ei*/, struct ecdata */*ed*/);
563
432c4e18 564/* --- @ec_getinfo@ --- *
565 *
566 * Arguments: @ec_info *ei@ = where to write the information
567 * @const char *p@ = string describing a curve
568 *
569 * Returns: Null on success, or a pointer to an error message.
570 *
571 * Use: Parses out information about a curve. The string is either a
572 * standard curve name, or a curve info string.
573 */
574
575extern const char *ec_getinfo(ec_info */*ei*/, const char */*p*/);
576
34e4f738 577/* --- @ec_sameinfop@ --- *
578 *
579 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
580 *
581 * Returns: Nonzero if the curves are identical (not just isomorphic).
582 *
583 * Use: Checks for sameness of curve parameters.
584 */
585
586extern int ec_sameinfop(ec_info */*ei*/, ec_info */*ej*/);
587
432c4e18 588/* --- @ec_freeinfo@ --- *
589 *
590 * Arguments: @ec_info *ei@ = elliptic curve information block to free
591 *
592 * Returns: ---
593 *
594 * Use: Frees the information block.
595 */
596
597extern void ec_freeinfo(ec_info */*ei*/);
598
599/* --- @ec_checkinfo@ --- *
600 *
601 * Arguments: @const ec_info *ei@ = elliptic curve information block
602 *
603 * Returns: Null if OK, or pointer to error message.
604 *
605 * Use: Checks an elliptic curve according to the rules in SEC1.
606 */
607
608extern const char *ec_checkinfo(const ec_info */*ei*/, grand */*gr*/);
609
b0ab12e6 610/*----- That's all, folks -------------------------------------------------*/
611
612#ifdef __cplusplus
613 }
614#endif
615
616#endif