General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / g-ec.c
diff --git a/g-ec.c b/g-ec.c
index 1f214f7..32220ff 100644 (file)
--- a/g-ec.c
+++ b/g-ec.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: g-ec.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ * $Id: g-ec.c,v 1.5 2004/04/17 09:58:37 mdw Exp $
  *
  * Abstraction for elliptic curve groups
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: g-ec.c,v $
- * 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>
@@ -47,6 +35,7 @@
 
 #define ge ec
 #include "group.h"
+#include "ec-raw.h"
 
 /*----- Data structures ---------------------------------------------------*/
 
@@ -161,7 +150,7 @@ static int gfromint(group *gg, ec *d, mp *x) {
 static int gtoec(group *gg, ec *d, ec *x)
   { gctx *g = (gctx *)gg; EC_OUT(g->ei.c, d, x); return (0); }
 
-static int gfromec(group *gg, ec *d, ec *x) {
+static int gfromec(group *gg, ec *d, const ec *x) {
   gctx *g = (gctx *)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);
@@ -179,6 +168,19 @@ static int gfrombuf(group *gg, buf *b, ec *d) {
   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;
+  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;
+  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);
+}
+
 /* --- @group_ec@ --- *
  *
  * Arguments:  @const ec_info *ei@ = elliptic curve parameters
@@ -199,7 +201,7 @@ static const group_ops gops = {
   gcheck,
   gmul, gsqr, ginv, gdiv, gexp, gmexp,
   gread, gwrite,
-  gtoint, gfromint, gtoec, gfromec, gtobuf, gfrombuf
+  gtoint, gfromint, gtoec, gfromec, gtobuf, gfrombuf, gtoraw, gfromraw
 };
 
 group *group_ec(const ec_info *ei)
@@ -208,11 +210,12 @@ group *group_ec(const ec_info *ei)
 
   g->g.ops = &gops;
   g->g.nbits = ei->c->f->nbits * 2;
-  g->g.noctets = ei->c->f->noctets * 2;
+  g->g.noctets = ei->c->f->noctets * 2 + 1;
   g->ei = *ei;
   EC_CREATE(&g->id);
   g->g.i = &g->id;
   EC_CREATE(&g->gen);
+  g->g.g = &g->gen;
   EC_IN(g->ei.c, &g->gen, &ei->g);
   g->g.r = ei->r;
   g->g.h = ei->h;