0

I'm trying to use Koblitz's method to convert String to ECPoint, Eclipse says that there are no errors in my code, but when I try to run my application, it gets an error everytime the code gets to the Koblitz method.

Sorry, I know this kind of post is not educative, but I can't seem to find the problem in my source code.

public static ECPoint ConvertToPoint(String S){
    BigInteger neg = new BigInteger("-1");
    BigInteger one = BigInteger.ONE;
    BigInteger adder = BigInteger.ONE;
    BigInteger two = new BigInteger("2");
    BigInteger M,MK,Y,s = null, n = BigInteger.ONE,x,x2,x3 = null,B,B2,B3,g,g2,g3;
    BigInteger X = null;
    BigInteger k = new BigInteger("20");
    BigInteger P  = new BigInteger("751"); //Prime number (P)
    BigInteger a  = new BigInteger("-1"); //Curve Parameter a
    BigInteger b  = new BigInteger("188"); //Curve Parameter b
    int e = 1;
    int m = 0;
    int r;

    if (S=="B"){
        M = new BigInteger("11");
        MK = M.multiply(k);
        X = MK.add(adder); // x = m.k + i
        Y = (X.multiply(X).multiply(X)).add(a.multiply(X)).add(b); // y = (x^3 + ax + b)

        BigInteger C = (P.subtract(one)).divide(two);

        while((Y.mod(one) != BigInteger.ZERO) && (P.gcd(Y) != one) && Y.modPow(C, P) != one){
            adder = adder.add(one);
            X = MK.add(adder);   
            Y = (X.multiply(X).multiply(X)).add(a.multiply(X)).add(b);
        }   
        s = (P.subtract(one)).divide(two.pow(e));
        while (s.mod(two)== BigInteger.ZERO){
            e = e+1;
            s = (P.subtract(one)).divide(two.pow(e));
        }
        while (((n.modPow(C, P)) != neg.mod(P)) || (sqrt(n).mod(one)== BigInteger.ZERO)){
            n = n.add(one);
        }
        x = Y.modPow((s.add(one).divide(two)), P);
        B = Y.modPow(s, P);
        g = n.modPow(s, P);
        r = e;

        while (B.modPow(two.pow(m), P) != BigInteger.ONE){
            m = m+1;
        }
        if (m>0){
            x2 = x.multiply(g.pow(2^(r-m-1)));
            x3 = x2.mod(P);
            B2 = B.multiply(g.pow(2^(r-m)));
            B3 = B2.mod(P);
            g2 = g.pow(2^(r-m));
            g3 = g.mod(P);
            r = m;
        }
        else if (m==0){
            x3 = x;
            B3 = B;
            g3 = g;
            r = e;
        }
    }
       //This is when the program crashes
   ECPoint T = new ECPoint(X,x3); 
   return T;
RedCrimson
  • 25
  • 6
  • 2
    If you are asking a question about an error or exception that you're seeing, you will want to post the message with your question and indicate for us which line is causing it. – Hovercraft Full Of Eels Apr 27 '14 at 15:09
  • 1
    Also, never do this: `if (S=="B")`. Instead use the String method `equals` to compare Strings, `if ("B".equals(S))`. Understand that `==` checks if the two *objects* are the same which is not what you're interested in. The methods, `equals` and `equalsIgnoreCase`, on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Apr 27 '14 at 15:10
  • @HovercraftFullOfEels: I've tried your suggestion, and have just debugged the program. I found that the problem seems to be in the first while: `while((Y.mod(one) != BigInteger.ZERO) && (P.gcd(Y) != one) && Y.modPow(C, P) != one)` Is my code correct there? The logcat says these: `FATAL EXCEPTION: main` and `java.lang.NullPointerException: affineX == null` and many more, but the first 2 lines of errors are those – RedCrimson Apr 27 '14 at 15:28
  • OK, I did see that on second look, thanks, but again, what error message or stack-trace are you seeing? Please post the entire message as an edit to your question. – Hovercraft Full Of Eels Apr 27 '14 at 15:29

1 Answers1

1

There are two problems that I see with your code, although I don't know if this is causing your error:

  • Again, as stated in my comment, don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.
  • The same goes for BigIntegers. You should either use the equals(...) method or the compareTo(...) method if you want to test for equality/inequality or > or <.
  • It's a truism that is usually correct, that all reference types should be checked for equality using their equals(...) method. This is not so for primitives which have no methods. Also enums can be tested for equality via == and != although equals(...) will work fine for them.

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Also you should avoid using trivial variable names such as b or s unless they are being used for trivial purposes such as the index of a for loop. Instead use names that have some meaning so that your code becomes self-commenting.

Following these suggestions as well as following good code formatting practices will allow others (such as us!) to better understand your code, and more importantly, will allow your future self to better understand just what you were thinking 6 months ago when you wrote the code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I've followed your instructions and repaired my code a bit, but now I'm getting this kind of message from logcat: `WAIT_FOR_CONCURRENT_GC blocked 94ms` and `GC_CONCURRENT freed`. I'm getting many of those. I've searched through the forums and found [this](http://stackoverflow.com/questions/11531657/what-does-wait-for-concurrent-gc-blocked-mean). I can't seem to know where my source code is creating gc – RedCrimson Apr 28 '14 at 07:57
  • I'm having trouble in the first `while`. The program seems to be looping in the first `while` and can't exit. Is there a specific curve parameter specification to be met in order for the koblitz method to succeed? – RedCrimson Apr 30 '14 at 07:04
  • What syntax do I use to check for inequality? For example, I want to check `if (A != B)`, can I use `if (!A.equals(B))`? Is this true? – RedCrimson May 02 '14 at 09:04