Points calculated using this elliptic curve point multiplication do not lie on the curve and this class brings Arithmetic exception

孤街浪徒 提交于 2019-12-03 09:04:17

The most important errors are in NISTCurves.P192: p and the order are in base-10, not in base-16. Also, when you construct the EllipticCurve-object, you provide the parameters in the wrong order. Your method requires (a, b, p), but you call it with (p, a, b) (so my guess about p not being prime was correct).

Another problem is in your verify-method, when you unwrap r and s. Since they are in unsigned format, you should use new BigInteger(1, rArr) instead of the normal constructor.

With those changes your code works for me (I can validate the signatures - I have not verified the correctness of the implementation).


(Old answer below:)

Since you have not given us the code that matches the stacktrace, this will merely be a guess:

During elliptic curve addition (with a curve over a prime field), you should only be calling BigInteger.modInverse() with the prime p (the order of the prime field) as the modulus.

The most probable way for this to fail sporadically with "BigInteger not invertible" is if p is not actually a prime.

Where are you getting p from? Try inserting

if(!ec.getP().isProbablePrime(100)) throw new RuntimeException("P is not a prime");

somewhere.

From the JDK java code for BigInteger:

 // Base and modulus are even, throw exception
 if (isEven())
     throw new ArithmeticException("BigInteger not invertible.");

It seems that for the modInverse() method, the BigInteger may not be an even number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!