A cyclic redundancy check (CRC) is the remainder left when a message, treated as a polynomial with binary coefficients, is divided by a fixed generator polynomial using modulo two arithmetic. In modulo two arithmetic, addition and subtraction are both the exclusive OR (XOR) of the bits, with no carries or borrows, so the whole division reduces to shifts and XORs. That is why a CRC can be built in hardware from a small shift register and a few XOR gates.
This is Part 2 of the series. Part 1 introduced CRC; this part covers modulo two arithmetic, polynomial division and the shift register that computes it. Part 3 continues the series.
Key takeaways
- Modulo two arithmetic is single-bit binary arithmetic with all carries and borrows ignored; each bit position is independent.
- Modulo two addition and subtraction give the same result, and both are an XOR.
- The CRC is the remainder of dividing the message (with r zeros appended) by a generator of degree r; the remainder has at most r bits.
- Most, but not all, errors change the remainder: an error pattern that is itself a multiple of the generator goes undetected.
- In hardware, an r-bit shift register with an XOR at each tap where the generator has a 1 computes the CRC one bit per clock.
Modulo two addition is XOR
Modulo two arithmetic is simple single-bit binary arithmetic with all carries or borrows ignored. Each digit is considered independently. For example, we can add two binary numbers X and Y as follows:
10101001 (X)
+ 00111010 (Y)
--------
10010011 (Z)
Check each column: 1+0 is 1, 0+0 is 0, 1+1 is 0 with the carry thrown away, and so on. From this example, modulo two addition is equivalent to an exclusive OR operation, bit by bit.
Subtraction gives the same result
What is less obvious is that modulo two subtraction gives the same result as addition. From the previous example, let us add X and Z:
10101001 (X)
+ 10010011 (Z)
--------
00111010 (Y)
In the first example we saw that X + Y = Z, therefore Y = Z - X. The second example shows that Z + X = Y as well. Hence modulo two addition is equivalent to modulo two subtraction, and both can be performed with an XOR. The reason is that in modulo two every value is its own negative: 1 + 1 = 0, so -1 = 1.
| a | b | a + b (mod 2) | a - b (mod 2) | a XOR b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 |
Polynomial division
In integer division, dividing A by B gives a quotient Q and a remainder R. Polynomial division is similar, except that A and B are polynomials, and the remainder is a polynomial whose degree is less than that of B.
If we consider a polynomial whose coefficients are zeros and ones (modulo two), it can be represented simply by its coefficients written as a binary string, one bit per power of x. For example, x³ + x + 1 is written 1011: one x³, no x², one x and one constant term.
In CRC terms, polynomial A is the binary message or data, and polynomial B is the generator polynomial. The remainder R is the cyclic redundancy checksum. If the data changes or becomes corrupt, a different remainder will normally be calculated.
Worked example
Take the 8-bit message 11010110 and the generator 1011 (x³ + x + 1, degree 3). First append three zeros, one for each degree of the generator, giving 11010110000. Then, from the left, wherever the leading bit is 1, XOR the generator in at that position. The running value after each step is:
11010110000 message with 3 zeros appended
1011
01100110000 after XOR at bit 0
1011
00111110000 after XOR at bit 1
1011
00010010000 after XOR at bit 2
1011
00000100000 after XOR at bit 3
1011
00000001100 after XOR at bit 5
1011
00000000111 after XOR at bit 7
remainder (CRC) = 111
The transmitter sends the message followed by the CRC: 11010110 111. The receiver divides the whole 11-bit codeword by the same generator. With no errors the remainder is 000. If one bit flips in transit, say the fourth bit, giving 11000110111, the remainder becomes 001 and the error is detected.
Notice that no step needed a real subtraction or a comparison of sizes. Using modulo two arithmetic, division is a shift operation and subtraction is an XOR operation. That is the whole algorithm.
When the remainder does not change
The first version of this article said that any change to polynomial A causes a change to the remainder R. That is nearly true, and it is what makes CRC useful, but it is not exact. An error can be written as a polynomial E that is XORed onto the codeword. The receiver's remainder changes only if E is not a multiple of the generator. If the error happens to equal the generator shifted to some position, for example flipping bits so that 11010110111 becomes 11011101111, the remainder is still 000 and the error passes unseen. Generators are chosen so that the error patterns most likely on a real channel are never multiples of them.
| Error type | Detected when |
|---|---|
| Any single-bit error | The generator has more than one term (always true in practice) |
| Any odd number of bit errors | The generator has x + 1 as a factor |
| Any burst of length r or less | The generator has degree r and a non-zero constant term |
| Longer bursts | Detected with high probability; for random errors about 1 in 2r slip through |
For a 32-bit CRC, a random corruption goes undetected roughly once in four billion cases, which is why CRC-32 is used on Ethernet frames.
The hardware: a shift register with XOR taps
Although the algorithm for cyclic redundancy calculations looks complicated, it only involves shifting and exclusive OR operations. CRC can therefore be implemented efficiently in hardware using a shift register modified with XOR gates. The shift register has the same number of bits as the degree of the generator polynomial, with an XOR gate feeding each bit where the generator polynomial coefficient is one. The xr term needs no gate: it is the bit being shifted out, which becomes the feedback.
The Verilog below computes the common CRC-8 with generator x8 + x2 + x + 1 (written 0x07, dropping the x8 term), one message bit per clock, most significant bit first, starting from zero. The feedback is the top register bit XORed with the incoming bit; it is fed into bit 0 and XORed into bits 1 and 2, the positions where the generator has a 1.
// Serial CRC-8, generator x^8 + x^2 + x + 1 (0x07), MSB first, initial value 0x00.
module crc8_serial (
input wire clk, rst_n,
input wire clear, // load the initial value
input wire en, // one message bit per clock when high
input wire din,
output reg [7:0] crc
);
wire fb = crc[7] ^ din; // feedback = bit shifted out XOR incoming bit
always @(posedge clk or negedge rst_n)
if (!rst_n) crc <= 8'h00;
else if (clear) crc <= 8'h00;
else if (en) begin
crc[7:3] <= crc[6:2];
crc[2] <= crc[1] ^ fb; // x^2 tap
crc[1] <= crc[0] ^ fb; // x^1 tap
crc[0] <= fb; // x^0 tap
end
endmodule
module tb;
reg clk = 0, rst_n = 0, clear = 0, en = 0, din = 0;
wire [7:0] crc;
reg [8*9-1:0] msg = "123456789";
integer i;
crc8_serial dut (clk, rst_n, clear, en, din, crc);
always #5 clk = ~clk;
initial begin
#12 rst_n = 1;
@(negedge clk) en = 1;
for (i = 8*9-1; i >= 0; i = i - 1) begin din = msg[i]; @(negedge clk); end
en = 0;
$display("CRC-8 of \"123456789\" = %h (expected f4)", crc);
$finish;
end
endmodule
The standard check value for this CRC-8 variant over the ASCII string "123456789" is F4, and the simulation prints exactly that:
CRC-8 of "123456789" = f4 (expected f4)
A serial CRC handles one bit per clock. High-speed links need many bits per clock, so designers unroll the same equations to process 8, 32 or more bits in parallel. The XOR network gets wider, but the mathematics is the same.
Common mistakes
- Forgetting to append r zeros before dividing in a hand calculation, which gives the wrong remainder.
- Mixing up bit order. Many protocols send the least significant bit first and use a reflected form of the CRC; the generator is the same but the register shifts the other way.
- Ignoring the initial value and the final XOR. CRC-32 for Ethernet, for example, starts the register at all ones and inverts the result, so a plain division gives a different number.
- Thinking a CRC protects against deliberate tampering. It detects accidental errors only; anyone can recompute it after changing the data.
FAQ
Why is it called cyclic?
The codes come from cyclic codes, where any cyclic shift (rotation) of a valid codeword is also a valid codeword, a property that follows from the polynomial structure.
How many bits is the CRC?
The same as the degree of the generator. A generator of degree 3, such as 1011, gives a 3-bit CRC; CRC-32 uses a degree 32 generator.
Can a CRC correct errors?
CRCs are used for detection. Some short CRCs can locate a single-bit error in a short frame, but in practice a failed CRC triggers a retransmission or a discard.


my 2 cents...
ReplyDeleteThe polynomial represents the behaviour of the output of the last flip flop.
The exponent on the variable x represents the number of clock cycles of delay.
From polynomials to hardware:
The maximum exponent denotes the number of flops The other exponents denote the flops that tap off of feedback line from last flop
Here is an article that describes different parallel CRC algorithms for Verilog/VHDL: CRC article
ReplyDeleteThe online tool that generates parallel/serial CRC is here:CRC generation tool
Thanks