Sub topic 9
How likely is a collision? The birthday bound
This chapter assumes you know that a hash function maps inputs to a fixed number of output bits, and that the pigeonhole principle forces collisions once there are more inputs than outputs. It also assumes you are comfortable with powers of two and basic Python.
The question
Pigeonhole tells you a collision must exist. It does not tell you when you will meet one. For a hash with bits output bits, there are m = 2**bits possible values. With n random inputs, how likely is it that at least two share a hash value?
Most people guess that you need about m / 2 items. The real number is far smaller.
The birthday approximation
The classic version asks how many people must be in a room before two share a birthday. With 23 people the chance already passes 50%, even though there are 365 days.
The reason is that you compare every pair, not each item against a fixed target. Among n items there are about n(n-1)/2 pairs, and each pair collides with probability 1/m. For small probabilities this gives:
P(collision) ≈ 1 - exp(-n(n-1) / (2m))
The 50% point is near n ≈ 1.177 * sqrt(m). So collisions become likely after about the square root of the number of possible values. That is half the bits of the output.
import math
def collision_probability(n, bits):
m = 2 ** bits
return 1 - math.exp(-n * (n - 1) / (2 * m))
print(collision_probability(77_000, 32)) # roughly one half
print(collision_probability(1_000_000, 64))The first line prints about 0.5. A 32-bit hash has over four billion values, yet 77,000 random items give even odds of a collision. The second line prints a very small number, around 2.7e-8, so a million items in a 64-bit space are very unlikely to collide.
What this means in practice
| Hash size | Items for about 50% collision chance |
|---|---|
| 16 bits | about 300 |
| 32 bits | about 77,000 |
| 64 bits | about 5 billion |
| 128 bits | about 2e19 |
| 256 bits | about 4e38 |
The bound has two consequences.
- For hash tables, a 32-bit hash is fine, because tables handle collisions by design. Collisions are expected and cheap.
- For identifiers or fingerprints, where a collision means two different things are treated as one, you need enough bits that
nstays far belowsqrt(m). This is why content-addressed systems use 128 bits or more.
Common errors
- Dividing instead of square-rooting. Saying a 32-bit hash is safe until 2 billion items confuses the collision of any pair with a match against one chosen value.
- Confusing the two attacks. Finding any two inputs with the same hash costs about
2^(bits/2)work. Finding an input matching a given hash costs about2^bits. This difference is why collision resistance is the first property to weaken. - Assuming a perfect hash. The formula assumes outputs are uniform. A biased hash, such as one that sums character codes, collides sooner than the formula says.
Exercise
A system stores records under 48-bit hash IDs. Using the birthday bound, about how many records can it hold before a collision reaches roughly even odds?
Show answer
About 1.177 sqrt(248) = 1.177 2**24, which is roughly 20 million records. Collision risk is already noticeable well before that, so 48 bits is too small for IDs that must be unique.
Exercise
Why does doubling the output length of a hash from 128 to 256 bits raise the collision work from about 264 to about 2128 rather than just doubling it?
Show answer
Collision work scales as 2**(bits/2), so it grows exponentially with the bit count. Adding 128 bits adds 64 to the exponent of the work.