我正在尝试使用对数和指数表在 GF(2^8) 中实现乘法和除法。我使用 3 的指数作为生成器,使用来自此处的说明。
但是我失败了一些琐碎的测试用例。
例子:
//passes
assert((GF256elm(4) / GF256elm(1)) == GF256elm(4));
assert((GF256elm(32) / GF256elm(16)) == GF256elm(2));
assert((GF256elm(15) / GF256elm(5)) == GF256elm(3));
assert((GF256elm(88) / GF256elm(8)) == GF256elm(11));
//fails, but should pass
assert((GF256elm(77) / GF256elm(11)) == GF256elm(7));
assert((GF256elm(77) / GF256elm(7)) == GF256elm(11));
前四行通过,但在第 5 行和第 6 行均失败。
经过进一步调查,我发现这些错误发生在“换行”时,即log3(a) + log3(b) > 255
(乘法情况)或log3(a) - log3(b) < 0
. 然而,该值是“修改”的,因此它们使用真实模数保持在 0~255 之间。
GF256elm& GF256elm::operator/=(const GF256elm& other) { //C++ operator override for division
int t = _logTable[val] - _logTable[other.val]; //log3(a) - log3(b)
int temp = ((t % 255) + 255) % 255; //this wraps the value to between 0~254 inclusive.
val = _expTable[temp];
return *this;
}
运算符/
是使用/=
上面的覆盖实现的,所以那里没有什么特别的事情发生。
我检查了生成的日志/exp 表是否正确。
我在这里想念什么?谢谢!