Cyclic Redundancy check Cyclic Redundancy test is a set of verification codes based on data computing. It is used to check whether the data is changed or transmitted incorrectly during transmission.

Algorithm principle

Assume that 15-bit binary information G = 101001110100001 needs to be sent during data transmission. The binary code can be expressed as an algebraic polynomial g (x) = x ^ 14 + x ^ 12 + x ^ 9 + x ^ 8 + x ^ 7 + x ^ 5 + 1, where the K-bit value in G, corresponds to the x ^ K coefficient in G (x. Multiply g (x) by x ^ m, then add M 0 after g, and divide it by h (x) of the degree m polynomial, and obtain the remainder of m-1) degree R (X) the corresponding binary code R is CRC encoding.

H (x) can freely choose or use international standard, generally according to h (x) Order m, CRC algorithm called CRC-M, such as CRC-32, CRC-64, etc. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for International Standards

Division operations of g (x) and H (x) can be performed through G and h xor (exclusive or) operations. For example, perform XOR operations on 11001 and 10101:

Understand the XOR algorithm, for example, using the CRC-8 Algorithm for 101001110100001 verification code. CRC-8 standard h (x) = x ^ 8 + x ^ 7 + x ^ 6 + x ^ 4 + x ^ 2 + 1, h is a 9-bit binary string 111010101.

After iterative operation, the final R is 10001100, which is the CRC verification code.

Through examples, we can find some rules and adjust the algorithms based on these rules:

1. For each iteration, B is determined based on the first part of GK, and B is the binary code for calculation with GK. If the first part of GK is 1, B = H; if the first part of GK is 0, B = 0, or skip this iteration, in the above example, when 0 is met, the system directly jumps to the non-zero position next to it.

2. During each iteration, the first step of GK will be removed, so you only need to consider the 2nd-bit post-calculation. In this way, we can discard the first place of H and take B to the next M bit of H. For example, the CRC-8 H is 111010101, B is only 11010101.

3. Each iteration is affected by the first M bit of GK. Therefore, an M-bit register S is constructed, which stores the first M bit of GK. Before each iteration, we discard the first digit of S, move the register one bit left, and add the last digit of G to the Register. If this method is used, the calculation procedure is as follows:

※Blue indicates the first part of register S. It is removed. B selects 0 or H based on the first part of register S. Yellow is the bit to be moved into the register. S is the s after the displacement.

Lookup Method

In the same example above, the data is divided into 6 blocks by every four digits.

The following table shows four iteration steps. The bit of the gray background is saved in the register.

After four iterations, B1 is removed from the register. We do not care about the removed part. We are concerned about the impact of these four iterations on B2 and B3. Note that the red part in the table is defined as follows:

B23 = 00111010

B1 = 00000000

B2 = 01010100

B3 = 10101010

B4 = 11010101

B '= b1 XOR B2 XOR B3 XOR B4

For B2 and B3, the four iterations actually involve XOR calculation between them and B1, B2, B3, and B4:

B23 XOR B1 XOR B2 XOR B3 XOR B4

It can be proved that the XOR operation satisfies the exchange law and the combination law, so:

B23 XOR B1 XOR B2 XOR B3 XOR B4 = B23 XOR (b1 XOR B2 XOR B3 XOR B4) = B23 XOR B'

B1 is determined by the 1st bits of B1, and B2 is determined by the 2nd bits after the B1 iteration (both the 1st and 2nd bits of B1). Likewise, b3 and B4 are determined by B1. B 'can be calculated through B1 '. In addition, B1 consists of four digits, which have a total of 2 ^ 4 values. So we can think of a more efficient algorithm. We can think of B 'all the possible values and 16 values as a table in advance, so that we don't have to perform those four iterations, instead, you can use the B1 table to obtain the B 'value, move B1 out, B3 in, calculate with B', and then perform the next iteration.

We can see that during each iteration, the data in the register is moved in and out in 4 bits. The key is to obtain the data through the first 4 bits of the register.

This algorithm can greatly increase the computing speed.

The above method is the half-byte lookup method, and also the single-byte and double-byte lookup methods, the principle is the same-calculate the possible values of 2 ^ 8 or 2 ^ 16 B 'in advance, and use the first 8-bit or 16-bit register lookup table in the iteration to obtain B '.

**Reverse Algorithm**
The previously discussed algorithm can be called a forward CRC algorithm, which means that the bit on the left of G is regarded as a high position, and the bit on the right is considered as a low position. Add M 0 to the right of G, and then the iterative calculation starts from the High Level and gradually adds the low level to the Register. In the actual data transmission process, the CRC code is calculated while receiving data. The forward algorithm regards the newly received data as low.

As the name suggests, the reverse algorithm regards the data on the left as low, and the data on the right as high. In this case, you need to add M 0 on the left of G, H also need to reverse, such as forward CRC-16 algorithm H = 0x4c11db8, reverse CRC-16 algorithm H = 0xedb88320. Select 0 or H for B, which is determined by the 1st bits on the right of the Register, rather than the 1st bits on the left. The register is still shifted to the left, that is, the iteration changes from low to high.