/* -*-c-*-
*
- * $Id: ec-info.c,v 1.1 2004/03/27 17:54:11 mdw Exp $
+ * $Id$
*
* Elliptic curve information management
*
* (c) 2004 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: ec-info.c,v $
- * Revision 1.1 2004/03/27 17:54:11 mdw
- * Standard curves and curve checking.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include "ec.h"
#include "gf.h"
#include "pgen.h"
#include "mprand.h"
+#include "mpint.h"
#include "rabin.h"
/*----- Main code ---------------------------------------------------------*/
* Use: Parses an elliptic curve description, which has the form
*
* * a field description
- * * an optional `/'
+ * * an optional `;'
* * `prime', `primeproj', `bin', or `binproj'
* * an optional `:'
* * the %$a$% parameter
field *f;
if ((f = field_parse(qd)) == 0) goto fail;
- qd_delim(qd, '/');
+ qd_delim(qd, ';');
switch (qd_enum(qd, "prime,primeproj,bin,binproj")) {
case 0:
if (F_TYPE(f) != FTY_PRIME) {
default:
goto fail;
}
+ if (!c) {
+ qd->e = "bad curve parameters";
+ goto fail;
+ }
if (a) MP_DROP(a);
if (b) MP_DROP(b);
return (c);
return (0);
}
+/* --- @ec_infofromdata@ --- *
+ *
+ * Arguments: @ec_info *ei@ = where to write the information
+ * @ecdata *ed@ = raw data
+ *
+ * Returns: ---
+ *
+ * Use: Loads elliptic curve information about one of the standard
+ * curves.
+ */
+
+void ec_infofromdata(ec_info *ei, ecdata *ed)
+{
+ field *f;
+
+ switch (ed->ftag) {
+ case FTAG_PRIME:
+ f = field_prime(&ed->p);
+ ei->c = ec_primeproj(f, &ed->a, &ed->b);
+ break;
+ case FTAG_NICEPRIME:
+ f = field_niceprime(&ed->p);
+ ei->c = ec_primeproj(f, &ed->a, &ed->b);
+ break;
+ case FTAG_BINPOLY:
+ f = field_binpoly(&ed->p);
+ ei->c = ec_binproj(f, &ed->a, &ed->b);
+ break;
+ case FTAG_BINNORM:
+ f = field_binnorm(&ed->p, &ed->beta);
+ ei->c = ec_binproj(f, &ed->a, &ed->b);
+ break;
+ default:
+ abort();
+ }
+
+ assert(f); assert(ei->c);
+ EC_CREATE(&ei->g); ei->g.x = &ed->gx; ei->g.y = &ed->gy; ei->g.z = 0;
+ ei->r = &ed->r; ei->h = &ed->h;
+}
+
/* --- @ec_infoparse@ --- *
*
* Arguments: @qd_parse *qd@ = parser context
* Returns: Zero on success, nonzero on failure.
*
* Use: Parses an elliptic curve information string, and stores the
- * information in @ei@. This has the form
+ * information in @ei@. This is either the name of a standard
+ * curve, or it has the form
*
* * elliptic curve description
- * * optional `/'
+ * * optional `;'
* * common point
* * optional `:'
* * group order
ec_curve *c = 0;
field *f;
ec g = EC_INIT;
+ const ecentry *ee;
mp *r = MP_NEW, *h = MP_NEW;
+ for (ee = ectab; ee->name; ee++) {
+ if (qd_enum(qd, ee->name) >= 0) {
+ ec_infofromdata(ei, ee->data);
+ goto found;
+ }
+ }
+
if ((c = ec_curveparse(qd)) == 0) goto fail;
- qd_delim(qd, '/'); if (!ec_ptparse(qd, &g)) goto fail;
+ qd_delim(qd, ';'); if (!ec_ptparse(qd, &g)) goto fail;
qd_delim(qd, ':'); if ((r = qd_getmp(qd)) == 0) goto fail;
qd_delim(qd, '*'); if ((h = qd_getmp(qd)) == 0) goto fail;
-
ei->c = c; ei->g = g; ei->r = r; ei->h = h;
+
+found:
return (0);
fail:
return (-1);
}
-/* --- @getinfo@ --- *
- *
- * Arguments: @ec_info *ei@ = where to write the information
- * @const ecdata *ed@ = raw data
- *
- * Returns: ---
- *
- * Use: Loads elliptic curve information about one of the standard
- * curves.
- */
-
-static mp *getmp(const mpw *v, size_t n)
-{
- mp *x = mp_new(n, 0);
- memcpy(x->v, v, MPWS(n));
- return (x);
-}
-
-static void getinfo(ec_info *ei, const ecdata *ed)
-{
- field *f;
- mp *p = 0, *a = 0, *b = 0;
-
- switch (ed->ftag) {
- case FTAG_PRIME:
- p = getmp(ed->p, ed->psz);
- f = field_prime(p);
- a = getmp(ed->a, ed->asz); b = getmp(ed->b, ed->bsz);
- ei->c = ec_primeproj(f, a, b);
- break;
- case FTAG_NICEPRIME:
- p = getmp(ed->p, ed->psz);
- f = field_niceprime(p);
- a = getmp(ed->a, ed->asz); b = getmp(ed->b, ed->bsz);
- ei->c = ec_primeproj(f, a, b);
- break;
- case FTAG_BINPOLY:
- p = getmp(ed->p, ed->psz);
- f = field_binpoly(p);
- a = getmp(ed->a, ed->asz); b = getmp(ed->b, ed->bsz);
- ei->c = ec_binproj(f, a, b);
- break;
- default:
- abort();
- }
-
- EC_CREATE(&ei->g);
- ei->g.x = getmp(ed->gx, ed->gxsz);
- ei->g.y = getmp(ed->gy, ed->gysz);
- ei->g.z = 0;
- ei->r = getmp(ed->r, ed->rsz);
- ei->h = getmp(ed->h, ed->hsz);
-
- MP_DROP(p);
- MP_DROP(a);
- MP_DROP(b);
-}
-
/* --- @ec_getinfo@ --- *
*
* Arguments: @ec_info *ei@ = where to write the information
const char *ec_getinfo(ec_info *ei, const char *p)
{
qd_parse qd;
- const ecentry *ee;
qd.p = p;
qd.e = 0;
- for (ee = ectab; ee->name; ee++) {
- if (qd_enum(&qd, ee->name) >= 0) {
- getinfo(ei, ee->data);
- goto found;
- }
- }
if (ec_infoparse(&qd, ei))
return (qd.e);
-found:
if (!qd_eofp(&qd)) {
ec_freeinfo(ei);
return ("junk found at end of string");
return (0);
}
+/* --- @ec_sameinfop@ --- *
+ *
+ * Arguments: @ec_info *ei, *ej@ = two elliptic curve parameter sets
+ *
+ * Returns: Nonzero if the curves are identical (not just isomorphic).
+ *
+ * Use: Checks for sameness of curve parameters.
+ */
+
+int ec_sameinfop(ec_info *ei, ec_info *ej)
+{
+ return (ec_samep(ei->c, ej->c) &&
+ MP_EQ(ei->r, ej->r) && MP_EQ(ei->h, ej->h) &&
+ EC_EQ(&ei->g, &ej->g));
+}
+
/* --- @ec_freeinfo@ --- *
*
* Arguments: @ec_info *ei@ = elliptic curve information block to free
* Use: Checks an elliptic curve according to the rules in SEC1.
*/
-static int primep(mp *p, grand *gr)
-{
- int i = rabin_iters(mp_bits(p));
- rabin r;
- mp *x = MP_NEW;
-
- switch (pfilt_smallfactor(p)) {
- case PGEN_DONE: return (1);
- case PGEN_FAIL: return (0);
- }
- rabin_create(&r, p);
- while (i) {
- x = mprand_range(x, p, gr, 0);
- if (rabin_rtest(&r, x) == PGEN_FAIL)
- break;
- i--;
- }
- MP_DROP(x);
- rabin_destroy(&r);
- return (!i);
-}
-
-static int primeeltp(mp *x, field *f)
-{
- return (!MP_ISNEG(x) && MP_CMP(x, <, f->m));
-}
-
-static const char *primecheck(const ec_info *ei, grand *gr)
+static const char *gencheck(const ec_info *ei, grand *gr, mp *q)
{
ec_curve *c = ei->c;
field *f = c->f;
- int i;
+ int i, j, n;
+ mp *qq;
+ mp *nn;
mp *x, *y;
ec p;
int rc;
- /* --- Check %$p$% is an odd prime --- */
-
- if (!primep(f->m, gr)) return ("p not prime");
-
- /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
-
- if (!primeeltp(c->a, f)) return ("a out of range");
- if (!primeeltp(c->b, f)) return ("b out of range");
- if (!primeeltp(ei->g.x, f)) return ("G_x out of range");
- if (!primeeltp(ei->g.x, f)) return ("G_y out of range");
-
- /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
-
- x = F_SQR(f, MP_NEW, c->a);
- x = F_MUL(f, x, x, c->a);
- x = F_QDL(f, x, x);
- y = F_SQR(f, MP_NEW, c->b);
- y = F_TPL(f, y, y);
- y = F_TPL(f, y, y);
- y = F_TPL(f, y, y);
- x = F_ADD(f, x, x, y);
- rc = F_ZEROP(f, x);
- MP_DROP(x);
- MP_DROP(y);
- if (rc) return ("not an elliptic curve");
-
/* --- Check %$G \in E$% --- */
if (EC_ATINF(&ei->g)) return ("generator at infinity");
/* --- Check %$r$% is prime --- */
- if (!primep(ei->r, gr)) return ("generator order not prime");
+ if (!pgen_primep(ei->r, gr)) return ("generator order not prime");
- /* --- Check %$0 < h \le 4$% --- */
-
- if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
- return ("cofactor out of range");
-
- /* --- Check %$h = \lfloor (\sqrt{p} + 1)^2/r \rlfoor$% --- *
+ /* --- Check that the cofactor is correct --- *
+ *
+ * Let %$q$% be the size of the field, and let %$n = h r = \#E(\gf{q})$% be
+ * the number of %$\gf{q}$%-rational points on our curve. Hasse's theorem
+ * tells us that
+ *
+ * %$|q + 1 - n| \le 2\sqrt{q}$%
+ *
+ * or, if we square both sides,
+ *
+ * %$(q + 1 - n)^2 \le 4 q$%.
+ *
+ * We'd like the cofactor to be uniquely determined by this equation, which
+ * is possible as long as it's not too big. (If it is, we have to mess
+ * about with Weil pairings, which is no fun.) For this, we need the
+ * following inequalities:
+ *
+ * * %$A = (q + 1 - n)^2 \le 4 q$% (both lower and upper bounds from
+ * Hasse's theorem);
+ *
+ * * %$B = (q + 1 - n - r)^2 > 4 q$% (check %$h - 1$% isn't possible);
+ * and
*
- * This seems to work with the approximate-sqrt in the library, but might
- * not be so good in some cases. Throw in some extra significate figures
- * for good measure.
+ * * %$C = (q + 1 - n + r)^2 > 4 q$% (check %$h + 1$% isn't possible).
*/
- x = mp_lsl(MP_NEW, f->m, 128);
- x = mp_sqrt(x, x);
- y = mp_lsl(MP_NEW, MP_ONE, 64);
- x = mp_add(x, x, y);
- x = mp_sqr(x, x);
- mp_div(&x, 0, x, ei->r);
- x = mp_lsr(x, x, 128);
- rc = MP_EQ(x, ei->h);
+ rc = 1;
+ qq = mp_add(MP_NEW, q, MP_ONE);
+ nn = mp_mul(MP_NEW, ei->r, ei->h);
+ nn = mp_sub(nn, qq, nn);
+ qq = mp_lsl(qq, q, 2);
+
+ y = mp_sqr(MP_NEW, nn);
+ if (MP_CMP(y, >, qq)) rc = 0;
+
+ x = mp_sub(MP_NEW, nn, ei->r);
+ y = mp_sqr(y, x);
+ if (MP_CMP(y, <=, qq)) rc = 0;
+
+ x = mp_add(x, nn, ei->r);
+ y = mp_sqr(y, x);
+ if (MP_CMP(y, <=, qq)) rc = 0;
+
MP_DROP(x);
MP_DROP(y);
- if (!rc) return ("incorrect cofactor");
+ MP_DROP(nn);
+ MP_DROP(qq);
+ if (!rc) return ("incorrect or ambiguous cofactor");
/* --- Check %$n G = O$% --- */
EC_DESTROY(&p);
if (!rc) return ("incorrect group order");
- /* --- Check that %$p^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
+ /* --- Check %$q^B \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- *
*
- * The spec says %$q$%, not %$p$%, but I think that's a misprint.
+ * Actually, give up if %$q^B \ge 2^{2000}$% because that's probably
+ * good enough for jazz.
*/
x = MP_NEW;
- mp_div(0, &x, f->m, ei->r);
- i = 20;
- while (i) {
- if (MP_EQ(x, MP_ONE)) break;
+ mp_div(0, &x, q, ei->r);
+ n = mp_bits(ei->r) - 1;
+ for (i = 0, j = n; i < 20; i++, j += n) {
+ if (j >= 2000)
+ break;
+ if (MP_EQ(x, MP_ONE)) {
+ MP_DROP(x);
+ return("curve embedding degree too low");
+ }
x = mp_mul(x, x, f->m);
mp_div(0, &x, x, ei->r);
- i--;
}
MP_DROP(x);
- if (i) return ("curve is weak");
/* --- Done --- */
return (0);
}
-static const char *bincheck(const ec_info *ei, grand *gr)
+static int primeeltp(mp *x, field *f)
+ { return (!MP_NEGP(x) && MP_CMP(x, <, f->m)); }
+
+static const char *primecheck(const ec_info *ei, grand *gr)
{
ec_curve *c = ei->c;
field *f = c->f;
- int i;
mp *x, *y;
- ec p;
int rc;
+ const char *err;
- /* --- Check that %$p$% is irreducible --- */
+ /* --- Check %$p$% is an odd prime --- */
- if (!gf_irreduciblep(f->m)) return ("p not irreducible");
+ if (!pgen_primep(f->m, gr)) return ("p not prime");
- /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
+ /* --- Check %$a$%, %$b$%, %$G_x$% and %$G_y$% are in %$[0, p)$% --- */
- if (mp_bits(c->a) > f->nbits) return ("a out of range");
- if (mp_bits(c->b) > f->nbits) return ("a out of range");
- if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range");
- if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range");
+ if (!primeeltp(c->a, f)) return ("a out of range");
+ if (!primeeltp(c->b, f)) return ("b out of range");
+ if (!primeeltp(ei->g.x, f)) return ("G_x out of range");
+ if (!primeeltp(ei->g.x, f)) return ("G_y out of range");
- /* --- Check that %$b \ne 0$% --- */
+ /* --- Check %$4 a^3 + 27 b^2 \not\equiv 0 \pmod{p}$% --- */
- if (F_ZEROP(f, c->b)) return ("b is zero");
+ x = F_SQR(f, MP_NEW, c->a);
+ x = F_MUL(f, x, x, c->a);
+ x = F_QDL(f, x, x);
+ y = F_SQR(f, MP_NEW, c->b);
+ y = F_TPL(f, y, y);
+ y = F_TPL(f, y, y);
+ y = F_TPL(f, y, y);
+ x = F_ADD(f, x, x, y);
+ rc = F_ZEROP(f, x);
+ MP_DROP(x);
+ MP_DROP(y);
+ if (rc) return ("not an elliptic curve");
- /* --- Check that %$G \in E$% --- */
+ /* --- Now do the general checks --- */
- if (EC_ATINF(&ei->g)) return ("generator at infinity");
- if (ec_check(c, &ei->g)) return ("generator not on curve");
+ err = gencheck(ei, gr, f->m);
+ return (err);
+}
- /* --- Check %$r$% is prime --- */
+static const char *bincheck(const ec_info *ei, grand *gr)
+{
+ ec_curve *c = ei->c;
+ field *f = c->f;
+ mp *x;
+ int rc;
+ const char *err;
- if (!primep(ei->r, gr)) return ("generator order not prime");
+ /* --- Check that %$m$% is prime --- */
- /* --- Check %$0 < h \le 4$% --- */
+ x = mp_fromuint(MP_NEW, f->nbits);
+ rc = pfilt_smallfactor(x);
+ mp_drop(x);
+ if (rc != PGEN_DONE) return ("degree not prime");
- if (MP_CMP(ei->h, <, MP_ONE) || MP_CMP(ei->h, >, MP_FOUR))
- return ("cofactor out of range");
+ /* --- Check that %$p$% is irreducible --- */
- /* --- Check %$h = \lfloor (\sqrt{2^m} + 1)^2/r \rlfoor$% --- *
- *
- * This seems to work with the approximate-sqrt in the library, but might
- * not be so good in some cases. Throw in some extra significate figures
- * for good measure.
- */
-
- x = mp_lsl(MP_NEW, MP_ONE, f->nbits + 128);
- x = mp_sqrt(x, x);
- y = mp_lsl(MP_NEW, MP_ONE, 64);
- x = mp_add(x, x, y);
- x = mp_sqr(x, x);
- mp_div(&x, 0, x, ei->r);
- x = mp_lsr(x, x, 128);
- rc = MP_EQ(x, ei->h);
- MP_DROP(x);
- MP_DROP(y);
- if (!rc) return ("incorrect cofactor");
+ if (!gf_irreduciblep(f->m)) return ("p not irreducible");
- /* --- Check %$n G = O$% --- */
+ /* --- Check that %$a, b, G_x, G_y$% have degree less than %$p$% --- */
- EC_CREATE(&p);
- ec_mul(c, &p, &ei->g, ei->r);
- rc = EC_ATINF(&p);
- EC_DESTROY(&p);
- if (!rc) return ("incorrect group order");
+ if (mp_bits(c->a) > f->nbits) return ("a out of range");
+ if (mp_bits(c->b) > f->nbits) return ("a out of range");
+ if (mp_bits(ei->g.x) > f->nbits) return ("G_x out of range");
+ if (mp_bits(ei->g.y) > f->nbits) return ("G_y out of range");
- /* --- Check %$2^{m B} \not\equiv 1 \pmod{r}$% for %$1 \le B < 20$% --- */
+ /* --- Check that %$b \ne 0$% --- */
- x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
- mp_div(0, &x, x, ei->r);
- i = 20;
- while (i) {
- if (MP_EQ(x, MP_ONE)) break;
- x = mp_mul(x, x, f->m);
- mp_div(0, &x, x, ei->r);
- i--;
- }
- MP_DROP(x);
- if (i) return ("curve is weak");
+ if (F_ZEROP(f, c->b)) return ("b is zero");
- /* --- Done --- */
+ /* --- Now do the general checks --- */
- return (0);
+ x = mp_lsl(MP_NEW, MP_ONE, f->nbits);
+ err = gencheck(ei, gr, x);
+ mp_drop(x);
+ return (err);
}
const char *ec_checkinfo(const ec_info *ei, grand *gr)
#include "fibrand.h"
-int main(void)
+int main(int argc, char *argv[])
{
const ecentry *ee;
const char *e;
int ok = 1;
+ int i;
grand *gr;
gr = fibrand_create(0);
- fputs("checking standard curves: ", stdout);
- for (ee = ectab; ee->name; ee++) {
- ec_info ei;
- getinfo(&ei, ee->data);
- e = ec_checkinfo(&ei, gr);
- ec_freeinfo(&ei);
- if (e) {
- fprintf(stderr, "\n*** curve %s fails: %s\n", ee->name, e);
- ok = 0;
+ if (argc > 1) {
+ for (i = 1; i < argc; i++) {
+ ec_info ei;
+ if ((e = ec_getinfo(&ei, argv[i])) != 0)
+ fprintf(stderr, "bad curve spec `%s': %s\n", argv[i], e);
+ else {
+ e = ec_checkinfo(&ei, gr);
+ ec_freeinfo(&ei);
+ if (!e)
+ printf("OK %s\n", argv[i]);
+ else {
+ printf("BAD %s: %s\n", argv[i], e);
+ ok = 0;
+ }
+ }
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
}
- putchar('.');
+ } else {
+ fputs("checking standard curves:", stdout);
fflush(stdout);
+ for (ee = ectab; ee->name; ee++) {
+ ec_info ei;
+ ec_infofromdata(&ei, ee->data);
+ e = ec_checkinfo(&ei, gr);
+ ec_freeinfo(&ei);
+ if (e) {
+ printf(" [%s fails: %s]", ee->name, e);
+ ok = 0;
+ } else
+ printf(" %s", ee->name);
+ fflush(stdout);
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
+ }
+ fputs(ok ? " ok\n" : " failed\n", stdout);
}
gr->ops->destroy(gr);
- fputs(ok ? " ok\n" : " failed\n", stdout);
return (!ok);
}