Zadanie 3 - CRC #27
48
Ring.py
Normal file
48
Ring.py
Normal file
@ -0,0 +1,48 @@
|
||||
from fractions import gcd
|
||||
|
||||
class Modulo:
|
||||
|
||||
def __init__(self, n):
|
||||
self.n = int(n)
|
||||
self.modulo_set = list(range(self.n))
|
||||
self.answer = []
|
||||
|
||||
def get_inverse_elements(self):
|
||||
self.inverse_elements = []
|
||||
for i in self.modulo_set:
|
||||
if gcd(i, self.n) == 1:
|
||||
self.inverse_elements.append(i)
|
||||
self.answer.append(self.inverse_elements)
|
||||
|
||||
def get_zero_divisors(self):
|
||||
self.zero_divisors = []
|
||||
for i in self.modulo_set:
|
||||
for j in self.modulo_set:
|
||||
if (i * j) % self.n == 0 and i != 0 and j != 0:
|
||||
self.zero_divisors.append(i)
|
||||
self.answer.append(list(set(self.zero_divisors)))
|
||||
|
||||
def get_nilpotent_elements(self):
|
||||
self.nilpotent_elements = []
|
||||
for i in self.modulo_set:
|
||||
for j in range(1, len(self.inverse_elements) + 1):
|
||||
if (i**j) % self.n == 0 and i != 0:
|
||||
self.nilpotent_elements.append(i)
|
||||
self.answer.append(list(set(self.nilpotent_elements)))
|
||||
|
||||
def get_idempotent_elements(self):
|
||||
self.idempotent_elements = []
|
||||
for i in self.modulo_set:
|
||||
if (i*i) % self.n == i:
|
||||
self.idempotent_elements.append(i)
|
||||
self.answer.append(self.idempotent_elements)
|
||||
|
||||
def get_all(self):
|
||||
self.get_inverse_elements()
|
||||
self.get_zero_divisors()
|
||||
self.get_nilpotent_elements()
|
||||
self.get_idempotent_elements()
|
||||
return self.answer
|
||||
|
||||
s = Modulo(input("Enter n: "))
|
||||
print(s.get_all())
|
85
crc16.py
Normal file
85
crc16.py
Normal file
@ -0,0 +1,85 @@
|
||||
from poly import Polynomial
|
||||
import sys
|
||||
import ast
|
||||
|
||||
Polynomial.n = 2
|
||||
|
||||
def normalize_byte(data):
|
||||
while len(data) != 8:
|
||||
if len(data) < 8:
|
||||
data = str(0) + data
|
||||
else:
|
||||
data = data[1:]
|
||||
return data
|
||||
|
||||
|
||||
def encode(in_data):
|
||||
data = in_data
|
||||
data = list(data)
|
||||
data_binary = [bin(ord(char)).replace('b', '') for char in data]
|
||||
data_binary = [normalize_byte(byte) for byte in data_binary]
|
||||
data_binary = [int(bit) for bit in list(''.join(data_binary))]
|
||||
data_binary.reverse()
|
||||
|
||||
M = [0] * 16 + data_binary
|
||||
L = [0] * (len(data_binary)) + [1] * 16
|
||||
G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
|
||||
result_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G))
|
||||
result = result_poly.coefficients + [0] * (16 - len(result_poly.coefficients))
|
||||
|
||||
result.reverse()
|
||||
data_binary.reverse()
|
||||
data_binary = data_binary + result
|
||||
data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)]
|
||||
data_binary = [int(''.join(map(str, byte)), 2) for byte in data_binary]
|
||||
|
||||
fcs = [hex(byte) for byte in data_binary]
|
||||
fcs = fcs[-2:]
|
||||
|
||||
data = [chr(byte) for byte in data_binary]
|
||||
data = ''.join(data[0:-2])
|
||||
|
||||
return data, fcs
|
||||
|
||||
|
||||
def check_fcs(in_data, fcs):
|
||||
data = in_data
|
||||
data = list(data)
|
||||
data_binary = [bin(ord(char)).replace('b', '') for char in data]
|
||||
data_binary = [normalize_byte(byte) for byte in data_binary]
|
||||
data_binary = [int(bit) for bit in list(''.join(data_binary))]
|
||||
fcs_binary = [bin(int(in_hex, 16)).replace('b', '') for in_hex in fcs]
|
||||
fcs_binary = [normalize_byte(byte) for byte in fcs_binary]
|
||||
fcs_binary = [int(bit) for bit in list(''.join(fcs_binary))]
|
||||
data_binary += fcs_binary
|
||||
data_binary.reverse()
|
||||
|
||||
C = [0] * 16 + data_binary
|
||||
L = [0] * len(data_binary) + [1] * 16
|
||||
G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
|
||||
S = Polynomial.divide(Polynomial.add(Polynomial(C), Polynomial(L)), Polynomial(G))
|
||||
|
||||
if S.coefficients == []:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
if sys.argv[1] == '-e' or sys.argv[1] == '-encode':
|
||||
arg, fcs = encode(sys.argv[2])
|
||||
print(arg, fcs)
|
||||
print(check_fcs(arg, fcs)) # powinno zawsze zwracać true
|
||||
elif sys.argv[1] == '-c' or sys.argv[1] == '-check':
|
||||
arg = sys.argv[2]
|
||||
fcs = ast.literal_eval(sys.argv[3])
|
||||
print(check_fcs(arg, fcs))
|
||||
else:
|
||||
raise IndexError
|
||||
except IndexError:
|
||||
print("To encode: python3 CRC16.py -e [argument]\nTo check result: python3 CRC16.py -c [argument] [fcs]")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
75
poly.py
Normal file
75
poly.py
Normal file
@ -0,0 +1,75 @@
|
||||
import sys
|
||||
import ast
|
||||
|
||||
class Polynomial:
|
||||
|
||||
n = 0
|
||||
|
||||
def __init__(self, coef_list):
|
||||
self.degree = len(coef_list) - 1
|
||||
self.coefficients = [x % Polynomial.n for x in coef_list]
|
||||
|
||||
@staticmethod
|
||||
def add(p1, p2):
|
||||
result = []
|
||||
f = p1.coefficients
|
||||
g = p2.coefficients
|
||||
if len(f) >= len(g):
|
||||
result = f
|
||||
for i in range(0, len(g)):
|
||||
result[i] = f[i] + g[i]
|
||||
else:
|
||||
result = g
|
||||
for i in range(0, len(f)):
|
||||
result[i] = f[i] + g[i]
|
||||
result = [x % int(Polynomial.n) for x in result]
|
||||
return Polynomial(result)
|
||||
|
||||
@staticmethod
|
||||
def multiply(p1, p2):
|
||||
result = [0] * (p1.degree + p2.degree + 1)
|
||||
f = p1.coefficients
|
||||
g = p2.coefficients
|
||||
for i in range(0, len(f)):
|
||||
for j in range(0, len(g)):
|
||||
result[i+j] += f[i] * g[j]
|
||||
result = [x % int(Polynomial.n) for x in result]
|
||||
return Polynomial(result)
|
||||
|
||||
@staticmethod
|
||||
def divide(p1, p2):
|
||||
def inverse(x):
|
||||
for i in range(1, int(Polynomial.n)):
|
||||
r = (i * x) % int(Polynomial.n)
|
||||
if r == 1:
|
||||
break
|
||||
else:
|
||||
raise ZeroDivisionError
|
||||
return i
|
||||
if p1.degree < p2.degree:
|
||||
return p1
|
||||
f = p1.coefficients
|
||||
g = p2.coefficients
|
||||
g_lead_coef = g[-1]
|
||||
g_deg = p2.degree
|
||||
while len(f) >= len(g):
|
||||
f_lead_coef = f[-1]
|
||||
tmp_coef = f_lead_coef * inverse(g_lead_coef)
|
||||
tmp_exp = len(f) - 1 - g_deg
|
||||
tmp = []
|
||||
for _ in range(tmp_exp):
|
||||
tmp.append(0)
|
||||
tmp.append(tmp_coef)
|
||||
tmp_poly = Polynomial(tmp)
|
||||
sub = Polynomial.multiply(p2, tmp_poly)
|
||||
f = [x - y for x, y in zip(f, sub.coefficients)]
|
||||
f = [x % int(Polynomial.n) for x in f]
|
||||
while f and f[-1] == 0:
|
||||
f.pop()
|
||||
return Polynomial(f)
|
||||
|
||||
@staticmethod
|
||||
def gcd(p1, p2):
|
||||
if len(p2.coefficients) == 0:
|
||||
return p1
|
||||
return Polynomial.gcd(p2, Polynomial.divide(p1, p2))
|
Loading…
Reference in New Issue
Block a user