General robustification.
[u/mdw/catacomb] / g-prime.c
index 03843be..03cce55 100644 (file)
--- a/g-prime.c
+++ b/g-prime.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: g-prime.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ * $Id: g-prime.c,v 1.2 2004/04/03 03:32:05 mdw Exp $
  *
  * Abstraction for prime groups
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: g-prime.c,v $
+ * 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.
@@ -78,8 +81,10 @@ static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; }
 
 static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); }
 
-static int gsamep(group *gg, group *hh)
-  { gctx *g = (gctx *)gg, *h = (gctx *)hh; return (g->mm.m == h->mm.m); }
+static int gsamep(group *gg, group *hh) {
+  gctx *g = (gctx *)gg, *h = (gctx *)hh;
+  return (MP_EQ(g->mm.m, h->mm.m));
+}
 
 static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); }
 
@@ -140,7 +145,7 @@ static int gtobuf(group *gg, buf *b, mp **x) {
 
 static int gfrombuf(group *gg, buf *b, mp **d) {
   gctx * g = (gctx *)gg; mp *x; if ((x = buf_getmp(b)) == 0) return (-1);
-  mp_div(0, &x, x, g->mm.r2); mp_drop(*d);
+  mp_div(0, &x, x, g->mm.m); mp_drop(*d);
   *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0);
 }
 
@@ -148,7 +153,7 @@ static int gfrombuf(group *gg, buf *b, mp **d) {
  *
  * Arguments:  @const gprime_param *gp@ = group parameters
  *
- * Returns:    A pointer to the group.
+ * Returns:    A pointer to the group, or null.
  *
  * Use:                Constructs an abstract group interface for a subgroup of a
  *             prime field.  Group elements are @mp *@ pointers.
@@ -166,8 +171,11 @@ static const group_ops gops = {
 
 group *group_prime(const gprime_param *gp)
 {
-  gctx *g = CREATE(gctx);
+  gctx *g;
 
+  if (!MP_ISPOS(gp->p) || !MP_ISODD(gp->p))
+    return (0);
+  g = CREATE(gctx);
   g->g.ops = &gops;
   g->g.nbits = mp_bits(gp->p);
   g->g.noctets = (g->g.nbits + 7) >> 3;