Is CRC32 additive?

前端 未结 4 954
遥遥无期
遥遥无期 2021-01-04 12:41

On several places I\'ve read that crc32 is additive and so: CRC(A xor B) = CRC(A) xor CRC(B).

The above statement was disproven by the following code I wrote:

<
4条回答
  •  逝去的感伤
    2021-01-04 13:10

    The CRC-32 algorithm is based on polynomial division, with some extra steps added. Pure polynomial remainder is additive.

    By that, I mean: mod(poly1 + poly2, poly3) = mod(mod(poly1, poly3) + mod(poly2, poly3), poly3)

    The CRC-32 algorithm builds on this, and is non-additive. To compute the CRC-32 of a byte array m:

    1. XOR the first 4 bytes by 0xFFFFFFFF.
    2. Treat earlier bytes as higher polynomial powers and treat lower order bits as higher polynomial powers. For example, the bytes 0x01 0x04 would be the polynomial x^15 + x^3.
    3. Multiply the polynomial by x^32.
    4. Take the remainder of this polynomial divided by the CRC-32 polynomial, 0x104C11DB7. The remainder polynomial has degree < 32.
    5. Treat lower powers as higher order bits. For example, the polynomial x^2 would be the 32-bit integer 0x40000000.
    6. XOR the result by 0xFFFFFFFF.

    The pure polynomial remainder operation is in step #4. It's steps #1 and #6 that make the CRC-32 algorithm non-additive. So if you undo the effect of steps #1 and #6, then you can modify the CRC-32 algorithm to be additive.

    (See also: Python CRC-32 woes)

提交回复
热议问题