org.spongycastle.math.ntru.euclid.BigIntEuclidean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scprov-jdk15on Show documentation
Show all versions of scprov-jdk15on Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms.
This jar contains JCE provider for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.7.
package org.spongycastle.math.ntru.euclid;
import java.math.BigInteger;
/**
* Extended Euclidean Algorithm in BigInteger
s
*/
public class BigIntEuclidean
{
public BigInteger x, y, gcd;
private BigIntEuclidean()
{
}
/**
* Runs the EEA on two BigInteger
s
* Implemented from pseudocode on Wikipedia.
*
* @param a
* @param b
* @return a BigIntEuclidean
object that contains the result in the variables x
, y
, and gcd
*/
public static BigIntEuclidean calculate(BigInteger a, BigInteger b)
{
BigInteger x = BigInteger.ZERO;
BigInteger lastx = BigInteger.ONE;
BigInteger y = BigInteger.ONE;
BigInteger lasty = BigInteger.ZERO;
while (!b.equals(BigInteger.ZERO))
{
BigInteger[] quotientAndRemainder = a.divideAndRemainder(b);
BigInteger quotient = quotientAndRemainder[0];
BigInteger temp = a;
a = b;
b = quotientAndRemainder[1];
temp = x;
x = lastx.subtract(quotient.multiply(x));
lastx = temp;
temp = y;
y = lasty.subtract(quotient.multiply(y));
lasty = temp;
}
BigIntEuclidean result = new BigIntEuclidean();
result.x = lastx;
result.y = lasty;
result.gcd = a;
return result;
}
}