| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: group.h,v 1.1 2004/04/01 12:50:09 mdw Exp $ |
| 4 | * |
| 5 | * General cyclic group abstraction |
| 6 | * |
| 7 | * (c) 2004 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: group.h,v $ |
| 33 | * Revision 1.1 2004/04/01 12:50:09 mdw |
| 34 | * Add cyclic group abstraction, with test code. Separate off exponentation |
| 35 | * functions for better static linking. Fix a buttload of bugs on the way. |
| 36 | * Generally ensure that negative exponents do inversion correctly. Add |
| 37 | * table of standard prime-field subgroups. (Binary field subgroups are |
| 38 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
| 39 | * |
| 40 | */ |
| 41 | |
| 42 | #ifndef CATACOMB_GROUP_H |
| 43 | #define CATACOMB_GROUP_H |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | /*----- Header files ------------------------------------------------------*/ |
| 50 | |
| 51 | #include <mLib/dstr.h> |
| 52 | |
| 53 | #ifndef CATACOMB_BUF_H |
| 54 | # include "buf.h" |
| 55 | #endif |
| 56 | |
| 57 | #ifndef CATACOMB_DH_H |
| 58 | # include "ec.h" |
| 59 | #endif |
| 60 | |
| 61 | #ifndef CATACOMB_GRAND_H |
| 62 | # include "grand.h" |
| 63 | #endif |
| 64 | |
| 65 | #ifndef CATACOMB_MP_H |
| 66 | # include "mp.h" |
| 67 | #endif |
| 68 | |
| 69 | #ifndef CATACOMB_QDPARSE_H |
| 70 | # include "qdparse.h" |
| 71 | #endif |
| 72 | |
| 73 | /*----- Data structures ---------------------------------------------------*/ |
| 74 | |
| 75 | #ifndef ge |
| 76 | typedef struct ge ge; /* Group element (abstract type) */ |
| 77 | #endif |
| 78 | |
| 79 | typedef struct group { |
| 80 | const struct group_ops *ops; /* Operations table */ |
| 81 | size_t nbits; /* Size of an element in bits */ |
| 82 | size_t noctets; /* Size of an element in octets */ |
| 83 | ge *i; /* Identity element */ |
| 84 | ge *g; /* Generator element */ |
| 85 | mp *r; /* Order of the generator */ |
| 86 | mp *h; /* Cofactor */ |
| 87 | } group; |
| 88 | |
| 89 | typedef struct group_expfactor { |
| 90 | ge *base; /* The base */ |
| 91 | mp *exp; /* The exponent */ |
| 92 | } group_expfactor; |
| 93 | |
| 94 | typedef struct group_ops { |
| 95 | unsigned ty; /* Type of this group */ |
| 96 | |
| 97 | /* --- Memory management --- */ |
| 98 | |
| 99 | void (*destroygroup)(group */*g*/); |
| 100 | ge *(*create)(group */*g*/); |
| 101 | void (*copy)(group */*g*/, ge */*d*/, ge */*x*/); |
| 102 | void (*burn)(group */*g*/, ge */*x*/); |
| 103 | void (*destroy)(group */*g*/, ge */*e*/); |
| 104 | |
| 105 | /* --- Comparisons --- */ |
| 106 | |
| 107 | int (*samep)(group */*g*/, group */*h*/); |
| 108 | int (*eq)(group */*g*/, ge */*x*/, ge */*y*/); |
| 109 | int (*identp)(group */*g*/, ge */*x*/); |
| 110 | |
| 111 | /* --- Other stuff --- */ |
| 112 | |
| 113 | const char *(*check)(group */*g*/, grand */*gr*/); |
| 114 | |
| 115 | /* --- Arithmetic --- */ |
| 116 | |
| 117 | void (*mul)(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/); |
| 118 | void (*sqr)(group */*g*/, ge */*d*/, ge */*x*/); |
| 119 | void (*inv)(group */*g*/, ge */*d*/, ge */*x*/); |
| 120 | void (*div)(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/); |
| 121 | void (*exp)(group */*g*/, ge */*d*/, ge */*x*/, mp */*n*/); |
| 122 | void (*mexp)(group */*g*/, ge */*d*/, |
| 123 | const group_expfactor */*f*/, size_t /*n*/); |
| 124 | |
| 125 | /* --- Debugging --- */ |
| 126 | |
| 127 | int (*read)(group */*g*/, ge */*d*/, |
| 128 | const mptext_ops */*ops*/, void */*p*/); |
| 129 | int (*write)(group */*g*/, ge */*x*/, |
| 130 | const mptext_ops */*ops*/, void */*p*/); |
| 131 | |
| 132 | /* --- Conversions --- */ |
| 133 | |
| 134 | mp *(*toint)(group */*g*/, mp */*d*/, ge */*x*/); |
| 135 | int (*fromint)(group */*g*/, ge */*d*/, mp */*x*/); |
| 136 | int (*toec)(group */*g*/, ec */*d*/, ge */*x*/); |
| 137 | int (*fromec)(group */*g*/, ge */*d*/, ec */*p*/); |
| 138 | int (*tobuf)(group */*h*/, buf */*b*/, ge */*x*/); |
| 139 | int (*frombuf)(group */*h*/, buf */*b*/, ge */*d*/); |
| 140 | |
| 141 | } group_ops; |
| 142 | |
| 143 | enum { |
| 144 | GTY_PRIME, /* Prime field subgroup */ |
| 145 | GTY_BINARY, /* Binary feld subgroup */ |
| 146 | GTY_EC /* Elliptic curve group */ |
| 147 | }; |
| 148 | |
| 149 | #define G_DESTROYGROUP(g) (g)->ops->destroygroup((g)) |
| 150 | #define G_CREATE(g) (g)->ops->create((g)) |
| 151 | #define G_COPY(g, d, x) (g)->ops->copy((g), (d), (x)) |
| 152 | #define G_BURN(g, x) (g)->ops->burn((g), (x)) |
| 153 | #define G_DESTROY(g, x) (g)->ops->destroy((g), (x)) |
| 154 | |
| 155 | #define G_SAMEP(g, h) (g)->ops->samep((g), (h)) |
| 156 | #define G_EQ(g, x, y) (g)->ops->eq((g), (x), (y)) |
| 157 | #define G_IDENTP(g, x) (g)->ops->identp((g), (x)) |
| 158 | |
| 159 | #define G_CHECK(g, gr) (g)->ops->check((g), (gr)) |
| 160 | |
| 161 | #define G_MUL(g, d, x, y) (g)->ops->mul((g), (d), (x), (y)) |
| 162 | #define G_SQR(g, d, x) (g)->ops->sqr((g), (d), (x)) |
| 163 | #define G_INV(g, d, x) (g)->ops->inv((g), (d), (x)) |
| 164 | #define G_DIV(g, d, x, y) (g)->ops->div((g), (d), (x), (y)) |
| 165 | #define G_EXP(g, d, x, n) (g)->ops->exp((g), (d), (x), (n)) |
| 166 | #define G_MEXP(g, d, f, n) (g)->ops->mexp((g), (d), (f), (n)) |
| 167 | |
| 168 | #define G_READ(g, d, o, p) (g)->ops->read((g), (d), (o), (p)) |
| 169 | #define G_WRITE(g, x, o, p) (g)->ops->write((g), (x), (o), (p)) |
| 170 | |
| 171 | #define G_TOINT(g, d, x) (g)->ops->toint((g), (d), (x)) |
| 172 | #define G_FROMINT(g, d, x) (g)->ops->fromint((g), (d), (x)) |
| 173 | #define G_TOEC(g, d, x) (g)->ops->toec((g), (d), (x)) |
| 174 | #define G_FROMEC(g, d, p) (g)->ops->fromec((g), (d), (p)) |
| 175 | #define G_TOBUF(g, b, x) (g)->ops->tobuf((g), (b), (x)) |
| 176 | #define G_FROMBUF(g, b, d) (g)->ops->frombuf((g), (b), (d)) |
| 177 | |
| 178 | /*----- Handy functions ---------------------------------------------------*/ |
| 179 | |
| 180 | /* --- @group_check@ --- * |
| 181 | * |
| 182 | * Arguments: @group *g@ = an abstract group |
| 183 | * @ge *x@ = a group element |
| 184 | * |
| 185 | * Returns: Zero on success, nonzero for failure. |
| 186 | * |
| 187 | * Use: Checks that @x@ is a valid group element. This may take a |
| 188 | * while, since it checks that %$x^h \ne 1$%. |
| 189 | */ |
| 190 | |
| 191 | extern int group_check(group */*g*/, ge */*x*/); |
| 192 | |
| 193 | /* --- @group_samep@ --- * |
| 194 | * |
| 195 | * Arguments: @group *g, *h@ = two abstract groups |
| 196 | * |
| 197 | * Returns: Nonzero if the groups are in fact identical (not just |
| 198 | * isomorphic). |
| 199 | * |
| 200 | * Use: Checks to see whether two groups are actually the same. This |
| 201 | * function does the full check: the group operatrion @samep@ |
| 202 | * just does the group-specific details. |
| 203 | */ |
| 204 | |
| 205 | extern int group_samep(group */*g*/, group */*h*/); |
| 206 | |
| 207 | /*----- Textual I/O on group elements -------------------------------------*/ |
| 208 | |
| 209 | extern int group_readstring(group */*g*/, ge */*d*/, |
| 210 | const char */*p*/, char **/*end*/); |
| 211 | extern int group_writestring(group */*g*/, ge */*d*/, |
| 212 | char */*p*/, size_t /*sz*/); |
| 213 | |
| 214 | extern int group_readfile(group */*g*/, ge */*d*/, FILE */*fp*/); |
| 215 | extern int group_writefile(group */*g*/, ge */*x*/, FILE */*fp*/); |
| 216 | |
| 217 | extern int group_readdstr(group */*g*/, ge */*d*/, |
| 218 | dstr */*dd*/, size_t */*off*/); |
| 219 | extern int group_writedstr(group */*g*/, ge */*x*/, dstr */*d*/); |
| 220 | |
| 221 | /*----- Standard implementations ------------------------------------------*/ |
| 222 | |
| 223 | /* --- @group_stdidentp@ --- * |
| 224 | * |
| 225 | * Arguments: @group *g@ = abstract group |
| 226 | * @ge *x@ = group element |
| 227 | * |
| 228 | * Returns: Nonzero if %$x$% is the group identity. |
| 229 | */ |
| 230 | |
| 231 | extern int group_stdidentp(group */*g*/, ge */*x*/); |
| 232 | |
| 233 | /* --- @group_stdcheck@ --- * |
| 234 | * |
| 235 | * Arguments: @group *g@ = abstract group |
| 236 | * @grand *gr@ = random number source. |
| 237 | * |
| 238 | * Returns: Null on success, or a pointer to an error message. |
| 239 | */ |
| 240 | |
| 241 | extern const char *group_stdcheck(group */*g*/, grand */*gr*/); |
| 242 | |
| 243 | /* --- @group_stdsqr@ --- * |
| 244 | * |
| 245 | * Arguments: @group *g@ = abstract group |
| 246 | * @ge *d@ = destination pointer |
| 247 | * @ge *x@ = group element |
| 248 | * |
| 249 | * Returns: --- |
| 250 | * |
| 251 | * Use: Computes %$d = x^2$% as %$d = x x$%. |
| 252 | */ |
| 253 | |
| 254 | extern void group_stdsqr(group */*g*/, ge */*d*/, ge */*x*/); |
| 255 | |
| 256 | /* --- @group_stddiv@ --- * |
| 257 | * |
| 258 | * Arguments: @group *g@ = abstract group |
| 259 | * @ge *d@ = destination pointer |
| 260 | * @ge *x@ = dividend |
| 261 | * @ge *y@ = divisor |
| 262 | * |
| 263 | * Returns: --- |
| 264 | * |
| 265 | * Use: Computes %$d = x/y$% as %$d = x y^{-1}$%. |
| 266 | */ |
| 267 | |
| 268 | extern void group_stddiv(group */*g*/, ge */*d*/, ge */*x*/, ge */*y*/); |
| 269 | |
| 270 | /* --- @group_stdexp@ --- * |
| 271 | * |
| 272 | * Arguments: @group *g@ = abstract group |
| 273 | * @ge *d@ = destination pointer |
| 274 | * @ge *x@ = base element |
| 275 | * @mp *n@ = exponent |
| 276 | * |
| 277 | * Returns: --- |
| 278 | * |
| 279 | * Use: Computes %$d = x^n$% efficiently. |
| 280 | */ |
| 281 | |
| 282 | extern void group_stdexp(group */*g*/, ge */*d*/, ge */*x*/, mp */*n*/); |
| 283 | |
| 284 | /* --- @group_stdmexp@ --- * |
| 285 | * |
| 286 | * Arguments: @group *g@ = abstract group |
| 287 | * @ge *d@ = destination pointer |
| 288 | * @const group_expfactor *f@ = vector of factors |
| 289 | * @size_t n@ = number of factors |
| 290 | * |
| 291 | * Returns: --- |
| 292 | * |
| 293 | * Use: Computes %$d = g_0^{x_0} g_1^{x_1} \ldots$% efficiently. |
| 294 | */ |
| 295 | |
| 296 | extern void group_stdmexp(group */*g*/, ge */*d*/, |
| 297 | const group_expfactor */*f*/, size_t /*n*/); |
| 298 | |
| 299 | /* --- @group_stdtoec@ --- * |
| 300 | * |
| 301 | * Arguments: @group *g@ = abstract group |
| 302 | * @ec *d@ = destination point |
| 303 | * @ge *x@ = group element |
| 304 | * |
| 305 | * Returns: @-1@, indicating failure. |
| 306 | * |
| 307 | * Use: Fails to convert a group element to an elliptic curve point. |
| 308 | */ |
| 309 | |
| 310 | extern int group_stdtoec(group */*g*/, ec */*d*/, ge */*x*/); |
| 311 | |
| 312 | /* --- @group_stdfromec@ --- * |
| 313 | * |
| 314 | * Arguments: @group *g@ = abstract group |
| 315 | * @ge *d@ = destination pointer |
| 316 | * @ec *p@ = elliptic curve point |
| 317 | * |
| 318 | * Returns: Zero for success, @-1@ on failure. |
| 319 | * |
| 320 | * Use: Converts %$p$% to a group element by converting its %$x$%- |
| 321 | * coordinate. |
| 322 | */ |
| 323 | |
| 324 | extern int group_stdfromec(group */*g*/, ge */*d*/, ec */*p*/); |
| 325 | |
| 326 | /*----- Prime field subgroups ---------------------------------------------*/ |
| 327 | |
| 328 | typedef struct gprime_param { |
| 329 | mp *p, *q; /* Prime numbers %$p$% and %$q$% */ |
| 330 | mp *g; /* Generates order-%$q$% subgroup */ |
| 331 | } gprime_param; |
| 332 | |
| 333 | /* --- @group_prime@ --- * |
| 334 | * |
| 335 | * Arguments: @const gprime_param *gp@ = group parameters |
| 336 | * |
| 337 | * Returns: A pointer to the group. |
| 338 | * |
| 339 | * Use: Constructs an abstract group interface for a subgroup of a |
| 340 | * prime field. Group elements are @mp *@ pointers. |
| 341 | */ |
| 342 | |
| 343 | group *group_prime(const gprime_param */*gp*/); |
| 344 | |
| 345 | /*----- Elliptic curve groups ---------------------------------------------*/ |
| 346 | |
| 347 | /* --- @group_ec@ --- * |
| 348 | * |
| 349 | * Arguments: @const ec_info *ei@ = elliptic curve parameters |
| 350 | * |
| 351 | * Returns: A pointer to the group. |
| 352 | * |
| 353 | * Use: Constructs an abstract group interface for an elliptic curve |
| 354 | * group. Group elements are @ec@ structures. The contents of |
| 355 | * the @ec_info@ structure becomes the property of the @group@ |
| 356 | * object; you can (and should) free the structure itself, but |
| 357 | * calling @ec_freeinfo@ on it is not allowed. |
| 358 | */ |
| 359 | |
| 360 | group *group_ec(const ec_info */*ei*/); |
| 361 | |
| 362 | /*----- General group initialization --------------------------------------*/ |
| 363 | |
| 364 | /* --- @group_parse@ --- * |
| 365 | * |
| 366 | * Arguments: @qd_parse *qd@ = quick-and-dirty parser |
| 367 | * |
| 368 | * Returns: Group pointer, or null for failure. |
| 369 | * |
| 370 | * Use: Parses a group description and returns the group. This has |
| 371 | * the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and |
| 372 | * SPEC is the appropriate kind of group specification of the |
| 373 | * given type. |
| 374 | */ |
| 375 | |
| 376 | extern group *group_parse(qd_parse */*qd*/); |
| 377 | |
| 378 | /* --- @group_fromstring@ --- * |
| 379 | * |
| 380 | * Arguments: @const char *p@ = pointer to string to read |
| 381 | * @group **gg@ = where to put the group pointer |
| 382 | * |
| 383 | * Returns: Null if OK, or an error string. |
| 384 | * |
| 385 | * Use: Parses a group spec from a string, and returns the group. |
| 386 | */ |
| 387 | |
| 388 | extern const char *group_fromstring(const char */*p*/, group **/*gg*/); |
| 389 | |
| 390 | /*----- That's all, folks -------------------------------------------------*/ |
| 391 | |
| 392 | #ifdef __cplusplus |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | #endif |