elliptic curve infinity point implementation returns exception I am trying to implement an elliptic curve discret logarithm problem, but I have got a problem due to the representation of the infinity point. I tried this code :

first define what is an infinity point

    public Point infinity() {
        BigInteger xInfinity = new BigDecimal(Double.POSITIVE_INFINITY).toBigInteger();
        BigInteger yInfinity = new BigDecimal(Double.POSITIVE_INFINITY).toBigInteger();

        return new Point(xInfinity, yInfinity, this.E);
    }


then to test if a given point is an infinity point to return the same point when adding to it

public boolean isInfinity() {
    if (this.x == new BigDecimal(Double.POSITIVE_INFINITY).toBigInteger()
            || this.y == new BigDecimal(Double.POSITIVE_INFINITY).toBigInteger()) {
        return true;
    }
    return false;
}


but I still get an exception even if i try to calculate with simple points


  java.lang.NumberFormatException: Infinite or NaN


Thanks to help me solving this.
So to understand why the code doesn't work, we need a bit of background on IEEE 754 floating point representation of which BigDecimal is essentially an arbitrary precision extension. Most notably it defines special values to indicate a computation yielded "infinity" (which is the Double.POSITIVE_INFINITY you are using) and for computations that yielded an invalid result (such as log(0) or 1/0), this is called "NotANumber" (NaN). So what BigDecimal does, is that it is essentially a double with higher precision, but using similar values for these special cases.

Now BigInteger on the other hand, is an extension of the usual int to arbitrary precision, taking up as many bits as needed to represent the number at hand. As it is an extension of int, this also means that it doesn't support special values such as "infinity" or "NaN", but rather only actually representable integers.

When you do new BigDecimal(Double.POSITIVE_INFINITY).toBigInteger(), you create a new BigDecimal with the special "infinity" value / flag and try to convert this to an "ordinary" integer. But as you can't represent infinity with an integer, as this would need an infinite amount of storage, an exception is thrown instead.

Now, how can you fix this? Well, this is actually a non-trivial question and depends on what you want to do with the points. If you want to do elliptic curve cryptography using affine coordinates (which is terribly inefficient), then the usual approach is to give your Point a flag which essentially indicates "infinity" and test for the flag when you implement doubling and addition. When you use a more modern curve which has a complete addition law (that is it works for all points), then there are specific, concrete coordinates defined for the point at infinity. Of course the best solution would be to use projective coordinates, which can represent the point at infinity perfectly easy using a Z coordinate of 0.