下面列出了怎么用java.security.spec.ECFieldFp的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
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);
}
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);
}
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);
}
/**
* 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 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());
}
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 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);
}
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);
}
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);
}
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);
}
}
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);
}
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);
}
}
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());
}
}
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);
}
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);
}
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);
}
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);
}
/**
* 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");
}
}
/**
* 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);
}
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);
}
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);
}
@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
}
/**
* 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
*/
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
*/
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
*/
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());
}
/**
* 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());
}
/**
* 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());
}
/**
* 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());
}