下面列出了怎么用java.security.spec.ECField的API类实例代码及写法,或者点击链接到github查看源代码。
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());
}
/**
* 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 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 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());
}
}
public static void assertECFieldEquals(String message, ECField expected, ECField actual) {
if (expected == actual) {
return;
}
assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize());
}
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);
}
public static void assertECFieldEquals(String message, ECField expected, ECField actual) {
if (expected == actual) {
return;
}
assertEquals(message + "[size]", expected.getFieldSize(), actual.getFieldSize());
}
MyEllipticCurve(ECField f, BigInteger a, BigInteger b, byte[] seed) {
super(f, a, b, seed);
}
public abstract ECField toECField();