/* -*-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
*
/*----- 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.
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)); }
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);
}
*
* 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.
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;