Section 5.6

Birthday bound

Definition

The birthday bound is the rule that values drawn at random from NN equally likely possibilities are likely to contain a repeat after only about N\sqrt{N} draws, far fewer than NN. Stated exactly, the probability that qq independent draws contain two equal values is at most q(q−1)/(2N)q(q-1)/(2N), and it passes one half near q=1.18Nq = 1.18\sqrt{N}. The name comes from birthdays: among 23 people, two share a birthday with probability above one half. The bound sets the length needed for random identifiers and for any other values that must not repeat.

The idea

A designer gives every device a random 32-bit identifier. There are 2322^{32} possible identifiers, about 4.3 billion, so a fleet of a hundred thousand devices seems to have room to spare. It does not. With 77,164 devices, the chance that two of them received the same identifier is already above one half. With 200,000 devices it is about 0.99.

The mistake is to compare the number of devices with the number of identifiers. A repeat is something that happens to a pair of devices, and pairs are far more numerous than devices: qq devices form q(q−1)/2q(q-1)/2 pairs. The 77,164 devices form about 2.98 billion pairs, and each pair has one chance in 4.3 billion of sharing an identifier. Repeats become likely when the number of pairs is of the same order as the number of possible values, and that happens when qq is near the square root of NN.

How it works

To find the probability of a repeat among qq draws from NN equally likely values, compute the probability of no repeat and subtract it from 1.

  1. The first draw cannot repeat anything.

  2. The second draw differs from the first with probability (N−1)/N(N-1)/N.

  3. The third draw differs from the first two with probability (N−2)/N(N-2)/N, given that those two differ from each other.

  4. Continue to draw number qq, which differs from the q−1q - 1 earlier values with probability (N−q+1)/N(N-q+1)/N.

  5. Multiply these probabilities. The product is the probability that all qq values are different. One minus the product is the probability of at least one repeat.

Each factor is a conditional probability §15.43: the chance that this draw is new, given that all earlier draws were different.

Two shortcuts replace the product. Write x=q(q−1)/(2N)x = q(q-1)/(2N), the number of pairs divided by NN.

When xx is small, the two limits nearly coincide and the probability is simply about xx, which is often written q2/(2N)q^2/(2N).

Turned around, the number of draws that gives a chosen repeat probability pp is about 2Nln(1/(1−p))\sqrt{2N \ln(1/(1-p))}, where ln\ln is the natural logarithm. For p=1/2p = 1/2 this is 1.18N1.18\sqrt{N}. For a small pp it is about 2Np\sqrt{2Np}.

Worked example

By hand. Draw 3 values from N=4N = 4. The probability of no repeat is (3/4)×(2/4)=6/16(3/4) \times (2/4) = 6/16, so the probability of a repeat is 1−6/16=10/16=0.6251 - 6/16 = 10/16 = 0.625.

Draw 4 values from N=16N = 16. The probability of no repeat is (15/16)×(14/16)×(13/16)=2730/4096(15/16) \times (14/16) \times (13/16) = 2730/4096, so the probability of a repeat is 1366/4096≈0.33351366/4096 \approx 0.3335. For the shortcuts, there are 4×3/2=64 \times 3 / 2 = 6 pairs, so x=6/16=0.375x = 6/16 = 0.375, the upper limit, and 1−e−0.375≈0.31271 - e^{-0.375} \approx 0.3127, the lower limit. Here qq is not small compared with NN, so the two limits lie well apart.

Birthdays. With 23 people and 365 equally likely birthdays, the exact product gives 0.5073. There are 23×22/2=25323 \times 22 / 2 = 253 pairs, so x=253/365≈0.693x = 253/365 \approx 0.693 and 1−e−0.693≈0.5001 - e^{-0.693} \approx 0.500.

Identifiers. For random 32-bit identifiers, N=232N = 2^{32} and N=65,536\sqrt{N} = 65{,}536. The chance of a repeat is about 0.39 with 65,536 identifiers and passes one half at 77,164. It is still about 1% with 9,292 identifiers. To keep it below one in a million, no more than 93 identifiers may be issued.

A larger space. A system sends 2322^{32} messages under one encryption key and gives each message a random 96-bit nonce §5.18, a number that must not repeat under that key. Then x=232(232−1)/(2×296)x = 2^{32}(2^{32} - 1)/(2 \times 2^{96}), which is just under 264/297=2−332^{64}/2^{97} = 2^{-33}, about 1.2×10−101.2 \times 10^{-10}. The chance of any repeat is at most that.

Stated precisely

Assume that qq values are drawn independently and uniformly at random from a set of NN values, with q≤Nq \le N. Let PP be the probability that at least two of the values are equal, and let x=q(q−1)/(2N)x = q(q-1)/(2N). Then P=1−∏i=1q−1(1−iN)and1−e−x≤P≤x.P = 1 - \prod_{i=1}^{q-1}\left(1 - \frac{i}{N}\right) \qquad \text{and} \qquad 1 - e^{-x} \;\le\; P \;\le\; x. The symbol ∏\prod means the product of the factors for i=1,2,…,q−1i = 1, 2, \dots, q - 1. Because 1−e−x1 - e^{-x} is at least x−x2/2x - x^2/2, the simple estimate P≈xP \approx x is too high by at most x2/2x^2/2. For x=0.01x = 0.01 the true value lies between 0.00995 and 0.01. The expected number of draws up to the first repeat is about πN/2≈1.25N\sqrt{\pi N/2} \approx 1.25\sqrt{N} for large NN. If q>Nq > N, a repeat is certain.

The name birthday bound is used for the inequality P≤q(q−1)/(2N)P \le q(q-1)/(2N) and also for the threshold q≈Nq \approx \sqrt{N} at which PP stops being small. One use in cryptography concerns hash functions. A hash function turns any input into a fixed-length value called a digest, and its collision resistance §5.5 is the difficulty of finding two different inputs with the same digest. With digests of nn bits, N=2nN = 2^n and the threshold is 2n/22^{n/2} inputs. This is why the collision resistance of a hash function is at most half its digest length, counted in bits.

The result depends on two assumptions. Independence: no draw depends on another. Uniform distribution: on every draw, each of the NN values is equally likely. If the draws are independent and all follow the same distribution but that distribution is not uniform, a repeat is more likely than the formula says, so the formula gives the most favorable case. If an attacker can choose the values, the formula does not apply.

Limits and common mistakes

The bound concerns a repeat between any two values. The probability that one of qq draws equals one particular value fixed in advance is only about q/Nq/N. Confusing the two cases is the most common error. The birthday bound applies to a search for any collision, not to a search for an input that matches a given digest.

The estimate q2/(2N)q^2/(2N) is valid only while it is small. For q=100,000q = 100{,}000 and N=232N = 2^{32} it gives 1.16, which is not a probability. The true value is about 0.69.

Staying under the threshold is not safety. At N\sqrt{N} draws a repeat already has probability 0.39. A design must first choose an acceptable probability, such as one in a billion, and derive the permitted number of draws from it.

Count every value ever drawn from the same space. For nonces, that means all messages under one key for its whole lifetime, on all devices that use the key, not the messages of one session.

The bound describes random values. A counter that starts at zero and goes up by one does not repeat until it runs out of values, so it has no birthday limit. A counter fails in other ways: when it is reset, when it is restored from an old copy, or when two senders use it at once.

A weak random number generator breaks both assumptions, and repeats can then occur far earlier than the bound predicts.

Questions answered

How do you approximate the collision probability for q samples in a space of size N?

Divide the number of pairs of samples by the size of the space: P≈q(q−1)/(2N)P \approx q(q-1)/(2N), or q2/(2N)q^2/(2N) when qq is large. Here a collision means that two of the qq samples are equal. This estimate is accurate while the result is small, and it is never below the true probability. When the result is not small, use P≈1−e−q(q−1)/(2N)P \approx 1 - e^{-q(q-1)/(2N)}, where e≈2.718e \approx 2.718 is the base of the natural logarithm. That expression stays between 0 and 1 and is accurate whenever qq is much smaller than NN.

Example: q=10,000q = 10{,}000 random 32-bit values, so N=232=4,294,967,296N = 2^{32} = 4{,}294{,}967{,}296. The number of pairs is 10,000×9,999/2=49,995,00010{,}000 \times 9{,}999 / 2 = 49{,}995{,}000. Dividing by NN gives 0.01164. The second expression gives 1−e−0.01164=0.011571 - e^{-0.01164} = 0.01157, and the exact calculation also gives 0.01157.

The exact value is 1−(1−1/N)(1−2/N)⋯(1−(q−1)/N)1 - (1 - 1/N)(1 - 2/N) \cdots (1 - (q-1)/N). All three expressions assume that the qq samples are independent and that each is uniformly distributed over the NN values. For independent samples that follow one non-uniform distribution, the true probability is higher. A quick check on any result: a collision reaches probability one half near q=1.18Nq = 1.18\sqrt{N}.

Related entries: Collision resistance (§5.5), Nonce (§5.18), Entropy and min-entropy (§5.26), SHA-256 (§5.2), Union bound (§15.42). Further reading: [66].