summaryrefslogtreecommitdiff
path: root/sys/src/libmp/port/mpinvert.c
blob: 59713747b85eff0bf9fc14b584646b94d2dcaafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "os.h"
#include <mp.h>

// use extended gcd to find the multiplicative inverse
// res = b**-1 mod m
void
mpinvert(mpint *b, mpint *m, mpint *res)
{
	mpint *v;

	v = mpnew(0);
	mpextendedgcd(b, m, v, res, nil);
	if(mpcmp(v, mpone) != 0)
		abort();
	mpfree(v);
	mpmod(res, m, res);
}