-
Notifications
You must be signed in to change notification settings - Fork 1
/
crc.py
34 lines (27 loc) · 1.12 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
# -*- coding: utf-8 -*-
from bitarray import bitarray
def xor_at(a, b, offset=0):
for k, bk in enumerate(b):
index = offset + k
a[index] = a[index] ^ bk
def crc(d, g):
# We'd prefer not to modify the argument in xor_at
dcopy = d.copy()
#
# TODO: compute and return remainder "r"
#
if __name__ == '__main__':
print("From Kurose & Ross (7e), page 478:")
g = bitarray('1001') # generator
d = bitarray('101110') # data (without padding/shifting)
p = bitarray('000') # padding
r = crc(d + p, g) # error-correction bits
assert r == bitarray('011') # known quotient
assert crc(d + r, g) == p # perform CRC check
print("From Wikipedia, [en.wikipedia.org/wiki/Cyclic_redundancy_check]:")
g = bitarray('1011') # generator
d = bitarray('11010011101100') # data (without padding/shifting)
p = bitarray('000') # padding
r = crc(d + p, g) # error-correction bits
assert r == bitarray('100') # known quotient
assert crc(d + r, g) == p # perform CRC check