3 * $Id: ec-info.c,v 1.2 2004/04/01 12:50:09 mdw Exp $
5 * Elliptic curve information management
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 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.)
40 * Revision 1.1 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
45 /*----- Header files ------------------------------------------------------*/
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @ec_curveparse@ --- *
58 * Arguments: @qd_parse *qd@ = parser context
60 * Returns: Elliptic curve pointer if OK, or null.
62 * Use: Parses an elliptic curve description, which has the form
64 * * a field description
66 * * `prime', `primeproj', `bin', or `binproj'
68 * * the %$a$% parameter
70 * * the %$b$% parameter
73 ec_curve *ec_curveparse(qd_parse *qd)
75 mp *a = MP_NEW, *b = MP_NEW;
79 if ((f = field_parse(qd)) == 0) goto fail;
81 switch (qd_enum(qd, "prime,primeproj,bin,binproj")) {
83 if (F_TYPE(f) != FTY_PRIME) {
84 qd->e = "field not prime";
88 if ((a = qd_getmp(qd)) == 0) goto fail;
90 if ((b = qd_getmp(qd)) == 0) goto fail;
91 c = ec_prime(f, a, b);
94 if (F_TYPE(f) != FTY_PRIME) {
95 qd->e = "field not prime";
99 if ((a = qd_getmp(qd)) == 0) goto fail;
101 if ((b = qd_getmp(qd)) == 0) goto fail;
102 c = ec_primeproj(f, a, b);
105 if (F_TYPE(f) != FTY_BINARY) {
106 qd->e = "field not binary";
110 if ((a = qd_getmp(qd)) == 0) goto fail;
112 if ((b = qd_getmp(qd)) == 0) goto fail;
116 if (F_TYPE(f) != FTY_BINARY) {
117 qd->e = "field not binary";
121 if ((a = qd_getmp(qd)) == 0) goto fail;
123 if ((b = qd_getmp(qd)) == 0) goto fail;
124 c = ec_binproj(f, a, b);
140 /* --- @ec_ptparse@ --- *
142 * Arguments: @qd_parse *qd@ = parser context
143 * @ec *p@ = where to put the point
145 * Returns: The point address, or null.
147 * Use: Parses an elliptic curve point. This has the form
154 ec *ec_ptparse(qd_parse *qd, ec *p)
156 mp *x = MP_NEW, *y = MP_NEW;
158 if (qd_enum(qd, "inf") >= 0) {
162 if ((x = qd_getmp(qd)) == 0) goto fail;
164 if ((y = qd_getmp(qd)) == 0) goto fail;
177 /* --- @getinfo@ --- *
179 * Arguments: @ec_info *ei@ = where to write the information
180 * @ecdata *ed@ = raw data
184 * Use: Loads elliptic curve information about one of the standard
188 static void getinfo(ec_info *ei, ecdata *ed)
194 f = field_prime(&ed->p);
195 ei->c = ec_primeproj(f, &ed->a, &ed->b);
198 f = field_niceprime(&ed->p);
199 ei->c = ec_primeproj(f, &ed->a, &ed->b);
202 f = field_binpoly(&ed->p);
203 ei->c = ec_binproj(f, &ed->a, &ed->b);
209 EC_CREATE(&ei->g); ei->g.x = &ed->gx; ei->g.y = &ed->gy; ei->g.z = 0;
210 ei->r = &ed->r; ei->h = &ed->h;
213 /* --- @ec_infoparse@ --- *
215 * Arguments: @qd_parse *qd@ = parser context
216 * @ec_info *ei@ = curve information block, currently
219 * Returns: Zero on success, nonzero on failure.
221 * Use: Parses an elliptic curve information string, and stores the
222 * information in @ei@. This is either the name of a standard
223 * curve, or it has the form
225 * * elliptic curve description
234 int ec_infoparse(qd_parse *qd, ec_info *ei)
240 mp *r = MP_NEW, *h = MP_NEW;
242 for (ee = ectab; ee->name; ee++) {
243 if (qd_enum(qd, ee->name) >= 0) {
244 getinfo(ei, ee->data);
248 if ((c = ec_curveparse(qd)) == 0) goto fail;
249 qd_delim(qd, '/'); if (!ec_ptparse(qd, &g)) goto fail;
250 qd_delim(qd, ':'); if ((r = qd_getmp(qd)) == 0) goto fail;
251 qd_delim(qd, '*'); if ((h = qd_getmp(qd)) == 0) goto fail;
252 ei->c = c; ei->g = g; ei->r = r; ei->h = h;
261 if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); }
265 /* --- @ec_getinfo@ --- *
267 * Arguments: @ec_info *ei@ = where to write the information
268 * @const char *p@ = string describing a curve
270 * Returns: Null on success, or a pointer to an error message.
272 * Use: Parses out information about a curve. The string is either a
273 * standard curve name, or a curve info string.
276 const char *ec_getinfo(ec_info *ei, const char *p)
282 if (ec_infoparse(&qd, ei))
286 return ("junk found at end of string");
291 /* --- @ec_sameinfop@ --- *
293 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
295 * Returns: Nonzero if the curves are identical (not just isomorphic).
297 * Use: Checks for sameness of curve parameters.
300 int ec_sameinfop(ec_info *ei, ec_info *ej)
302 return (ec_samep(ei->c, ej->c) &&
303 MP_EQ(ei->r, ej->r) && MP_EQ(ei->h, ej->h) &&
304 EC_EQ(&ei->g, &ej->g));
307 /* --- @ec_freeinfo@ --- *
309 * Arguments: @ec_info *ei@ = elliptic curve information block to free
313 * Use: Frees the information block.
316 void ec_freeinfo(ec_info *ei)
323 f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f);
326 /* --- @ec_checkinfo@ --- *
328 * Arguments: @const ec_info *ei@ = elliptic curve information block
330 * Returns: Null if OK, or pointer to error message.
332 * Use: Checks an elliptic curve according to the rules in SEC1.
335 static int primeeltp(mp *x, field *f)
337 return (!MP_ISNEG(x) && MP_CMP(x, <, f->m));
340 static const char *primecheck(const ec_info *ei, grand *gr)
349 /* --- Check %$p$% is an odd prime --- */
351 if (!pgen_primep(f->m, gr)) return ("p not prime");
353 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
355 if (!primeeltp(c->a, f)) return ("a out of range");
356 if (!primeeltp(c->b, f)) return ("b out of range");
357 if (!primeeltp(ei->g.x, f)) return ("G_x out of range");
358 if (!primeeltp(ei->g.x, f)) return ("G_y out of range");
360 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
362 x = F_SQR(f, MP_NEW, c->a);
363 x = F_MUL(f, x, x, c->a);
365 y = F_SQR(f, MP_NEW, c->b);
369 x = F_ADD(f, x, x, y);
373 if (rc) return ("not an elliptic curve");
375 /* --- Check %$G \in E$% --- */
377 if (EC_ATINF(&ei->g)) return ("generator at infinity");
378 if (ec_check(c, &ei->g)) return ("generator not on curve");
380 /* --- Check %$r$% is prime --- */
382 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
384 /* --- Check %$0 < h \le 4$% --- */
386 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
387 return ("cofactor out of range");
389 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
391 * This seems to work with the approximate-sqrt in the library, but might
392 * not be so good in some cases. Throw in some extra significate figures
396 x = mp_lsl(MP_NEW, f->m, 128);
398 y = mp_lsl(MP_NEW, MP_ONE, 64);
401 mp_div(&x, 0, x, ei->r);
402 x = mp_lsr(x, x, 128);
403 rc = MP_EQ(x, ei->h);
406 if (!rc) return ("incorrect cofactor");
408 /* --- Check %$n G = O$% --- */
411 ec_mul(c, &p, &ei->g, ei->r);
414 if (!rc) return ("incorrect group order");
416 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
418 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
422 mp_div(0, &x, f->m, ei->r);
425 if (MP_EQ(x, MP_ONE)) break;
426 x = mp_mul(x, x, f->m);
427 mp_div(0, &x, x, ei->r);
431 if (i) return ("curve is weak");
438 static const char *bincheck(const ec_info *ei, grand *gr)
447 /* --- Check that %$p$% is irreducible --- */
449 if (!gf_irreduciblep(f->m)) return ("p not irreducible");
451 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
453 if (mp_bits(c->a) > f->nbits) return ("a out of range");
454 if (mp_bits(c->b) > f->nbits) return ("a out of range");
455 if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range");
456 if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range");
458 /* --- Check that %$b \ne 0$% --- */
460 if (F_ZEROP(f, c->b)) return ("b is zero");
462 /* --- Check that %$G \in E$% --- */
464 if (EC_ATINF(&ei->g)) return ("generator at infinity");
465 if (ec_check(c, &ei->g)) return ("generator not on curve");
467 /* --- Check %$r$% is prime --- */
469 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
471 /* --- Check %$0 < h \le 4$% --- */
473 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
474 return ("cofactor out of range");
476 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
478 * This seems to work with the approximate-sqrt in the library, but might
479 * not be so good in some cases. Throw in some extra significate figures
483 x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128);
485 y = mp_lsl(MP_NEW, MP_ONE, 64);
488 mp_div(&x, 0, x, ei->r);
489 x = mp_lsr(x, x, 128);
490 rc = MP_EQ(x, ei->h);
493 if (!rc) return ("incorrect cofactor");
495 /* --- Check %$n G = O$% --- */
498 ec_mul(c, &p, &ei->g, ei->r);
501 if (!rc) return ("incorrect group order");
503 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
505 x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
506 mp_div(0, &x, x, ei->r);
509 if (MP_EQ(x, MP_ONE)) break;
510 x = mp_mul(x, x, f->m);
511 mp_div(0, &x, x, ei->r);
515 if (i) return ("curve is weak");
522 const char *ec_checkinfo(const ec_info *ei, grand *gr)
524 switch (F_TYPE(ei->c->f)) {
525 case FTY_PRIME: return (primecheck(ei, gr)); break;
526 case FTY_BINARY: return (bincheck(ei, gr)); break;
528 return ("unknown curve type");
531 /*----- Test rig ----------------------------------------------------------*/
544 gr = fibrand_create(0);
545 fputs("checking standard curves: ", stdout);
546 for (ee = ectab; ee->name; ee++) {
548 getinfo(&ei, ee->data);
549 e = ec_checkinfo(&ei, gr);
552 fprintf(stderr, "\n*** curve %s fails: %s\n", ee->name, e);
558 gr->ops->destroy(gr);
559 fputs(ok ? " ok\n" : " failed\n", stdout);
565 /*----- That's all, folks -------------------------------------------------*/