You are on page 1of 5

CRC Series, Part 2: CRC Mathematics and Theory | Michael Barr

CRC Series, Part 2: CRC Mathematics and Theory


By Michael Barr

Checksum algorithms based solely on addition the packet--for example, the received checksum
are easy to implement and can be executed bits do not accurately describe the received
efficiently on any microcontroller. However, message bits--it may either discard the packet
many common types of transmission errors and request a retransmission (error detection)
cannot be detected when such simple or attempt to repair the damage on its own
checksums are used. This article describes a (error correction). If packet repairs are to be
stronger type of checksum, commonly known as attempted, the checksum is said to be an error
a CRC. correcting code.

A cyclic redundancy check (CRC) is based on The key to repairing corrupted packets is a
division instead of addition. The error detection stronger checksum algorithm. Specifically,
capabilities of a CRC make it a much stronger what's needed is a checksum algorithm that
checksum and, therefore, often worth the price distributes the set of valid bit sequences
of additional computational complexity. randomly and evenly across the entire set of
possible bit sequences. For example, if the
Additive checksums are error detection codes minimum number of bits that must change to
as opposed to error correction codes. A turn any one valid packet into some other valid
mismatch in the checksum will tell you there's packet is seven, then any packet with three or
been an error but not where or how to fix it. In fewer bit inversions can be corrected
implementation terms, there's not much automatically by the receiver. (If four bit errors
difference between an error detection code and occur during transmission, the packet will seem
an error correction code. In both cases, you closer to some other valid packet with only
take the message you want to send, compute three errors in it.) In this case, error correction
some mathematical function over its bits can only be done for up to three bit errors,
(usually called a checksum), and append the while error detection can be done for up to six.
resulting bits to the message during
transmission. This spreading of the valid packets across the
space of possible packets can be measured by
Error Correction the Hamming distance, which is the number of
bit positions in which any two equal-length
The difference between error detection and packets differ. In other words, it's the number
error correction lies primarily in what happens of bit errors that must occur if one of those
next. If the receiving system detects an error in packets is to be incorrectly received as the

www.barrgroup.com | Copyright Barr Group. All rights reserved. 1


CRC Series, Part 2: CRC Mathematics and Theory | Michael Barr

other. A simple example is the case of the two Binary Long Division
binary strings 1001001 and 1011010, which are
separated by a Hamming distance of three. (To It turns out that once you start to focus on
see which bits must be changed, simply XOR the maximizing the "minimum Hamming distance
two strings together and note the bit positions across the entire set of valid packets," it
that are set. In our example, the result is becomes obvious that simple checksum
0010011.) algorithms based on binary addition don't have
the necessary properties. A change in one of
The beauty of all this is that the mere presence the message bits does not affect enough of the
of an error detection or correction code within checksum bits during addition. Fortunately, you
a packet means that not all of the possible don't have to develop a better checksum
packets are valid. Figure 1 shows what a packet algorithm on your own. Researchers figured out
looks like after a checksum has been appended long ago that modulo-2 binary division is the
to it. Since the checksum bits contain redundant simplest mathematical operation that provides
information (they are completely a function of the necessary properties for a strong checksum.
the message bits that precede them), not all of
the 2(m+c) possible packets are valid packets. In All of the CRC formulas you will encounter are
fact, the stronger the checksum algorithm used, simply checksum algorithms based on modulo-2
the greater the number of invalid packets will binary division. Though some differences exist
be. in the specifics across different CRC formulas,
the basic mathematical process is always the
same:

• The message bits are appended with c


zero bits; this augmented message is
the dividend
Figure 1. A packet of information including
• A predetermined c+1-bit binary
checksum
sequence, called the "generator
polynomial", is the divisor
By adjusting the ratio of the lengths m and c
• The checksum is the c-bit remainder
and carefully selecting the checksum algorithm,
that results from the division operation
we can increase the number of bits that must
be in error for any one valid packet to be
In other words, you divide the augmented
inadvertently changed into another valid packet
message by the generator polynomial, discard
during transmission or storage and, hence, the
the quotient, and use the remainder as your
likelihood of successful transmission. In
checksum. It turns out that the mathematically
essence, what we want to do is to maximize the
appealing aspect of division is that remainders
"minimum Hamming distance across the entire
fluctuate rapidly as small numbers of bits within
set of valid packets." In other words, to
the message are changed. Sums, products, and
distribute the set of 2m valid packets as evenly
quotients do not share this property. To see
as possible across the set of possible bit
what I mean, look at the example of modulo-2
sequences of length m + c. This has the useful
division in Figure 2. In this example, the
real-world effect of increasing the percentage
message contains eight bits while the checksum
of detectable and/or correctable errors.
is to have four bits. As the division is performed,

www.barrgroup.com | Copyright Barr Group. All rights reserved. 2


CRC Series, Part 2: CRC Mathematics and Theory | Michael Barr

the remainder takes the values 0111, 1111, Generator Polynomials


0101, 1011, 1101, 0001, 0010, and, finally,
0100. The final remainder becomes the Why is the predetermined c+1-bit divisor that's
checksum for the given message. used to calculate a CRC called a generator
polynomial? In my opinion, far too many
explanations of CRCs actually try to answer that
question. This leads their authors and readers
down a long path that involves tons of detail
about polynomial arithmetic and the
mathematical basis for the usefulness of CRCs.
This academic stuff is not important for
understanding CRCs sufficiently to implement
and/or use them and serves only to create
potential confusion. So I'm not going to answer
that question here.2

Suffice it to say here only that the divisor is


sometimes called a generator polynomial and
that you should never make up the divisor's
Figure 2. An example of modulo-2 binary value on your own. Several mathematically
division well-understood generator polynomials have
been adopted as parts of various international
For most people, the overwhelmingly confusing communications standards; you should always
thing about CRCs is the implementation. use one of those. If you have a background in
Knowing that all CRC algorithms are simply long polynomial arithmetic then you know that
division algorithms in disguise doesn't help. certain generator polynomials are better than
Modulo-2 binary division doesn't map well to others for producing strong checksums. The
the instruction sets of general-purpose ones that have been adopted internationally
processors. So, whereas the implementation of are among the best of these.
a checksum algorithm based on addition is
straightforward, the implementation of a binary Table 1 lists some of the most commonly used
division algorithm with an m+c-bit numerator generator polynomials for 16- and 32-bit CRCs.
and a c+1-bit denominator is nowhere close.1 Remember that the width of the divisor is
For one thing, there aren't generally any m+c or always one bit wider than the remainder. So,
c+1-bit registers in which to store the operands. for example, you'd use a 17-bit generator
You will learn how to deal with this problem in polynomial whenever a 16-bit checksum is
the next article, where I talk about various required.
software implementations of the CRC
algorithms. For now, let's just focus on their
strengths and weaknesses as potential
checksums.

Table 1. International standard CRC polynomials

www.barrgroup.com | Copyright Barr Group. All rights reserved. 3


CRC Series, Part 2: CRC Mathematics and Theory | Michael Barr

As is the case with other types of checksums, • A message with any one bit in error
the width of the CRC plays an important role in • A message with any two bits in error
the error detection capabilities of the (no matter how far apart, which
algorithm. Ignoring special types of errors that column, and so on)
are always detected by a particular checksum • A message with any odd number of bits
algorithm, the percentage of detectable errors in error (no matter where they are)
is limited strictly by the width of a checksum. A • A message with an error burst as wide
checksum of c bits can only take one of 2c as the checksum itself
unique values. Since the number of possible
messages is significantly larger than that, the The first class of detectable error is also
potential exists for two or more messages to detected by an addition-based checksum, or
have an identical checksum. If one of those even a simple parity bit. However, the middle
messages is somehow transformed into one of two classes of errors represent much stronger
the others during transmission, the checksum detection capabilities than those other types of
will appear correct and the receiver will checksum. The fourth class of detectable error
unknowingly accept a bad message. The chance sounds at first to be similar to a class of errors
of this happening is directly related to the width detected by addition-based checksums, but in
of the checksum. Specifically, the chance of the case of CRCs, any odd number of bit errors
such an error is 1/2c. Therefore, the probability will be detected. So the set of error bursts too
of any random error being detected is 1-1/2c. wide to detect is now limited to those with an
even number of bit errors. All other types of
To repeat, the probability of detecting any errors fall into the relatively high 1-1/2c
random error increases as the width of the probability of detection.
checksum increases. Specifically, a 16-bit
checksum will detect 99.9985% of all errors.
Ethernet, SLIP, and PPP
This is far better than the 99.6094% detection
rate of an eight-bit checksum, but not nearly as
Ethernet, like most physical layer protocols,
good as the 99.9999% detection rate of a 32-bit
employs a CRC rather than an additive
checksum. All of this applies to both CRCs and
checksum. Specifically, it employs the CRC-32
addition-based checksums. What really sets
algorithm. The likelihood of an error in a packet
CRCs apart, however, is the number of special
sent over Ethernet being undetected is,
cases that can be detected 100% of the time.
therefore, extremely low. Many types of
For example, I pointed out last month that two
common transmission errors are detected 100%
opposite bit inversions (one bit becoming 0, the
of the time, with the less likely ones detected
other becoming 1) in the same column of an
99.9999% of the time. Even if an error would
addition would cause the error to be
somehow manage to get through at the
undetected. Well, that's not the case with a
Ethernet layer, it would probably be detected at
CRC.
the IP layer checksum (if the error is in the IP
header) or in the TCP or UDP layer checksum
By using one of the mathematically well-
above that. After all the chances of two or more
understood generator polynomials like those in
different checksum algorithms not detecting
Table 1 to calculate a checksum, it's possible to
the same error is extremely remote.
state that the following types of errors will be
detected without fail:

www.barrgroup.com | Copyright Barr Group. All rights reserved. 4


CRC Series, Part 2: CRC Mathematics and Theory | Michael Barr

However, many embedded systems that use Footnotes


TCP/IP will not employ Ethernet. Instead, they
will use either the serial line Internet protocol [1] Implementing modulo-2 division is much
(SLIP) or point-to-point protocol (PPP) to send more straightforward in hardware than it is in
and receive IP packets directly over a serial software. You simply need to shift the message
connection of some sort. Unfortunately, SLIP bits through a linear feedback shift register as
does not add a checksum or a CRC to the data they are received. The bits of the divisor are
from the layers above. So unless a pair of represented by physical connections in the
modems with error correction capabilities sits in feedback paths. Due to the increased simplicity
between the two communicating systems, any and efficiency, CRCs are usually implemented in
transmission errors must hope to be detected hardware whenever possible.
by the relatively weak, addition-based Internet
checksum described last month. The newer, [2] If you really want to understand the
compressed SLIP (CSLIP) shares this weakness underlying mathematical basis for CRCs, I
with its predecessor. PPP, on the other hand, recommend the following reference: Bertsekas,
does include a 16-bit CRC in each of its frames, Dimitri and Robert Gallager. Data Networks,
which can carry the same maximum size IP second ed. Inglewood Cliffs, NJ: Prentice-Hall,
packet as an Ethernet frame. So while PPP 1992, pp. 61-64.
doesn't offer the same amount of error
detection capability as Ethernet, by using PPP This article began as a column in the December
you'll at least avoid the much larger number of 1999 issue of Embedded Systems Programming.
undetected errors that may occur with SLIP or If you wish to cite the article in your own work,
CSLIP. you may find the following MLA-style
information helpful:
Read my article on CRC calculations in C, to
learn about various software implementations Barr, Michael. "For the Love of the Game,"
of CRCs. We'll start with an inefficient, but Embedded Systems Programming, December
comprehendible, implementation and work to 1999 , pp. 47-54.
gradually increase its efficiency. You'll see then
that the desire for an efficient implementation
is the cause of much of the confusion Related webinars and topics can be found at:
https://barrgroup.com/Embedded-
surrounding CRCs. In the meantime, stay
Systems/How-To/Additive-Checksums
connected.
https://barrgroup.com/Embedded-
Systems/How-To/CRC-Calculation-C-Code

www.barrgroup.com | Copyright Barr Group. All rights reserved. 5

You might also like