Search code examples
javabiginteger

BigInteger as neutral symbol


I am currently working on an algorithm related to cryptography. More specifically, adding points on an elliptic curve. There is an option where I have to handle a situation like adding a point for example P(x,y) = (1,4) and some symbol for the neutral point e.g. Q=(e, e). The result of such "addition" should be Point (1,4). It (e) cannot be a zero value, because then the point will be Q(qx,qy)=(0,0) and another function will be activated, therefore the result will also differ. Can you assign a symbol to BigInteger? I need something like

if(qx == e){
  BigInteger r1 = x1;
  BigInteger r2 = x2;
}

Here is full function:

static void addPoints(BigInteger x1, BigInteger y1, BigInteger x2, BigInteger y2, BigInteger a, BigInteger b, BigInteger p) throws Exception {
        BigInteger lambda = null;
        BigInteger x3 = null;
        BigInteger y3 = null;
       
        if (x1.compareTo(x2) == 0 && y1.compareTo(y2) == 0) { //same points
            lambda = (((new BigInteger("3").multiply(x1.pow(2))).add(a))
                    .multiply(Modul1.getReverseNumber(
                            (new BigInteger("2").multiply(y1)), p)))
                    .mod(p);

            x3 = ((lambda.pow(2)).subtract(new BigInteger("2").multiply(x1))).mod(p);
            y3 = ((lambda.multiply(x1.subtract(x3))).subtract(y1)).mod(p);

        } else if (x1.compareTo(x2) != 0) { //points are diffrent
            lambda = ((y2.subtract(y1)).multiply(
                    Modul1.getReverseNumber(x2.subtract(x1), p)
            )).mod(p);

            x3 = (((lambda.pow(2)).subtract(x1)).subtract(x2)).mod(p);
            y3 = ((lambda.multiply(x1.subtract(x3))).subtract(y1)).mod(p);

        } else if (x1.compareTo(x2) == 0 && y1.compareTo(p.subtract(y2)) == 0) { //y2 is negate
            System.out.println(O);
        } else { //Point add Neutral Point
            System.out.println("Punkt P + neutral : (" + x1 + "," + y1 + ")");
        }
    }

Solution

  • I solved it a little around. I used String as function parameters for one point. If it is an infinity symbol the result is the first Point, otherwise the null BigInteger is given the value of this String.

     static void addPoints(BigInteger x1, BigInteger y1, String e1, String e2, BigInteger a, BigInteger b, BigInteger p) throws Exception {
            BigInteger lambda = null;
            BigInteger x3 = null;
            BigInteger y3 = null;
    
            if (e1.equals("e") || e2.equals("e")) {
                System.out.println("Punkt P + O to: (" + x1 + "," + y1 + ")");
            } else {
                BigInteger x2 = new BigInteger(e1);
    
                BigInteger y2 = new BigInteger(e2);
    
    
                String O = "symbol O";
                if (x1.compareTo(x2) == 0 && y1.compareTo(y2) == 0) {
                    lambda = (((new BigInteger("3").multiply(x1.pow(2))).add(a))
                            .multiply(Modul1.getReverseNumber(
                                    (new BigInteger("2").multiply(y1)), p)))
                            .mod(p);
    
                    x3 = ((lambda.pow(2)).subtract(new BigInteger("2").multiply(x1))).mod(p);
                    y3 = ((lambda.multiply(x1.subtract(x3))).subtract(y1)).mod(p);
    
                    System.out.println("lamda to: " + lambda);
                    System.out.println("x3: " + x3);
                    System.out.println("y3: " + y3);
                    System.out.println("Punkt P+P = (" + x3 + "," + y3 + ")");
                } else if (x1.compareTo(x2) != 0) {
                    lambda = ((y2.subtract(y1)).multiply(
                            Modul1.getReverseNumber(x2.subtract(x1), p)
                    )).mod(p);
    
                    x3 = (((lambda.pow(2)).subtract(x1)).subtract(x2)).mod(p);
                    y3 = ((lambda.multiply(x1.subtract(x3))).subtract(y1)).mod(p);
    
                    System.out.println("Punkt P+Q = (" + x3 + "," + y3 + ")");
                } else if (x1.compareTo(x2) == 0 && y1.compareTo(p.subtract(y2)) == 0) {
                    System.out.println("Infinity,Infinity");
                }
    
            }
        }