Wish I had the intuition to just summon god-tier generator polynomials for cyclic redundancy checks. But nah, shoutout to Philip Koopman for actually coming up with best polynomials. They are excellent for detecting single-bit, two-bit and burst errors. Especially the CRC32 polynomial is used in virtually every stack to detect bit manipulation errors where file transfers take place.
I was experimenting with different polynomials to hack the pattern (if there is) to find the relationship between error and the actual bytes that got flipped. I ported the GNU libiberty xcrc32 (here ‘x’ is wrapper around the standard function) source code with some modifications into my client-server program to send a 20MB png file internally.
What I modified:
- CRC seed = The
xcrc32()implementation from GNU libiberty isn’t the “standard Ethernet CRC-32” with reflection and XOR. I changed the initial value = 0xFFFFFFFF. - I set the file size to 8-byte
uint64_t, and added properhtonll_u64()conversion for network order.
You can safely use this modified CRC32 to check correctness for:
- Any binary file (images, executables, archives)
- Any text file
- Arbitrary in-memory messages (structs, packets, frames)
Basically, “if you can serialize it to bytes, you can protect it with this CRC.”
This youtube video demonstrates how the algorithm works in theory and in hardware. Highly recommend!