]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libmp/port/mpinvert.c
mp: add mptod and dtomp
[plan9front.git] / sys / src / libmp / port / mpinvert.c
1 #include "os.h"
2 #include <mp.h>
3
4 // use extended gcd to find the multiplicative inverse
5 // res = b**-1 mod m
6 void
7 mpinvert(mpint *b, mpint *m, mpint *res)
8 {
9         mpint *v;
10
11         v = mpnew(0);
12         mpextendedgcd(b, m, v, res, nil);
13         if(mpcmp(v, mpone) != 0)
14                 abort();
15         mpfree(v);
16         mpmod(res, m, res);
17 }