# The Cyclic Redundancy Check with example

- A significant role of the Data Link layer is to convert the potentially unreliable physical link between two machines into an apparently very reliable link.
- This is done by including redundant information in each transmitted frame. Depending on the nature of the link and the data one can either:
- include just enough redundancy to make it possible to detect errors and then arrange for the retransmission of damaged frames, or
- include enough redundancy to enable the receiver to correct any errors produced during transmission.

Most current networks take the former approach.

- One widely used parity bit based error detection scheme is the cyclic redundancy check or CRC.
- The CRC is based on some fairly impressive looking mathematics. It is helpful as you deal with its mathematical description that you recall that it is ultimately just a way to use parity bits.
- The presentation of the CRC is based on two simple but not quite “everyday” bits of mathematics:
- polynomial division
- arithmetic over the field of integers mod 2.

- Arithmetic over the field of integers mod 2 is simply arithmetic on single bit binary numbers with all carries (overflows) ignored. So 1 + 1 = 0 and so does 1 – 1. In fact, addition and subtraction are equivalent in this form of arithmetic.
- Polynomial division isn’t too bad either. There is an algorithm for performing polynomial division that looks a lot like the standard algorithm for integer division. More interestingly from the point of view of understanding the CRC, the definition of division (i.e. the definition of the quotient and remainder) are parallel.
- When one says “dividing a by b produces quotient q with remainder r” where all the quantities involved are positive integers one really means that

*a = q b + r*and that

*0 <=r < b* - When one says “dividing a by b produces quotient q with remainder r” where all the quantities are polynomials, one really means the same thing as when working with integers except that the meaning of “less than” is a bit different. For polynomials, less than means of lesser degree. So, the remainder of a polynomial division must be a polynomial of degree less than the divisor.

- When one says “dividing a by b produces quotient q with remainder r” where all the quantities involved are positive integers one really means that
- Now, we can put this all together to explain the idea behind the CRC.
- Any particular use of the CRC scheme is based on selecting a generator polynomial G(x) whose coefficients are all either 0 or 1.

Just to be different from the book, we will use

*x*^{3}+ x^{2}+ 1as our example of a generator polynomial.

- Given a message to be transmitted:

*b*_{n}b_{n-1}b_{n-2}. . . b_{2}b_{1}b_{0}view the bits of the message as the coefficients of a polynomial

*B(x) = b*_{n}x^{n}+ b_{n-1}x^{n-1}+ b_{n-2}x^{n-2}+ . . . b_{2}x^{2}+ b_{1}x + b_{0} - Multiply the polynomial corresponding to the message by
*x*where k is the degree of the generator polynomial and then divide this product by the generator to obtain polynomials Q(x) and R(x) such that:^{k}

*x*^{k}B(x) = Q(x) G(x) + R(x)Treating all the coefficients not as integers but as integers modulo 2.

- Finally, treat the coefficients of the remainder polynomial, R(X) as “parity bits”. That is, append them to the message before actually transmitting it.
- Since the degree of R(x) is less than k, the bits of the transmitted message will correspond to the polynomial:

*x*^{k}B(x) + R(x) - Since addition and subtraction are identical in the field of integers mod 2, this is the same as

*x*^{k}B(x) – R(x) - From the equation that defines division, however, we can conclude that:

*x*^{k}B(x) – R(x) = Q(x) G(x)In other words, if the transmitted message’s bits are viewed as the coeeficients of a polynomial, then that polynomial will be divisible by G(X).

- This last fact is the basis of error checking using the CRC. When a message is received the corresponding polynomial is divided by G(x). If the remainder is non-zero, an error is detected. Otherwise, the message is assumed to be correct.

- Any particular use of the CRC scheme is based on selecting a generator polynomial G(x) whose coefficients are all either 0 or 1.

- How about an example:
- Suppose we want to send a nice short message like 11010111 using the CRC with the polynomial
*x*as our generator.^{3}+ x^{2}+ 1 - The message corresponds to the polynomial:

*x*^{7}+ x^{6}+ x^{4}+ x^{2}+ x + 1 - Given G(x) is of degree 3, we need to multiply this polynomial by
*x*and then divide the result by G(x).^{3} - I’ll have to think about how to get this formatted better, but basically we have:

*x*^{7}+*x*^{2}+1 *x*^{3}+*x*^{2}+1 ) *x*^{10}+*x*^{9}+*x*^{7}+*x*^{5}+*x*^{4}+*x*^{3}*x*^{10}+*x*^{9}+*x*^{7}*x*^{5}+*x*^{4}+*x*^{3}*x*^{5}+*x*^{4}+*x*^{2}*x*^{3}+*x*^{2}*x*^{3}+*x*^{2}+1 1 - The punchline is that the remainder is just “1”. So, the parity bits added in this case would be 001.

- Suppose we want to send a nice short message like 11010111 using the CRC with the polynomial
- I hope this is all strange enough that you feel compelled to ask “Why bother?”. If so, the answer comes in two parts:
- While the computation of parity bits through polynomial division may seem rather complicated, with a little reflection on how the division algorithm works we will see how to implement a very simple algorithm for computing CRC bits and/or verifying the correctness of the CRC associated with a received message.
- The relationship between the bits and the polynomials will give us some mathematical leverage that will make it possible to prove facts about the sorts of errors the CRC associated with a given generator will detect.

- Let’s start by seeing how the mathematics underlying the CRC can be used to investigate its ability to detect errors.
- Suppose that we transmit the message corresponding to some polynomial B(x) after adding CRC bits. In this case, the transmitted bits will correspond to some polynomial, T(x), where

*T(x) = B(x) x*^{k}– R(x)where k is the degree of the generator polynomial and R(x) is the remainder obtained when B(x) is divided by the generator.

- Remember, the key property of T(x) is that it is divisible by G(x) (i.e. G(x) is a factor of T(x)).
- Now, if during transmission some of the bits of the message are damaged, the actual bits received will correspond to a different polynomial, T'(x). If we imagine computing

*E(x) = T(x) – T'(x)*then the coefficients of E(x) will correspond to a bit string with a one in each position where T(x) differed from T'(x) and zeroes everywhere else. Thus, E(x) corresponds to a bitmap of the positions at which errors occurred.

- As long as T'(x) is not divisible by G(x), our CRC bits will enable us to detect errors. SO, the cases we are really interesting are those where T'(x) is divisible by G(x).
- Given that we already know that T(x) is divisible by G(x), T'(x) must be divisible by G(x) if and only if E(x) is divisible by G(x). So, we can investigate the forms of errors that will go undetected by investigating polynomials, E(x), that are divisible by G(x). Better yet, one might prefer to say we can design good parity bit schemes by looking for polynomial, G(x), that do not evenly divide examples of E(x) that correspond to anticipated error patterns.
- Well, at the very least, it would be nice to make sure that the CRC did as well as adding a single parity bit. That is, we would like to avoid using any G(x) that did not guarantee we could detect all instances of errors that change an odd number of bits.
- If a received message T'(x) contains an odd number of inverted bits, then E(x) must contain an odd number of terms with coefficients equal to 1.
- As a result, E(1) must equal to 1 (since if x = 1 then
*x*for all i).^{i}= 1 - If G(x) is a factor of E(x), then G(1) would also have to be 1.
- So, if we make sure that G(1) = 0, we can conclude that G(x) does not divide any E(x) corresponding to an odd number of error bits. In this case, a CRC based on G(x) will detect any odd number of errors.
- As long as G(x) has some factor of the form
*x*, G(1) will equal 0. So, it isn’t hard to find such a polynomial.^{i}+ 1

- As a sanity check, consider the CRC associated with the simplest G(x) that contains a factor of the form
*x*, namely^{i}+ 1*x + 1*.- The CRC for any message consisting entirely of zeroes will be zero.
- Given that the code is guaranteed to detect any error involving an odd number of bits, if we start with all zeroes and add 1’s in various posisiton, the parity bit must flip each time we add a one.
- Accordingly, the value of the parity bit will be 1 if and only if the number of 1’s is odd.
- In other words, when the generator is
*x+1*the CRC is just a single even parity bit!

- Having discovered this amusing fact, let’s make sure that the CRC does more than a single parity bit if we choose an appropriate polynomial of higher degree. Consider how the CRC behaves is G(x) is
*x*for some k larger than one.^{k}+1- Obviously, this CRC will catch any error that changes an odd number of bits.
- I argued last time, however, that one generally worries more about burst errors than isolated errors. So, consider the case where a burst error affects some subset of j consecutive bits for j < k.
- In this case, the error polynomial will look like

*E(x) = x*^{n1}+ x^{n2}+ … x^{nr}where we assume that

*n*for all i and that_{i}> n_{i+1}*n*._{1}– n_{r}<= jFactoring out the lowest degree term in this polynomial gives:

*E(x) = x*^{nr}(x^{n1-nr}+ x^{n2-nr}+ … + 1 ) - Now,
*G(x) = x*can not divide^{k}+ 1*x*. So, the only way that G(x) can divide E(x) is if if divides^{nr}*x*. However, G(x) can not possible divide a polynomial of degree less than k. So, it can not divide E(x).^{n1-nr}+ x^{n2-nr}+ … + 1 - Thus, we can conclude that the CRC based on our simple G(x) detects all burst errors of length less than its degree.

- Suppose that we transmit the message corresponding to some polynomial B(x) after adding CRC bits. In this case, the transmitted bits will correspond to some polynomial, T(x), where