Since the question is about modular inverse, I suppose it will benefit the seeker to look at another SO question.
Crux of the answer is -
Net 4.0+ implements BigInteger with a special modular arithmetics function ModPow (which produces “X power Y modulo Z”), you don't need a third-party library to emulate ModInverse. If m is a prime, all you need to do is to compute:
In C# as per MSDN documentation this is defined as
public static BigInteger ModPow(
BigInteger value,
BigInteger exponent,
BigInteger modulus
)
Using this we can do something like to calculate inverse of the k modulo p
like
BigInteger bi= ModPow(k, -1, p );
int b= (int) bi;
s= (a* bi )%p;