Example usage for org.bouncycastle.math.ec ECCurve getB

List of usage examples for org.bouncycastle.math.ec ECCurve getB

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECCurve getB.

Prototype

public ECFieldElement getB() 

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();/*from   w  ww.  j av a 2s  .  com*/

    if (yTilde == null) {
        throw new IllegalArgumentException("invalid point compression");
    }

    // 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();//from  w  w w.j av  a2s .  c om

    if (yTilde == null) {
        throw new IllegalArgumentException("invalid point compression");
    }

    // 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:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

static void serializeCurve(Output output, ECCurve curve) throws KryoException {
    byte[] bytes;
    int length;//from w  ww  .  j a  va 2 s .  c o  m
    // save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work.
    String curveName = curve.getClass().getSimpleName();

    if (CustomNamedCurves.getByName(curveName) != null) {
        // we use the name instead of serializing the full curve
        output.writeInt(usesName, true);
        output.writeString(curveName);
        return;
    }

    else if (curveName.endsWith("Curve")) {
        String cleanedName = curveName.substring(0, curveName.indexOf("Curve"));

        if (!cleanedName.isEmpty()) {
            ASN1ObjectIdentifier oid = CustomNamedCurves.getOID(cleanedName);
            if (oid != null) {
                // we use the OID (instead of serializing the entire curve)
                output.writeInt(usesOid, true);
                curveName = oid.getId();
                output.writeString(curveName);
                return;
            }
        }
    }

    // we have to serialize the ENTIRE curve.
    // save out the curve info
    BigInteger a = curve.getA().toBigInteger();
    BigInteger b = curve.getB().toBigInteger();
    BigInteger order = curve.getOrder();
    BigInteger cofactor = curve.getCofactor();
    BigInteger q = curve.getField().getCharacteristic();

    /////////////
    bytes = a.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = b.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = order.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = cofactor.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = q.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    // coordinate system
    int coordinateSystem = curve.getCoordinateSystem();
    output.writeInt(coordinateSystem, true);
}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

License:Open Source License

/**
 * Gets the key specification from a public key. Example: "2048" for a RSA 
 * or DSA key or "secp256r1" for EC key. The EC curve is only detected 
 * if <i>publickey</i> is an object known by the bouncy castle provider.
 * @param publicKey The public key to get the key specification from
 * @return The key specification, "unknown" if it could not be determined and
 * null if the key algorithm is not supported
 */// w w w .j  a va2  s  .c om
public static String getKeySpecification(final PublicKey publicKey) {
    if (log.isTraceEnabled()) {
        log.trace(">getKeySpecification");
    }
    String keyspec = null;
    if (publicKey instanceof RSAPublicKey) {
        keyspec = Integer.toString(((RSAPublicKey) publicKey).getModulus().bitLength());
    } else if (publicKey instanceof DSAPublicKey) {
        keyspec = Integer.toString(((DSAPublicKey) publicKey).getParams().getP().bitLength());
    } else if (publicKey instanceof ECPublicKey) {
        final ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        if (ecPublicKey.getParams() instanceof ECNamedCurveSpec) {
            keyspec = ((ECNamedCurveSpec) ecPublicKey.getParams()).getName();
            // Prefer to return a curve name alias that also works with the default and BC provider
            for (String keySpecAlias : getEcKeySpecAliases(keyspec)) {
                if (isNamedECKnownInDefaultProvider(keySpecAlias)) {
                    keyspec = keySpecAlias;
                    break;
                }
            }
        } else {
            keyspec = KEYSPEC_UNKNOWN;
            // Try to detect if it is a curve name known by BC even though the public key isn't a BC key
            final ECParameterSpec namedCurve = ecPublicKey.getParams();
            if (namedCurve != null) {
                final int c1 = namedCurve.getCofactor();
                final EllipticCurve ec1 = namedCurve.getCurve();
                final BigInteger a1 = ec1.getA();
                final BigInteger b1 = ec1.getB();
                final int fs1 = ec1.getField().getFieldSize();
                //final byte[] s1 = ec1.getSeed();
                final ECPoint g1 = namedCurve.getGenerator();
                final BigInteger ax1 = g1.getAffineX();
                final BigInteger ay1 = g1.getAffineY();
                final BigInteger o1 = namedCurve.getOrder();
                if (log.isDebugEnabled()) {
                    log.debug("a1=" + a1 + " b1=" + b1 + " fs1=" + fs1 + " ax1=" + ax1 + " ay1=" + ay1 + " o1="
                            + o1 + " c1=" + c1);
                }
                @SuppressWarnings("unchecked")
                final Enumeration<String> ecNamedCurves = ECNamedCurveTable.getNames();
                while (ecNamedCurves.hasMoreElements()) {
                    final String ecNamedCurveBc = ecNamedCurves.nextElement();
                    final ECNamedCurveParameterSpec parameterSpec2 = ECNamedCurveTable
                            .getParameterSpec(ecNamedCurveBc);
                    final ECCurve ec2 = parameterSpec2.getCurve();
                    final BigInteger a2 = ec2.getA().toBigInteger();
                    final BigInteger b2 = ec2.getB().toBigInteger();
                    final int fs2 = ec2.getFieldSize();
                    final org.bouncycastle.math.ec.ECPoint g2 = parameterSpec2.getG();
                    final BigInteger ax2 = g2.getX().toBigInteger();
                    final BigInteger ay2 = g2.getY().toBigInteger();
                    final BigInteger h2 = parameterSpec2.getH();
                    final BigInteger n2 = parameterSpec2.getN();
                    if (a1.equals(a2) && ax1.equals(ax2) && b1.equals(b2) && ay1.equals(ay2) && fs1 == fs2
                            && o1.equals(n2) && c1 == h2.intValue()) {
                        // We have a matching curve here!
                        if (log.isDebugEnabled()) {
                            log.debug("a2=" + a2 + " b2=" + b2 + " fs2=" + fs2 + " ax2=" + ax2 + " ay2=" + ay2
                                    + " h2=" + h2 + " n2=" + n2 + " " + ecNamedCurveBc);
                        }
                        // Since this public key is a SUN PKCS#11 pub key if we get here, we only return an alias if it is recognized by the provider
                        if (isNamedECKnownInDefaultProvider(ecNamedCurveBc)) {
                            keyspec = ecNamedCurveBc;
                            break;
                        }
                    }
                }
            }
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("<getKeySpecification: " + keyspec);
    }
    return keyspec;
}

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/*from  www . j  ava  2s .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: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   www  . j a va2 s .c  o  m
        return c.createPoint(x.toBigInteger(), y.toBigInteger(), false);
    }
}