1

I am working on a library that allows me to work with elliptic curves. It is still at embryonic state, and so far it only consists of two classes, EllipticCurve and Point.

Right now I'm implementing the basic operations of existence, belonging, sum, reflection, etc.

Unfortunately, I'm stuck now that I have to implement the concept of zero, i.e., the point of the elliptic curve E crossing the line through P and -P, where P = (x,y) and -P = (x,-y). So, my question could be rephrased as "how do I implement a point at infinity?"

Here's part of the Point class so far:

public class Point implements Comparable<Point> {
    private static final BigDecimal MINUSONE = new BigDecimal(-1);

    private BigDecimal x;
    private BigDecimal y;
    private EllipticCurve e;

    public Point(BigDecimal x, BigDecimal y, EllipticCurve e) {
        if(x != null && y != null && e != null) {
            if(liesOn(x,y,e)) {
                this.x = x;
                this.y = y;
                this.e = e;
            }
        }
    }

    public Point reflect() {
        return new Point(x,y.multiply(MINUSONE),e);
    }

    public Point add(Point o) {
        if(this.e == o.getE()) {
            if(this == o) {
                return this.multiply(2);
            }
            if(o == this.reflect()) {
                return /*THE INFAMOUS ZERO POINT*/;
            }

            BigDecimal a;
            BigDecimal b;

            /*
             * computation I still haven't implemented
             */

            return new Point(a,b,e);
        }
    }
    /*
     * other methods
     */
}

P.S.: I am aware of the existence of java.security.spec.EllipticCurve, but since I'm going to use this class mostly for mathematical purposes, I felt the need to create my personal library ex novo.

Luigi D.
  • 165
  • 1
  • 10

1 Answers1

1

There is no way to represent infinity per se using BigDecimal. The only way in Java I know is:

Double.POSITIVE_INFINITY;

or Integer, Float etc. You also can't take valueOf from the above, since it will throw a NumberFormatException.

You can use a workaround, i. e. a very large value that you know will always be larger than any other.

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55
  • I had that thought through my mind, but I am using BigDecimals instead of Double because I will probably have to handle some huge numbers. – Luigi D. Mar 24 '15 at 11:54
  • Yep, I see the problem. Well, like I said, to my best knowledge, you simply can't implement infinity in BigDecimal. This is Java however, so performance is not that critical, I think defining some `static` value replacing infinity will not hurt you in any way (but keep in mind, this it rather against good programming practice, and other users will probably diss me for it ;) ). – Michał Szydłowski Mar 24 '15 at 11:57