X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/898a4e2555438ff8adb08b4d82690d08715e1048..b817bfc642225b8c3c0b6a7e42d1fb949b61a606:/gf-gcd.c diff --git a/gf-gcd.c b/gf-gcd.c index 7c09d3a..8eb9bbf 100644 --- 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 * @@ -27,17 +27,6 @@ * 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);