Sub topic 8
Measuring the avalanche effect
This chapter assumes you can read Python, understand XOR and binary notation, and know how a hash table maps a hash value to a bucket. Earlier chapters showed that a summing hash sends anagrams to the same bucket. Here you will measure how well a hash separates inputs that are almost identical.
What the avalanche effect is
A hash has the avalanche effect when flipping one input bit changes about half of the output bits. Each output bit should flip with probability close to 1/2, whichever input bit you flip.
This matters for two reasons. If similar keys such as user1 and user2 produce similar hashes, they cluster in the same buckets. Weak avalanche behavior also gives an attacker structure to exploit, which is why cryptographic hashes demand it.
Counting differing bits
Two outputs differ in the bits where their XOR is 1. Counting those bits gives the Hamming distance, which is the number of positions where two bit strings differ.
import hashlib
def bit_diff(a: bytes, b: bytes) -> int:
return bin(int.from_bytes(a, "big") ^ int.from_bytes(b, "big")).count("1")
m1 = b"hello world"
m2 = bytearray(m1)
m2[0] ^= 1 # flip a single input bit
d1 = hashlib.sha256(m1).digest()
d2 = hashlib.sha256(bytes(m2)).digest()
print(bit_diff(d1, d2), "of 256 output bits differ")For SHA-256 the count should land near 128. The exact number varies, but a single run in the range of roughly 100 to 156 is normal.
Comparing a weak hash with a strong one
One flip is a weak test. A better test flips each input bit in turn and averages the results. The sketch below does this for the FNV-1a hash from the previous chapter, whose 32-bit output makes the ideal average 16.
import hashlib
def fnv1a_32(data: bytes) -> int:
h = 0x811C9DC5
for byte in data:
h ^= byte
h = (h * 0x01000193) & 0xFFFFFFFF
return h
def sha256_32(data: bytes) -> int:
return int.from_bytes(hashlib.sha256(data).digest()[:4], "big")
def average_flips(hash_fn, message: bytes) -> float:
base = hash_fn(message)
total = 0
trials = 0
for i in range(len(message)):
for bit in range(8):
changed = bytearray(message)
changed[i] ^= 1 << bit
total += bin(base ^ hash_fn(bytes(changed))).count("1")
trials += 1
return total / trials
msg = b"The quick brown fox"
print("FNV-1a :", round(average_flips(fnv1a_32, msg), 2))
print("SHA-256:", round(average_flips(sha256_32, msg), 2))SHA-256 truncated to 32 bits should average close to 16. FNV-1a usually comes out lower or less even. Its multiply step spreads a change only toward the high bits, so a flip in the last byte barely affects the low bits.
Common mistakes
- Judging from one sample. Average over many inputs and many bit positions.
- Expecting exactly half. Random output varies, so judge the average and the spread.
- Expecting zero flips to be fine. A count near 0 means similar inputs collide, and a count near the full width means the output is a predictable complement.
- Treating avalanche as proof of security. It is necessary for a cryptographic hash but not sufficient.
Exercise
Exercise
A hash gives an average of 3 flipped bits out of 32 when you flip one input bit. What does that tell you, and how does it affect a hash table?
Show answer
The avalanche effect is weak, because the ideal average is about 16. Similar keys get similar hash values and tend to share buckets, so chains grow longer and lookups slow down.