The Birthday paradox / attack
Authors: Zademn, ireland Reviewed by:
Prerequisites
Probability theory (for the main idea)
Hashes (an application)
Motivation
Breaking a hash function (insert story)
The birthday paradox
*insert story / introduction about why it's called a paradox + use*
Birthday version
Question 1
What is the probability that 1 person has the same birthday as you?
Solution
Let A be the event that someone has the same birthday as you and Aˉ be the complementary event
The events are mutually exclusive => Pr(A)=1−Pr(Aˉ)
Let Ei be the events that person i does not have your birthday
Then
Pr(A)=1−Pr(Aˉ)=1−∏i=1nPr(Ei)=1−(365364)n
Reminder: Pr(A,B)=Pr(A)⋅Pr(B)if A,Bare independent events
Question 2
What is the probability that 2 out of n people in a room share the same birthday?
Suppose the birthdays are distributed independently and uniformly
Solution
Let A be the event that 2 people have the same birthday, let Aˉ be the complementary event (no 2 people have the same birthday)
Event 1 = Person 1 is born => Pr(E1)=365365
Event 2 = Person 2 is born on a different day than Person 1 => Pr(E2)=365364
⋮
Event n = Person n is born on a different day than Person 1,...,n−1⇒ ⇒Pr(En)=365365−(n−1)
Pr(Aˉ)=Pr(E1)⋅Pr(E2)⋅⋯⋅Pr(En)=365365⋅365364⋅⋯⋅365365−(n−1)=(3651)n⋅(365−n)!365!=∏i=1n−1(1−365i)
General case
Question 1
Instead of 365 days we have d⇒1−(dd−1)n
Question 2
Instead of 365 days we have d⇒1−i=1∏n−1(1−di)
Code examples
An useful approximation
From the Taylor approximation we know ex=1+x+2!x2+⋯=>ex≈1+x for x≪1 Apply for each event: ⇒x=−da⇒e−a/d≈1−da⇒Pr(A)=1−∏i=1n−1e−i/d=1−e−n(n−1)/2d≈1−e−n2/2d
If we want to solve for n knowing Pr(A) we take the ln => n≈2dln(1−Pr(A)1)
Finding a collision
Let H:M⟶T be a hash function with ∣M∣>>∣T∣
Let's denote N=∣T∣
Algorithm
1. Choose s≈N random distinct messages in M
2. Compute ti=H(mi) for 1≤i≤N
3. Look for (ti=tj)→ If not found go to step 1
Example:
Consider the following hash function:
We make the following function to find the hashes:
We use it as follows:
Eliminating Storage Requirements with Pollard's Rho
While the above algorithm works to find a hash collision in time O(N), it also requires storing O(N)hash values. As such, it represents a classic time-space tradeoff over the naive approach, which involves randomly selecting pairs of inputs until they hash to the same value. While the naive approach does not require any additional storage, it does have runtime O(N).
However, there is a better approach combining the best of both worlds: constant storage requirements and O(N)runtime. This approach is based on Pollard's Rho algorithm, which is better-known for its application to solving discrete logarithms. The core insight behind the algorithm is that by the Birthday Paradox, we expect to encounter a hash collision after trying O(N)random inputs. However, it is possible to detect whether a collision has occurred without needing to store all of the inputs and hashes if the inputs are chosen in a clever way.
This is done by choosing the next input based on the hash of the previous input, according to the following sequence:
Where H:M⟶T is our hash function and g:T⟶M is a "sufficiently random" function which takes a hash value and produces a new. We define the composition of the functions to bef=H∘g:T⟶T.
By the Birthday Paradox, we expect that the sequence will have a collision (where xi=xj for two distinct values i,j) after O(N)values. But once this occurs, then the sequence will begin to cycle, because xi+1=g(H(xi))=g(H(xj))=xj+1.
Therefore, we can detect that a collision has occurred by using standard cycle-detection algorithms, such as Floyd's tortoise and hare!
And finally, we can locate the first place in the sequence where the collision occurred, which will let us determine what the colliding inputs to the hash function are. This is done by determining how many iterations apart the colliding inputs are, and then stepping together one iteration at a time until the collision occurs.
For Floyd's tortoise and hare, this is done by noting that when we found our collision after n iterations, we were comparing H(xn)=H(x2n). And because the sequence is cyclic, if the first colliding input is xi, then it collides withH(xi)=H(xi+n). So we define the new sequence yi=xn+i, and step through the xi and yi sequences together until we find our collision!
This is implemented in the following code snippet.
Finally, there is a third algorithm for finding hash collisions in O(N)time, namely the van Oorschot-Wiener algorithm based on Distinguished Points. While it does have additional storage requirements over Pollard's Rho, the main advantage of this algorithm is that it parallelizes extremely well, achieving O(mN)runtime when run on m processors.
Resources
https://en.wikipedia.org/wiki/Birthday_problem - wiki entry
https://en.wikipedia.org/wiki/Birthday_attack - wiki entry
https://www.youtube.com/watch?v=ofTb57aZHZs - vsauce2 video
Last updated
Was this helpful?