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

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

Introduction

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

Prototype

public abstract BigInteger toBigInteger();

Source Link

Usage

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECPointsCompact.java

License:Open Source License

public static BigInteger y(ECCurve curve, BigInteger x) {
    // Andrey Jivsov https://www.ietf.org/archive/id/draft-jivsov-ecc-compact-05.txt.
    ECFieldElement X = curve.fromBigInteger(x);
    ECFieldElement rhs = X.square().add(curve.getA()).multiply(X).add(curve.getB());

    // y' = sqrt( C(x) ), where y'>0
    ECFieldElement yTilde = rhs.sqrt();

    if (yTilde == null) {
        throw new IllegalArgumentException("invalid point compression");
    }//from  ww w  .  j  a  v  a  2s.  co  m

    // y = min(y',p-y')
    BigInteger yT = yTilde.toBigInteger();
    BigInteger yTn = yTilde.negate().toBigInteger();
    BigInteger y = yT.compareTo(yTn) == -1 ? yT : yTn;

    return y;
}

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECPointsCompact.java

License:Open Source License

@Deprecated
public static ECPoint decompressFPPoint(ECCurve curve, BigInteger X) {
    // See Andrey Jivsov https://www.ietf.org/archive/id/draft-jivsov-ecc-compact-05.txt.
    ECFieldElement x = curve.fromBigInteger(X);
    ECFieldElement rhs = x.square().add(curve.getA()).multiply(x).add(curve.getB());

    // y' = sqrt( C(x) ), where y'>0
    ECFieldElement yTilde = rhs.sqrt();

    if (yTilde == null) {
        throw new IllegalArgumentException("invalid point compression");
    }/*w  ww .  j  ava 2  s.c  o  m*/

    // y = min(y',p-y')
    BigInteger yT = yTilde.toBigInteger();
    BigInteger yTn = yTilde.negate().toBigInteger();
    BigInteger y = yT.compareTo(yTn) == -1 ? yT : yTn;

    // Q=(x,y) is the canonical representation of the point
    ECPoint Q = curve.createPoint(X, y);

    return Q;
}

From source file:org.jmrtd.Util.java

License:Open Source License

/**
 * This just solves the curve equation for y.
 *
 * @param affineX the x coord of a point on the curve
 * @param params EC parameters for curve over Fp
 * @return the corresponding y coord// w  ww .j  ava  2  s.  c  o m
 */
public static BigInteger computeAffineY(BigInteger affineX, ECParameterSpec params) {
    ECCurve bcCurve = toBouncyCastleECCurve(params);
    ECFieldElement a = bcCurve.getA();
    ECFieldElement b = bcCurve.getB();
    ECFieldElement x = bcCurve.fromBigInteger(affineX);
    ECFieldElement y = x.multiply(x).add(a).multiply(x).add(b).sqrt();
    return y.toBigInteger();
}

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  ava 2s.co 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;
}

From source file:service.ACService.java

License:Open Source License

private static ECPoint reconstructPoint(ECCurve c, BigInteger i, boolean negate) {
    ECFieldElement x = c.fromBigInteger(i);
    ECFieldElement y = x.multiply(x).multiply(x).add(c.getA().multiply(x)).add(c.getB()).sqrt();
    if (negate) {
        return c.createPoint(x.toBigInteger(), y.toBigInteger().negate(), false);
    } else {/*from  w  w  w .  j a v  a 2s. co m*/
        return c.createPoint(x.toBigInteger(), y.toBigInteger(), false);
    }
}