0

I am currently trying to generate random ECC points in Java on particular curve. After generating points, I want to make sure if points exist on the specific curve or not. Here is my code,

import org.bouncycastle.math.ec.ECCurve;   
import org.bouncycastle.math.ec.ECFieldElement;   
import org.bouncycastle.math.ec.ECPoint;  
import org.bouncycastle.asn1.sec.SECNamedCurves;  
import org.bouncycastle.asn1.x9.X9ECParameters;    
import java.security.SecureRandom;   
import java.math.BigInteger;  


    X9ECParameters ecSpec = SECNamedCurves.getByName("secp256k1"); 

    // Domain parameters 
    ECCurve.Fp eccurve = (ECCurve.Fp)ecSpec.getCurve(); 
    ECFieldElement  a = eccurve.getA();
    ECFieldElement  b = eccurve.getB();
    BigInteger q = eccurve.getQ();
    BigInteger coFactor = ecSpec.getH(); 
    BigInteger n = ecSpec.getN();
    ECPoint G = ecSpec.getG();

for(int i=0;i<100;i++)
{
    BigInteger x = org.bouncycastle.util.BigIntegers.createRandomInRange(BigInteger.ONE, n.subtract(BigInteger.ONE), new SecureRandom());
    BigInteger y = org.bouncycastle.util.BigIntegers.createRandomInRange(BigInteger.ONE, n.subtract(BigInteger.ONE), new SecureRandom());

    ECPoint P1 =eccurve.createPoint(x, y);

    ECFieldElement lhs = P1.getYCoord().multiply(P1.getYCoord());
    ECFieldElement rhs = 
      P1.getXCoord().multiply(P1.getXCoord())
        .multiply(P1.getXCoord()).add(a.multiply(P1.getXCoord()))
        .add(b);

    boolean pointIsOnCurve = lhs.equals(rhs);
   //ecCurve.validatePoint(x, y);
    if(pointIsOnCurve==true)
             System.out.println("point is on curve");
}

Now problem is lhs is never equal to rhs. There is only one case when pointIsOnCurve returns true i.e. if I assign ECPoint P1= G. I have tried ecCurve.validatePoint(x, y) as well and it returns true for all the points, no matter whatever the case is. So, how this Validatepoints works? Is it validating about points being on curve? Additionally what is wrong with Code? Why lhs and rhs doesnot return same value?

Can anyone please tell me whats wrong with my code? I am badly stuck.

Norbert
  • 6,026
  • 3
  • 17
  • 40
Eshaal
  • 125
  • 2
  • 13

0 Answers0