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 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); } }
/** * Tests for constructor <code>ECFieldF2m(int)</code><br> * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameter m. * * Assertion: IllegalArgumentException if m is not positive. */ public final void testECFieldF2mint() { for(int i=0; i<intCtorTestParameters.length; i++) { ECFieldF2mDomainParams tp = intCtorTestParameters[i]; try { // perform test new ECFieldF2m(tp.m); if (tp.x != null) { // exception has been expected fail(getName() + ", set " + i + " FAILED: expected exception has not been thrown"); } } catch (Exception e){ if (tp.x == null || !e.getClass().isInstance(tp.x)) { // exception: failure // if it has not been expected // or wrong one has been thrown fail(getName() + ", set " + i + " FAILED: unexpected " + e); } } } }
/** * Tests for constructor <code>ECFieldF2m(int m, int[] ks)</code><br> * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. ks represents trinomial basis. * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and ks. ks represents pentanomial basis. * * Assertion: IllegalArgumentException if m is not positive. * * Assertion: NullPointerException if ks is null. * * Assertion: IllegalArgumentException if ks is invalid. */ public final void testECFieldF2mintintArray() { for(int i=0; i<constructorTestParameters.length; i++) { ECFieldF2mDomainParams tp = constructorTestParameters[i]; try { // perform test ECFieldF2m test = new ECFieldF2m(tp.m, tp.ks); if (tp.x != null) { // exception has been expected fail(getName() + ", set " + i + " FAILED: expected exception has not been thrown"); } } catch (Exception e){ if (tp.x == null || !e.getClass().isInstance(tp.x)) { // exception: failure // if it has not been expected // or wrong one has been thrown fail(getName() + ", set " + i + " FAILED: unexpected " + e); } } } }
/** * Tests for constructor <code>ECFieldF2m(int m, BigInteger rp)</code><br> * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. * * Assertion: IllegalArgumentException if m is not positive. * * Assertion: NullPointerException if rp is null. * * Assertion: IllegalArgumentException if rp is invalid. */ public final void testECFieldF2mintBigInteger() { for(int i=0; i<constructorTestParameters.length; i++) { ECFieldF2mDomainParams tp = constructorTestParameters[i]; try { // perform test new ECFieldF2m(tp.m, tp.rp); if (tp.x != null) { // exception has been expected fail(getName() + ", set " + i + " FAILED: expected exception has not been thrown"); } } catch (Exception e){ if (tp.x == null || !e.getClass().isInstance(tp.x)) { // exception: failure // if it has not been expected // or wrong one has been thrown fail(getName() + ", set " + i + " FAILED: unexpected " + e); } } } }
/** * Test #5 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code> * constructor<br> * Assertion: array <code>seed</code> is copied to prevent subsequent modification<br> * Test preconditions: pass <code>seed</code> to the ctor then modify it<br> * Expected: getSeed() must return unmodified array */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies that byte array of EllipticCurve can't be modified", method = "EllipticCurve", args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class} ) public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray05() { ECFieldF2m f = new ECFieldF2m(5); BigInteger a = BigInteger.valueOf(0L); BigInteger b = BigInteger.valueOf(19L); byte[] seed = new byte[24]; byte[] seedCopy = seed.clone(); EllipticCurve c = new EllipticCurve(f, a, b, seedCopy); // modify array passed seedCopy[0] = (byte) 1; // check that above modification did not changed // internal state of test object assertTrue(Arrays.equals(seed, 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 = "", method = "hashCode", args = {} ) public final void testHashCode01() { ECFieldF2m f = new ECFieldF2m(2000); 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()); }
/** * Test #2 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 testHashCode02() { ECFieldF2m f = new ECFieldF2m(2000, new int[] {981, 2, 1}); 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()); }
/** * Tests that object state is preserved against modifications * through array reference passed to the constructor. */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "Verifies that object state is preserved against modifications through array reference passed to the constructor.", method = "ECFieldF2m", args = {int.class, int[].class} ) public final void testIsStatePreserved01() { // reference array int[] a = new int[] {367}; // reference array copy int[] aCopy = a.clone(); // create obj using copy ECFieldF2m f = new ECFieldF2m(1999, aCopy); // modify copy aCopy[0] = 5; // compare reference with returned array assertTrue(Arrays.equals(a, f.getMidTermsOfReductionPolynomial())); }
/** * Tests for constructor <code>ECFieldF2m(int m, int[] ks)</code><br> * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. ks represents trinomial basis. * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and ks. ks represents pentanomial basis. * * Assertion: IllegalArgumentException if m is not positive. * * Assertion: NullPointerException if ks is null. * * Assertion: IllegalArgumentException if ks is invalid. */ public final void testECFieldF2mintintArray() { for(int i=0; i<intIntArrayCtorTestParameters.length; i++) { ECFieldF2mDomainParams tp = intIntArrayCtorTestParameters[i]; try { // perform test new ECFieldF2m(tp.m, tp.ks); if (tp.x != null) { // exception has been expected fail(getName() + ", set " + i + " FAILED: expected exception has not been thrown"); } } catch (Exception e){ if (tp.x == null || !e.getClass().isInstance(tp.x)) { // exception: failure // if it has not been expected // or wrong one has been thrown fail(getName() + ", set " + i + " FAILED: unexpected " + e); } } } }
/** * Tests for constructor <code>ECFieldF2m(int, BigInteger)</code><br> * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. rp represents trinomial basis. * * Assertion: constructs new <code>ECFieldF2m</code> object * using valid parameters m and rp. rp represents pentanomial basis. * * Assertion: IllegalArgumentException if m is not positive. * * Assertion: NullPointerException if rp is null. * * Assertion: IllegalArgumentException if rp is invalid. */ public final void testECFieldF2mintBigInteger() { for(int i=0; i<intBigIntegerCtorTestParameters.length; i++) { ECFieldF2mDomainParams tp = intBigIntegerCtorTestParameters[i]; try { // perform test new ECFieldF2m(tp.m, tp.rp); if (tp.x != null) { // exception has been expected fail(getName() + ", set " + i + " FAILED: expected exception has not been thrown"); } } catch (Exception e){ if (tp.x == null || !e.getClass().isInstance(tp.x)) { // exception: failure // if it has not been expected // or wrong one has been thrown fail(getName() + ", set " + i + " FAILED: unexpected " + e); } } } }
/** * 初始化密钥 * * @return * @throws Exception */ public static Map<String, Object> initKey() throws Exception { BigInteger x1 = new BigInteger( "2fe13c0537bbc11acaa07d793de4e6d5e5c94eee8", 16); BigInteger x2 = new BigInteger( "289070fb05d38ff58321f2e800536d538ccdaa3d9", 16); ECPoint g = new ECPoint(x1, x2); // the order of generator BigInteger n = new BigInteger( "5846006549323611672814741753598448348329118574063", 10); // the cofactor int h = 2; int m = 163; int[] ks = { 7, 6, 3 }; ECFieldF2m ecField = new ECFieldF2m(m, ks); // y^2+xy=x^3+x^2+1 BigInteger a = new BigInteger("1", 2); BigInteger b = new BigInteger("1", 2); EllipticCurve ellipticCurve = new EllipticCurve(ecField, a, b); ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, g, n, h); // 公钥 ECPublicKey publicKey = new ECPublicKeyImpl(g, ecParameterSpec); BigInteger s = new BigInteger( "1234006549323611672814741753598448348329118574063", 10); // 私钥 ECPrivateKey privateKey = new ECPrivateKeyImpl(s, ecParameterSpec); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
/** * 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.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded); return new ECPoint(p.getX().toBigInteger(), p.getY().toBigInteger()); }
private static EllipticCurve convertCurve( ECCurve curve, byte[] seed) { if (curve instanceof ECCurve.Fp) { return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), 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); } } }
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 (curve instanceof ECCurve.Fp) { return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), 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); } } }
/** * 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.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded); return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger()); }
private static ECField convertField(FiniteField field) { if (ECAlgorithms.isFpField(field)) { return new ECFieldFp(field.getCharacteristic()); } else //if (ECAlgorithms.isF2mField(curveField)) { Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial(); int[] exponents = poly.getExponentsPresent(); int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1)); return new ECFieldF2m(poly.getDegree(), ks); } }
public static ECField convertField(FiniteField field) { if (ECAlgorithms.isFpField(field)) { return new ECFieldFp(field.getCharacteristic()); } else //if (ECAlgorithms.isF2mField(curveField)) { Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial(); int[] exponents = poly.getExponentsPresent(); int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1)); return new ECFieldF2m(poly.getDegree(), ks); } }
/** * Test #5 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code> * constructor<br> * Assertion: array <code>seed</code> is copied to prevent subsequent modification<br> * Test preconditions: pass <code>seed</code> to the ctor then modify it<br> * Expected: getSeed() must return unmodified array */ public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray05() { ECFieldF2m f = new ECFieldF2m(5); BigInteger a = BigInteger.valueOf(0L); BigInteger b = BigInteger.valueOf(19L); byte[] seed = new byte[24]; byte[] seedCopy = seed.clone(); EllipticCurve c = new EllipticCurve(f, a, b, seedCopy); // modify array passed seedCopy[0] = (byte) 1; // check that above modification did not changed // internal state of test object assertTrue(Arrays.equals(seed, c.getSeed())); }
protected void setUp() throws Exception { super.setUp(); ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger .valueOf(1)); EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger .valueOf(1), BigInteger.valueOf(1)); w = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1)); params = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1); ecpks = new ECPublicKeySpec(w, params); }
protected void setUp() throws Exception { super.setUp(); ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger .valueOf(1)); EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger .valueOf(1), BigInteger.valueOf(1)); s = BigInteger.valueOf(1); ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1); ecpks = new ECPrivateKeySpec(s, ecparams); }
/** * 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() { ECFieldF2m f = new ECFieldF2m(2000); 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()); }
/** * Test #2 for <code>hashCode()</code> method.<br> * * Assertion: must return the same value if invoked * repeatedly on the same object. */ public final void testHashCode02() { ECFieldF2m f = new ECFieldF2m(2000, new int[] {981, 2, 1}); 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()); }
/** * Test #5 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 testHashCode05() { assertTrue(new ECFieldF2m(2000, new int[] {981, 2, 1}).hashCode() == new ECFieldF2m(2000, BigInteger.valueOf(0L). setBit(0).setBit(1).setBit(2). setBit(981).setBit(2000)).hashCode()); }
/** * Test #4 for <code>equals()</code> method.<br> * * Assertion: pentanomial basis - objects equal if their m, and rp * are mutually equal. */ public final void testEqualsObject04() { ECFieldF2m f1 = new ECFieldF2m(2000, new int[] {981, 2, 1}); ECFieldF2m f2 = new ECFieldF2m(2000, BigInteger.valueOf(0L). setBit(0).setBit(1).setBit(2). setBit(981).setBit(2000)); assertTrue(f1.equals(f2) && f2.equals(f1)); }
/** * Test #5 for <code>equals()</code> method.<br> * * Assertion: objects equal if their m, and rp are mutually equal. */ public final void testEqualsObject05() { ECFieldF2m f1 = new ECFieldF2m(2000); ECFieldF2m f2 = new ECFieldF2m(2000, BigInteger.valueOf(0L). setBit(0).setBit(1).setBit(2). setBit(981).setBit(2000)); assertFalse(f1.equals(f2) || f2.equals(f1)); }
/** * Test #1 for <code>getMidTermsOfReductionPolynomial()</code> method.<br> * * Assertion: returns mid terms of reduction polynomial */ public final void testGetMidTermsOfReductionPolynomial01() { int[] a = new int[] {981,2,1}; int[] b = new ECFieldF2m(2000, BigInteger.valueOf(0L).setBit(0).setBit(1). setBit(2).setBit(981).setBit(2000)). getMidTermsOfReductionPolynomial(); assertTrue(Arrays.equals(a, b)); }