Easy Painless Learning CRC guide

Source: Internet
Author: User
Tags arithmetic modulus

Original address: http://www.repairfaq.org/filipg/LINK/F_crc_v31.html 1. Preface 1.1 about copyright and author

"Everything you wanted to know about CRC algorithms, but were afraid to ask for fear that errors in your understanding MIG HT is detected. "
  You want to know all about CRC algorithms, but are afraid to ask something you don't understand, because it may be wrong. Author: Ross N. Williams e-mail: ross@guest.adelaide.edu.au Date: August 1993 version: 3.00 FTP address: ftp.adelaide.edu.au/pub/rocksoft/ Crc_v3.txt: http://www.on.net/clients/rocksoft/rocksoft/Company: Rocksoft (TM) Pty Ltd Address: Lerwick Avenue, Hazelwood Park 5066, Australia Fax: +61 8 373-4911 (c/-internode Systems Pty Ltd) Tel: +61 8 379-9217 (10am to 10pm Adelaide Australia time) Note: "Rocksoft" is a trademark of Rocksoft Pty Ltd, Australia Status: Copyright (C) Ross Williams, 1993,1994,1995,1996. However, permission is granted to make and distribute verbatim copies the This document provided so this information bloc K and copyright notice is included. Also, the C code modules included in this document are fully public DOMAIN (PD). Thank you: Jean-loup gailly (jloup@chorus.fr) and Mark Adler (me@quest.jpl.nasa.gov) who both proof read this document a nd picked out lots of nits as, some big fat bugs.

C source code used in this document: Crcmodel.h (9KB) crcmodel.c (6KB) crctable.c (8KB) 1.2 abstract

This document describes the CRCs (cyclic redundancy codes, cyclic redundancy code) and details the implementation methods based on Table-driven. There's actually a lot of literature on CRCs, especially based on Table-driven, but it's hard to understand (at least for me). Therefore, this document attempts to explain CRCs in a straightforward manner (not loosely), and it records every detail of the implementation method. In addition, this document gives a parameterized model of CRC algorithm--ROCKSOFT^TM models CRC algorithm. This model algorithm can represent most CRC by means of parameterization, so if you want to know a particular algorithm, you may wish to refer to the method in this document first. The above C source code provides a low speed CRC algorithm model implementation, the last section gives the implementation of high-speed Table-driven 2 forms, and provides a generation of CRC lookup table program. 2. Introduction: Error Detection

The goal of the error detection technique is to enable the receiver to know if the information received is corrupted because of noise interference in the transport channel and may introduce an error to the transmitted information. In general, the sender invokes a function to generate a value called checksum (checksum) for the information that is about to be transmitted, and attaches that value to that information. When the receiver receives the information, the same function is invoked to calculate the checksum of the information and to compare it with the checksum that accompanies the information to confirm that the information is correct. For example, we simply sum the bytes in the transport message, then use the and mod 256 (that is, modulo 256), and they might look like this: (All numbers are decimal)

Message                    :  6/  4 message with
checksum      :  6  4
transmission:  6  4 33

As above, the 2nd byte of information is compromised during transmission (from 23 to 27). However, the receiver is able to compare the computed checksum (37) with the checksum (33) that accompanies the information. However, if the checksum itself is compromised during transmission, the correct information may be judged to be wrong. Indeed, this is a security flaw. This can lead to a dangerous error when the and/or checksum of the transmitted information is corrupted and the transmitted information is consistent. Unfortunately, the possibility of such a mistake is completely unavoidable, and the best way to reduce this possibility is to increase the amount of checksum (for example, officers transferred Guevara and from 1 bytes to 2 bytes).

Other error detection techniques involve the complex conversion of information and the injection of redundant information. However, this document only explains the CRC algorithm, and after the error detection process, the transmitted information consists of the original information data and the checksum that is appended thereafter. Such as:

<original intact message> <checksum>
:<       raw information      > < checksum >
3. The need for complexity

In the checksum example in the previous section, we detected whether the transmitted information was corrupted by simply adding the bytes directly, and then checking the checksum algorithm for the 256 modulo.

Message                    :  6/  4 message with
checksum      :  6  4
transmission: 
  
   6  4 33
  

The problem with this algorithm is that it's too simple. If there is a lot of random damage to the data at the same time, the wrong message has a 1/256 chance of being considered correct. Such as:

Message                    :  6/  4 message with
checksum      :  6  4
transmission:  8  5 33

In order to enhance the calibration effect, we try to change from 8-bit registers to 16-bit registers (that is, modulo 65536 replaces the original 256), and obviously the probability of judgment error is reduced from 1/256 to 1/65536. This is basically a good idea, but the problem now is that the formula used is not "random"--the simple summation formula, no matter how wide the sum register is, each byte moved can only affect one byte of it. For example, in the second example above, even though the sum register has a megabyte-level width, the error is still not detected. The solution to this problem is to replace the simple summation formula with more complex formulas, and this complex formula should allow each byte to be moved to affect the sum register.

Therefore, we see that a robust checksum algorithm is required at least for the following two areas:

width
The width of a register can reduce this error probability to a low enough probability (if it is a 32-bit register, the probability is 1/2^32).
  
Complexity
A formula is needed to allow each byte to be moved to change the registers of any number of digits, thus generating enough confusion and randomness.

Note : The term "checksum" is presumably used to describe the early summation formula, but it now has a broader meaning, including the values produced by complex algorithms such as CRC. CRC algorithm can meet the requirements of complexity well and can adapt to different checksum widths. 4. The basic idea behind CRC algorithm

Where can we find formulas that are more complex and appropriate than the sum formula? Naturally think of a variety of designs, we can use the PI or hash, so that each moved into the byte and register all the bytes in the form of a digital table. You can even save a large phone book on the web and have each byte moved in by indexing a new phone number as the next register value. Therefore, the method of processing is infinite.

However, we do not need to go this far, and the algorithm described next is sufficient to meet the requirements. Addition is clearly not sufficient to form a valid checksum, but division can (as long as the divisor width and the checksum register width is the same). The basic idea of the

CRC algorithm is to simply treat the information as a huge binary number and then remove it with another fixed binary number and get the remainder from it. When the message is received, the receiver performs the division operation with the same divisor, and then compares the resulting remainder with the checksum (at which point the checksum is actually the remainder).

Example: Suppose the message is composed of 2 bytes (6,23), as in the example above. The hexadecimal number is expressed as 0x0617, and the binary representation is 0000-0110-0001-0111. Assuming the width of the checksum register is 1 bytes, and constant factor 1001 is the divisor, then the checksum equals the remainder of 0000-0110-0001-0111 divided by 1001. In this way, this method is obviously also applicable to 32-bit checksum registers, and the resulting checksum is confusing. So, we're going to use long division that's a long, very good one (what you learned in school, remember.) ), but this time, it is used in binary:

          ... 0000010101101 = 00AD = 173 = quotient ____-___-___-___-9= 1001) 0000011000010111 = 0617 = 1559 = Dividend DIVI SOR 0000.,,....,.,,,----.,,....,.,,, 0000,,....,.,,, 0000,,....,.,,,----,,.
             ,.,,, 0001,....,.,,, 0000,....,.,,,----,....,.,,, 0011 ...,.,,, 0000 ...,.,,,----....,.,,, 0110 ...,.,,, 0000 ...,.,,,----.
                ,.,,, 1100 ...,.,,, 1001.,.,,, = =,.,,, 0110.,.,,, 0000.,.,,,----.,.,,, 1100,.,,, 1001,.,,, = = =
                   =,.,,, 0111.,,, 0000.,,,----.,,, 1110,,, 1001,,, = =,,, 1011, 1001,, =
     ===,,                0101, 0000,----1011 1001 = = 0010 = 2 = remainder

In decimal, 1559 divided by 9 gets a quotient of 173, and the remainder is 2.
  
Although not every bit of input information has an impact on the quotient, the 4-bit remainder is affected considerably in the calculation, and its value is more affected if the input information (dividend) has more bytes. That's why we use division rather than addition to compute the checksum.
  
As you would expect, using 4-bit checksums, the transmission information will become 0x06172 (0617 is the original information, 2 is the checksum). The receiver then uses 0x0617 divided by 0x9 to determine whether the remainder is 0x2. 5. Polynomial algorithm

The long Division check method described in the previous section is very similar to what is known as the CRC Test, in fact, the CRC checksum is a bit odd, and we need to delve into some strange digital systems to understand them.
  
When dealing with CRC algorithms, you will hear a word--polynomial. A given CRC algorithm is referred to as using a specific polynomial, and CRC algorithms are usually manipulated using polynomial operations. What do you mean by that?
  
Unlike the previous section, the divisor, divisor (information), quotient, and remainder are treated as positive integers, but they are regarded as the binary coefficients of the polynomials. Each number is expanded to a bit string, and the bit is the coefficient of a polynomial. For example, the normal number 23 (decimal) hexadecimal is 0x17 and the binary is 0b10111, so its corresponding polynomial is:

   1*x^4 + 0*x^3 + 1*x^2 + 1*x^1 + 1*x^0

Alternatively, it is simpler to say:

   X^4 + x^2 + x^1 + x^0

With this technique, the divisor (information) and divisor can be expressed in a polynomial, except for a few more x now, we can all use all the algorithms as before. For example, suppose we multiply the 0b1101 by the 0b1011, we can do this by using a polynomial:

(x^3 + x^2 + x^0) (x^3 + x^1 + x^0)
= (x^6 + x^4 + x^3
 + x^5 + x^3 + x^2
 + x^3 + x^1 + x^0) = x^6 + x^5 + x^4 + 3*x^3 + x^2 + x^1 + x^0

At this point, in order to get the correct answer, we need to do some processing, because binary binary into one, so 3*x^3 items need to carry, get:

   X^7 + x^3 + x^2 + x^1 + x^0

Yes, polynomial operations are like ordinary arithmetic operations, just a little abstract, and all the items are explicitly included in the calculation. So, what's the problem now?

The problem is, if we don't know how much x is, we can't go on. We don't know 3*x^3 equals x^4 + x^3, because we don't know X is 2. In polynomial operations, the relationship between all coefficients is unknown, so the coefficients of each X-power are actually strongly typed, meaning that the coefficients of the x^2 and the x^3 coefficients are two completely different types.
Because each X-power coefficient is separated from each other, mathematicians come up with a variety of polynomial algorithms that simply change the rules of the polynomial coefficients. Thus, an operation rule called "Polynomial Mode 2" is designed, which requires that all coefficients of polynomials must be 0 or 1, and that the Multimode 2 division uses modulo 2 subtraction (binary subtraction without borrow). Back to the previous example:

(x^3 + x^2 + x^0) (x^3 + x^1 + x^0)
= (x^6 + x^4 + x^3
 + x^5 + x^3 + x^2
 + x^3 + x^1 + x^0)
= x^6 + x^5 + x^4 + 3*x^3 + x^2 + x^1 + x^0

We try to use a different rule of operation, 3*x^3 the previous operation rule to produce a carry (because we know x=2). Now we're going to use the modulo 2 algorithm, assuming we don't know what X is and don't carry it, modulo 2 for all the polynomial coefficients, and you get the following results:

= x^6 + x^5 + x^4 + x^3 + x^2 + x^1 + x^0

As Knuth [Knuth81] Says (p.400):
"The reader should pay attention to the similarity between the polynomial algorithm and the multi-precision arithmetic (4.3.1), where Cardinal B replaces X. The main difference is that the coefficients of the polynomial x^k u_k are not related to the adjacent coefficients (x^{k-1} and X^{k+1}), so there is no concept of carry or borrow. In fact, a polynomial operation with a modulus of B is essentially the same as a multiple-precision arithmetic operation with a cardinality of B, except that the latter has a carry or borrow operation. ”
  
Therefore, the polynomial modulo 2 operation is a binary arithmetic operation with a modulus of 2, without rounding or borrow. Polynomial is a useful mathematical tool in the study and analysis of CRC or other error correction algorithms. In order to illustrate them, we do not have too much expansion and verbose, there is no excessive explanation of the remainder, more through the example of direct use of related arithmetic. Remember, then, the characteristics of the polynomial modulo 2 algorithm-a binary operation without a carry or borrow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.