【发布时间】:2013-08-25 10:36:06
【问题描述】:
我正在尝试使用对数和指数表在 GF(2^8) 中实现乘法和除法。我使用 3 的指数作为生成器,使用来自 here 的指令。
但是我在一些琐碎的测试用例中失败了。
示例:
//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 表是否正确。
我在这里缺少什么?谢谢!
【问题讨论】:
-
我很抱歉。是的,我已经实现了它,应该提到它。它基本上只是在 LHS 和 RHS 术语上使用
/=并返回结果,所以没有什么花哨的。编辑了我的答案。
标签: c++ cryptography galois-field