org.bouncycastle.pqc.math.ntru.euclid.IntEuclidean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15to18 Show documentation
Show all versions of bcprov-jdk15to18 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.
package org.bouncycastle.pqc.math.ntru.euclid;
/**
* Extended Euclidean Algorithm in int
s
*/
public class IntEuclidean
{
public int x, y, gcd;
private IntEuclidean()
{
}
/**
* Runs the EEA on two int
s
* Implemented from pseudocode on Wikipedia.
*
* @param a
* @param b
* @return a IntEuclidean
object that contains the result in the variables x
, y
, and gcd
*/
public static IntEuclidean calculate(int a, int b)
{
int x = 0;
int lastx = 1;
int y = 1;
int lasty = 0;
while (b != 0)
{
int quotient = a / b;
int temp = a;
a = b;
b = temp % b;
temp = x;
x = lastx - quotient * x;
lastx = temp;
temp = y;
y = lasty - quotient * y;
lasty = temp;
}
IntEuclidean result = new IntEuclidean();
result.x = lastx;
result.y = lasty;
result.gcd = a;
return result;
}
}