/* -*-c-*-
*
- * $Id: g-ec.c,v 1.3 2004/04/04 19:04:11 mdw Exp $
+ * $Id$
*
* Abstraction for elliptic curve groups
*
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: g-ec.c,v $
- * Revision 1.3 2004/04/04 19:04:11 mdw
- * Raw I/O of elliptic curve points and group elements.
- *
- * Revision 1.2 2004/04/03 03:32:05 mdw
- * General robustification.
- *
- * Revision 1.1 2004/04/01 12:50:09 mdw
- * Add cyclic group abstraction, with test code. Separate off exponentation
- * functions for better static linking. Fix a buttload of bugs on the way.
- * Generally ensure that negative exponents do inversion correctly. Add
- * table of standard prime-field subgroups. (Binary field subgroups are
- * currently unimplemented but easy to add if anyone ever finds a good one.)
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <ctype.h>
#define ge ec
#include "group.h"
#include "ec-raw.h"
-
-/*----- Data structures ---------------------------------------------------*/
-
-typedef struct gctx {
- group g;
- ec id, gen;
- ec_info ei;
-} gctx;
+#include "group-guts.h"
/*----- Main code ---------------------------------------------------------*/
/* --- Group operations --- */
static void gdestroygroup(group *gg) {
- gctx *g = (gctx *)gg;
+ gctx_ec *g = (gctx_ec *)gg;
EC_DESTROY(&g->gen);
ec_freeinfo(&g->ei);
DESTROY(g);
static void gdestroy(group *gg, ec *x) { EC_DESTROY(x); DESTROY(x); }
static int gsamep(group *gg, group *hh) {
- gctx *g = (gctx *)gg, *h = (gctx *)hh;
+ gctx_ec *g = (gctx_ec *)gg, *h = (gctx_ec *)hh;
return (ec_sameinfop(&g->ei, &h->ei));
}
static int geq(group *gg, ec *x, ec *y) {
- gctx *g = (gctx *)gg; EC_FIX(g->ei.c, x, x); EC_FIX(g->ei.c, y, y);
+ gctx_ec *g = (gctx_ec *)gg; EC_FIX(g->ei.c, x, x); EC_FIX(g->ei.c, y, y);
return (EC_EQ(x, y));
}
static int gidentp(group *gg, ec *x) { return (EC_ATINF(x)); }
static const char *gcheck(group *gg, grand *gr)
- { gctx *g = (gctx *)gg; return (ec_checkinfo(&g->ei, gr)); }
+ { gctx_ec *g = (gctx_ec *)gg; return (ec_checkinfo(&g->ei, gr)); }
static void gmul(group *gg, ec *d, ec *x, ec *y)
- { gctx *g = (gctx *)gg; EC_ADD(g->ei.c, d, x, y); }
+ { gctx_ec *g = (gctx_ec *)gg; EC_ADD(g->ei.c, d, x, y); }
static void gsqr(group *gg, ec *d, ec *x)
- { gctx *g = (gctx *)gg; EC_DBL(g->ei.c, d, x); }
+ { gctx_ec *g = (gctx_ec *)gg; EC_DBL(g->ei.c, d, x); }
static void ginv(group *gg, ec *d, ec *x)
- { gctx *g = (gctx *)gg; EC_NEG(g->ei.c, d, x); }
+ { gctx_ec *g = (gctx_ec *)gg; EC_NEG(g->ei.c, d, x); }
static void gdiv(group *gg, ec *d, ec *x, ec *y)
- { gctx *g = (gctx *)gg; EC_SUB(g->ei.c, d, x, y); }
+ { gctx_ec *g = (gctx_ec *)gg; EC_SUB(g->ei.c, d, x, y); }
static void gexp(group *gg, ec *d, ec *x, mp *n)
- { gctx *g = (gctx *)gg; ec_imul(g->ei.c, d, x, n); }
+ { gctx_ec *g = (gctx_ec *)gg; ec_imul(g->ei.c, d, x, n); }
static void gmexp(group *gg, ec *d, const group_expfactor *f, size_t n) {
- gctx *g = (gctx *)gg; size_t i;
+ gctx_ec *g = (gctx_ec *)gg; size_t i;
ec_mulfactor *ff = xmalloc(n * sizeof(ec_mulfactor));
for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; }
ec_immul(g->ei.c, d, ff, n); xfree(ff);
}
static int gread(group *gg, ec *d, const mptext_ops *ops, void *p) {
- gctx *g = (gctx *)gg;
+ gctx_ec *g = (gctx_ec *)gg;
ec t = EC_INIT;
int rc = -1;
int ch;
}
static int gwrite(group *gg, ec *x, const mptext_ops *ops, void *p) {
- gctx *g = (gctx *)gg; int rc = -1; ec t = EC_INIT; EC_OUT(g->ei.c, &t, x);
- if (EC_ATINF(&t)) rc = ops->put("inf", 3, p);
+ gctx_ec *g = (gctx_ec *)gg; int rc = -1; ec t = EC_INIT;
+ EC_OUT(g->ei.c, &t, x); if (EC_ATINF(&t)) rc = ops->put("inf", 3, p);
else if (!ops->put("0x", 2, p) && !mp_write(t.x, 16, ops, p) &&
!ops->put(", 0x", 4, p) && !mp_write(t.y, 16, ops, p)) rc = 0;
EC_DESTROY(&t); return (rc);
}
static mp *gtoint(group *gg, mp *d, ec *x) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; mp *i; if (EC_ATINF(x)) i = 0;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; mp *i; if (EC_ATINF(x)) i = 0;
else { EC_OUT(g->ei.c, &t, x); i = MP_COPY(t.x); EC_DESTROY(&t); }
mp_drop(d); return (i);
}
static int gfromint(group *gg, ec *d, mp *x) {
- gctx *g = (gctx *)gg; ec t = EC_INIT;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT;
if (!ec_find(g->ei.c, &t, x)) return (-1);
EC_IN(g->ei.c, d, &t); EC_DESTROY(&t); return (0);
}
static int gtoec(group *gg, ec *d, ec *x)
- { gctx *g = (gctx *)gg; EC_OUT(g->ei.c, d, x); return (0); }
+ { gctx_ec *g = (gctx_ec *)gg; EC_OUT(g->ei.c, d, x); return (0); }
-static int gfromec(group *gg, ec *d, ec *x) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; int rc; EC_IN(g->ei.c, &t, x);
+static int gfromec(group *gg, ec *d, const ec *x) {
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; int rc; EC_IN(g->ei.c, &t, x);
rc = EC_CHECK(g->ei.c, &t); if (!rc) EC_COPY(d, &t); EC_DESTROY(&t);
return (rc);
}
static int gtobuf(group *gg, buf *b, ec *x) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; int rc;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; int rc;
EC_OUT(g->ei.c, &t, x); rc = buf_putec(b, &t); EC_DESTROY(&t); return (rc);
}
static int gfrombuf(group *gg, buf *b, ec *d) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; int rc;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; int rc;
if (buf_getec(b, &t)) return (-1);
EC_IN(g->ei.c, &t, &t); rc = EC_CHECK(g->ei.c, &t);
if (!rc) EC_COPY(d, &t); EC_DESTROY(&t); return (rc);
}
static int gtoraw(group *gg, buf *b, ec *x) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; int rc;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; int rc;
EC_OUT(g->ei.c, &t, x); rc = ec_putraw(g->ei.c, b, &t);
EC_DESTROY(&t); return (rc);
}
static int gfromraw(group *gg, buf *b, ec *d) {
- gctx *g = (gctx *)gg; ec t = EC_INIT; int rc;
+ gctx_ec *g = (gctx_ec *)gg; ec t = EC_INIT; int rc;
if (ec_getraw(g->ei.c, b, &t)) return (-1);
EC_IN(g->ei.c, &t, &t); rc = EC_CHECK(g->ei.c, &t);
if (!rc) EC_COPY(d, &t); EC_DESTROY(&t); return (rc);
*/
static const group_ops gops = {
- GTY_EC,
+ GTY_EC, "ec",
gdestroygroup, gcreate, gcopy, gburn, gdestroy,
gsamep, geq, gidentp,
gcheck,
group *group_ec(const ec_info *ei)
{
- gctx *g = CREATE(gctx);
+ gctx_ec *g = CREATE(gctx_ec);
g->g.ops = &gops;
g->g.nbits = ei->c->f->nbits * 2;