Example usage for org.bouncycastle.math.ec ECFieldElement square

List of usage examples for org.bouncycastle.math.ec ECFieldElement square

Introduction

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

Prototype

public abstract ECFieldElement square();

Source Link

Usage

From source file:org.ScripterRon.BitcoinCore.ECKey.java

License:Apache License

/**
 * Decompress a compressed public key (x coordinate and low-bit of y-coordinate).
 *
 * @param       xBN                 X-coordinate
 * @param       yBit                Sign of Y-coordinate
 * @return                          Uncompressed public key
 *//*from   w w  w.j  av  a2  s  .  c  o m*/
private static ECPoint decompressKey(BigInteger xBN, boolean yBit) {
    SecP256K1Curve curve = (SecP256K1Curve) ecParams.getCurve();
    ECFieldElement x = curve.fromBigInteger(xBN);
    ECFieldElement alpha = x.multiply(x.square().add(curve.getA())).add(curve.getB());
    ECFieldElement beta = alpha.sqrt();
    if (beta == null)
        throw new IllegalArgumentException("Invalid point compression");
    ECPoint ecPoint;
    BigInteger nBeta = beta.toBigInteger();
    if (nBeta.testBit(0) == yBit) {
        ecPoint = curve.createPoint(x.toBigInteger(), nBeta);
    } else {
        ECFieldElement y = curve.fromBigInteger(curve.getQ().subtract(nBeta));
        ecPoint = curve.createPoint(x.toBigInteger(), y.toBigInteger());
    }
    return ecPoint;
}