0

I am using EC curve P-256. I generate a key pair. Then, form the private key, I calculate the public key.

For some reason, the two public key values do not correspond.

See included code with the function to generate a key pair:

public static AsymmetricCipherKeyPair  Generate_EC_P256_Key_Pair(SecureRandom random)
    {
        // Select the curve P-256 //
        string curveName = "P-256";
        X9ECParameters ecP = NistNamedCurves.GetByName(curveName);
        ECDomainParameters dom_parameters = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N);

        // Generate EC Key Pair //
        ECKeyPairGenerator pGen = new ECKeyPairGenerator();
        ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(dom_parameters, random);
        pGen.Init(genParam);
        AsymmetricCipherKeyPair keypair = pGen.GenerateKeyPair();

        AsymmetricKeyParameter Priv_key = keypair.Private;
        AsymmetricKeyParameter Pub_key = keypair.Public;

        ECPrivateKeyParameters private_key = (ECPrivateKeyParameters)keypair.Private;
        ECPublicKeyParameters public_key = (ECPublicKeyParameters)keypair.Public;

        BigInteger priv_key_exp = private_key.D;
        BigInteger test2 = public_key.Q.XCoord.ToBigInteger();
        BigInteger test3 = public_key.Q.YCoord.ToBigInteger();

        ECPoint pub_key_1 = dom_parameters.G.Multiply(priv_key_exp);
        BigInteger test4 = pub_key_1.XCoord.ToBigInteger();
        BigInteger test5 = pub_key_1.YCoord.ToBigInteger();

        Console.WriteLine("Exponent: " + priv_key_exp.ToString(16));
        Console.WriteLine("X-Coord: " + test2.ToString(16));
        Console.WriteLine("X-Coord: " + test4.ToString(16));
        Console.WriteLine("\n");
        Console.WriteLine("Y-Coord: " + test3.ToString(16));
        Console.WriteLine("Y-Coord: " + test5.ToString(16));

        return keypair;
    }

If you compare the (X,Y) coordinates of both the generated public key and the calculated pubic key. You will get a different value. I would expect the same value!! What is wrong?

Kobodjo
  • 11
  • 1

2 Answers2

1

James K Polk pointed me into the right direction. I had to Normalize the point first ("pub_key_1 =pub_key_1.Normalize()") before I could obtain the coordinates. I changed the code accordingly and now it gives me the correct results.

Thanks!

public static AsymmetricCipherKeyPair  Generate_EC_P256_Key_Pair(SecureRandom random)
    {
        // Select the curve P-256 //
        string curveName = "P-256";
        X9ECParameters ecP = NistNamedCurves.GetByName(curveName);
        ECDomainParameters dom_parameters = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N);

        // Generate EC Key Pair //
        ECKeyPairGenerator pGen = new ECKeyPairGenerator();
        ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(dom_parameters, random);
        pGen.Init(genParam);
        AsymmetricCipherKeyPair keypair = pGen.GenerateKeyPair();


        ECPrivateKeyParameters private_key = (ECPrivateKeyParameters)keypair.Private;
        ECPublicKeyParameters public_key = (ECPublicKeyParameters)keypair.Public;

        BigInteger priv_key_exp = private_key.D;

        BigInteger genx = public_key.Q.XCoord.ToBigInteger();
        BigInteger geny = public_key.Q.YCoord.ToBigInteger();
        BigInteger genx_aff = public_key.Q.AffineXCoord.ToBigInteger();
        BigInteger geny_aff = public_key.Q.AffineYCoord.ToBigInteger();

        ECPoint pub_key_1 = dom_parameters.G.Multiply(priv_key_exp);

        pub_key_1 =pub_key_1.Normalize();

        BigInteger calcx = pub_key_1.XCoord.ToBigInteger();
        BigInteger calcy = pub_key_1.YCoord.ToBigInteger();

        Console.WriteLine("Exponent: " + priv_key_exp.ToString(16));
        Console.WriteLine("Generated X-Coord        : " + genx.ToString(16));
        Console.WriteLine("Generated X-Coord Affine : " + genx_aff.ToString(16));
        Console.WriteLine("Calculated X-Coord Affine: " + calcx.ToString(16));
        Console.WriteLine("\n");
        Console.WriteLine("Generated Y-Coord        : " + geny.ToString(16));
        Console.WriteLine("Generated Y-Coord Affine : " + geny_aff.ToString(16));
        Console.WriteLine("Calculated Y-Coord Affine: " + calcy.ToString(16));


        return keypair;
    }
Kobodjo
  • 11
  • 1
0

You are getting the wrong X and Y coordinates. Internally, points are stored in an alternate representation (X, Y, Z) involving something called projective coordinates. You want the affine coordinates. The equivalent (X, Y) affine coordinates are retrieved via the properties AffineXCoord and AffineYCoord.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125