我正在尝试在Android上制作一些eliptic cureve加密代码。 (https://github.com/k3d3/ed25519-java)。它在计算机上工作正常但在Android上我得到了StackOverflowError。
问题出在哪里取决于Android设备。一个是在另一个expmod()的scalarmult()中。调试器显示它工作了一段时间然后突然死亡。有任何想法吗?记忆问题?
static BigInteger[] scalarmult(BigInteger[] P, BigInteger e) {
//System.out.println("scalarmult open with e = " + e);
if (e.equals(BigInteger.ZERO)) {
System.out.println("scalarmult close with Q = 0,1");
return new BigInteger[]{BigInteger.ZERO, BigInteger.ONE};
}
BigInteger[] Q = scalarmult(P, e.divide(BigInteger.valueOf(2)));
//System.out.println("scalarmult asQ = " + Q[0] + "," + Q[1]);
Q = edwards(Q, Q);
//System.out.println("scalarmult aeQ = " + Q[0] + "," + Q[1] + " e="+e+" testbit="+(e.testBit(0)?1:0));
if (e.testBit(0)) Q = edwards(Q, P);
//System.out.println("scalarmult close with Q = " + Q[0] + "," + Q[1]);
return Q;
}
static BigInteger expmod(BigInteger b, BigInteger e, BigInteger m) {
//System.out.println("expmod open with b=" + b + " e=" + e + " m=" + m);
if (e.equals(BigInteger.ZERO)) {
//System.out.println("expmod close with 1z");
return BigInteger.ONE;
}
BigInteger t = expmod(b, e.divide(BigInteger.valueOf(2)), m).pow(2).mod(m);
//System.out.println("expmod 1/2 t="+t+" e="+e+" testbit="+(e.testBit(0)?1:0));
if (e.testBit(0)) {
t = t.multiply(b).mod(m);
}
//System.out.println("expmod close with " + t);
return t;
}
堆栈跟踪(最后一部分的剪切,因为它只是重复它自己):
10-06 19:00:17.855: E/AndroidRuntime(749): FATAL EXCEPTION: main
10-06 19:00:17.855: E/AndroidRuntime(749): java.lang.StackOverflowError
10-06 19:00:17.855: E/AndroidRuntime(749): at java.lang.ref.FinalizerReference.<init>(FinalizerReference.java:34)
10-06 19:00:17.855: E/AndroidRuntime(749): at java.lang.ref.FinalizerReference.add(FinalizerReference.java:48)
10-06 19:00:17.855: E/AndroidRuntime(749): at java.math.BigInt.<init>(BigInt.java:24)
10-06 19:00:17.855: E/AndroidRuntime(749): at java.math.BigInteger.divide(BigInteger.java:903)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)
10-06 19:00:17.855: E/AndroidRuntime(749): at com.test.crypto.Curve25519.expmod(Curve25519.java:45)