如果我有以下这些方法:
public int add (int a , int b) {
assert(a>= 0 && a < 256 && b >= 0 && b < 256);
// the addition is simply an xor operation in GF (256) since we are working on modulo(2) then 1+1=0 ; 0+0=0; 1+0=0+1=1;
return a ^ b;
}
第二种方法是:
public int FFMulFast(int a, int b){
int t = 0;;
if (a == 0 || b == 0)
return 0;
// The multiplication is done by using lookup tables. We have used both logarithmic and exponential table for mul
// the idea is firstly look to Logarithmic table then add their powers and find the corresponding of this to exponential table
t = (Log[(a & 0xff)] & 0xff) + (Log[(b & 0xff)] & 0xff);
if (t > 255) t = t - 255;
return Exp[(t & 0xff)];
}
现在我想用这些方法计算多项式f(x)= a0 + a1x + a2 x(pow 2)+ ... a2 x(pow k-1)其中这些系数a0,a1,a2 i有生成如下:
public void generate (int k) {
byte a [] = new byte [k];
Random rnd = new SecureRandom () ;
a.nextBytes (a); // the element of byte array are also negative as for example -122; -14; etc
}
现在我想计算我的多项式,但我不确定它是否有效,因为这个负系数。我知道JAVA只支持有符号字节,但我不确定下面的方法是否能正常工作:
private int evaluate(byte x, byte[] a) {
assert x != 0; // i have this x as argument to another method but x has //only positive value so is not my concern , my concern is second parameter of //method which will have also negative values
assert a.length > 0;
int r = 0;
int xi = 1;
for (byte b : a) {
r = add(r, FFMulFast(b, xi));
xi = FFMulFast(xi, x);
}
return r;
}
有什么建议吗?此外,如果这个不工作,任何人都可以建议我如何在不使用掩码的情况下将负值转换为正值,因为这样会将数据类型更改为int然后我不能使用getBytes(a)方法
答案 0 :(得分:0)
for (byte b : a) {
r = add(r, FFMulFast(b, xi));
xi = FFMulFast(xi, x);
}
如果add()
和FFMulFast()
方法预期为正值,则必须使用:
for (byte b : a) {
r = add(r, FFMulFast(b & 0xff, xi));
xi = FFMulFast(xi, x & 0xff);
}