优化Java Card上的代码序列

时间:2016-06-03 17:21:27

标签: java optimization javacard

我正在尝试在Java Card上实施BCH代码校正。我已经实现了encode函数,现在我正在解码输入。我的结果似乎是正确的但是decode函数花费的时间非常大。我发现下面给出的代码序列是最耗时的。

    Util.arrayCopy(data, OFFSET_START, transientMemory, OFFSET_START,LENGTH);
    short i, j, u, q, t2, count = 0, syn_error = 0;
    t2 = (short) (2 * T);
    for (i = 1; i <= t2; i++)
    {
        for (j = 0; j < LENGTH; j++)
            if (transientMemory[j] != 0)
            {
                short t = (short)(i*j);
                if(t < 0) // (i*j > 32767 )
                {
                    t &= (short)0x7FFF;
                    t += 64;  // hack because n is always 511
                }
                t %= n;
                s[i] ^= ALPHA_TO[t]; // stored in EEPROM
            }   
        if (s[i] != 0)
            syn_error = 1; 
        s[i] = (short) INDEX_OF[s[i]]; //stored in EEPROM

   }
   Util.arrayCopy(transientMemory, OFFSET_START, data, OFFSET_START,LENGTH);
   ISOException.throwIt(ISO7816.SW_CORRECT_LENGTH_00); // for stopping the program

我为transientMemory预先分配了两个RAM byte[]temp_ram short[]的缓冲区,但似乎执行时间仍然很长(370 s)。在这种情况下,t2的值为104LENGTH的值为386。我是否可以使这段代码更好地工作

2 个答案:

答案 0 :(得分:2)

你可以使用一些力量减少。某些低端CPU的乘法和除法成本很高,所以我要替换

def main():
    total = 0
    count = 0

    while True:
        entry=int(input('Enter a number or 0 to quit:'))
        if entry % 2 == 0:
            print (format (entry), 'is an even number.')
        else:
            print (format (entry),'is an odd number.')

        if entry == 0:
            print ('All done!')
            break
main()
在内循环之前将short t = (short)(i*j); 声明为t += i后,由short t = 0确定。

如果我正确理解了这个块

if(t < 0) // (i*j > 32767 )
{
    t &= (short)0x7FFF;
    t += 64;  // hack because n is always 511
}
t %= n;
如果t %= 511t

int的效果相同。由于t必须符合unsigned short,假设普通Java可以编写

t += (t & 511) + ((t >>> 9) & 63);
if (t >= 511) t -= 511;

不知道如何将其转换为您需要的内容,因为我不知道如何表达short的转换(在普通Java中,移位操作数被提升为int)。它可能会像我写的那样工作......

答案 1 :(得分:0)

我设法通过在RAM而不是EEPROM中存储一些变量来以某种方式优化我的代码。这是更新的代码

<!DOCTYPE html>
<html>
  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Vjeverica</title>

  </head>

  <body>
    <div id="root">
      <%- reactOutput %>
    </div>
    <script src="bundle.js"></script>

  </body>
</html>

现在执行时间似乎接近 byte[] transientMemory = p_Memory.geByteRam(); short[] temp_ram = p_Memory.getShortRam(); short index = 0; for(; index < n ; index++) temp_ram[index]=ALPHA_TO[index]; Util.arrayCopyNonAtomic(data, OFFSET_START, transientMemory, OFFSET_START,LENGTH); short i, j, u, q, t2, count = 0, syn_error = 0,s_local=0; t2 = (short) (2 * T); for (i = 1; i <= t2; i++) { s_local = s[i]; for (j = 0; j < LENGTH; j++) if (transientMemory[j] != 0) { short t = (short)(i*j); if(t < 0) // (i*j > 32767 ) { t &= (short)0x7FFF; t += 64; // hack because n is always 511 } t %= n; s_local ^= temp_ram[t]; } if (s_local != 0) syn_error = 1; s[i] = (short) INDEX_OF[s_local]; } for(; index < n ; index++) ALPHA_TO[index]=temp_ram[index];