-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRC.py
133 lines (115 loc) · 3.45 KB
/
CRC.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from math import log2
from random import randrange
def Sender(dataword: int, key: int) -> int:
n = int(log2(dataword)) + 1
k = int(log2(key)) + 1
codeword = dataword << (k-1)
temp = key << (n-1)
remainder = codeword ^ temp
n -= 1
while(n>0):
temp = key << (n-1)
if(remainder & (1<<(n+k-2))):
remainder = remainder ^ temp
else:
remainder = remainder ^ 0
n -= 1
codeword = codeword + remainder
return codeword
def Receiver1(codeword: int, key: int) -> bool:
flipbits = int(input("How many bits to flip? "))
n = int(log2(codeword)) + 1
k = int(log2(key)) + 1
flip = 0
tmp = n
while(flipbits>0):
pos = randrange(n)
while(pos==tmp):
pos = randrange(n)
tmp = pos
flip = flip | (1 << pos)
flipbits -= 1
print(f"Bit flipped at position: {pos+1}")
codeword = codeword ^ flip
t = int(log2(codeword)) + 1
add = ""
if(t < n):
add = "0" * (n-t)
print(f"Receiver received Codeword = {add}{bin(codeword)[2:]}")
temp = key << (n-k)
remainder = codeword ^ temp
n -= 1
while(n>=k):
temp = key << (n-k)
if(remainder & (1<<(n-1))):
remainder = remainder ^ temp
else:
remainder = remainder ^ 0
n -= 1
return (remainder != 0)
def Receiver2(codeword: int, key: int) -> bool:
chances = int(input("How many times to flip? "))
n = int(log2(codeword)) + 1
k = int(log2(key)) + 1
i = 0
while( 2*i < (n-1) and chances > 0):
codeword = codeword ^ ((1 << i) | (1 << (n-i-1)))
i += 1
chances -= 1
t = int(log2(codeword)) + 1
add = ""
if(t < n):
add = "0" * (n-t)
print(f"Receiver received Codeword = {add}{bin(codeword)[2:]}")
temp = key << (n-k)
remainder = codeword ^ temp
n -= 1
while(n>=k):
temp = key << (n-k)
if(remainder & (1<<(n-1))):
remainder = remainder ^ temp
else:
remainder = remainder ^ 0
n -= 1
return (remainder != 0)
def Receiver3(codeword: int, key: int) -> bool:
distance = int(input("Enter distance to flip the bits: "))
n = int(log2(codeword)) + 1
k = int(log2(key)) + 1
t = n - 1
while(t >= 0):
codeword = codeword ^ (1 << t)
t -= distance
t = int(log2(codeword)) + 1
add = ""
if(t < n):
add = "0" * (n-t)
print(f"Receiver received Codeword = {add}{bin(codeword)[2:]}")
temp = key << (n-k)
remainder = codeword ^ temp
n -= 1
while(n>=k):
temp = key << (n-k)
if(remainder & (1<<(n-1))):
remainder = remainder ^ temp
else:
remainder = remainder ^ 0
n -= 1
return (remainder != 0)
if __name__ == '__main__':
data = int(input("Enter the dataword: "), 2)
key = int(input("Enter the generator key: "), 2)
code = Sender(data, key)
print(f"Sender generated Codeword = {bin(code)[2:]}")
choice = int(input("Select one:\n1. Flip one or more random bits.\n2. Flip two extreme bits\n3. Flip two bits with given distance.\n"))
error = True
if(choice == 1):
error = Receiver1(code, key)
elif(choice == 2):
error = Receiver2(code, key)
elif(choice == 3):
error = Receiver3(code, key)
if(error):
print("Error was detected")
else:
print("No error was detected!")