Makefile.m4: Remove mplimits.[ch] on clean.
[u/mdw/catacomb] / group-test.c
index 5fdf147..5608ec0 100644 (file)
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: group-test.c,v 1.1 2004/04/01 12:50:09 mdw Exp $
+ * $Id: group-test.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
  *
  * Testing group operations
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: group-test.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 <stdarg.h>
@@ -82,10 +70,10 @@ static void showec(const char *p, ec *q) {
     mp_writefile(q->x, stderr, 16); fputs(", ", stderr);
     mp_writefile(q->x, stderr, 16); putchar('\n');
   }
-}  
+}
 
 static void showmp(const char *p, mp *x, int r) {
-  fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r); 
+  fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r);
   putc('\n', stderr);
 }
 
@@ -450,6 +438,71 @@ static int vfrombuf(dstr *v)
   return (ok);
 }
 
+static int vtoraw(dstr *v)
+{
+  group *g = getgroup(v[0].buf);
+  ge *x = getge(g, v[1].buf);
+  int ir = *(int *)v[2].buf;
+  dstr c = DSTR_INIT;
+  int ic;
+  buf b;
+  int ok = 1;
+
+  dstr_ensure(&c, v[3].len);
+  buf_init(&b, c.buf, v[3].len);
+  ic = G_TORAW(g, &b, x);
+  c.len = BLEN(&b);
+  if (ic != ir || (!ic && (c.len != v[3].len ||
+                          memcmp(c.buf, v[3].buf, c.len)))) {
+    ok = 0;
+    fprintf(stderr, "*** toraw failed\n");
+    fprintf(stderr, "*** group: %s\n", v[0].buf);
+    show(g, "x", x);
+    if (ir) fprintf(stderr, "*** expected failure\n");
+    else {
+      fprintf(stderr, "*** expected: "); type_hex.dump(&v[3], stderr);
+      fprintf(stderr, "\n*** computed: "); type_hex.dump(&c, stderr);
+      fputc('\n', stderr);
+    }
+  }
+  G_DESTROY(g, x); dstr_destroy(&c);
+  G_DESTROYGROUP(g);
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
+  return (ok);
+}
+
+static int vfromraw(dstr *v)
+{
+  group *g = getgroup(v[0].buf);
+  int ir = *(int *)v[2].buf;
+  ge *r = getge(g, v[3].buf);
+  int ic;
+  ge *c = G_CREATE(g);
+  buf b;
+  int ok = 1;
+
+  buf_init(&b, v[1].buf, v[1].len);
+  ic = G_FROMRAW(g, &b, c);
+  if ((ic < 0) != (ir < 0) || (ir >= 0 &&
+                              (ir != BLEN(&b) || !G_EQ(g, r, c)))) {
+    ok = 0;
+    fprintf(stderr, "*** fromraw failed\n");
+    fprintf(stderr, "*** group: %s\n", v[0].buf);
+    fprintf(stderr, "*** input string: "); type_hex.dump(&v[1], stderr);
+    fputc('\n', stderr);
+    if (ir < 0) fprintf(stderr, "*** expected failure\n");
+    else {
+      show(g, "expected", r); show(g, "computed", c);
+      fprintf(stderr, "*** expected used = %d\n", ir);
+      fprintf(stderr, "*** computed used = %lu\n", (unsigned long)BLEN(&b));
+    }
+  }
+  G_DESTROY(g, r); G_DESTROY(g, c);
+  G_DESTROYGROUP(g);
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
+  return (ok);
+}
+
 static const test_chunk tests[] = {
   { "check",   vcheck,         { &type_string, &type_string } },
   { "checkelt",        vcheckelt,      { &type_string, &type_string, &type_int } },
@@ -493,6 +546,10 @@ static const test_chunk tests[] = {
                                  &type_int, &type_hex } },
   { "frombuf", vfrombuf,       { &type_string, &type_hex,
                                  &type_int, &type_string } },
+  { "toraw",   vtoraw,         { &type_string, &type_string,
+                                 &type_int, &type_hex } },
+  { "fromraw", vfromraw,       { &type_string, &type_hex,
+                                 &type_int, &type_string } },
   { 0 }
 };