CRC algorithm and implementation

Source: Internet
Author: User
Tags 0xc0 rfc aliyun

Abstract:This paper first discusses the CRC of the mathematical algorithm, and then take the common CRC-ITU as an example, through the implementation of hardware circuit, this paper introduces the bit algorithm, finally focuses on the byte fast look-up algorithm, the corresponding C language implementation is provided.

Keywords:CRC, FCS, generate polynomial, check error retransmission

Introduction

CRC is called cyclic redundancy check, and the Chinese name is called cyclic redundancy check. It is an important linear grouping code. It has simple coding and decoding methods and strong error checking and Error Correction capabilities. It is widely used in the communication field to implement error control. In fact, in addition to data communication, CRC is also useful in many other fields. For example, when we read files on a floppy disk and decompress a zip file, we occasionally encounter a "bad CRC" error, which can be seen in data storage applications.

The error control theory is based on the algebra theory. Here, we will focus on introducing the CRC algorithm and implementation. We can only explain the principle in detail. If you need to learn more about the principles of linear code, grouping code, cyclic code, and Error Correction Code, read the relevant information.

The process of checking errors using CRC can be simply described as follows: Based on the K-bit binary code sequence to be transmitted at the sending end, generates an R-bit supervision code (CRC Code) for verification based on certain rules, which is attached to the original information to form a new binary code sequence with K + R bits, and then send it out. At the receiving end, check the rules observed between the Information Code and CRC code to determine whether an error occurs during transmission. This rule is called "generate polynomial" in error control theory ".

1. General Algorithms of the generation of Mathematics

In the algebraic coding theory, a code group is represented as a polynomial, and each code element in the code group is treated as a polynomial coefficient. For example, 1100101 indicates
1. X6 + 1 · X5 + 0 · X4 + 0 · X3 + 1 · X2 + 0 · x + 1, that is, X6 + X5 + X2 + 1.

Set the original information polynomial Before encoding to p (x), the maximum power of P (x) plus 1 equals K, the generated polynomial to G (x), g (x) the maximum power of is equal to R; the CRC polynomial is R (x); the encoded information polynomial with CRC is T (X ).

Sender's encoding method: multiply p (x) by XR (that is, the corresponding binary code sequence shifts the r bit left) and divide it by G (x). The obtained remainder is R (X ). Expressed
T (x) = xrp (x) + R (X)

Receiver decoding method: Divide T (x) by G (x). If the remainder is 0, no error occurs during transmission. Otherwise, the transmission is incorrect.

For example, if the Information Code is set to 1100 and the generated polynomial is 1011, that is, p (x) = X3 + X2, g (x) = X3 + x + 1, the process of calculating CRC is

      xrP(x)     x3(x3+x2)     x6+x5                    x     -------- = ---------- = -------- = (x3+x2+x) + --------       G(x)       x3+x+1      x3+x+1                 x3+x+1

That is, R (x) = x. Note that the maximum power of g (x) is r = 3, and the CRC value is 010.

If vertical division is used, the calculation process is

1110 ------- 1011/1100000 (1100 shifted to 3 places) 1011 ---- 1110 1011 ----- 1010 1011 ----- 0010 0000 ---- 010

Therefore, T (x) = (X6 + X5) + (x) = X6 + X5 + X, that is, 1100000 + 010 = 1100010

If the transmission is correct,

       T(x)     x6+x5+x      ------ = --------- = x3+x2+x,       G(x)     x3+x+1

No. Looking back at the vertical division above, if the dividend number is 1100010, it is obvious that the Division can be exhausted in the third division.

The above calculation process helps us understand the concept of CRC. But directly Programming to Implement the above algorithm is not only tedious, but also inefficient. In fact, CRC is not directly calculated and verified in the project.

The following table lists some CRC data that can be found in the standard:

Name Generate Polynomials Note * Application Example
CRC-4 X4 + x + 1 ITU g.704
CRC-12 X12 + X11 + X3 + x + 1
CRC-16 X16 + X12 + X2 + 1 1005 IBM SDLC
CRC-ITU ** X16 + X12 + X5 + 1 1021 ISO HDLc, ITU X.25, V.34/V.41/v.42, PPP-FCS
CRC-32 X32 + X26 + x23 +... + X2 + x + 1 04c11db7 Zip, rar, IEEE 802 LAN/FDDI, IEEE 1394, PPP-FCS
CRC-32c X32 + x28 + x27 +... + X8 + X6 + 1 1edc6f41 Sctp
* The highest power coefficient of the generated polynomial is fixed. Therefore, in the notation, the highest 1 is removed. For example, 04c11db7 is actually 127c11db7. ** CRC-CCITT. The predecessor of ITU is CCITT.

2. Implementation of hardware circuits

Polynomial division, which can be implemented by division circuit. The division circuit consists of a group of shift registers and a modulo 2 sub-device (exclusive or unit. Taking the CRC-ITU as an example, it consists of 16-level shift register and 3 processors, see (encoding/decoding sharing ). Each register is initialized to "1" Before encoding and decoding, and the information bit is moved with the clock. When all information bits are input, CRC results are output from the register group.

3-bit Algorithm

The above CRC-ITU division circuit, can be simulated by software. Define a register group and initialize it to "1 ". According to the circuit diagram, each input information bit is equivalent to a clock pulse, Which is shifted from high to low. The information bit before the shift is added to bit0 to generate a temporary bit. bit15 is moved to the temporary bit, and bit10 and bit3 are also added to the temporary bit. After all information bits are entered, their values are retrieved from the register group. This is the CRC code.

Typedef unsigned char bit; typedef unsigned char byte; typedef unsigned short 2010; typedef Union {2010val; struct {b2bit0: 1; b2bit1: 1; b2bit2: 1; service.eu-central-1.maxcompute.aliyun-inc.com/api; /service.cn.maxcompute.aliyun.com/api; // initialize the CRC register group: The Shift Register is set to full "1" Void crcinitregisters () {regs. val = 0 xFFFF;} // CRC input a bitvoid crcinputbit (bit in) {Bit A; A = regs. bits. bit0 ^ In; regs. bits. bit0 = regs. bits. bit1; regs. bits. bit1 = regs. bits. bit2; regs. bits. bit2 = regs. bits. bit3; regs. bits. bit3 = regs. bits. bit4 ^ A; regs. bits. bit4 = regs. bits. bit5; regs. bits. bit5 = regs. bits. bit6; regs. bits. bit6 = regs. bits. bit7; regs. bits. bit7 = regs. bits. bit8; regs. bits. bit8 = regs. bits. bit9; regs. bits. bit9 = regs. bits. bit10; regs. bits. bit10 = regs. bits. bit11 ^ A; regs. bits. bit11 = regs. bits. bit12; regs. bits. bit12 = regs. bits. bit13; regs. bits. bit13 = regs. bits. bit14; regs. bits. bit14 = regs. bits. bit15; regs. bits. bit15 = A;} // output CRC code (value of the Register group) b2crcgetregisters () {return regs. val;} the step-by-step shift/XOR operation in crcinputbit can be simplified: void crcinputbit (bit in) {Bit A; A = regs. bits. bit0 ^ In; regs. val> = 1; if (a) regs. val ^ = 0x8408 ;}

If you are careful, you can find the relationship between 0x8408 and 0X1021 (the simplified form of the CRC-ITU. Since we output the bit stream from low to high, 0x8408 is obtained by turning around 0 x. Write the generated polynomial into g (x) = 1 + X5 + X12 + x16. Is it better?

The following is a typical PPP frame. The last two bytes are called the frame check sequence, which is the CRC of the first 11 bytes.

FF 03 C0 21 04 03 00 07 0D 03 06 D0 3A

Calculate the CRC value of the PPP frame and verify it.

Byte PPP [13] = {0xff, 0x03, 0xc0, 0x21, 0x04, 0x03, 0x00, 0x07, 0x0d, 0x03, 0x06, 0x00, 0x00}; int I, j; b2result; ///////// compute the following FC // initialize crcinitregisters (); // bit-by-bit input, each byte is low first, does not include two FCS bytes for (I = 0; I <11; I ++) {for (j = 0; j <8; j ++) {crcinputbit (PPP [I]> J) & 1) ;}// get CRC: return the value of the Register group to result = ~ Crcgetregisters (); // fill in the FCS, first low and then high PPP [11] = Result & 0xff; PPP [12] = (result> 8) & 0xff; ////////// the following command is used to verify the FC. // initialize crcinitregisters (); // a bit-by-bit input. Each byte is low first, including two FCS bytes for (I = 0; I <13; I ++) {for (j = 0; j <8; j ++) {crcinputbit (PPP [I]> J) & 1) ;}/// the verification result is: crcgetregisters ();

We can see that the calculated CRC value is 0x3ad0, which is the same as that of the original FCS. The verification result is equal to 0. Initialization to all "1", and the value of the Register group to obtain CRC, are the requirements of the CRC-ITU. In fact, no matter whether the initialization is full "1" or full "0", the verification result is 0 if the CRC is obtained or not.

4-byte Algorithm

Bit algorithms perform operations by bit, which is less efficient and not suitable for high-speed communication. A Digital Communication System (various communication standards) generally performs CRC verification on a frame of data, while bytes are the basic unit of frames. The most common is a fast algorithm for byte lookup. This algorithm is based on the fact that the CRC code after the current byte is calculated, which is equal to the 8-bit low of the remaining CRC code of the previous byte and shifted to the 8-bit left, the CRC code obtained after the first byte CRC shifts 8 bits to the right and the sum of the Current byte. If we calculate all the CRC values of the 8-bit binary sequence numbers (256 in total) and put them in a table, we only need to find the corresponding values in the table for encoding.

The calculation algorithm of the CRC-ITU is as follows: A. Register group is initialized to full "1" (0 xFFFF ). B. The register group moves one byte to the right. C. Calculate the exclusive or operation between the removed byte and the Data byte to obtain an index pointing to the value table. D. perform an exclusive or operation on the table values indicated by the index and the register group. F. Add 1 to the Data Pointer. If not all data is processed, repeat Step B. G. Obtain the CRC after the register group is reversed. The Verification Algorithm for the CRC-ITU is as follows: A. Register group is initialized to full "1" (0 xFFFF ). B. The register group moves one byte to the right. C. Calculate the exclusive or operation between the removed byte and the Data byte to obtain an index pointing to the value table. D. perform an exclusive or operation on the table values indicated by the index and the register group. E. Add 1 to the Data Pointer. If not all data is processed, repeat Step B (the data includes two bytes of CRC ). F. whether the value of the Register group is equal to "magic value" (0xf0b8). If the value is equal, it passes; otherwise, it fails.

Below is the general CRC-ITU lookup table and the C language program for calculating and verifying CRC:

// CRC-ITU lookup table const 2010crctab16 [] = {0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, primary, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, primary, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0 xbfdb, 0xae52, 0 xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xf Ae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0 xbdcb, 0xac42, 0x9ed9, 0x8f50, 0 xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x0000c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0 xdecd, 0xcf44, 0 xfddf, 0xec56, 0x98e9, 0x8960, 0 xbbfb, 0xa A72, 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0 xffcf, 0xee46, 0 fingerprint, 0xcd54, 0xb9eb, 0xa862, 0x9af9, values, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, numbers, 0xf0b7, 0x0840, 0x19c9, numbers, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8 500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, clerk, 0x18c1, 0x0948, 0x3bd3, 0x2a5a, clerk, clerk, 0x7df7, clerk, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, primary, 0x5cf5, primary, primary, 0xd785, 0xe51e, 0xf 497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x060, primary, primary, 0x3efb, 0xd68d, 0xc704, primary, primary, 0x90a9, 0x8120, 0xb3bb, 0xa133, 0x5ac5, 0x4b4c, primary, 0x685e, 0x1ce1, 0x0d68, primary, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa 022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78,}; // calculates the 16-bit CRC of the given length data. Service.ap-southeast-1.maxcompute.aliyun-inc.com/api) ^ crctab16 [(FCS ^ * pdata) & 0xff]; nlength --; pdata ++;} RETURN ~ Check whether the 16-bit CRC value of the given length is correct. Bool iscrc16good (const byte * pdata, int nlength) {XHTML FCs = 0 xFFFF; // initialize while (nlength> 0) {FCs = (FCS> 8) ^ crctab16 [(FCS ^ * pdata) & 0xff]; nlength --; pdata ++;} return (FCs = 0xf0b8); // 0xf0b8 is the CRC-ITU's "Magic value "}

Using the byte algorithm, the above-mentioned PPP frame FCS computing and verification process can be achieved using the following program snippets:

Byte PPP [13] = {0xff, 0x03, 0xc0, 0x21, 0x04, 0x03, 0x00, 0x07, 0x0d, 0x03, 0x06, 0x00, 0x00}; b2result; // calculate CRC result = getcrc16 (PPP, 11); // fill in the FCS, PPP [11] = Result & 0xff; PPP [12] = (result> 8) & 0xff; // verify if (iscrc16good (PPP, 13 )) {......}

In this example, the data length is 11, indicating that CRC calculation does not require two or four bytes of data to be aligned.

For the generation algorithm of search table, and other CRC algorithms such as CRC-32, refer to RFC 1661, RFC 3309 and other documents. It should be noted that, although the CRC algorithm is essentially the same, there may be differences between different protocols, standard-defined initialization, shift sequence, verification methods, and so on.

Conclusion

CRC is one of the important technologies in the modern communication field. Master the CRC algorithms and implementation methods, and play a major role in the design of communication systems, analysis of communication protocols, and software protection. For example, in a multi-serial data transmission system designed by the author, the speed of each serial port is 460 kbps, and the error rate is greater than 10-6 without verification, after simple parity check, the performance is not significantly improved. CRC is used for error re-transmission, and the error rate is reduced to less than 10-15, which meets the requirements of practical applications.

References

1. Simpson, W., editor, "The Point-to-Point Protocol (PPP)", STD 51, RFC 1661,199 4
2. j. Stone, "stream control transmission protocol (sctp) checksum change", RFC 3309,200 2
3. J. satran, "Internet Protocol small computer system interface (iSCSI) cyclic redundancy check (CRC)/checksum considerations", RFC 3385,200 2
4. International Standardization, "high-level data link control (HDLC) Procedures", ISO/IEC 3309,199 2
5. ITU-T V.41, "code-independent error-control system", 1989
6. Guo tiyun, data transmission (revised), People's post and telecommunications Press, 1998

[Related resources]
◆ RFC/STD documents: Internet FAQ Archives
◆ ITU Official Website: http://www.itu.int/home/index.html
◆ Bhw98 column: http://www.csdn.net/develop/author/netauthor/bhw98/

First Release: 2003-04-10
Final revision:

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.