Author Topic: CRC dumbed down  (Read 2223 times)

0 Members and 2 Guests are viewing this topic.

Offline RoGeorgeTopic starter

  • Super Contributor
  • ***
  • Posts: 8450
  • Country: ro
CRC dumbed down
« on: October 03, 2025, 07:46:06 am »
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.  ;D

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)2 or (5)10, is the CRC equal with 333 modulo 5, so is CRC == 3 in this example?

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1894
  • Country: de
Re: CRC dumbed down
« Reply #1 on: October 03, 2025, 09:55:37 am »
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.
« Last Edit: October 03, 2025, 09:57:23 am by 0xdeadbeef »
Trying is the first step towards failure - Homer J. Simpson
 
The following users thanked this post: RoGeorge

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1551
  • Country: us
  • Very dangerous - may attack at any time
Re: CRC dumbed down
« Reply #2 on: October 03, 2025, 10:27:57 am »
A great explanation of how CRC works....


 
The following users thanked this post: RoGeorge, golden_labels

Offline RoGeorgeTopic starter

  • Super Contributor
  • ***
  • Posts: 8450
  • Country: ro
Re: CRC dumbed down
« Reply #3 on: October 03, 2025, 12:24:24 pm »
A great explanation of how CRC works....

https://youtube.com/watch?v=izG7qT0EpBw

That video, together with the followup video about the hardware implementation https://youtube.com/watch?v=sNkERQlK8j8 , cleared out all my questions, thank you!

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6406
  • Country: nz
Re: CRC dumbed down
« Reply #4 on: October 03, 2025, 12:32:10 pm »
Is the CRC the same as the reminder of a modulo n division?

No, 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 CRC

The 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.
 
The following users thanked this post: oPossum, bson, RoGeorge

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2435
  • Country: pl
Re: CRC dumbed down
« Reply #5 on: October 03, 2025, 01:09:14 pm »
oPossum, this is a beautifully simple introduction! :D

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.
Why 📎 | We live in times when half of people have IQ below 100.
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4833
  • Country: us
Re: CRC dumbed down
« Reply #6 on: October 03, 2025, 08:17:06 pm »
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.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6406
  • Country: nz
Re: CRC dumbed down
« Reply #7 on: October 03, 2025, 09:52:15 pm »
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.

Note that various CPUs have instructions to do binary polynomial multiplication directly e.g. RISC-V's clmul in the Zbc extension (included as standard but optional in RVA23)

https://five-embeddev.com/riscv-bitmanip/1.0.0/bitmanip.html#insns-clmul
 
The following users thanked this post: RoGeorge, SiliconWizard


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf