CRC check (1): CRC principle, calculation example and selection of optimal polynomial

CRC( Cyclic Redundancy Check), that is, cyclic redundancy check, generates a fixed-length check code by calculation to verify whether data is wrong or damaged during transmission, thereby ensuring data integrity. zSuppose we want to send lowercase letters . In Unicode, represented zby numbers 0x7A, binary is 0111 1010. In order for the receiver to verify that the data has not been modified during transmission, we calculate the CRC of the data 1000and append it to our message: 0111 1010 1000. This checksum 1000is called redundant because it is added to the original data.

1 CRC calculation

1.1 Polynomials

Mathematically, the CRC is polynomialthe remainder of the modulo two polynomial ( ) division of the data. Therefore, the CRC can be written as

CRC = data % 2 polynomial

The polynomial can be any mathematical polynomial (without any coefficients), but its highest exponent cannot exceed the number of CRC bits. For example, if we want to generate an 8-bit CRC, then the highest exponent of the polynomial must also be 8, for example: x 8 + x 2 + x + 1 x^8+x^2+x+1x8+x2+x+1 . And polynomials can be converted into binary representation, in fact, it is to see whether each index exists, if it exists, it will be 1, and if it does not exist, it will be 0. For examplex 8 + x 2 + x + 1 x^8+x^2+x+1x8+x2+x+1 converted to binary as100000111.

  • Choosing a different polynomial will result in different CRCs for the same data. So the sender and receiver need to use a common polynomial
  • For CRC, some polynomials are better suited than others depending on the specific usage scenario. For example, certain polynomials may be easier to detect consecutive bit errors or alternating bit errors.

Next, to be able to calculate the CRC, we need to understand modular two division.

1.2 Modulo two division calculation and examples

(1) Modulo 2 operation
Modulo 2 operation is a bit-by-bit operation on binary numbers. In this operation, there is no carry or borrow, and each bit is treated as a number independently of its neighbors. In fact, it is the XOR operation in C language:

bit1 bit2 XOR
0 0 0
0 1 1
1 0 1
1 1 0

(2) Modular two division
The execution of modular two division is similar to arithmetic division, the only difference is that we use modular two subtraction (XOR) instead of arithmetic subtraction to calculate the remainder at each step. Assuming that the dividend is 100100, and the divisor is 1101, let's see how to calculate the CRC:

  1. The length of the calculated divisor Lis 4 in this case.
  2. Add a bit after the dividend L-1, here is 3, which is also the length of the CRC. Note: the highest bit of the divisor must be 1, the additional number of bits corresponds to the highest exponent in the binomial: x 3 + x 2 + 1 x^3+x^2+1x3+x2+1
  3. Calculate with binary modulo 2, and the remainder obtained is the result of CRC:001

insert image description here

1.3 CRC types

CRC types are named by their bit size. The common ones are: CRC-8, CRC-16, CRC-32, CRC-64, and CRC-1 (a special case of the parity bit). Obviously for the same type, different polynomials also have different results. For example, for the dividend 01000001, its CRC-8 results under different polynomials are as follows:

binary polynomial CRC-8
110100111 11001100
100000111 11000000
101001001 01100110
111010101 01001000
  • GSM-BAbove is the standard CRC-8 polynomial used in different applications like bluetooth,

2 Choice of polynomials

How should we choose a suitable CRC and corresponding polynomial? There are three aspects to consider: random error detection accuracy, burst error detection accuracy, and redundancy factor.
(1) Random error detection accuracy
Random errors refer to errors that appear randomly in the data. For example, when transmitting data, a single bit is flipped, or several bits are lost during transmission. Depending on the bit size of the CRC we use, we can detect most of these random errors. However, for CRC-n, 1/2 n 1/2^n of these errors1/2n cannot be detected. The table below shows the percentage of undetected errors for each CRC type:

CRC undetectable error percentage%
CRC-8 1 / 2 8 1/2^8 1/28 0.39
CRC-16 1 / 2 16 1/2^{16} 1/216 0.0015
CRC-32 1 / 2 32 1/2^{32} 1/232 0.00000002
CRC-64 1 / 2 64 1/2^{64} 1/264 5.4 × 1 0 − 20 5.4×10^{-20} 5.4×1020

(2) Accuracy of burst error detection
Errors in data transmission are usually not random, but errors of several bits appear continuously. This kind of error is called burst error, which is the most common error in data communication.
CRC-n can detect single burst errors with a maximum length of n bits. However, this depends heavily on the polynomial used to calculate the CRC. Some polynomials are capable of detecting multiple burst errors in transmitted data.

CRC burst error detection
CRC-8 At least one consecutive error burst occurred, but this length ≤ 8 bits
CRC-16 At least one consecutive error burst occurred, but this length ≤ 16 bits
CRC-32 At least one consecutive error burst occurred, but this length ≤ 32 bits
CRC-64 At least one consecutive error burst occurred, but this length ≤ 64 bits

(3) Redundancy coefficient
The cost of using CRC for error detection is to increase additional data. For example, CRC-32 transmits two extra bytes more than CRC-16. Obviously, a CRC with a lower number of bits requires fewer bits to be computed and stored.

Based on these three factors, we can decide which CRC type to choose for our application. However, the CRC polynomial also affects the efficiency and quality of error detection. If you want to study it in depth, it will be a very complicated topic, and this article will not study it. Fortunately, there are many standard polynomials available online for specific CRC types, and in most cases using one of these will give you good error detection .

3 summary

CRC check is often used in the process of doing projects. When I was in college, I had a shallow understanding of this calculation process, but I hope to understand the principle, so I wrote this article, the next one, I The implementation of the look-up table method of CRC32 will be introduced.

Guess you like

Origin blog.csdn.net/tilblackout/article/details/131195357