Gather up another utility.
[u/mdw/catacomb] / g-prime.c
index 03cce55..aa17685 100644 (file)
--- a/g-prime.c
+++ b/g-prime.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: g-prime.c,v 1.2 2004/04/03 03:32:05 mdw Exp $
+ * $Id: g-prime.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
  *
  * Abstraction for prime groups
  *
  * MA 02111-1307, USA.
  */
 
-/*----- 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.
- * 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 <mLib/sub.h>
@@ -106,7 +91,7 @@ static void gsqr(group *gg, mp **d, mp **x) {
 
 static void ginv(group *gg, mp **d, mp **x) {
   gctx *g = (gctx *)gg; mp *r = mpmont_reduce(&g->mm, *d, *x);
-  mp_gcd(0, 0, &r, g->mm.m, r); *d = mpmont_mul(&g->mm, r, r, g->mm.r2);
+  r = mp_modinv(r, r, g->mm.m); *d = mpmont_mul(&g->mm, r, r, g->mm.r2);
 }
 
 static void gexp(group *gg, mp **d, mp **x, mp *n)
@@ -134,8 +119,8 @@ static mp *gtoint(group *gg, mp *d, mp **x)
   { gctx *g = (gctx *)gg; return (mpmont_reduce(&g->mm, d, *x)); }
 
 static int gfromint(group *gg, mp **d, mp *x) {
-  gctx *g = (gctx *)gg; mp_div(0, &x, x, g->mm.m); mp_drop(*d);
-  *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return (0);
+  gctx *g = (gctx *)gg; mp_div(0, d, x, g->mm.m);
+  *d = mpmont_mul(&g->mm, *d, *d, g->mm.r2); return (0);
 }
 
 static int gtobuf(group *gg, buf *b, mp **x) {
@@ -149,6 +134,20 @@ static int gfrombuf(group *gg, buf *b, mp **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);
+}
+
 /* --- @group_prime@ --- *
  *
  * Arguments:  @const gprime_param *gp@ = group parameters
@@ -166,7 +165,8 @@ 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)