Example usage for org.bouncycastle.math.ec ECCurve getField

List of usage examples for org.bouncycastle.math.ec ECCurve getField

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECCurve getField.

Prototype

public FiniteField getField() 

Source Link

Usage

From source file:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

static void serializeCurve(Output output, ECCurve curve) throws KryoException {
    byte[] bytes;
    int length;//from   w  ww .  ja v a2 s.co  m
    // save out if it's a NAMED curve, or a UN-NAMED curve. If it is named, we can do less work.
    String curveName = curve.getClass().getSimpleName();

    if (CustomNamedCurves.getByName(curveName) != null) {
        // we use the name instead of serializing the full curve
        output.writeInt(usesName, true);
        output.writeString(curveName);
        return;
    }

    else if (curveName.endsWith("Curve")) {
        String cleanedName = curveName.substring(0, curveName.indexOf("Curve"));

        if (!cleanedName.isEmpty()) {
            ASN1ObjectIdentifier oid = CustomNamedCurves.getOID(cleanedName);
            if (oid != null) {
                // we use the OID (instead of serializing the entire curve)
                output.writeInt(usesOid, true);
                curveName = oid.getId();
                output.writeString(curveName);
                return;
            }
        }
    }

    // we have to serialize the ENTIRE curve.
    // save out the curve info
    BigInteger a = curve.getA().toBigInteger();
    BigInteger b = curve.getB().toBigInteger();
    BigInteger order = curve.getOrder();
    BigInteger cofactor = curve.getCofactor();
    BigInteger q = curve.getField().getCharacteristic();

    /////////////
    bytes = a.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = b.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = order.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = cofactor.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = q.toByteArray();
    length = bytes.length;
    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    // coordinate system
    int coordinateSystem = curve.getCoordinateSystem();
    output.writeInt(coordinateSystem, true);
}