用于素数域的ECC随机点生成

时间:2016-08-03 01:00:39

标签: java cryptography bouncycastle elliptic-curve java-security

我目前正试图在特定曲线上用Java生成随机ECC点。生成点后,我想确定特定曲线上是否存在点。这是我的代码,

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");
}

现在问题是lhs永远不会等于rhs。只有一种情况是pointIsOnCurve返回true,即如果我指定ECPoint P1 = G.我也尝试了ecCurve.validatePoint(x,y),无论情况如何,它都会返回所有点的true。那么,这个Validatepoints如何工作?是否验证曲线上的点?另外Code有什么问题?为什么lhs和rhs不会返回相同的值?

任何人都可以告诉我我的代码有什么问题吗?我被困住了。

0 个答案:

没有答案