In my program, I can successfully restore an EC private key from a PEM file using Spongy Castle (Bouncy Castle equivalent for Android). As the file does not contain the public key, I am trying to recompute the public key from the private key.
I am able to compute a valid public key. However, my public key contains much more data (311 bytes) than my original reference (92 bytes). This is even more than the private key (152 bytes).
How can I get the compressed (?) version of the public key
PrivateKey privateKey; // private key from pem file
PublicKey referencePublicKey; // public key in my testing environment, generated with an EC KeyPairGenerator
KeyFactory factory = KeyFactory.getInstance("EC"); // using SpongyCastle provider
ECPrivateKeySpec privSpec = factory.getKeySpec(privateKey, ECPrivateKeySpec.class);
ECParameterSpec params = privSpec.getParams();
ECPoint q = params.getG().multiply(privSpec.getD());
ECPublicKeySpec pubSpec = new ECPublicKeySpec(q, params);
PublicKey publicKey = factory.generatePublic(pubSpec);
System.out.println( referencePublicKey.equals(publicKey) ); // true
System.out.println( referencePublicKey.getEncoded().length ); // 92
System.out.println( publicKey.getEncoded().length ); // 311