CRC dumbed down - Page 1
Forum section: electronics / general

author: RoGeorge
author_rank: Super Contributor
timestamp: October 03, 2025, 07:46:06 am
content: Usually one will use a ready made library to calculate the CRC in software, or use a shift register made of FF and XOR gates to obtain the CRC in hardware, but this time I've made the mistake of reading about the math behind, and got bamboozled.Is the CRC the same as the reminder of a modulo n division?For example, if my datastream is (333)10, and the CRC polynom is X2+1, so (101)2or (5)10, is the CRC equal with 333 modulo 5, so is CRC == 3 in this example?

author: 0xdeadbeef
author_rank: Super Contributor
timestamp: October 03, 2025, 09:55:37 am
content: A CRC calculation is the implementation of a polynomial division. The data represented as binary polynomial is divided by the generator polynomial and the remainder of this division is the CRC.E.g. the generator polynomial 1x4 + 1x3 + 1x2 + 0x1 + 1x0 has the binary representation 0b11101 which, truncated to 4bit, equals 13.The polynomial division is carried out using bitwise exclusive OR operations.The start value does not increase the size of the bit stream. It's just treated as the carry or remainder from a previous calculation. Effectively, it's XORed to the data nibble.Also, zero padding is necessary (degree R -1). Without zero padding, the R-1 lowest nibbles would always contribute to the remainder.Fun Fact: in 2007, I wrote a little document to the guy who wrote the SENT SAE J2716 specification (Single Edge Nibble Transmission) explaining why the "CRC" algorithm shown there wasn't really a CRC calculation and that the look-up table of 256 elements could be reduced to 16 elements. As far as I recall, reply was more or less "but it works". The reduced table actually made it into the next revision of the spec (without mentioning me of course) but the SENT "CRC" is still not really a normal CRC.

author: oPossum
author_rank: Super Contributor
timestamp: October 03, 2025, 10:27:57 am
content: A great explanation of how CRC works....

author: RoGeorge
author_rank: Super Contributor
timestamp: October 03, 2025, 12:24:24 pm
content: Quote from: oPossum on October 03, 2025, 10:27:57 amThat video, together with the followup video about the hardware implementationhttps://youtube.com/watch?v=sNkERQlK8j8, cleared out all my questions, thank you!

author: brucehoult
author_rank: Super Contributor
timestamp: October 03, 2025, 12:32:10 pm
content: Quote from: RoGeorge on October 03, 2025, 07:46:06 amNo, but for a typical application within a computer or in RAM or disk a modulo division and a CRC will provide the same amount of protection, namely a division by N checksum will detect (N-1)/N of all possible random corruptions of the data.A CRC provides the same amount of protection overall, but with a distribution tailored to specifically bit-serial communication channels, namely (IIRC) with a "good" CRC:- detection of all 1 bit errors- detection of essentially all 2 bit errors (of any separation)- detection of all errors that flip an odd number of bits- detection of all burst errors shorter than the CRCThe last one is what makes CRC the best for an actual noisy physical channel.If your data is being transmitted, say, byte-serial on 8 wires e.g. a "parallel port", or stored in 8 Nx1 RAM chips (etc) then you should NOT use a CRC on the whole data, but a separate CRC on each of bit 0, bit 1, ... bit 7 from each byte.Otherwise the specific property that a CRC is designed for is wasted and you might as well use a cheaper checksum.

author: golden_labels
author_rank: Super Contributor
timestamp: October 03, 2025, 01:09:14 pm
content: oPossum,this is a beautifully simple introduction!If anybody is discouraged by the use of polynomials, a small note. Ben Eater already gave a hint, but let me be more explicit. I guess polynomials were introduced to most people as equations, a “special kind of a function.” This perspective only stands in a way of seeing, what happens here. Forget it. Instead assume a polynomial is nothing more than a “fancy kind of a number.” Each time your brain tries to sum these x-es, just kick it in the butt.Why is CRC described in terms of polynomials? Because they provide a convenient, very flexible, universal recipe. Maths is all about finding possibly general patterns. They’re easy to prove, they can be recycled for a wide variety of applications, they remain connected with other similar patterns. See that this method works regardless of what x-es are, as long as they follow some simple rules. If they’re chosen right, the entire operation can be implemented with XOR gates.

author: ejeffrey
author_rank: Super Contributor
timestamp: October 03, 2025, 08:17:06 pm
content: Also the reason we use these "fancy kinds of numbers" rather that regular integers is to make it easier to implement in hardware. In "binary polynomial arithmetic" each bit position is independent and addition is done modulo 2. This eliminates carry, so that addition is just XOR. Multiplication is done by shift and add just like integer multiplication but the addition is done without carry.

author: brucehoult
author_rank: Super Contributor
timestamp: October 03, 2025, 09:52:15 pm
content: Quote from: ejeffrey on October 03, 2025, 08:17:06 pmNote that various CPUs have instructions to do binary polynomial multiplication directly e.g. RISC-V'sclmulin the Zbc extension (included as standard but optional in RVA23)https://five-embeddev.com/riscv-bitmanip/1.0.0/bitmanip.html#insns-clmul