0

I am developing software in JavaCard to addition points in ECC. the issue is I need some basis operations, so for the moment, I need multiplication and inversion, I already have addition and subtraction.

I was trying to develop montgomery multiplication but it is for GF(2^m) (I think).

so my example is:

public static void multiplicationGF_p2(){
        byte A = (byte) 7;
        byte p = (byte) 5;
        byte B = (byte) 2;
        byte C = (byte) 0;
        byte n = (byte)8;
        byte i =  (byte)(n - 1);
        for(; i >= 0; i--){
            C = (byte)(((C & 0xFF) + (C & 0xFF) ) + ((A & 0xff) << getBytePos(B,i)));
            if((C & 0xFF) >= (byte)(p & 0xFF)){
                C = (byte) ((C & 0xFF)-(p & 0xFF));
            }
            if((C & 0xFF) >= (byte)(p & 0xFF)){
                C = (byte) ((C & 0xFF)-(p & 0xFF));
            }
        }
    }

for example A = 2, B =3, p= 3 C must be 0, C = A. B (mode p) but this example A = 7, B=2, p=5 , C must be 4, but I have 49.

can someone help me with that?

more methods:

public static byte getBytePos(byte b, byte pos){
        return (byte)(((b & 0xff) >> pos) & 1);
}

I am trying to be simple, for the moment, but the idea is make multiplication of very big number like arrays[10] of bytes

Raul Benitez
  • 35
  • 1
  • 7

1 Answers1

0

I have supposed that something was wrong here:

 C = (byte)(((C & 0xFF) + (C & 0xFF) ) + ((A & 0xff) << getBytePos(B,i)));

I have created a method to multiply byte numbers, not just using shift to the right <<

So:

public static byte bmult(byte x, byte y){ 
        byte total = (byte)0;
        byte i;
        byte n = (byte)8; // multiplication for 8 bits or 1 byte
        for(i = n ; i >= 0 ; i--)
        {
            total <<= 1;
            if( (((y & 0xff) & (1 << i)) >> i) != (byte)0 )
            {
                total = (byte)(total + x);
            }
        }

        return total;
    }

so then I have added it in my original method, (in the line marked):

C = (byte)(((C & 0xFF) + (C & 0xFF) ) + bmult(A, getBytePos(B,i))  );

for now it is working correctly, I need to test it more

someone has another solution ?

Raul Benitez
  • 35
  • 1
  • 7