【发布时间】:2019-07-17 09:35:26
【问题描述】:
私钥是随机生成的,与任何钱包无关。
我想为比特币准备公钥生成的自定义(简单)实现。然而,经过几次尝试,我的结果是不正确的。我将它们与在线生成器进行了比较。我已经认识到我使用除法而不是 modinv。不幸的是,在将除法更改为 modinv 后,我得到了“java.lang.ArithmeticException:BigInteger not invertible.”。我厌倦了关注https://www.mobilefish.com/services/cryptocurrency/cryptocurrency.html#refProdedure 和https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication 你能帮我找出我做错的地方吗?
public class ECDSAUtils {
private static final CurvePoint G = new CurvePoint(new BigInteger("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16), new BigInteger("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16));
private static CurvePoint zero;
private static BigInteger base;
private static final BigInteger three = new BigInteger("3", 10);
public static void main(String[] args){
ECDSAUtils e = new ECDSAUtils();
BigInteger privateKey = new BigInteger("fdc668381ab251673ef8552851a2c7cf346a6e09ea86be0f55a94d2a12253557", 16);
CurvePoint r = e.mult(G, privateKey);
System.out.println(r.x.toString(16).toUpperCase() + " " + r.y.toString(16).toUpperCase());
}
public ECDSAUtils(){
zero = new CurvePoint(new BigInteger("0", 16), new BigInteger("0", 16));
base = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
}
public static CurvePoint add(CurvePoint p, CurvePoint q){
CurvePoint result = null;
if (p.equals(zero)){
result = q;
} else if (q.equals(zero)){
result = p;
} else {
BigInteger lambda = q.y.subtract(p.y).modInverse(q.x.subtract(p.x)).mod(base);
BigInteger x = lambda.multiply(lambda).subtract(p.x).subtract(q.x).mod(base);
BigInteger y = lambda.multiply(p.x.subtract(x)).subtract(p.y).mod(base);
result = new CurvePoint(x, y);
}
return result;
}
public static CurvePoint doublePoint(CurvePoint p){
BigInteger lambda = p.x.multiply(p.x).multiply(three).modInverse(p.y.add(p.y)).mod(base);
BigInteger x = lambda.multiply(lambda).subtract(p.x).subtract(p.x).mod(base);
BigInteger y = lambda.multiply(p.x.subtract(x)).subtract(p.y).mod(base);
return new CurvePoint(x, y);
}
public CurvePoint mult(CurvePoint N, BigInteger p) {
CurvePoint Q = zero;
//EDIT:
for (int i = p.bitLength() - 1; i > -1; i --) {
if (p.testBit(i)) {
Q = add(Q, N);
}
N = doublePoint(N);
}
return Q;
}
}
public class CurvePoint {
BigInteger x;
BigInteger y;
public CurvePoint(BigInteger x, BigInteger y) {
this.x = x;
this.y = y;
}
}
Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible.
at java.math.MutableBigInteger.mutableModInverse(MutableBigInteger.java:1986)
at java.math.BigInteger.modInverse(BigInteger.java:3154)
at naive.ECDSAUtils.doublePoint(ECDSAUtils.java:41)
at naive.ECDSAUtils.mult(ECDSAUtils.java:51)
at naive.ECDSAUtils.main(ECDSAUtils.java:15)
【问题讨论】:
-
所以你的目标是手动实现椭圆曲线的加、倍、乘?另外,
CurvePoint是什么? -
是的,一般来说,我想实现使用 secp256k1 生成公钥的过程,我想以清晰、易读的方式逐步完成。
-
public class CurvePoint { BigInteger x; BigInteger y; public CurvePoint(BigInteger x, BigInteger y) { this.x = x; this.y = y; } } -
您的
p值不正确,它不是 secp256k1 素数。事实上,它甚至不是素数! -
p 是我代码中的私钥。字段的基础是“base” var。我会编辑它。
标签: java encryption bitcoin public-key encryption-asymmetric