-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymult.py
43 lines (35 loc) · 920 Bytes
/
polymult.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
# P(x) = x^8 + x^4 + x^3 + x + 1
P_x = 283
def pos_offset(n):
offset = 0
while n >= 256:
offset += 1
n >>= 1
return offset-1 if offset > 0 else offset
def main():
args = sys.argv[1:]
assert len(args) == 2, ("Usage: python3 filename.py a b\n" \
"\t\twhere a and b are polynomials " \
"in hexadecimal notation.")
[a, b] = args
try:
a = int(a, 16)
b = int(b, 16)
except ValueError:
print("Input contains invalid hex string.")
exit(1)
if a == 0 or b == 0:
return print(hex(0))
result = 0 if b & 1 == 0 else a
while b != 1:
a <<= 1
b >>= 1
if b & 1 == 0:
continue
result ^= a
while result >= 256:
result ^= P_x << pos_offset(result)
print(hex(result))
if __name__ == "__main__":
main()