Batu Lab NotesPractical developer guides

Use reduce to compute a checksum modulo a base

By Batu ยท English technical notes

Also published in our Blogger archive.

A simple modular checksum can be expressed as a left-to-right reduction. The accumulator holds the current residue; for each byte, the reducer adds the byte and applies % base. Reducing after each addition is equivalent to summing all values and taking one final remainder for ordinary integers, while keeping the accumulator within the selected base throughout this calculation.

For the payload [65, 66, 67] and base 97, the byte sum is 198, whose remainder is 4. The first assertion compares the reduction result with that direct formulation. The explicit initial value 0 is also important: it defines the empty payload checksum as 0, which the second assertion checks. Without an initial value, reduce() raises an error for an empty iterable.

This construction is useful when the accumulation rule itself is the point of the example or when a sequence must be folded into one state. A direct sum(payload) % base is often clearer for this exact additive rule. More importantly, this checksum is not cryptographic: distinct payloads can have the same residue, and the assertions do not establish collision resistance or data-integrity guarantees. Use a specified checksum or cryptographic algorithm when interoperability or adversarial tampering matters.

The official functools.reduce documentation describes its cumulative left-to-right behavior and the role of an initial value.

AI assistance disclosure: this article was drafted with AI assistance and uses only synthetic bytes.

from functools import reduce

payload = [65, 66, 67]
base = 97

checksum = reduce(
    lambda total, byte: (total + byte) % base,
    payload,
    0,
)

assert checksum == sum(payload) % base
assert reduce(lambda total, byte: (total + byte) % base, [], 0) == 0
print(f"checksum={checksum}")
checksum=4