When RefIn == True
, then you need to reflect the data before proceeding with the CRC calculation, ie. poly division in XOR arithmetic. Now how do you reflect your original message (00010011
)? For this it is important to know the data width:
For n = 2
bits, the reflected message is 00100011
For n = 4
bits, the reflected message is 10001100
For n = 8
bits, the reflected message is 11001000
And now you do poly division in XOR arithmetic.
If you append the CRC to the original message and repeat the calculation (poly division), you will get a so called residue. For certain CRC model parameters, the residue will always be the same for a valid message+CRC sequence.
There is a Python package (yacrc
1) which outputs detailed CRC calculation steps. This can help you better understand what happens under the hood.
Here is an example for your parameters:
# Install as: pip3 install yacrc
from yacrc import CRC
# When refin == True, data width must be specified!
obj = CRC(
width = 4,
poly = 0b0011,
init = 0b0000,
refin = True,
refout = True,
xorout = 0b0000,
data = 8,
optimize = False
)
### CALCULATE CRC FOR ORIGINAL MESSAGE
msg_1 = '00010011'
crc_1, steps_1 = obj.crc_steps(msg_1, appended = False)
print(f'\nORIGINAL MESSAGE:\n{steps_1}')
### CALCULATE CRC FOR ORIGINAL MESSAGE WITH APPENDED CRC
msg_2 = msg_1 + f'{crc_1:0{obj.width}b}'
crc_2, steps_2 = obj.crc_steps(msg_2, appended = True)
print(f'\nAPPENDED MESSAGE:\n{steps_2}')
### VERIFY RESIDUE
# Undo final XOROUT
residue_2 = crc_2 ^ obj.xorout
print(f'\nRESIDUE CHECK: {residue_2 == obj.residue}')
And here is the output of the above code:
CRC-4/G-704 has the same set of parameters
ORIGINAL MESSAGE:
======================
MODEL: CRC-4/
======================
MESSAGE 00010011
----------------------
REFIN 11001000
AUGMENT 110010000000
POLY 10011.......
-----.......
0101000.....
POLY 10011......
-----......
00111000...
POLY 10011....
-----....
0111100..
POLY 10011...
-----...
0110100.
POLY 10011..
-----..
01001..
POLY 10011.
-----.
000010
----
REFOUT 0100
----------------------
CRC = 0x4
======================
APPENDED MESSAGE:
==========================
MODEL: CRC-4/
==========================
MESSAGE 000100110100
--------------------------
REFIN 110010000010
AUGMENT 1100100000100000
POLY 10011.....,.....
-----.....,.....
0101000...,.....
POLY 10011....,.....
-----....,.....
00111000.,.....
POLY 10011..,.....
-----..,.....
0111100,.....
POLY 10011.,.....
-----.,.....
0110101.....
POLY 10011,.....
-----,.....
0100110....
POLY 10011.....
-----.....
0000000000
----
REFOUT 0000
--------------------------
CRC = 0x0
==========================
RESIDUE CHECK: True
1 Disclosure: I am the author of the yacrc
package.