java.security.spec.ECFieldFp Java Examples

The following examples show how to use java.security.spec.ECFieldFp. 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: ECOperations.java    From dragonwell8_jdk with GNU General Public License v2.0 7 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #2
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 #3
Source File: ECOperations.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
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 6 votes vote down vote up
public static ECParameterSpec getBrainpoolP256r1Params() {
  BigInteger p =
      new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", 16);
  BigInteger a =
      new BigInteger("7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", 16);
  BigInteger b =
      new BigInteger("26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", 16);
  BigInteger x =
      new BigInteger("8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", 16);
  BigInteger y =
      new BigInteger("547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", 16);
  BigInteger n =
      new BigInteger("A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  EllipticCurve curve = new EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(x, y);
  return new ECParameterSpec(curve, g, n, h);
}
 
Example #6
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getBrainpoolP224r1Params() {
  // name = "brainpoolP224r1",
  // oid = '2b2403030208010105',
  // ref = "RFC 5639",
  BigInteger p = new BigInteger("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF", 16);
  BigInteger a = new BigInteger("68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43", 16);
  BigInteger b = new BigInteger("2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B", 16);
  BigInteger x = new BigInteger("0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D", 16);
  BigInteger y = new BigInteger("58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD", 16);
  BigInteger n = new BigInteger("D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F", 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  EllipticCurve curve = new EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(x, y);
  return new ECParameterSpec(curve, g, n, h);
}
 
Example #7
Source File: EcUtil.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getNistCurveSpec(
    String decimalP, String decimalN, String hexB, String hexGX, String hexGY) {
  final BigInteger p = new BigInteger(decimalP);
  final BigInteger n = new BigInteger(decimalN);
  final BigInteger three = new BigInteger("3");
  final BigInteger a = p.subtract(three);
  final BigInteger b = new BigInteger(hexB, 16);
  final BigInteger gx = new BigInteger(hexGX, 16);
  final BigInteger gy = new BigInteger(hexGY, 16);
  final int h = 1;
  ECFieldFp fp = new ECFieldFp(p);
  java.security.spec.EllipticCurve curveSpec = new java.security.spec.EllipticCurve(fp, a, b);
  ECPoint g = new ECPoint(gx, gy);
  ECParameterSpec ecSpec = new ECParameterSpec(curveSpec, g, n, h);
  return ecSpec;
}
 
Example #8
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 #9
Source File: EcdhTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * Returns this key as ECPublicKeySpec or null if the key cannot be represented as
 * ECPublicKeySpec. The later happens for example if the order of cofactor are not positive.
 */
public ECPublicKeySpec getSpec() {
  try {
    ECFieldFp fp = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(fp, a, b);
    ECPoint g = new ECPoint(gx, gy);
    // ECParameterSpec requires that the cofactor h is specified.
    if (h == null) {
      return null;
    }
    ECParameterSpec params = new ECParameterSpec(curve, g, n, h);
    ECPoint pubPoint = new ECPoint(pubx, puby);
    ECPublicKeySpec pub = new ECPublicKeySpec(pubPoint, params);
    return pub;
  } catch (Exception ex) {
    System.out.println(comment + " throws " + ex.toString());
    return null;
  }
}
 
Example #10
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 #11
Source File: ECOperations.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #12
Source File: Util.java    From julongchain with Apache License 2.0 6 votes vote down vote up
public static ECParameterSpec getECParameterSpec() {
    BigInteger n = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "7203DF6B" + "21C6052B" + "53BBF409" + "39D54123", 16);
    BigInteger p = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "00000000" + "FFFFFFFF" + "FFFFFFFF", 16);
    BigInteger a = new BigInteger(
            "FFFFFFFE" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "FFFFFFFF" + "00000000" + "FFFFFFFF" + "FFFFFFFC", 16);
    BigInteger b = new BigInteger(
            "28E9FA9E" + "9D9F5E34" + "4D5A9E4B" + "CF6509A7" + "F39789F5" + "15AB8F92" + "DDBCBD41" + "4D940E93", 16);
    BigInteger gx = new BigInteger(
            "32C4AE2C" + "1F198119" + "5F990446" + "6A39C994" + "8FE30BBF" + "F2660BE1" + "715A4589" + "334C74C7", 16);
    BigInteger gy = new BigInteger(
            "BC3736A2" + "F4F6779C" + "59BDCEE3" + "6B692153" + "D0A9877C" + "C62A4740" + "02DF32E5" + "2139F0A0", 16);

    EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(p), a, b);
    return new ECParameterSpec(ellipticCurve, new ECPoint(gx, gy), n, 1);
}
 
Example #13
Source File: ECOperations.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static Optional<ECOperations> forParameters(ECParameterSpec params) {

        EllipticCurve curve = params.getCurve();
        if (!(curve.getField() instanceof ECFieldFp)) {
            return Optional.empty();
        }
        ECFieldFp primeField = (ECFieldFp) curve.getField();

        BigInteger three = BigInteger.valueOf(3);
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
            return Optional.empty();
        }
        IntegerFieldModuloP field = fields.get(primeField.getP());
        if (field == null) {
            return Optional.empty();
        }

        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
        if (orderField == null) {
            return Optional.empty();
        }

        ImmutableIntegerModuloP b = field.getElement(curve.getB());
        ECOperations ecOps = new ECOperations(b, orderField);
        return Optional.of(ecOps);
    }
 
Example #14
Source File: DNSSEC.java    From dnsjava with BSD 2-Clause "Simplified" License 6 votes vote down vote up
ECKeyInfo(
    int length,
    String p_str,
    String a_str,
    String b_str,
    String gx_str,
    String gy_str,
    String n_str) {
  this.length = length;
  p = new BigInteger(p_str, 16);
  a = new BigInteger(a_str, 16);
  b = new BigInteger(b_str, 16);
  gx = new BigInteger(gx_str, 16);
  gy = new BigInteger(gy_str, 16);
  n = new BigInteger(n_str, 16);
  curve = new EllipticCurve(new ECFieldFp(p), a, b);
  spec = new ECParameterSpec(curve, new ECPoint(gx, gy), n, 1);
}
 
Example #15
Source File: ECFieldFpTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #4 for <code>ECFieldFp</code> constructor
 *
 * Assertion: NullPointerException if <code>p</code> is null
 */
public final void testECFieldFp05() {
    try {
        new ECFieldFp(null);
        fail(getName() +
                " FAILED: expected exception has not been thrown");
    } catch (NullPointerException e) {
    }
}
 
Example #16
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #2 for <code>getSeed()</code> method<br>
 * Assertion: returned array is copied to prevent subsequent modification<br>
 * Test preconditions: <code>ECFieldF2m</code> instance
 * created using valid parameters; <code>getSeed()</code>
 * called and then returned array modified<br>
 * Expected: internal state must not be affected by the modification
 */
public final void testGetSeed02() {
    ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
    BigInteger a = BigInteger.ONE;
    BigInteger b = BigInteger.valueOf(19L);
    byte[] seed = new byte[24];
    EllipticCurve c = new EllipticCurve(f, a, b, seed.clone());
    byte[] seedRet = c.getSeed();
    // modify returned array
    seedRet[0] = (byte) 1;
    // check that above modification did not changed
    // internal state of test object
    assertTrue(Arrays.equals(seed, c.getSeed()));
}
 
Example #17
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #3 for <code>getSeed()</code> method<br>
 * Assertion: returned array is copied to prevent subsequent modification<br>
 * Test preconditions: <code>ECFieldF2m</code> instance
 * created using valid parameters<br>
 * Expected: repeated method calls must return different refs
 */
public final void testGetSeed03() {
    ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
    BigInteger a = BigInteger.ONE;
    BigInteger b = BigInteger.valueOf(19L);
    byte[] seed = new byte[24];
    EllipticCurve c = new EllipticCurve(f, a, b, seed);
    c.getSeed();
    assertNotSame(c.getSeed(), c.getSeed());
}
 
Example #18
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.security.spec.EllipticCurve#getSeed()
 * Assertion: null if not specified
 */
public final void testGetSeed04() {
    //Regression for HARMONY-732
    ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
    BigInteger a = BigInteger.ONE;
    assertNull(new EllipticCurve(f, a, a).getSeed());
}
 
Example #19
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #1 for <code>hashCode()</code> method.<br>
 *
 * Assertion: must return the same value if invoked
 * repeatedly on the same object.
 */
public final void testHashCode01() {
    int hc = 0;
    EllipticCurve f = new EllipticCurve(new ECFieldFp(BigInteger
            .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
            new byte[24]);
    hc = f.hashCode();
    assertTrue(hc == f.hashCode() && hc == f.hashCode()
            && hc == f.hashCode() && hc == f.hashCode()
            && hc == f.hashCode() && hc == f.hashCode()
            && hc == f.hashCode() && hc == f.hashCode());
}
 
Example #20
Source File: EllipticCurveTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #2 for <code>hashCode()</code> method.<br>
 *
 * Assertion: must return the same value if invoked
 * on equal (according to the <code>equals(Object)</code> method) objects.
 */
public final void testHashCode02() {
    assertEquals(new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
            BigInteger.ONE, BigInteger.valueOf(19L), new byte[24])
            .hashCode(), new EllipticCurve(new ECFieldFp(BigInteger
            .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
            new byte[24]).hashCode());
}
 
Example #21
Source File: ECFieldFpTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #3 for <code>ECFieldFp</code> constructor
 *
 * Assertion: IllegalArgumentException if <code>p</code> is not positive
 */
public final void testECFieldFp03() {
    try {
        new ECFieldFp(BigInteger.valueOf(-1L));
        fail(getName() +
                " FAILED: expected exception has not been thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example #22
Source File: ECTestBC.java    From EccPlayground with Apache License 2.0 5 votes vote down vote up
@Test
   public void generatePoints() {

// curve definition
String a = "1";
String b = "0";
String q = "23"; // modulus in the elliptic curve

// base point initialization
String basePointX = "1";
String basePointY = "5";
String order = "23";

// private key
String privateKey = "6";

// public key points
String publicKeyX = "11";
String publicKeyY = "1";

// y^2 = x^3 + ax + b mod q
EllipticCurve curve = new EllipticCurve(new ECFieldFp(new BigInteger(q)), // q
									  // (modulus)
	new BigInteger(a), // a
	new BigInteger(b)); // b

ECParameterSpec spec = new ECParameterSpec(curve, new ECPoint(new BigInteger(basePointX), new BigInteger(
	basePointY)), // G
	new BigInteger(order), // n
	1); // h
   }
 
Example #23
Source File: ECFieldFpTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #4 for <code>ECFieldFp</code> constructor
 *
 * Assertion: IllegalArgumentException if <code>p</code> is not positive
 */
public final void testECFieldFp04() {
    try {
        new ECFieldFp(BigInteger.valueOf(0L));
        fail(getName() +
                " FAILED: expected exception has not been thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example #24
Source File: EC5Util.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public static EllipticCurve convertCurve(
    ECCurve curve, 
    byte[]  seed)
{
    // TODO: the Sun EC implementation doesn't currently handle the seed properly
    // so at the moment it's set to null. Should probably look at making this configurable
    if (ECAlgorithms.isFpCurve(curve))
    {
        return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
    }
    else
    {
        ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
        int ks[];
        
        if (curveF2m.isTrinomial())
        {
            ks = new int[] { curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        }
        else
        {
            ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        } 
    }
}
 
Example #25
Source File: ECNamedCurveSpec.java    From ripple-lib-java with ISC License 5 votes vote down vote up
private static EllipticCurve convertCurve(
    ECCurve  curve,
    byte[]   seed)
{
    if (ECAlgorithms.isFpCurve(curve))
    {
        return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
    }
    else
    {
        ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
        int ks[];
        
        if (curveF2m.isTrinomial())
        {
            ks = new int[] { curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
        }
        else
        {
            ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };

            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
        } 
    }

}
 
Example #26
Source File: ECPointUtil.java    From ripple-lib-java with ISC License 5 votes vote down vote up
/**
 * Decode a point on this curve which has been encoded using point
 * compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
 * 
 * @param curve
 *            The elliptic curve.
 * @param encoded
 *            The encoded point.
 * @return the decoded point.
 */
public static ECPoint decodePoint(
   EllipticCurve curve, 
   byte[] encoded)
{
    ECCurve c = null;
    
    if (curve.getField() instanceof ECFieldFp)
    {
        c = new ECCurve.Fp(
                ((ECFieldFp)curve.getField()).getP(), curve.getA(), curve.getB());
    }
    else
    {
        int k[] = ((ECFieldF2m)curve.getField()).getMidTermsOfReductionPolynomial();
        
        if (k.length == 3)
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[2], k[1], k[0], curve.getA(), curve.getB());
        }
        else
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[0], curve.getA(), curve.getB());
        }
    }
    
    org.ripple.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);

    return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger());
}
 
Example #27
Source File: ECFieldFpTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #1 for <code>hashCode()</code> method.<br>
 *
 * Assertion: must return the same value if invoked
 * repeatedly on the same object.
 */
public final void testHashCode01() {
    ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
    int hc = f.hashCode();
    assertTrue(hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode() &&
               hc == f.hashCode());
}
 
Example #28
Source File: ECPointUtil.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * Decode a point on this curve which has been encoded using point
 * compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
 * 
 * @param curve
 *            The elliptic curve.
 * @param encoded
 *            The encoded point.
 * @return the decoded point.
 */
public static ECPoint decodePoint(
   EllipticCurve curve, 
   byte[] encoded)
{
    ECCurve c = null;
    
    if (curve.getField() instanceof ECFieldFp)
    {
        c = new ECCurve.Fp(
                ((ECFieldFp)curve.getField()).getP(), curve.getA(), curve.getB());
    }
    else
    {
        int k[] = ((ECFieldF2m)curve.getField()).getMidTermsOfReductionPolynomial();
        
        if (k.length == 3)
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[2], k[1], k[0], curve.getA(), curve.getB());
        }
        else
        {
            c = new ECCurve.F2m(
                    ((ECFieldF2m)curve.getField()).getM(), k[0], curve.getA(), curve.getB());
        }
    }
    
    org.ripple.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);

    return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger());
}
 
Example #29
Source File: ECNamedCurveSpec.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private static EllipticCurve convertCurve(
    ECCurve  curve,
    byte[]   seed)
{
    if (ECAlgorithms.isFpCurve(curve))
    {
        return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
    }
    else
    {
        ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
        int ks[];
        
        if (curveF2m.isTrinomial())
        {
            ks = new int[] { curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
        }
        else
        {
            ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };

            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
        } 
    }

}
 
Example #30
Source File: EC5Util.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static EllipticCurve convertCurve(
    ECCurve curve, 
    byte[]  seed)
{
    // TODO: the Sun EC implementation doesn't currently handle the seed properly
    // so at the moment it's set to null. Should probably look at making this configurable
    if (ECAlgorithms.isFpCurve(curve))
    {
        return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
    }
    else
    {
        ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
        int ks[];
        
        if (curveF2m.isTrinomial())
        {
            ks = new int[] { curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        }
        else
        {
            ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
            
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        } 
    }
}