The Birthday Paradox

An entire family of attacks on cryptographic hash functions hides inside a parlour-trick about shared birthdays.

Pick people from a crowd, one at a time, and after each arrival ask: do any two people in the room share a birthday? The intuitive guess is something like one hundred and eighty-three, half of three hundred and sixty-five. The actual number is twenty-three.

This is the birthday paradox: not a paradox in the logical sense, just a violation of intuition. It is also the foundation of an entire family of attacks on cryptographic hash functions, and the reason a 256-bit hash gives only 128 bits of collision security.

The question

How many people do you need in a room before two of them share a birthday, with better than even odds?

The intuitive guess is huge, because your birthday only matches each other person with probability 1/3651/365. But the question isn’t about you. It’s about any match in the entire room.

Five people

Imagine the room filling up. With five people in it, the chance of any two sharing a birthday is about 2.7%.

Ten people

At ten, it’s around 11.7%. The probability is rising faster than you’d guess: each new person adds a collision opportunity with everyone already in the room. Match candidates grow as (n2)\binom{n}{2}, not nn.

Twenty-three

At twenty-three, the probability crosses one half. From a pool of 365 possible birthdays, twenty-three people are enough to make a match more likely than not.

Fifty

By fifty, the chance is 97%. Past sixty, you’d be unlucky to not find a match. The collisions, once they start, are everywhere.

The formula

The first person has any birthday. The second person must avoid the first: probability 364/365364/365. The third must avoid two: 363/365363/365. And so on. Multiply, then subtract from one: that’s the chance of at least one match.

Cryptography

The same calculation applies whenever you’re throwing samples into a finite space. A hash function maps inputs to one of 2N2^N outputs. An attacker hashing random inputs is filling a room with people; the “birthday” is the hash output; a “match” is a collision.

365 possible birthdaysPEOPLE00.5P(match)0.0000.52350

The calculation, in full

Order the nn people 1,2,,n1, 2, \ldots, n. The first has any birthday. The second must avoid the first’s birthday: probability 364/365364/365. The third must avoid the two existing birthdays: 363/365363/365. Continuing, the probability that no two people share a birthday is

P(no match)  =  k=1n1 ⁣(1k365).P(\text{no match}) \;=\; \prod_{k=1}^{n-1}\!\left(1 - \frac{k}{365}\right).

The probability of at least one match is the complement:

P(match)  =  1k=1n1 ⁣(1k365).P(\text{match}) \;=\; 1 - \prod_{k=1}^{n-1}\!\left(1 - \frac{k}{365}\right).

Plug in n=23n = 23 and you get 0.50730.5073. Plug in n=50n = 50 and you get 0.97040.9704.

A cleaner approximation

For small xx, ln(1x)x\ln(1 - x) \approx -x. So

lnP(no match)    k=1n1k365  =  n(n1)2365.\ln P(\text{no match}) \;\approx\; -\sum_{k=1}^{n-1}\frac{k}{365} \;=\; -\frac{n(n-1)}{2 \cdot 365}.

The probability of at least one match crosses 1/21/2 when n(n1)/(2365)ln2\,n(n-1)/(2 \cdot 365) \approx \ln 2, or n2365ln222.5n \approx \sqrt{2 \cdot 365 \cdot \ln 2} \approx 22.5.

The general rule: in a uniform space of NN possibilities, expect a collision after roughly N\sqrt{N} samples, not NN.

Why cryptographers care

A hash function HH maps arbitrary inputs to one of 2N2^N outputs. If HH is well-designed, finding two distinct inputs xyx \neq y with H(x)=H(y)H(x) = H(y), a collision, should be hard. The birthday bound tells us how hard.

After 2N=2N/2\sqrt{2^N} = 2^{N/2} random hash queries, an attacker expects to have seen a collision by pure coincidence. So an NN-bit hash function offers only N/2N/2 bits of collision security, not NN.

This is why SHA-256 is the standard for “128 bits of security”: its 256-bit output gives 21282^{128} birthday-attack resistance against collisions. It is also why SHA-1 (160 bits → 80 bits of collision security) was retired ahead of schedule when SHAttered showed an 263\approx 2^{63}-cost attack was practical.

The same square-root rule shows up in Pollard’s ρ\rho for discrete logarithms, in distinguishability bounds for keyed PRFs under random sampling, and in the design of Merkle-tree commit schemes. Once you see the birthday in a crowd, you start seeing it everywhere.