-
Notifications
You must be signed in to change notification settings - Fork 4
/
FFUtil.cpp
49 lines (41 loc) · 856 Bytes
/
FFUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "FFUtil.hpp"
int FFUtil::extEuclideanGCD(int a, int b, int& x, int& y)
{
x = 1;
y = 0;
int xx = 0;
int yy = 1;
int g = a;
int gg = b;
while(gg != 0)
{
int quotient = g / gg;
int txx = x - quotient * xx;
int tyy = y - quotient * yy;
int tgg = g - quotient * gg;
x = xx;
y = yy;
g = gg;
xx = txx;
yy = tyy;
gg = tgg;
}
return g;
}
/*
* ModInverse
*
* Description:
*
* Finds the inverse of r mod n by solving the linear
* congruence defined by ri * r = 1 (mod n) where ri is
* the modular inverse of r and is unique for this modulus.
*
*/
int FFUtil::modInverse(int r, int n)
{
r = r % n;
int x, y, gcd;
gcd = FFUtil::extEuclideanGCD(r, n, x, y);
return (gcd != 1) ? 0 : x % n;
}