public static ECParameterSpec encodeECParameterSpec(EllipticCurveParameters params) { // Field final BigInteger pInt = new BigInteger(1, params.getP()); final ECField field = new ECFieldFp(pInt); final BigInteger aInt = new BigInteger(1, params.getA()); final BigInteger bInt = new BigInteger(1, params.getB()); final EllipticCurve curve = new EllipticCurve(field, aInt, bInt); // Fixed Point G final BigInteger xInt = new BigInteger(1, params.getX()); final BigInteger yInt = new BigInteger(1, params.getY()); final ECPoint g = new ECPoint(xInt, yInt); // Order N final BigInteger nInt = new BigInteger(1, params.getN()); return new ECParameterSpec(curve, g, nInt, params.getH()); }
private static BigInteger[] addPointsA(BigInteger[] P1, BigInteger[] P2, ECParameterSpec params) { final BigInteger p = ((ECFieldFp) params.getCurve().getField()).getP(); if (P2[0] == null || P2[1] == null) return P1; if (P1[0] == null || P1[1] == null) return P2; BigInteger d = (P2[1].subtract(P1[1])).multiply((P2[0].subtract(P1[0])) .modInverse(p)); BigInteger[] R = new BigInteger[2]; R[0] = d.pow(2).subtract(P1[0]).subtract(P2[0]).mod(p); R[1] = d.multiply(P1[0].subtract(R[0])).subtract(P1[1]).mod(p); return R; }
private static ECCurve convertCurve( EllipticCurve ec) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b); } else { throw new IllegalStateException("not implemented yet!!!"); } }
public static ECCurve convertCurve( EllipticCurve ec) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b); } 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); } }
public static String getCurveName(ECParameterSpec spec) throws InvalidAlgorithmParameterException { int curve_id; /* Ecc object doesn't need to be initialied before call */ if (!(spec.getCurve().getField() instanceof ECFieldFp)) { throw new InvalidAlgorithmParameterException( "Currently only ECFieldFp fields supported"); } ECFieldFp field = (ECFieldFp)spec.getCurve().getField(); EllipticCurve curve = spec.getCurve(); curve_id = wc_ecc_get_curve_id_from_params( field.getFieldSize(), field.getP().toByteArray(), curve.getA().toByteArray(), curve.getB().toByteArray(), spec.getOrder().toByteArray(), spec.getGenerator().getAffineX().toByteArray(), spec.getGenerator().getAffineY().toByteArray(), spec.getCofactor()); return wc_ecc_get_curve_name_from_id(curve_id); }
private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) { // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H final ECPoint generator = params.getGenerator(); final EllipticCurve curve = params.getCurve(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final ECFieldFp field = (ECFieldFp)curve.getField(); final BigInteger p = field.getP(); final BigInteger order = params.getOrder(); final int cofactor = params.getCofactor(); final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params); if (!toBouncyCastleECPoint(ephemeralGenerator, params).isValid()) { LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$ } return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor); }
private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) { // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H final ECPoint generator = params.getGenerator(); final EllipticCurve curve = params.getCurve(); final BigInteger a = curve.getA(); final BigInteger b = curve.getB(); final ECFieldFp field = (ECFieldFp)curve.getField(); final BigInteger p = field.getP(); final BigInteger order = params.getOrder(); final int cofactor = params.getCofactor(); final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params); if (!toSpongyCastleECPoint(ephemeralGenerator, params).isValid()) { LOGGER.warning("Se ha generado un punto invalido"); //$NON-NLS-1$ } return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor); }
public static ECParameterSpec CfcaCurve() { // 素数P BigInteger p = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16); // 基于素数P的有限域 ECFieldFp gfp = new ECFieldFp(p); // 在有限域上的椭圆曲线y2 = x3 + ax + b EllipticCurve ellipticCurve = new EllipticCurve(gfp, new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16), new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16)); // 基点G ECPoint G = new ECPoint(new BigInteger("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16), new BigInteger("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16)); // G的阶 BigInteger n = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16); // 设置基点 ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, G, n, 1); return ecParameterSpec; }
ECParameterSpec getECParameterSpec() { final String curveName = NativeCrypto.EC_GROUP_get_curve_name(groupCtx); final byte[][] curveParams = NativeCrypto.EC_GROUP_get_curve(groupCtx); final BigInteger p = new BigInteger(curveParams[0]); final BigInteger a = new BigInteger(curveParams[1]); final BigInteger b = new BigInteger(curveParams[2]); final ECField field = new ECFieldFp(p); final EllipticCurve curve = new EllipticCurve(field, a, b); final OpenSSLECPointContext generatorCtx = new OpenSSLECPointContext(this, new NativeRef.EC_POINT(NativeCrypto.EC_GROUP_get_generator(groupCtx))); final ECPoint generator = generatorCtx.getECPoint(); final BigInteger order = new BigInteger(NativeCrypto.EC_GROUP_get_order(groupCtx)); final BigInteger cofactor = new BigInteger(NativeCrypto.EC_GROUP_get_cofactor(groupCtx)); ECParameterSpec spec = new ECParameterSpec(curve, generator, order, cofactor.intValue()); Platform.setCurveName(spec, curveName); return spec; }
/** * 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; } }
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; }
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); }
/** * 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); }
private static ECCurve convertCurve( EllipticCurve ec, BigInteger order, int coFactor) { ECField field = ec.getField(); BigInteger a = ec.getA(); BigInteger b = ec.getB(); if (field instanceof ECFieldFp) { return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b, order, BigInteger.valueOf(coFactor)); } else { throw new IllegalStateException("not implemented yet!!!"); } }
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); } }
private void decodeTest() { EllipticCurve curve = new EllipticCurve( new ECFieldFp(new BigInteger("6277101735386680763835789423207666416083908700390324961279")), // q new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b ECPoint p = ECPointUtil.decodePoint(curve, Hex.decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")); if (!p.getAffineX().equals(new BigInteger("188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012", 16))) { fail("x uncompressed incorrectly"); } if (!p.getAffineY().equals(new BigInteger("7192b95ffc8da78631011ed6b24cdd573f977a11e794811", 16))) { fail("y uncompressed incorrectly"); } }
/** * Test #1 for <code>getSeed()</code> method<br> * Assertion: returns <code>seed</code><br> * Test preconditions: <code>ECFieldF2m</code> instance * created using valid parameters<br> * Expected: must return <code>seed</code> which is equal * to the one passed to the constructor */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies positive case.", method = "getSeed", args = {} ) public final void testGetSeed01() { 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); byte[] seedRet = c.getSeed(); assertNotNull(seedRet); assertTrue(Arrays.equals(seed, seedRet)); }
/** * 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 */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that modification of byte array doesn't change internal state of test object.", method = "getSeed", args = {} ) 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())); }
/** * 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 */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that repeated calls of getSeed method must return different refs.", method = "getSeed", args = {} ) 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()); }
/** * Test #1 for <code>hashCode()</code> method.<br> * * Assertion: must return the same value if invoked * repeatedly on the same object. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that several calls of hashCode method for the same objects return the same values.", method = "hashCode", args = {} ) 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()); }
/** * Test #1 for <code>hashCode()</code> method.<br> * * Assertion: must return the same value if invoked * repeatedly on the same object. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "hashCode", args = {} ) 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()); }
private EcData(ECKey key, ECPoint q, BigInteger x) { ECParameterSpec params = key.getParams(); EllipticCurve curve = params.getCurve(); curveModulus = ((ECFieldFp) curve.getField()).getP().toByteArray(); curveA = curve.getA().toByteArray(); curveB = curve.getB().toByteArray(); gX = params.getGenerator().getAffineX().toByteArray(); gY = params.getGenerator().getAffineY().toByteArray(); n = params.getOrder().toByteArray(); if (q == null) { qX = null; qY = null; } else { qX = q.getAffineX().toByteArray(); qY = q.getAffineY().toByteArray(); } this.x = x == null ? null : x.toByteArray(); }
/** * Test #3 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br> * Assertion: throws <code>IllegalArgumentException</code> if * <code>w</code> is point at infinity<br> * Test preconditions: pass <code>ECPoint.POINT_INFINITY</code> * as mentioned parameter value<br> * Expected: must throw <code>IllegalArgumentException</code> */ public final void testECPublicKeySpec03() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); try { new ECPublicKeySpec(ECPoint.POINT_INFINITY, new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10)); fail("Expected IAE not thrown"); } catch (IllegalArgumentException ok) { } }
/** * Test for <code>getParams()</code> method<br> * Assertion: returns associated EC parameters<br> * Test preconditions: <code>ECPublicKeySpec</code> instance * created using valid parameters<br> * Expected: must return params value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetParams() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); ECPublicKeySpec ks = new ECPublicKeySpec(g, params); ECParameterSpec paramsRet = ks.getParams(); assertEquals(params, paramsRet); assertSame(params, paramsRet); }
/** * Test for <code>getW()</code> method<br> * Assertion: returns associated public point<br> * Test preconditions: <code>ECPublicKeySpec</code> instance * created using valid parameters<br> * Expected: must return w value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetW() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); ECPublicKeySpec ks = new ECPublicKeySpec(g, params); ECPoint wRet = ks.getW(); assertEquals(g, wRet); assertSame(g, wRet); }
/** * Test for <code>getCurve()</code> method<br> * Assertion: returns curve<br> * Test preconditions: <code>ECParameterSpec</code> instance * created using valid parameters<br> * Expected: must return ref to the <code>EllipticCurve</code> object * which is equal to the one passed to the constructor; (both must refer * the same object) */ public final void testGetCurve() { // Valid (see note below) parameters set EllipticCurve curve = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); BigInteger order = BigInteger.valueOf(5L); int cofactor = 10; ECParameterSpec ps = new ECParameterSpec(curve, generator, order, cofactor); EllipticCurve curveRet = ps.getCurve(); assertEquals(curve, curveRet); assertSame(curve, curveRet); }
/** * Test for <code>getGenerator()</code> method<br> * Assertion: returns generator<br> * Test preconditions: <code>ECParameterSpec</code> instance * created using valid parameters<br> * Expected: must return ref to the <code>ECPoint</code> object * which is equal to the one passed to the constructor; (both must refer * the same object) */ public final void testGetGenerator() { // Valid (see note below) parameters set EllipticCurve curve = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); BigInteger order = BigInteger.valueOf(5L); int cofactor = 10; ECParameterSpec ps = new ECParameterSpec(curve, generator, order, cofactor); ECPoint generatorRet = ps.getGenerator(); assertEquals(generator, generatorRet); assertSame(generator, generatorRet); }
/** * Test for <code>getOrder()</code> method<br> * Assertion: returns order<br> * Test preconditions: <code>ECParameterSpec</code> instance * created using valid parameters<br> * Expected: must return ref to the <code>BigInteger</code> object * which is equal to the one passed to the constructor; (both must refer * the same object) */ public final void testGetOrder() { // Valid (see note below) parameters set EllipticCurve curve = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); BigInteger order = BigInteger.valueOf(5L); int cofactor = 10; ECParameterSpec ps = new ECParameterSpec(curve, generator, order, cofactor); BigInteger orderRet = ps.getOrder(); assertEquals(order, orderRet); assertSame(order, orderRet); }
/** * Test for <code>getParams()</code> method<br> * Assertion: returns associated EC parameters<br> * Test preconditions: <code>ECPrivateKeySpec</code> instance * created using valid parameters<br> * Expected: must return params value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetParams() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); ECPrivateKeySpec ks = new ECPrivateKeySpec(BigInteger.ZERO, params); ECParameterSpec paramsRet = ks.getParams(); assertEquals(params, paramsRet); assertSame(params, paramsRet); }
/** * Test for <code>getS()</code> method<br> * Assertion: returns associated private value<br> * Test preconditions: <code>ECPrivateKeySpec</code> instance * created using valid parameters<br> * Expected: must return s value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetS() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); BigInteger s = BigInteger.valueOf(5L); ECPrivateKeySpec ks = new ECPrivateKeySpec(s, params); BigInteger sRet = ks.getS(); assertEquals(s, sRet); assertSame(s, sRet); }
public static ECPoint scalarMult(ECPoint p, BigInteger kin, EllipticCurve curve) { ECPoint r = ECPoint.POINT_INFINITY; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); BigInteger k = kin.mod(prime); int length = k.bitLength(); byte[] binarray = new byte[length]; for (int i = 0; i <= length-1; i++) { binarray[i] = k.mod(TWO).byteValue(); k = k.divide(TWO); } for (int i = length-1; i >= 0; i--) { // i should start at length-1 not -2 because the MSB of binarry may not be 1 r = doublePoint(r, curve); if (binarray[i] == 1) r = addPoint(r, p, curve); } return r; }
private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) { if (r.equals(s)) return doublePoint(r, curve); else if (r.equals(ECPoint.POINT_INFINITY)) return s; else if (s.equals(ECPoint.POINT_INFINITY)) return r; BigInteger prime = ((ECFieldFp) curve.getField()).getP(); BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(r.getAffineX().subtract(s.getAffineX()).modInverse(prime)).mod(prime); slope = new NativeBigInteger(slope); BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime); BigInteger yOut = s.getAffineY().negate().mod(prime); yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
private 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; }