```
from functools import reduce
+def gf_degree(a) :
+ res = 0
+ a >>= 1
+ while (a != 0) :
+ a >>= 1;
+ res += 1;
+ return res
+
# constants used in the multGF2 function
mask1 = mask2 = polyred = None
-def setGF2(degree, irPoly):
+def setGF2(irPoly):
"""Define parameters of binary finite field GF(2^m)/g(x)
- - degree: extension degree of binary field
- irPoly: coefficients of irreducible polynomial g(x)
"""
+ # degree: extension degree of binary field
+ degree = gf_degree(irPoly)
+
def i2P(sInt):
"""Convert an integer into a polynomial"""
return [(sInt >> i) & 1
if __name__ == "__main__":
# Define binary field GF(2^3)/x^3 + x + 1
- setGF2(3, 0b1011)
+ setGF2(0b1011) # degree 3
# Evaluate the product (x^2 + x + 1)(x^2 + 1)
print("{:02x}".format(multGF2(0b111, 0b101)))
# Define binary field GF(2^8)/x^8 + x^4 + x^3 + x + 1
# (used in the Advanced Encryption Standard-AES)
- setGF2(8, 0b100011011)
+ setGF2(0b100011011) # degree 8
# Evaluate the product (x^7)(x^7 + x + 1)
print("{:02x}".format(multGF2(0b10000000, 0b10000011)))