short VS byte

JavaCard Applet Development Related Questions and Answers.
Lehrling81
Posts: 10
Joined: Fri Nov 20, 2015 1:47 am
Points :41
Contact:

short VS byte

Post by Lehrling81 » Wed Mar 23, 2016 8:28 am

I finished an applet . In my code, I have implemented block cipher using bytes type. But then I optimize my code and rewrite encryption algorithm for short values. I also found for short it runs faster than bytes. Why? Another question is which operation is more appropriate for optimization purpose.

Code before optimization

Code: Select all

 public static void add(byte[] x, short xLen, byte[] y, short yOffset,
               short yLen) {
          short carry = 0, i = 0;

          while (i < yLen) {
               carry += (short) ((x[i] & 0xFF) + (y[i + yOffset] & 0xFF));
               x[i++] = (byte) (carry & 0xFF);
               carry = (short) ((carry >>> 8) & 0xFF);
          }

          while (i < xLen) {
               carry += (short) (x[i] & 0xFF);
               x[i++] = (byte) (carry & 0xFF);
               carry = (short) ((carry >>> 8) & 0xFF);
          }
     }
     private void mainStep(byte[] n1, byte[] key, short keyOffset)
     {
          add(n1, (short) 4, key, keyOffset, (short) 4);

          Util.arrayFillNonAtomic(om, (short) 0, (short) 4, (byte) 0);
          om[0] = sbox[0 + (n1[0] & 0xF)];
          om[0] |= (byte)(bArr[1 + ((n1[0] >>> 4) & 0xF)]<<4);
          om[1]  = (byte)(bArr[2 + (n1[1] & 0xF)]);
          om[1] |= (byte)(bArr[3 + ((n1[1] >>> 4) & 0xF)]<<4);
          om[2]  = (byte)(bArr[4 + (n1[2] & 0xF)]);
          om[2] |= (byte)(bArr[5 + ((n1[2] >>> 4) & 0xF)]<<4);
          om[3]  = (byte)(bArr[6 + (n1[3] & 0xF)]);
          om[3] |= (byte)(bArr[7 + ((n1[3] >>> 4) & 0xF)]<<4);

          n1[3] = (byte) ((om[2] << 3) | ((om[1] & 0xFF) >>> 5));
          n1[2] = (byte) ((om[1] << 3) | ((om[0] & 0xFF) >>> 5));
          n1[1] = (byte) ((om[0] << 3) | ((om[3] & 0xFF) >>> 5));
          n1[0] = (byte) ((om[3] << 3) | ((om[2] & 0xFF) >>> 5));
     }


Code after optimization:

Code: Select all

private void mainStep(byte [] bKey, short j)
     {
          short om1 = Util.getShort(bKey, j);
          short om0 = (short)(n_0 + om1);
          //change from add function
          if((((om1 < 0)&& (om0 >= 0)) && (n_0 >= 0))
                    ||
               (((om1 < 0) || (om0 >= 0)) && (n_0 < 0)))
          {
               n_1 += 0x1;
          }
          n_0 = om0;
          n_1 += Util.getShort(bKey, (short) (j+2));
         
          om0 = (short)(bArr[n_0 & 0xFF]&0xFF);
          om0 |= (short)((bArr[1+ ((n_0 >>> 8) & 0xFF)])<< 8);
          om1 = (short)((bArr[2+ (n_1 & 0xFF)] & 0xFF));
          om1 |= (short)((sbox[3+ ((n_1 >>> 8) & 0xFF)])<< 8);


          n_0 = (short)(( om0 << 11 ) | ((om1 >>> 5) & 0x7FF)) ;
          n_1 = (short)(( om1 << 11 ) | ((om0 >>> 5) & 0x7FF)) ;
     }

popmun09
Posts: 36
Joined: Tue Feb 16, 2016 9:49 pm
Points :230
Contact:

Re: short VS byte

Post by popmun09 » Wed Mar 23, 2016 8:53 am

It's hard to say. The real performance depends on many factors, even the driver for the card reader can affect the application performance.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 31 guests

JavaCard OS : Disclaimer