gpt4 book ai didi

java - 自定义 Java 公钥生成(比特币)

转载 作者:搜寻专家 更新时间:2023-11-01 02:56:41 34 4
gpt4 key购买 nike

私钥是随机生成的,不与任何钱包关联。

我想为比特币准备公钥生成的自定义(简单)实现。然而,经过几次尝试,我的结果是不正确的。我将它们与在线生成器进行了比较。我发现我使用了除法而不是 modinv。不幸的是,在将 division 更改为 modinv 之后,我得到了“java.lang.ArithmeticException: BigInteger not invertible.”。我厌倦了跟随https://www.mobilefish.com/services/cryptocurrency/cryptocurrency.html#refProdedurehttps://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)

最佳答案

  • 当前表达式

    编码如下:

     y.modInverse(x).mod(p)

    这是错误的,会导致出现观察到的错误消息。以下适用:

    必须按如下方式编码:

     y.multiply(x.modInverse(p)).mod(p)
  • add方法中,案例:

    未处理。此处,add 方法必须返回 zero(即代表无穷远点的点)。在几何上,这种情况对应于垂直割线(2 个交叉点)。

  • 类比的话

    未在 doublePoint 方法中处理。此处,doublePoint 方法也必须返回 zero。在几何上,这种情况对应于垂直切线(1 个交点)。

  • mult 方法不起作用。但是关于代码中的注释,这可能是已知的。

  • 如果使用小素数有限域,测试会更容易。 Here您可以指定椭圆曲线并确定相应的点。注意,必须使用b != 0的曲线,否则点(0,0)是曲线的规则点,不能作为a代表无穷远点。

关于java - 自定义 Java 公钥生成(比特币),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072796/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com