X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/898a4e2555438ff8adb08b4d82690d08715e1048..b817bfc642225b8c3c0b6a7e42d1fb949b61a606:/mp-gcd.c diff --git a/mp-gcd.c b/mp-gcd.c index 6135e54..5e663a1 100644 --- a/mp-gcd.c +++ b/mp-gcd.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-gcd.c,v 1.6 2004/03/21 22:52:06 mdw Exp $ + * $Id: mp-gcd.c,v 1.7 2004/04/08 01:36:15 mdw Exp $ * * Extended GCD calculation * @@ -27,33 +27,6 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: mp-gcd.c,v $ - * Revision 1.6 2004/03/21 22:52:06 mdw - * Merge and close elliptic curve branch. - * - * Revision 1.5.4.1 2004/03/21 22:39:46 mdw - * Elliptic curves on binary fields work. - * - * Revision 1.5 2000/10/08 12:02:41 mdw - * Use Euclid's algorithm rather than the binary one. - * - * Revision 1.4 2000/06/17 11:34:46 mdw - * More hacking for the signs of the outputs. - * - * Revision 1.3 1999/12/10 23:18:39 mdw - * Change interface for suggested destinations. - * - * Revision 1.2 1999/11/22 20:49:56 mdw - * Fix bug which failed to favour `x' when `y' wasn't wanted and the two - * arguments needed swapping. - * - * Revision 1.1 1999/11/17 18:02:16 mdw - * New multiprecision integer arithmetic suite. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "mp.h" @@ -256,6 +229,27 @@ void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) MP_DROP(a); MP_DROP(b); } +/* -- @mp_modinv@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *x@ = argument + * @mp *p@ = modulus + * + * Returns: The inverse %$x^{-1} \bmod p$%. + * + * Use: Computes a modular inverse. An assertion fails if %$p$% + * has no inverse. + */ + +mp *mp_modinv(mp *d, mp *x, mp *p) +{ + mp *g = MP_NEW; + mp_gcd(&g, 0, &d, p, x); + assert(MP_EQ(g, MP_ONE)); + mp_drop(g); + return (d); +} + /*----- Test rig ----------------------------------------------------------*/ #ifdef TEST_RIG