3 * $Id: ec-info.c,v 1.3 2004/04/01 21:28:41 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.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.
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.)
45 * Revision 1.1 2004/03/27 17:54:11 mdw
46 * Standard curves and curve checking.
50 /*----- Header files ------------------------------------------------------*/
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @ec_curveparse@ --- *
63 * Arguments: @qd_parse *qd@ = parser context
65 * Returns: Elliptic curve pointer if OK, or null.
67 * Use: Parses an elliptic curve description, which has the form
69 * * a field description
71 * * `prime', `primeproj', `bin', or `binproj'
73 * * the %$a$% parameter
75 * * the %$b$% parameter
78 ec_curve *ec_curveparse(qd_parse *qd)
80 mp *a = MP_NEW, *b = MP_NEW;
84 if ((f = field_parse(qd)) == 0) goto fail;
86 switch (qd_enum(qd, "prime,primeproj,bin,binproj")) {
88 if (F_TYPE(f) != FTY_PRIME) {
89 qd->e = "field not prime";
93 if ((a = qd_getmp(qd)) == 0) goto fail;
95 if ((b = qd_getmp(qd)) == 0) goto fail;
96 c = ec_prime(f, a, b);
99 if (F_TYPE(f) != FTY_PRIME) {
100 qd->e = "field not prime";
104 if ((a = qd_getmp(qd)) == 0) goto fail;
106 if ((b = qd_getmp(qd)) == 0) goto fail;
107 c = ec_primeproj(f, a, b);
110 if (F_TYPE(f) != FTY_BINARY) {
111 qd->e = "field not binary";
115 if ((a = qd_getmp(qd)) == 0) goto fail;
117 if ((b = qd_getmp(qd)) == 0) goto fail;
121 if (F_TYPE(f) != FTY_BINARY) {
122 qd->e = "field not binary";
126 if ((a = qd_getmp(qd)) == 0) goto fail;
128 if ((b = qd_getmp(qd)) == 0) goto fail;
129 c = ec_binproj(f, a, b);
145 /* --- @ec_ptparse@ --- *
147 * Arguments: @qd_parse *qd@ = parser context
148 * @ec *p@ = where to put the point
150 * Returns: The point address, or null.
152 * Use: Parses an elliptic curve point. This has the form
159 ec *ec_ptparse(qd_parse *qd, ec *p)
161 mp *x = MP_NEW, *y = MP_NEW;
163 if (qd_enum(qd, "inf") >= 0) {
167 if ((x = qd_getmp(qd)) == 0) goto fail;
169 if ((y = qd_getmp(qd)) == 0) goto fail;
182 /* --- @getinfo@ --- *
184 * Arguments: @ec_info *ei@ = where to write the information
185 * @ecdata *ed@ = raw data
189 * Use: Loads elliptic curve information about one of the standard
193 static void getinfo(ec_info *ei, ecdata *ed)
199 f = field_prime(&ed->p);
200 ei->c = ec_primeproj(f, &ed->a, &ed->b);
203 f = field_niceprime(&ed->p);
204 ei->c = ec_primeproj(f, &ed->a, &ed->b);
207 f = field_binpoly(&ed->p);
208 ei->c = ec_binproj(f, &ed->a, &ed->b);
211 f = field_binnorm(&ed->p, &ed->beta);
212 ei->c = ec_binproj(f, &ed->a, &ed->b);
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;
222 /* --- @ec_infoparse@ --- *
224 * Arguments: @qd_parse *qd@ = parser context
225 * @ec_info *ei@ = curve information block, currently
228 * Returns: Zero on success, nonzero on failure.
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
234 * * elliptic curve description
243 int ec_infoparse(qd_parse *qd, ec_info *ei)
249 mp *r = MP_NEW, *h = MP_NEW;
251 for (ee = ectab; ee->name; ee++) {
252 if (qd_enum(qd, ee->name) >= 0) {
253 getinfo(ei, ee->data);
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;
270 if (c) { f = c->f; ec_destroycurve(c); F_DESTROY(f); }
274 /* --- @ec_getinfo@ --- *
276 * Arguments: @ec_info *ei@ = where to write the information
277 * @const char *p@ = string describing a curve
279 * Returns: Null on success, or a pointer to an error message.
281 * Use: Parses out information about a curve. The string is either a
282 * standard curve name, or a curve info string.
285 const char *ec_getinfo(ec_info *ei, const char *p)
291 if (ec_infoparse(&qd, ei))
295 return ("junk found at end of string");
300 /* --- @ec_sameinfop@ --- *
302 * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
304 * Returns: Nonzero if the curves are identical (not just isomorphic).
306 * Use: Checks for sameness of curve parameters.
309 int ec_sameinfop(ec_info *ei, ec_info *ej)
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));
316 /* --- @ec_freeinfo@ --- *
318 * Arguments: @ec_info *ei@ = elliptic curve information block to free
322 * Use: Frees the information block.
325 void ec_freeinfo(ec_info *ei)
332 f = ei->c->f; ec_destroycurve(ei->c); F_DESTROY(f);
335 /* --- @ec_checkinfo@ --- *
337 * Arguments: @const ec_info *ei@ = elliptic curve information block
339 * Returns: Null if OK, or pointer to error message.
341 * Use: Checks an elliptic curve according to the rules in SEC1.
344 static int primeeltp(mp *x, field *f)
346 return (!MP_ISNEG(x) && MP_CMP(x, <, f->m));
349 static const char *primecheck(const ec_info *ei, grand *gr)
358 /* --- Check %$p$% is an odd prime --- */
360 if (!pgen_primep(f->m, gr)) return ("p not prime");
362 /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
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");
369 /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
371 x = F_SQR(f, MP_NEW, c->a);
372 x = F_MUL(f, x, x, c->a);
374 y = F_SQR(f, MP_NEW, c->b);
378 x = F_ADD(f, x, x, y);
382 if (rc) return ("not an elliptic curve");
384 /* --- Check %$G \in E$% --- */
386 if (EC_ATINF(&ei->g)) return ("generator at infinity");
387 if (ec_check(c, &ei->g)) return ("generator not on curve");
389 /* --- Check %$r$% is prime --- */
391 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
393 /* --- Check %$0 < h \le 4$% --- */
395 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
396 return ("cofactor out of range");
398 /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
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
405 x = mp_lsl(MP_NEW, f->m, 128);
407 y = mp_lsl(MP_NEW, MP_ONE, 64);
410 mp_div(&x, 0, x, ei->r);
411 x = mp_lsr(x, x, 128);
412 rc = MP_EQ(x, ei->h);
415 if (!rc) return ("incorrect cofactor");
417 /* --- Check %$n G = O$% --- */
420 ec_mul(c, &p, &ei->g, ei->r);
423 if (!rc) return ("incorrect group order");
425 /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
427 * The spec says %$q$%, not %$p$%, but I think that's a misprint.
431 mp_div(0, &x, f->m, ei->r);
434 if (MP_EQ(x, MP_ONE)) break;
435 x = mp_mul(x, x, f->m);
436 mp_div(0, &x, x, ei->r);
440 if (i) return ("curve is weak");
447 static const char *bincheck(const ec_info *ei, grand *gr)
456 /* --- Check that %$p$% is irreducible --- */
458 if (!gf_irreduciblep(f->m)) return ("p not irreducible");
460 /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
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");
467 /* --- Check that %$b \ne 0$% --- */
469 if (F_ZEROP(f, c->b)) return ("b is zero");
471 /* --- Check that %$G \in E$% --- */
473 if (EC_ATINF(&ei->g)) return ("generator at infinity");
474 if (ec_check(c, &ei->g)) return ("generator not on curve");
476 /* --- Check %$r$% is prime --- */
478 if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
480 /* --- Check %$0 < h \le 4$% --- */
482 if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
483 return ("cofactor out of range");
485 /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
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
492 x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128);
494 y = mp_lsl(MP_NEW, MP_ONE, 64);
497 mp_div(&x, 0, x, ei->r);
498 x = mp_lsr(x, x, 128);
499 rc = MP_EQ(x, ei->h);
502 if (!rc) return ("incorrect cofactor");
504 /* --- Check %$n G = O$% --- */
507 ec_mul(c, &p, &ei->g, ei->r);
510 if (!rc) return ("incorrect group order");
512 /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
514 x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
515 mp_div(0, &x, x, ei->r);
518 if (MP_EQ(x, MP_ONE)) break;
519 x = mp_mul(x, x, f->m);
520 mp_div(0, &x, x, ei->r);
524 if (i) return ("curve is weak");
531 const char *ec_checkinfo(const ec_info *ei, grand *gr)
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;
537 return ("unknown curve type");
540 /*----- Test rig ----------------------------------------------------------*/
553 gr = fibrand_create(0);
554 fputs("checking standard curves: ", stdout);
555 for (ee = ectab; ee->name; ee++) {
557 getinfo(&ei, ee->data);
558 e = ec_checkinfo(&ei, gr);
561 fprintf(stderr, "\n*** curve %s fails: %s\n", ee->name, e);
567 gr->ops->destroy(gr);
568 fputs(ok ? " ok\n" : " failed\n", stdout);
574 /*----- That's all, folks -------------------------------------------------*/