X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/34e4f738bcba58e6d8c4cabbb0b3232a65b42a9d..e9026a0a6d8fc5cbcfa8d658176bfd2776cb550e:/g-prime.c diff --git a/g-prime.c b/g-prime.c index 03843be..97f455f 100644 --- 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.3 2004/04/04 19:04:11 mdw Exp $ * * Abstraction for prime groups * @@ -30,6 +30,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: g-prime.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. @@ -78,8 +84,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 +148,21 @@ 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); +} + +static int gtoraw(group *gg, buf *b, mp **x) { + gctx *g = (gctx *)gg; octet *q; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); + if ((q = buf_get(b, g->g.noctets)) == 0) { MP_DROP(t); return (-1); } + mp_storeb(t, q, g->g.noctets); MP_DROP(t); return (0); +} + +static int gfromraw(group *gg, buf *b, mp **d) { + gctx * g = (gctx *)gg; mp *x; octet *q; + if ((q = buf_get(b, g->g.noctets)) == 0) return (-1); + x = mp_loadb(MP_NEW, q, g->g.noctets); + 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 +170,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. @@ -161,13 +183,17 @@ static const group_ops gops = { gcheck, gmul, gsqr, ginv, group_stddiv, gexp, gmexp, gread, gwrite, - gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf + gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf, + gtoraw, gfromraw }; 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;