Definition
The birthday bound is the rule that values drawn at random from equally likely possibilities are likely to contain a repeat after only about draws, far fewer than . Stated exactly, the probability that independent draws contain two equal values is at most , and it passes one half near . 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 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: devices form 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 is near the square root of .
How it works
To find the probability of a repeat among draws from equally likely values, compute the probability of no repeat and subtract it from 1.
The first draw cannot repeat anything.
The second draw differs from the first with probability .
The third draw differs from the first two with probability , given that those two differ from each other.
Continue to draw number , which differs from the earlier values with probability .
Multiply these probabilities. The product is the probability that all 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 , the number of pairs divided by .
The probability of a repeat is at most . Each pair is equal with probability , and adding this over all pairs can only overestimate. This step is the union bound §15.42.
The probability of a repeat is at least , where is the base of the natural logarithm. When is much smaller than , the probability is very close to this value.
When is small, the two limits nearly coincide and the probability is simply about , which is often written .
Turned around, the number of draws that gives a chosen repeat probability is about , where is the natural logarithm. For this is . For a small it is about .
Worked example
By hand. Draw 3 values from . The probability of no repeat is , so the probability of a repeat is .
Draw 4 values from . The probability of no repeat is , so the probability of a repeat is . For the shortcuts, there are pairs, so , the upper limit, and , the lower limit. Here is not small compared with , so the two limits lie well apart.
Birthdays. With 23 people and 365 equally likely birthdays, the exact product gives 0.5073. There are pairs, so and .
Identifiers. For random 32-bit identifiers, and . 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 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 , which is just under , about . The chance of any repeat is at most that.
Stated precisely
Assume that values are drawn independently and uniformly at random from a set of values, with . Let be the probability that at least two of the values are equal, and let . Then The symbol means the product of the factors for . Because is at least , the simple estimate is too high by at most . For the true value lies between 0.00995 and 0.01. The expected number of draws up to the first repeat is about for large . If , a repeat is certain.
The name birthday bound is used for the inequality and also for the threshold at which 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 bits, and the threshold is 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 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 draws equals one particular value fixed in advance is only about . 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 is valid only while it is small. For and it gives 1.16, which is not a probability. The true value is about 0.69.
Staying under the threshold is not safety. At 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: , or when is large. Here a collision means that two of the 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 , where is the base of the natural logarithm. That expression stays between 0 and 1 and is accurate whenever is much smaller than .
Example: random 32-bit values, so . The number of pairs is . Dividing by gives 0.01164. The second expression gives , and the exact calculation also gives 0.01157.
The exact value is . All three expressions assume that the samples are independent and that each is uniformly distributed over the 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 .
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].