In [32]:
import re
dimension = 8 # GF(2^8)
polynomial_str = 'x^6 + x^5 + x^3'
print(polynomial_str)
def get_binary_representation(polynomial_str):
# regex allows for simple notation as above, with or without spaces between powers
match_obj = re.findall( r'x\^(\d)\s?', polynomial_str)
binary_number = [0 for x in range(dimension)]
if match_obj:
# each item will be a power
for item in match_obj:
binary_number[dimension - int(item) - 1] = 1
else:
print('No match !')
return binary_number
print('Binary representation of %s is: %s' % (str(polynomial_str), get_binary_representation(polynomial_str)))
In [58]:
def XTIME(power, binary_p, binary_f):
# constrain pass by value
binary_p = binary_p[:]
if binary_p[0] == 0:
binary_p.remove(binary_p[0])
binary_p.append(0)
else:
binary_p.append(0)
binary_p = [ (binary_p[i] + binary_f[i]) % 2 for i in range(len(binary_p))]
binary_p.remove(binary_p[0])
if power == 1:
return binary_p
else:
return XTIME(power-1, binary_p, binary_f)
# case 1 - no recursion, first character is 0
binary_p = [0, 1, 1, 0, 1, 0, 0, 0]
binary_f = [1, 0, 0, 0, 1, 1, 0, 1, 1]
power = 1
xtime_val = XTIME(power, binary_p, binary_f)
assert xtime_val == [1, 1, 0, 1, 0, 0, 0, 0], "Assertion for test case 1 failed !"
print(binary_p)
print('Result of XTIME is: ' + str(xtime_val))
print(binary_p, '\n')
# case 2 - no recursion, first character is 1
binary_p = [1, 1, 1, 0, 1, 0, 0, 0]
binary_f = [1, 0, 0, 0, 1, 1, 0, 1, 1]
power = 1
xtime_val = XTIME(power, binary_p, binary_f)
assert xtime_val == [1, 1, 0, 0, 1, 0, 1, 1], "Assertion for test case 2 failed !"
print(binary_p)
print('Result of XTIME is: ' + str(xtime_val))
print(binary_p, '\n')
# case 3 - recursion
binary_p = [1, 1, 1, 0, 1, 0, 0, 0]
binary_f = [1, 0, 0, 0, 1, 1, 0, 1, 1]
power = 2
xtime_val = XTIME(power, binary_p, binary_f)
assert xtime_val == [1, 0, 0, 0, 1, 1, 0, 1], "Assertion for test case 3 failed !"
print(binary_p)
print('Result of XTIME is: ' + str(XTIME(power, binary_p, binary_f)))
print(binary_p, '\n')
In [ ]: