java.security.spec.ECField Java Examples

The following examples show how to use java.security.spec.ECField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EC5Util.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public static ECCurve convertCurve(
    EllipticCurve ec)
{
    ECField field = ec.getField();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    if (field instanceof ECFieldFp)
    {
        ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);

        if (customCurves.containsKey(curve))
        {
            return (ECCurve)customCurves.get(curve);
        }

        return curve;
    }
    else
    {
        ECFieldF2m fieldF2m = (ECFieldF2m)field;
        int m = fieldF2m.getM();
        int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
        return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b); 
    }
}
 
Example #2
Source File: EC5Util.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
public static ECCurve convertCurve(
    EllipticCurve ec)
{
    ECField field = ec.getField();
    BigInteger a = ec.getA();
    BigInteger b = ec.getB();

    if (field instanceof ECFieldFp)
    {
        ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);

        if (customCurves.containsKey(curve))
        {
            return (ECCurve)customCurves.get(curve);
        }

        return curve;
    }
    else
    {
        ECFieldF2m fieldF2m = (ECFieldF2m)field;
        int m = fieldF2m.getM();
        int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
        return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b); 
    }
}
 
Example #3
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static void printParameters(ECParameterSpec spec) {
  System.out.println("cofactor:" + spec.getCofactor());
  EllipticCurve curve = spec.getCurve();
  System.out.println("A:" + curve.getA());
  System.out.println("B:" + curve.getB());
  ECField field = curve.getField();
  System.out.println("field size:" + field.getFieldSize());
  if (field instanceof ECFieldFp) {
    ECFieldFp fp = (ECFieldFp) field;
    System.out.println("P:" + fp.getP());
  }
  ECPoint generator = spec.getGenerator();
  System.out.println("Gx:" + generator.getAffineX());
  System.out.println("Gy:" + generator.getAffineY());
  System.out.println("order:" + spec.getOrder());
}
 
Example #4
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Decompress a point
 *
 * @param x The x-coordinate of the point
 * @param bit0 true if the least significant bit of y is set.
 * @param ecParams contains the curve of the point. This must be over a prime order field.
 */
public static ECPoint getPoint(BigInteger x, boolean bit0, ECParameterSpec ecParams)
    throws GeneralSecurityException {
  EllipticCurve ec = ecParams.getCurve();
  ECField field = ec.getField();
  if (!(field instanceof ECFieldFp)) {
    throw new GeneralSecurityException("Only curves over prime order fields are supported");
  }
  BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
  if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
    throw new GeneralSecurityException("x is out of range");
  }
  // Compute rhs == x^3 + a x + b (mod p)
  BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
  BigInteger y = modSqrt(rhs, p);
  if (bit0 != y.testBit(0)) {
    y = p.subtract(y).mod(p);
  }
  return new ECPoint(x, y);
}
 
Example #5
Source File: EcUtil.java    From wycheproof with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the modulus of the field used by the curve specified in ecParams.
 *
 * @param curve must be a prime order elliptic curve
 * @return the order of the finite field over which curve is defined.
 */
public static BigInteger getModulus(EllipticCurve curve) throws GeneralSecurityException {
  java.security.spec.ECField field = curve.getField();
  if (field instanceof java.security.spec.ECFieldFp) {
    return ((java.security.spec.ECFieldFp) field).getP();
  } else {
    throw new GeneralSecurityException("Only curves over prime order fields are supported");
  }
}
 
Example #6
Source File: BaseTestSupport.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void assertECFieldEquals(String message, ECField expected, ECField actual) {
    if (expected == actual) {
        return;
    }

    assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize());
}
 
Example #7
Source File: GenerationTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static ECParameterSpec initECParams(
        String sfield, String a, String b, String gx, String gy,
        String n, int h) {
    ECField field = new ECFieldFp(bigInt(sfield));
    EllipticCurve curve = new EllipticCurve(field,
                                            bigInt(a), bigInt(b));
    ECPoint g = new ECPoint(bigInt(gx), bigInt(gy));
    return new ECParameterSpec(curve, g, bigInt(n), h);
}
 
Example #8
Source File: DOMKeyValue.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static Curve initializeCurve(String name, String oid,
        String sfield, String a, String b,
        String x, String y, String n, int h) {
    BigInteger p = bigInt(sfield);
    ECField field = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(field, bigInt(a),
                                            bigInt(b));
    ECPoint g = new ECPoint(bigInt(x), bigInt(y));
    return new Curve(name, oid, curve, g, bigInt(n), h);
}
 
Example #9
Source File: EcUtil.java    From wycheproof with Apache License 2.0 5 votes vote down vote up
/**
 * Decompress a point on an elliptic curve.
 *
 * @param bytes The compressed point. Its representation is z || x where z is 2+lsb(y) and x is
 *     using a unsigned fixed length big-endian representation.
 * @param ecParams the specification of the curve. Only Weierstrass curves over prime order fields
 *     are implemented.
 */
public static ECPoint decompressPoint(byte[] bytes, ECParameterSpec ecParams)
    throws GeneralSecurityException {
  EllipticCurve ec = ecParams.getCurve();
  ECField field = ec.getField();
  if (!(field instanceof ECFieldFp)) {
    throw new GeneralSecurityException("Only curves over prime order fields are supported");
  }
  BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
  int expectedLength = 1 + (p.bitLength() + 7) / 8;
  if (bytes.length != expectedLength) {
    throw new GeneralSecurityException("compressed point has wrong length");
  }
  boolean lsb;
  switch (bytes[0]) {
    case 2:
      lsb = false;
      break;
    case 3:
      lsb = true;
      break;
    default:
      throw new GeneralSecurityException("Invalid format");
  }
  BigInteger x = new BigInteger(1, Arrays.copyOfRange(bytes, 1, bytes.length));
  if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
    throw new GeneralSecurityException("x is out of range");
  }
  // Compute rhs == x^3 + a x + b (mod p)
  BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
  BigInteger y = modSqrt(rhs, p);
  if (lsb != y.testBit(0)) {
    y = p.subtract(y).mod(p);
  }
  return new ECPoint(x, y);
}
 
Example #10
Source File: EcFieldDef.java    From swim with Apache License 2.0 5 votes vote down vote up
public static EcFieldDef from(ECField field) {
  if (field instanceof ECFieldFp) {
    return EcPrimeFieldDef.from((ECFieldFp) field);
  } else if (field instanceof ECFieldF2m) {
    return EcCharacteristic2FieldDef.from((ECFieldF2m) field);
  } else {
    throw new IllegalArgumentException(field.toString());
  }
}
 
Example #11
Source File: GenerationTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static ECParameterSpec initECParams(
        String sfield, String a, String b, String gx, String gy,
        String n, int h) {
    ECField field = new ECFieldFp(bigInt(sfield));
    EllipticCurve curve = new EllipticCurve(field,
                                            bigInt(a), bigInt(b));
    ECPoint g = new ECPoint(bigInt(gx), bigInt(gy));
    return new ECParameterSpec(curve, g, bigInt(n), h);
}
 
Example #12
Source File: DOMKeyValue.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Curve initializeCurve(String name, String oid,
        String sfield, String a, String b,
        String x, String y, String n, int h) {
    BigInteger p = bigInt(sfield);
    ECField field = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(field, bigInt(a),
                                            bigInt(b));
    ECPoint g = new ECPoint(bigInt(x), bigInt(y));
    return new Curve(name, oid, curve, g, bigInt(n), h);
}
 
Example #13
Source File: GenerationTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static ECParameterSpec initECParams(
        String sfield, String a, String b, String gx, String gy,
        String n, int h) {
    ECField field = new ECFieldFp(bigInt(sfield));
    EllipticCurve curve = new EllipticCurve(field,
                                            bigInt(a), bigInt(b));
    ECPoint g = new ECPoint(bigInt(gx), bigInt(gy));
    return new ECParameterSpec(curve, g, bigInt(n), h);
}
 
Example #14
Source File: DOMKeyValue.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Curve initializeCurve(String name, String oid,
        String sfield, String a, String b,
        String x, String y, String n, int h) {
    BigInteger p = bigInt(sfield);
    ECField field = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(field, bigInt(a),
                                            bigInt(b));
    ECPoint g = new ECPoint(bigInt(x), bigInt(y));
    return new Curve(name, oid, curve, g, bigInt(n), h);
}
 
Example #15
Source File: BaseTestSupport.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void assertECFieldEquals(String message, ECField expected, ECField actual) {
    if (expected == actual) {
        return;
    }

    assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize());
}
 
Example #16
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
MyEllipticCurve(ECField f, BigInteger a, BigInteger b, byte[] seed) {
    super(f, a, b, seed);
}
 
Example #17
Source File: EcFieldDef.java    From swim with Apache License 2.0 votes vote down vote up
public abstract ECField toECField();