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