Expunge revision histories in files.
[u/mdw/catacomb] / gf-gcd.c
index 7c09d3a..8eb9bbf 100644 (file)
--- a/gf-gcd.c
+++ b/gf-gcd.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: gf-gcd.c,v 1.2 2004/03/21 22:52:06 mdw Exp $
+ * $Id: gf-gcd.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
  *
  * Euclidian algorithm on binary polynomials
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: gf-gcd.c,v $
- * Revision 1.2  2004/03/21 22:52:06  mdw
- * Merge and close elliptic curve branch.
- *
- * Revision 1.1.2.1  2004/03/21 22:39:46  mdw
- * Elliptic curves on binary fields work.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "gf.h"
@@ -186,6 +175,28 @@ void gf_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
   MP_DROP(a); MP_DROP(b);
 }
 
+/* -- @gf_modinv@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *x@ = argument
+ *             @mp *p@ = modulus
+ *
+ * Returns:    The inverse %$x^{-1} \bmod p$%.
+ *
+ * Use:                Computes a modular inverse, the catch being that the
+ *             arguments and results are binary polynomials.  An assertion
+ *             fails if %$p$% has no inverse.
+ */
+
+mp *gf_modinv(mp *d, mp *x, mp *p)
+{
+  mp *g = MP_NEW;
+  gf_gcd(&g, 0, &d, p, x);
+  assert(MP_EQ(g, MP_ONE));
+  mp_drop(g);
+  return (d);
+}
+
 /*----- Test rig ----------------------------------------------------------*/
 
 #ifdef TEST_RIG
@@ -202,7 +213,7 @@ static int gcd(dstr *v)
   mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
   gf_gcd(&gg, &xx, &yy, a, b);
   if (!MP_EQ(x, xx)) {
-    fputs("\n*** mp_gcd(x) failed", stderr);
+    fputs("\n*** gf_gcd(x) failed", stderr);
     fputs("\na      = ", stderr); mp_writefile(a, stderr, 16);
     fputs("\nb      = ", stderr); mp_writefile(b, stderr, 16);
     fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 16);
@@ -211,7 +222,7 @@ static int gcd(dstr *v)
     ok = 0;
   }
   if (!MP_EQ(y, yy)) {
-    fputs("\n*** mp_gcd(y) failed", stderr);
+    fputs("\n*** gf_gcd(y) failed", stderr);
     fputs("\na      = ", stderr); mp_writefile(a, stderr, 16);
     fputs("\nb      = ", stderr); mp_writefile(b, stderr, 16);
     fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 16);
@@ -231,7 +242,7 @@ static int gcd(dstr *v)
   }
 
   if (!MP_EQ(g, gg)) {
-    fputs("\n*** mp_gcd(gcd) failed", stderr);
+    fputs("\n*** gf_gcd(gcd) failed", stderr);
     fputs("\na      = ", stderr); mp_writefile(a, stderr, 16);
     fputs("\nb      = ", stderr); mp_writefile(b, stderr, 16);
     fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 16);