Normal basis support (translates to poly basis internally). Rewrite
[u/mdw/catacomb] / ec-info.c
1 /* -*-c-*-
2 *
3 * $Id: ec-info.c,v 1.3 2004/04/01 21:28:41 mdw Exp $
4 *
5 * Elliptic curve information management
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: ec-info.c,v $
33 * Revision 1.3 2004/04/01 21:28:41 mdw
34 * Normal basis support (translates to poly basis internally). Rewrite
35 * EC and prime group table generators in awk, so that they can reuse data
36 * for repeated constants.
37 *
38 * Revision 1.2 2004/04/01 12:50:09 mdw
39 * Add cyclic group abstraction, with test code. Separate off exponentation
40 * functions for better static linking. Fix a buttload of bugs on the way.
41 * Generally ensure that negative exponents do inversion correctly. Add
42 * table of standard prime-field subgroups. (Binary field subgroups are
43 * currently unimplemented but easy to add if anyone ever finds a good one.)
44 *
45 * Revision 1.1 2004/03/27 17:54:11 mdw
46 * Standard curves and curve checking.
47 *
48 */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include "ec.h"
53 #include "ectab.h"
54 #include "gf.h"
55 #include "pgen.h"
56 #include "mprand.h"
57 #include "rabin.h"
58
59 /*----- Main code ---------------------------------------------------------*/
60
61 /* --- @ec_curveparse@ --- *
62 *
63 * Arguments: @qd_parse *qd@ = parser context
64 *
65 * Returns: Elliptic curve pointer if OK, or null.
66 *
67 * Use: Parses an elliptic curve description, which has the form
68 *
69 * * a field description
70 * * an optional `/'
71 * * `prime', `primeproj', `bin', or `binproj'
72 * * an optional `:'
73 * * the %$a$% parameter
74 * * an optional `,'
75 * * the %$b$% parameter
76 */
77
78 ec_curve *ec_curveparse(qd_parse *qd)
79 {
80 mp *a = MP_NEW, *b = MP_NEW;
81 ec_curve *c;
82 field *f;
83
84 if ((f = field_parse(qd)) == 0) goto fail;
85 qd_delim(qd, '/');
86 switch (qd_enum(qd, "prime,primeproj,bin,binproj")) {
87 case 0:
88 if (F_TYPE(f) != FTY_PRIME) {
89 qd->e = "field not prime";
90 goto fail;
91 }
92 qd_delim(qd, ':');
93 if ((a = qd_getmp(qd)) == 0) goto fail;
94 qd_delim(qd, ',');
95 if ((b = qd_getmp(qd)) == 0) goto fail;
96 c = ec_prime(f, a, b);
97 break;
98 case 1:
99 if (F_TYPE(f) != FTY_PRIME) {
100 qd->e = "field not prime";
101 goto fail;
102 }
103 qd_delim(qd, ':');
104 if ((a = qd_getmp(qd)) == 0) goto fail;
105 qd_delim(qd, ',');
106 if ((b = qd_getmp(qd)) == 0) goto fail;
107 c = ec_primeproj(f, a, b);
108 break;
109 case 2:
110 if (F_TYPE(f) != FTY_BINARY) {
111 qd->e = "field not binary";
112 goto fail;
113 }
114 qd_delim(qd, ':');
115 if ((a = qd_getmp(qd)) == 0) goto fail;
116 qd_delim(qd, ',');
117 if ((b = qd_getmp(qd)) == 0) goto fail;
118 c = ec_bin(f, a, b);
119 break;
120 case 3:
121 if (F_TYPE(f) != FTY_BINARY) {
122 qd->e = "field not binary";
123 goto fail;
124 }
125 qd_delim(qd, ':');
126 if ((a = qd_getmp(qd)) == 0) goto fail;
127 qd_delim(qd, ',');
128 if ((b = qd_getmp(qd)) == 0) goto fail;
129 c = ec_binproj(f, a, b);
130 break;
131 default:
132 goto fail;
133 }
134 if (a) MP_DROP(a);
135 if (b) MP_DROP(b);
136 return (c);
137
138 fail:
139 if (f) F_DESTROY(f);
140 if (a) MP_DROP(a);
141 if (b) MP_DROP(b);
142 return (0);
143 }
144
145 /* --- @ec_ptparse@ --- *
146 *
147 * Arguments: @qd_parse *qd@ = parser context
148 * @ec *p@ = where to put the point
149 *
150 * Returns: The point address, or null.
151 *
152 * Use: Parses an elliptic curve point. This has the form
153 *
154 * * %$x$%-coordinate
155 * * optional `,'
156 * * %$y$%-coordinate
157 */
158
159 ec *ec_ptparse(qd_parse *qd, ec *p)
160 {
161 mp *x = MP_NEW, *y = MP_NEW;
162
163 if (qd_enum(qd, "inf") >= 0) {
164 EC_SETINF(p);
165 return (p);
166 }
167 if ((x = qd_getmp(qd)) == 0) goto fail;
168 qd_delim(qd, ',');
169 if ((y = qd_getmp(qd)) == 0) goto fail;
170 EC_DESTROY(p);
171 p->x = x;
172 p->y = y;
173 p->z = 0;
174 return (p);
175
176 fail:
177 if (x) MP_DROP(x);
178 if (y) MP_DROP(y);
179 return (0);
180 }
181
182 /* --- @getinfo@ --- *
183 *
184 * Arguments: @ec_info *ei@ = where to write the information
185 * @ecdata *ed@ = raw data
186 *
187 * Returns: ---
188 *
189 * Use: Loads elliptic curve information about one of the standard
190 * curves.
191 */
192
193 static void getinfo(ec_info *ei, ecdata *ed)
194 {
195 field *f;
196
197 switch (ed->ftag) {
198 case FTAG_PRIME:
199 f = field_prime(&ed->p);
200 ei->c = ec_primeproj(f, &ed->a, &ed->b);
201 break;
202 case FTAG_NICEPRIME:
203 f = field_niceprime(&ed->p);
204 ei->c = ec_primeproj(f, &ed->a, &ed->b);
205 break;
206 case FTAG_BINPOLY:
207 f = field_binpoly(&ed->p);
208 ei->c = ec_binproj(f, &ed->a, &ed->b);
209 break;
210 case FTAG_BINNORM:
211 f = field_binnorm(&ed->p, &ed->beta);
212 ei->c = ec_binproj(f, &ed->a, &ed->b);
213 break;
214 default:
215 abort();
216 }
217
218 EC_CREATE(&ei->g); ei->g.x = &ed->gx; ei->g.y = &ed->gy; ei->g.z = 0;
219 ei->r = &ed->r; ei->h = &ed->h;
220 }
221
222 /* --- @ec_infoparse@ --- *
223 *
224 * Arguments: @qd_parse *qd@ = parser context
225 * @ec_info *ei@ = curve information block, currently
226 * uninitialized
227 *
228 * Returns: Zero on success, nonzero on failure.
229 *
230 * Use: Parses an elliptic curve information string, and stores the
231 * information in @ei@. This is either the name of a standard
232 * curve, or it has the form
233 *
234 * * elliptic curve description
235 * * optional `/'
236 * * common point
237 * * optional `:'
238 * * group order
239 * * optional `*'
240 * * cofactor
241 */
242
243 int ec_infoparse(qd_parse *qd, ec_info *ei)
244 {
245 ec_curve *c = 0;
246 field *f;
247 ec g = EC_INIT;
248 const ecentry *ee;
249 mp *r = MP_NEW, *h = MP_NEW;
250
251 for (ee = ectab; ee->name; ee++) {
252 if (qd_enum(qd, ee->name) >= 0) {
253 getinfo(ei, ee->data);
254 goto found;
255 }
256 }
257 if ((c = ec_curveparse(qd)) == 0) goto fail;
258 qd_delim(qd, '/'); if (!ec_ptparse(qd, &g)) goto fail;
259 qd_delim(qd, ':'); if ((r = qd_getmp(qd)) == 0) goto fail;
260 qd_delim(qd, '*'); if ((h = qd_getmp(qd)) == 0) goto fail;
261 ei->c = c; ei->g = g; ei->r = r; ei->h = h;
262
263 found:
264 return (0);
265
266 fail:
267 EC_DESTROY(&g);
268 if (r) MP_DROP(r);
269 if (h) MP_DROP(h);
270 if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); }
271 return (-1);
272 }
273
274 /* --- @ec_getinfo@ --- *
275 *
276 * Arguments: @ec_info *ei@ = where to write the information
277 * @const char *p@ = string describing a curve
278 *
279 * Returns: Null on success, or a pointer to an error message.
280 *
281 * Use: Parses out information about a curve. The string is either a
282 * standard curve name, or a curve info string.
283 */
284
285 const char *ec_getinfo(ec_info *ei, const char *p)
286 {
287 qd_parse qd;
288
289 qd.p = p;
290 qd.e = 0;
291 if (ec_infoparse(&qd, ei))
292 return (qd.e);
293 if (!qd_eofp(&qd)) {
294 ec_freeinfo(ei);
295 return ("junk found at end of string");
296 }
297 return (0);
298 }
299
300 /* --- @ec_sameinfop@ --- *
301 *
302 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
303 *
304 * Returns: Nonzero if the curves are identical (not just isomorphic).
305 *
306 * Use: Checks for sameness of curve parameters.
307 */
308
309 int ec_sameinfop(ec_info *ei, ec_info *ej)
310 {
311 return (ec_samep(ei->c, ej->c) &&
312 MP_EQ(ei->r, ej->r) && MP_EQ(ei->h, ej->h) &&
313 EC_EQ(&ei->g, &ej->g));
314 }
315
316 /* --- @ec_freeinfo@ --- *
317 *
318 * Arguments: @ec_info *ei@ = elliptic curve information block to free
319 *
320 * Returns: ---
321 *
322 * Use: Frees the information block.
323 */
324
325 void ec_freeinfo(ec_info *ei)
326 {
327 field *f;
328
329 EC_DESTROY(&ei->g);
330 MP_DROP(ei->r);
331 MP_DROP(ei->h);
332 f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f);
333 }
334
335 /* --- @ec_checkinfo@ --- *
336 *
337 * Arguments: @const ec_info *ei@ = elliptic curve information block
338 *
339 * Returns: Null if OK, or pointer to error message.
340 *
341 * Use: Checks an elliptic curve according to the rules in SEC1.
342 */
343
344 static int primeeltp(mp *x, field *f)
345 {
346 return (!MP_ISNEG(x) && MP_CMP(x, <, f->m));
347 }
348
349 static const char *primecheck(const ec_info *ei, grand *gr)
350 {
351 ec_curve *c = ei->c;
352 field *f = c->f;
353 int i;
354 mp *x, *y;
355 ec p;
356 int rc;
357
358 /* --- Check %$p$% is an odd prime --- */
359
360 if (!pgen_primep(f->m, gr)) return ("p not prime");
361
362 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
363
364 if (!primeeltp(c->a, f)) return ("a out of range");
365 if (!primeeltp(c->b, f)) return ("b out of range");
366 if (!primeeltp(ei->g.x, f)) return ("G_x out of range");
367 if (!primeeltp(ei->g.x, f)) return ("G_y out of range");
368
369 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
370
371 x = F_SQR(f, MP_NEW, c->a);
372 x = F_MUL(f, x, x, c->a);
373 x = F_QDL(f, x, x);
374 y = F_SQR(f, MP_NEW, c->b);
375 y = F_TPL(f, y, y);
376 y = F_TPL(f, y, y);
377 y = F_TPL(f, y, y);
378 x = F_ADD(f, x, x, y);
379 rc = F_ZEROP(f, x);
380 MP_DROP(x);
381 MP_DROP(y);
382 if (rc) return ("not an elliptic curve");
383
384 /* --- Check %$G \in E$% --- */
385
386 if (EC_ATINF(&ei->g)) return ("generator at infinity");
387 if (ec_check(c, &ei->g)) return ("generator not on curve");
388
389 /* --- Check %$r$% is prime --- */
390
391 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
392
393 /* --- Check %$0 < h \le 4$% --- */
394
395 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
396 return ("cofactor out of range");
397
398 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
399 *
400 * This seems to work with the approximate-sqrt in the library, but might
401 * not be so good in some cases. Throw in some extra significate figures
402 * for good measure.
403 */
404
405 x = mp_lsl(MP_NEW, f->m, 128);
406 x = mp_sqrt(x, x);
407 y = mp_lsl(MP_NEW, MP_ONE, 64);
408 x = mp_add(x, x, y);
409 x = mp_sqr(x, x);
410 mp_div(&x, 0, x, ei->r);
411 x = mp_lsr(x, x, 128);
412 rc = MP_EQ(x, ei->h);
413 MP_DROP(x);
414 MP_DROP(y);
415 if (!rc) return ("incorrect cofactor");
416
417 /* --- Check %$n G = O$% --- */
418
419 EC_CREATE(&p);
420 ec_mul(c, &p, &ei->g, ei->r);
421 rc = EC_ATINF(&p);
422 EC_DESTROY(&p);
423 if (!rc) return ("incorrect group order");
424
425 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
426 *
427 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
428 */
429
430 x = MP_NEW;
431 mp_div(0, &x, f->m, ei->r);
432 i = 20;
433 while (i) {
434 if (MP_EQ(x, MP_ONE)) break;
435 x = mp_mul(x, x, f->m);
436 mp_div(0, &x, x, ei->r);
437 i--;
438 }
439 MP_DROP(x);
440 if (i) return ("curve is weak");
441
442 /* --- Done --- */
443
444 return (0);
445 }
446
447 static const char *bincheck(const ec_info *ei, grand *gr)
448 {
449 ec_curve *c = ei->c;
450 field *f = c->f;
451 int i;
452 mp *x, *y;
453 ec p;
454 int rc;
455
456 /* --- Check that %$p$% is irreducible --- */
457
458 if (!gf_irreduciblep(f->m)) return ("p not irreducible");
459
460 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
461
462 if (mp_bits(c->a) > f->nbits) return ("a out of range");
463 if (mp_bits(c->b) > f->nbits) return ("a out of range");
464 if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range");
465 if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range");
466
467 /* --- Check that %$b \ne 0$% --- */
468
469 if (F_ZEROP(f, c->b)) return ("b is zero");
470
471 /* --- Check that %$G \in E$% --- */
472
473 if (EC_ATINF(&ei->g)) return ("generator at infinity");
474 if (ec_check(c, &ei->g)) return ("generator not on curve");
475
476 /* --- Check %$r$% is prime --- */
477
478 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
479
480 /* --- Check %$0 < h \le 4$% --- */
481
482 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
483 return ("cofactor out of range");
484
485 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
486 *
487 * This seems to work with the approximate-sqrt in the library, but might
488 * not be so good in some cases. Throw in some extra significate figures
489 * for good measure.
490 */
491
492 x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128);
493 x = mp_sqrt(x, x);
494 y = mp_lsl(MP_NEW, MP_ONE, 64);
495 x = mp_add(x, x, y);
496 x = mp_sqr(x, x);
497 mp_div(&x, 0, x, ei->r);
498 x = mp_lsr(x, x, 128);
499 rc = MP_EQ(x, ei->h);
500 MP_DROP(x);
501 MP_DROP(y);
502 if (!rc) return ("incorrect cofactor");
503
504 /* --- Check %$n G = O$% --- */
505
506 EC_CREATE(&p);
507 ec_mul(c, &p, &ei->g, ei->r);
508 rc = EC_ATINF(&p);
509 EC_DESTROY(&p);
510 if (!rc) return ("incorrect group order");
511
512 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
513
514 x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
515 mp_div(0, &x, x, ei->r);
516 i = 20;
517 while (i) {
518 if (MP_EQ(x, MP_ONE)) break;
519 x = mp_mul(x, x, f->m);
520 mp_div(0, &x, x, ei->r);
521 i--;
522 }
523 MP_DROP(x);
524 if (i) return ("curve is weak");
525
526 /* --- Done --- */
527
528 return (0);
529 }
530
531 const char *ec_checkinfo(const ec_info *ei, grand *gr)
532 {
533 switch (F_TYPE(ei->c->f)) {
534 case FTY_PRIME: return (primecheck(ei, gr)); break;
535 case FTY_BINARY: return (bincheck(ei, gr)); break;
536 }
537 return ("unknown curve type");
538 }
539
540 /*----- Test rig ----------------------------------------------------------*/
541
542 #ifdef TEST_RIG
543
544 #include "fibrand.h"
545
546 int main(void)
547 {
548 const ecentry *ee;
549 const char *e;
550 int ok = 1;
551 grand *gr;
552
553 gr = fibrand_create(0);
554 fputs("checking standard curves: ", stdout);
555 for (ee = ectab; ee->name; ee++) {
556 ec_info ei;
557 getinfo(&ei, ee->data);
558 e = ec_checkinfo(&ei, gr);
559 ec_freeinfo(&ei);
560 if (e) {
561 fprintf(stderr, "\n*** curve %s fails: %s\n", ee->name, e);
562 ok = 0;
563 }
564 putchar('.');
565 fflush(stdout);
566 }
567 gr->ops->destroy(gr);
568 fputs(ok ? " ok\n" : " failed\n", stdout);
569 return (!ok);
570 }
571
572 #endif
573
574 /*----- That's all, folks -------------------------------------------------*/