3

Short background to my question: I'm working on a small hobby project with a microcontroller communicating with a PC over UART. Right now I'm using COBS byte stuffing with 0x00 packet delimiter bytes and a simple 1-byte checksum that is either a two's complement running sum or a one's complement sum. My one's complement checksum implementation is very similar to Internet checksum (page 7. The interesting part is seen below)

//  Fold 32-bit sum to 16 bits //
while (sum>>16)
    sum = (sum & 0xffff) + (sum >> 16);

checksum = ~sum; 

A few days ago I found out about the Fletcher checksum:
Fletcher article
Fletcher implementation
Fletcher Wikipedia

Short code snippet from the Fletcher Wiki page:

for( index = 0; index < count; ++index )
{
    sum1 = (sum1 + data[index]) % 255;
    sum2 = (sum2 + sum1) % 255;
}

return (sum2 << 8) | sum1;

My question: In both articles above they say that Fletcher uses "one's complement [mod(255)] checksums" as though a mod-255 and one's complement sum are the same. Is that really true?

  1. It makes sense to me that the carry bits that makes one's complement checksums superior, could work in pretty much the same way in a mod-255 sum compared to the one's complement adder above. But with a mod-255 sum you can never get the value 0xFF (-0), only 0x00 (+0)?
  2. I guess that the mod-operator is slower (although it's linear so you can wait with the mod- calculation until the end of the sum).
  3. A nice feature (when using COBS) is that mod-255 will never produce 0x00 check bytes since the mod-255 sum can never be 0xFF (although that's easily fixed even in the one's complement adder above).

Thank you so much for your time!

Kind regards / Henrik

Community
  • 1
  • 1
Henrik123
  • 53
  • 5

1 Answers1

0

No, Modulo 255 is not equivalent to One's Complement. I think the confusion here is that Fletcher's checksum utilizes both modulo 255 addition and taking the one's complement as the final step. Also, the much slower modulo operator is likely just to model the ADC operator that it likely used in asm.

One's Complement is the inversion of the bits (bitwise NOT), typically done as the final operation of the calculation, known as "taking the one's complement". It's done to optimize the verification process and force the correct result to be -0 (or 1111 1111 in binary). It is only really useful on low-level hardware that uses one's complement arithmetic. Most others use two's complement nowadays and so to achieve the same thing you must subtract one in addition to the bit inversion.

Modulo 255 is different. The intention is likely to model the "Add with Carry" instruction. I would not be surprised if Fletcher-16 was implemented in assembly with the ADC instruction.

Basically rather than just disregard overflow, it adds in the carry bit whenever set. Modulo 255 achieves the same effect here. This is often done to improve error detection qualities of a checksum at very little cost.

bryc
  • 12,710
  • 6
  • 41
  • 61