Cyclic redundancy check

From Exampleproblems

Jump to: navigation, search

A cyclic redundancy check (CRC) is a type of hash function used to produce a checksum - which is a small number of bits - against a byte or a larger block of data, such as a packet of network traffic or a block of a computer file. The checksum is used to detect and correct errors after transmission or storage. A CRC is computed and appended before transmission or storage, and verified afterwards by recipient to confirm that no changes occurred on transit. Correction can also be done if information lost is lower than information held by the checksum. CRCs are popular because they are simple to implement in binary hardware, are easy to analyze mathematically, and are particularly good at detecting common errors caused by noise in transmission channels.

Contents

Introduction

CRCs are based on division in a commutative ring, namely the ring of polynomials over the integers modulo 2. In simpler terms, this is the set of polynomials where each coefficient is only one bit, and arithmetic operations wrap around. For example:

(x2 + x) + (x + 1) = x2 + 2x + 1 = x2 + 1

Two becomes zero because 2 is 10 in binary, and we discard all bits except the last one. Multiplication is similar:

(x2 + x)(x + 1) = x3 + 2x2 + x = x3 + x

We can also divide polynomials mod 2 and find the quotient and remainder. For example, suppose we're dividing x3 + x2 + x by x + 1. We would find that

x3 + x2 + x = (x + 1)(x2 + 1) − 1 = (x + 1)(x2 + 1) + 1.

In other words, the division yields a quotient of x2 + 1 with a remainder of -1, which, since it is odd, has a last bit of 1.

Any string of bits can be interpreted as the coefficients of a polynomial of this sort, and to find the CRC, we divide by another fixed polynomial. The coefficients of the remainder polynomial are the CRC, and there are simple, efficient algorithms for computing this remainder, such as the one shown below. CRCs are often referred to as "checksums," but such designations are not strictly accurate since, technically, a checksum is calculated through addition and not through division as is the case with CRCs.

The main portion of the algorithm can be expressed in pseudocode as follows:

 function crc(bit array bitString[1..len], int polynomial) {
     shiftRegister := initial value // commonly all 0 bits or all 1 bits
     for i from 1 to len {
         if most significant bit of shiftRegister xor bitString[i] = 1 {
             shiftRegister := (shiftRegister left shift 1) xor polynomial
             least significant bit of shiftRegister = 1
         } else
             shiftRegister := (shiftRegister left shift 1)
     }
     return shiftRegister
 }

Note: A common speedup uses a lookup table indexed by multiple most-significant bits of the shiftRegister to process multiple bits at once. A 256-entry lookup table is a particularly common choice.

There are two variations which can be applied to the above implementation; applying one or both gives a total of four equivalent ways to compute a checksum:

  1. The shiftRegister can be reversed, so its least-significant bit is tested and it is shifted to the right by 1 bit each step. This requires a polynomial with its bits reversed, and produces a bit-reversed result. This variant is actually the one most commonly in use.
  2. Instead of changing multiple bits in the shiftRegister based on the xor of one bit of the shiftRegister and one bit of the bitString, it is possible to xor (compute the parity of) all the bits of the shiftRegister selected by the polynomial and the bitString and add that single bit to the shiftRegister. With suitable adjustments to the polynomial, this also produces the same remainder. This variation is difficult in software, but used in some hardware implementations, and is often used when describing the close relative to a CRC, the linear feedback shift register.

The specific CRC is defined by the polynomial used. To produce an n-bit CRC requires a degree-n polynomial, of the form xn + … + 1. This is naturally expressed as an n+1-bit string, but the leading (xn) term is normally implicit, leaving an n-bit string Thus, depending on the bit-order convention used, the standard CRC-16, x16+x15+x2+1, will be represented as the hexadecimal number 0x8005 or as 0xa001.

One of the most commonly encountered is known as CRC-32, used by (among others) Ethernet, FDDI, PKZIP, WinZip, and PNG. Its polynomial can be written 0x04C11DB7 or 0xEDB88320.

Polynomials and types

There are many more CRC types than indicated here.

  • at least 5 separate versions of CRC-16, CRC-32 and CRC-64 are existent and in common use
  • CRC-64, CRC-128 and CRC-256 exist and are standardized but are not in common use
  • CRCs greater than CRC-32 have been replaced by hash functions like MD2, MD4 and MD5 in modern telecommunications, data storage and authentication practices
  • the current state of the art in hash functions is SHA-1, WHIRLPOOL-512 (and others in development and testing)


Polynomial CRC Specifications [ITU-IEEE Syntax]

CRC-1 x + 1 (Used in hardware, also known as parity bit)
CRC-7 x^7 + x^3 + 1 [used in some telecom systems]
CRC-8-Fletcher A := A + D[i], B := B + A
CRC-8 x8 + x2 + x + 1
CRC-12 x12 + x11 + x3 + x + 1 [used in telecom systems]
CRC-16-Fletcher (A basis for CRC-16 Adler)
CRC-16-Adler_A [A = 1 + D1 + D2 + ... + Dn (mod 65521)]
CRC-16-Adler_B [B = n×D1 + (n-1)×D2 + (n-2)×D3 + ... + Dn + n (mod 65521)]
CRC-16-CCITT x16 + x12 + x5 + 1
CRC-16-IBM x15 +x14 + x10 + x8 + x1 + 1
CRC-32-Adler [Adler-32(D) = B × 65536 + A]
CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
CRC-32C (Castagnoli) x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
CRC-64-ISO x64 + x4 + x3 + x + 1 (as described in ISO 3309)
CRC-64-ECMA-182 x64 + x62 + x57 + x55 + x54 + x53 + x52 + x47 + x46 + x45 + x40 + x39 + x38 + x37 + x35 + x33 + x32 + x31 + x29 + x27 + x24 + x23 + x22 + x21 + x19 + x17 + x13 + x12 + x10 + x9 + x7 + x4 + x + 1 (as described in ECMA-182 p.63)
CRC-128 (IEEE? / ITU?)


Adler-32 Note: D is the string of bytes for which the checksum is to be calculated, and n is the length of D.

CRCs and data integrity

While useful for error detection, CRCs cannot be safely relied upon to verify data integrity (that no changes whatsoever have occurred), since, because of the linear structure of CRC polynomials, it is extremely easy to intentionally change data without modifying its CRC. Cryptographic hash functions can be used to verify data integrity.

Computational costs of CRCs vs Hashes

Checksum Algorithm Relative Cost
32-bit Adler 1.00

32-bit CRC (IEEE)

1.46

64-bit CRC (ISO)

2.23
128-bit CRC 2.29
128-bit MD4 4.48
128-bit MD5 4.51

Note that 128-bit MD4 is 1.96 times as expensive as 128-bit CRC.

See also

General category

Specific Technological References

  • Jacksum — by Dipl.-Inf. (FH) Johann N. Löfflmann in Java. Various message verification functions. Released under the GPL.

External links

fr:Cyclic redundancy check ko:순환 중복 검사 id:CRC it:Cyclic redundancy check nl:Cyclic Redundancy Check ja:巡回冗長検査 pl:CRC pt:CRC ru:CRC sv:Cyclic Redundancy Check

Argan Oil
Natural Skin Care
Organic Skin Care
visitor stats