Negative exponents using the BigInteger class

W.Howe :

I'm doing some maths using the Java BigInteger class but getting an error when I'm trying to use a negative number as an exponent. Am I right in thinking that you can rearrange:

b · φ(N)^−1 mod N

as:

      b
------------
φ(N)^1 mod N

If not how can I rearrange the expression as to not get a negative exponent error in my Java code?

President James Moveon Polk :

Arithmetic mod N must be performed with modified rules. In particular inverses must be calculated much differently. The basic axiom of the inverse holds true:

x * x-1 = 1 mod N.

but you cannot compute x-1 mod N by computing 1/x as a floating point or decimal value. Instead, you must use an algorithm specifically for this purpose. Usually a variant of the extended euclidean algorithm is used.

Conveniently, Java's BigInteger class already includes this algorithm for you: modInverse(). So your calculation should look something like:

BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger phiInverse = phi.modInverse(N);
BigInteger result = b.multiply(phiInverse).mod(N);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88863&siteId=1