All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.pqc.math.ntru.euclid.BigIntEuclidean Maven / Gradle / Ivy

Go to download

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 and up.

There is a newer version: 1.70
Show newest version
package org.bouncycastle.pqc.math.ntru.euclid;

import java.math.BigInteger;

/**
 * Extended Euclidean Algorithm in BigIntegers
 */
public class BigIntEuclidean
{
    public BigInteger x, y, gcd;

    private BigIntEuclidean()
    {
    }

    /**
     * Runs the EEA on two BigIntegers
* 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; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy