简体   繁体   English

伽罗瓦域中的乘法/除法不正确(2 ^ 8)

[英]Incorrect Multiplication/Division in Galois Field (2^8)

I'm attempting to implement multiplication and division in GF(2^8) using log and exponential tables. 我正在尝试使用日志和指数表在GF(2 ^ 8)中实现乘法和除法。 I'm using the exponent of 3 as my generator, using instructions from here . 我使用指数3作为我的生成器,使用此处的指令。

However I'm failing some trivial test cases. 但是我失败了一些琐碎的测试用例。

example: 例:

//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));  

The first four line passes, however it fails on both 5th and 6th line. 前四行通过,但它在第5和第6行都失败了。
Upon further investigation I found out that these error occur when there is a 'wrap over', ie log3(a) + log3(b) > 255 (multiplication case) or log3(a) - log3(b) < 0 . 经过进一步研究,我发现当存在“包裹”时会发生这些错误,即log3(a) + log3(b) > 255 (乘法情况)或log3(a) - log3(b) < 0 However the value is "modded" such that they remain in 0~255 using true modulus. 然而,该值是“修改的”,使得它们使用真模数保持在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;
}

the / operator is implemented using the /= override above so nothing special happens there. /运算符是使用上面的/= override实现的,所以没有特别的事情发生。

I have checked that the generated log/exp tables are correct. 我已经检查过生成的log / exp表是否正确。

What am I missing here? 我在这里错过了什么? Thanks! 谢谢!

First, read this question and all its answers and comments carefully: 首先,请仔细阅读此问题及其所有答案和评论:

Addition and multiplication in a Galois Field 伽罗瓦域中的加法和乘法

I think your code is OK, but you have two problems. 我认为你的代码没问题,但你有两个问题。

First, the comments are wrong; 首先,评论是错误的; you are keeping the exponent in the range 0-254, not 0-255. 你保持指数在0-254范围内,而不是0-255。

Second, your "trivial" test cases are wrong. 其次,你的“琐碎”测试案例是错误的。

In this field, think of numbers as polynomials whose coefficients you get from the binary representation of the number. 在此字段中,将数字视为多项式,其系数来自数字的二进制表示。 For example, since 5 = 2^2 + 1, in this field "5" means x^2 + 1. 例如,由于5 = 2 ^ 2 + 1,因此在该字段中“5”表示x ^ 2 + 1。

So "5" * "3" = (x^2 + 1) * (x + 1) = x^3 + x^2 + x + 1, or "15". 所以“5”*“3”=(x ^ 2 + 1)*(x + 1)= x ^ 3 + x ^ 2 + x + 1,或“15”。 This is why your test case assert((GF256elm(15) / GF256elm(5)) == GF256elm(3)); 这就是你的测试用例assert((GF256elm(15) / GF256elm(5)) == GF256elm(3)); works. 作品。 It has nothing to do with your usual notion that five times three equals fifteen. 它与你通常认为五次三等于十五的概念无关。 Similarly for your other working test cases, which you will notice mostly involve powers of two. 同样,对于其他工作测试用例,您会注意到这些测试用例主要涉及两个权限。

However, "7" * "11" = (x^2 + x + 1) * (x^3 + x + 1) = x^5 + x^4 + 2x^3 + 2x^2 +2x + 1 然而,“7”*“11”=(x ^ 2 + x + 1)*(x ^ 3 + x + 1)= x ^ 5 + x ^ 4 + 2x ^ 3 + 2x ^ 2 + 2x + 1

But the coefficients are all modulo 2, so this is actually x^5 + x^4 + 1 = "49". 但系数都是模2,所以这实际上是x ^ 5 + x ^ 4 + 1 =“49”。 This is why your last two test cases fail. 这就是您最后两个测试用例失败的原因。

If you try assert(GF256elm(49) / GF256elm(7) == GF256elm(11)); 如果你尝试assert(GF256elm(49) / GF256elm(7) == GF256elm(11)); you should find it checks out. 你应该找到它结账。

x % n evaluates to an integer between 0 and (n - 1), inclusive. x % n计算结果为0到(n - 1)之间的整数。

This means that x % 255 evaluates to an integer between 0 and 254, not 0 and 255. 这意味着x % 255计算结果为0到254之间的整数,而不是0到255之间的整数。

You should replace 255 with 256, or alternatively, perform a bitwise AND with 0xff for the same result. 您应该用256替换255,或者对于相同的结果执行与0xff的按位AND。 The latter is faster, though it is quite likely that compilers are smart enough to optimize them to the same bytecode. 后者更快,尽管编译器很可能足够聪明,可以将它们优化为相同的字节码。

There is nothing wrong with the code. 代码没有任何问题。 Finite field multiplication/division is different from normal arithmetic. 有限域乘法/除法与常规算法不同。 Please refer to this question in cryptostackxchange. 请参考cryptostackxchange中的这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM