Search code examples
javacryptographybouncycastle

Elliptic curve point


Presently iam working on a project that uses elliptic curve. Please provide me a solution that determines whether a point is on the elliptic curve or not? and also how to get a point on the elliptic curve


Solution

  • Checking whether a point is on the elliptic curve is easy. Just check whether your point (x,y) fulfills the equation defining your elliptic curve : y^2 = x^3 + ax + b (remember to perform the calculation in the correct field).

    Using Bouncycastle you can do it like this:

    ECCurve curve = //...
    ECFieldElement x = //...
    ECFieldElement y = //...
    
    ECFieldElement a = curve.getA();
    ECFieldElement b = curve.getB();
    ECFieldElement lhs = y.multiply(y);
    ECFieldElement rhs = x.multiply(x).multiply(x).add(a.multiply(x)).add(b);
    
    boolean pointIsOnCurve = lhs.equals(rhs);
    

    You have tagged the question with cryptography, so I assume you are asking about elliptic curves over a finite field. The curve will have a generator, g and an order. To get a random point, just generate a random integer, x, between 0 and (order - 1), and choose x * g.

    You can do it using Bouncycastle like this:

    X9ECParameters x9 = NISTNamedCurves.getByName("P-224"); // or whatever curve you want to use
    ECPoint g = x9.getG();
    BigInteger n = x9.getN();
    int nBitLength = n.bitLength();
    BigInteger x;
    do
    {
        x = new BigInteger(nBitLength, random);
    }
    while (x.equals(ZERO)  || (x.compareTo(n) >= 0));
    ECPoint randomPoint = g.multiply(x);