Example usage for org.bouncycastle.math.ec ECFieldElement.Fp toBigInteger

List of usage examples for org.bouncycastle.math.ec ECFieldElement.Fp toBigInteger

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECFieldElement.Fp toBigInteger.

Prototype

public abstract BigInteger toBigInteger();

Source Link

Usage

From source file:edu.biu.scapi.primitives.dlog.ECFpUtility.java

License:Open Source License

/**
 * This function finds the y coordinate of a point in the curve for a given x, if it exists.
 * @param params the parameters of the group
 * @param x/*from  w  ww. j  ava 2 s  .c  om*/
 * @return the y coordinate of point in the curve for a given x, if it exists
 *          else, null
 */
public BigInteger findYInCurveEquationForX(ECFpGroupParams params, BigInteger x) {

    /* get a, b, p from group params */
    BigInteger a = params.getA();
    BigInteger b = params.getB();
    BigInteger p = params.getP();

    // compute x^3
    BigInteger x3 = x.modPow(new BigInteger("3"), p);
    // compute x^3+ax+b
    BigInteger rightSide = x3.add(a.multiply(x)).add(b).mod(p);
    //try to compute y = square_root(x^3+ax+b)
    //If it exists return it
    //else, return null
    //We compute the square root via the ECFieldElement.Fp of Bouncy Castle, since BigInteger does not implement this function.
    //ECFieldElement.Fp ySquare = new ECFieldElement.Fp(params.getQ(), rightSide);
    ECFieldElement.Fp ySquare = new ECFieldElement.Fp(params.getP(), rightSide);

    //TODO I am not sure which one of the square roots it returns (if they exist). We need to check this!! (Yael)
    ECFieldElement.Fp y = (Fp) ySquare.sqrt();
    if (y != null) {
        return y.toBigInteger().mod(p);
    } else {
        return null;
    }
}