The birthday paradox in action: calculating the probability of a hash collision
solipsys.co.uk
solipsys.co.uk
[...] People often ask why they need to memorise formulas, or why they need to practice solving equations, when they can simply look stuff up whenever they need it, and on-line computer algebra systems can solve equations faster than they can, and more reliably.
But this is an example of why the ability simply to look stuff up is near useless on its own. Searches are deep and wide, and you need intuition to guide you. You need to recognise what might work, things you've seen before, directions to take that are more likely to be fruitful. [...]
Applying the formula for 160bit SHA-1 you need 1.7e23 objects to get a 1% chance of collision. The current Linus kernel repository has 2.7 million objects. So to get a collision you'd need a repository that's 6e16 times larger. That should be plenty.
For some wacky perspective that's 10 million kernel sized contributions for every man woman and child on earth together in a single repository. It would seem git will reach plenty of other bottlenecks before SHA-1 becomes a problem...
Probabilities are fun, because anything non-zero is non-zero ;) (I don't expect to ever witness a git hash collision of course)
If you enjoy this stuff, you'll get a pleasant tickle out of this one:
What is the probability that there exists a SHA-1 hash which hashes to itself? In other words, are there any fixed points?
(This post was edited to fix the phrasing mistake nicely pointed out by a commenter. Originally, I asked "probability that a SHA-1" when I meant "probability that any SHA-1")
I do get a pleasant tickle from expanding that to the question of if there is any fixed point in SHA-1, but the chance for a particular value is the answer you always get for "what is the chance of a specific hash".
I am making this remark because the question reminded me of the question "If it is the 13th of the month, what is the probability that it is Friday?" (no tricks with weird calendars; you should just use the Gregorian one).
I have no idea how you might go about finding that one expected instance.
Only if there is a unique fixed point ….
Without using any knowledge about how they are
computed, & only using the fact that a hash is,
in effect, a result chosen uniformly at random
from a set of results, what is the probability
that ...
So in particular, consider a collection of numbers, and for each one, choose a target uniformly at random from the same collection. So for the set X you have a function f:X->X. What is the probability that there is a fixed point x such that f(x)=x?This, by the way, is a very well known calculation/result in my area of math.
frr uggcf//ra.jvxvcrqvn.bet/jvxv/Qrenatrzrag#Yvzvg_bs_engvb_bs_qrenatrzrag_gb_crezhgngvba_nf_a_nccebnpurf_.R2.88.9R
one minus derangements over permutations
see
https://en.wikipedia.org/wiki/Derangement#Limit_of_ratio_of_...
In case you are one of the many, many people who get this wrong
and are astonished, you may be thinking of a different question.
My birthday is a specific date. If we assume birthdays are
uniformly distributed, how many people must you ask before you
find someone who has the same birthday as me?
On average, about 182.
If you ask some 150 to 200 people, the odds are about 50% that
you'll find someone who shares my specific birthday.
But that's not the question I asked. They don't just need to share
my birthday, it can be that any birthday is shared among them, and
that changes the odds dramatically.1) that if you have a specific hash, how many more hashes would have to be generated to get the same hash.
2) how many hashes would you have to generate before a collision occurs between any of them.
So in the two cases above we're dealing with sets of hashes, and we don't care how they are generated.
Xylakant's comment was about a different type of collision. This collision happens because you are mapping an arbitrary large text into a fixed size hash, which means that different texts can map to the same hash, hence creating a collision.
> It is very important to note that this is the probability
> that any two hashes collide in a set of hashes. It is not
> the probability to find a second plaintext that hashes to
> the same hash as a given one.
ColinWright: > Indeed, pretty much exactly that point is made ...
> > In case you are one of the many, many people who get this wrong
> > and are astonished, you may be thinking of a different question.
> > My birthday is a specific date. If we assume birthdays are
> > uniformly distributed, how many people must you ask before you
> > find someone who has the same birthday as me?
> > ...
> > But that's not the question I asked. They don't just need to share
> > my birthday, it can be that any birthday is shared among them, and
> > that changes the odds dramatically.
dsego: > I don't see how that point relates to Xylakant's comment.
OK, I'll try to explain my thinking: > The text you quoted basically points to the difference
> between these two cases:
> 1) that if you have a specific hash, how many more hashes
> would have to be generated to get the same hash.
> 2) how many hashes would you have to generate before a
> collision occurs between any of them.
Yes. > So in the two cases above we're dealing with sets of hashes,
> and we don't care how they are generated.
That's true. > Xylakant's comment was about a different type of collision.
> This collision happens because you are mapping an arbitrary
> large text into a fixed size hash, which means that different
> texts can map to the same hash, hence creating a collision.
If you're using cryptographic hashes then I believe these amount to the same thing.The distinguishing characteristics of a cryptographic hash are that it has no internal structure, and it is completely unpredictable based on knowledge of similar source texts. In particular, techniques such as differential analysis don't work.
In that sense, finding a text to map to a hash to give a collision is exactly the same as picking a random number that happens to match. The act of starting with a text and using that to create a hash is identical to the act of choosing a random element of the hash space. If these two things were not identical, then the hash would have some sort of predictability or structure that related it to the source text.
The situation is different if you are using non-cryptographic hashes such as CRCs. Perhaps I should edit the original article to help make that distinction, but I'm not really sure it's worth it.
[1] When generating emails more restrictions apply: The colliding plaintexts have to be at least somewhat coherent and probably should express something the attacker wants to express.
Edit: Forgot a negation in a crucial place. Darn.
You're exactly right, and I believe it's covered. I didn't go into detail about the problems involved in generating a plain text that hashes to a specific hash,such as you mention. I did simply mention that the problem I'm talking about is not that one.
So I don't understand the point dsego was making, because I think my reference is relevant.
At this point I'm no longer sure it really matter.
Not a problem, and you're welcome!
The paradoxy bits of the problem are the things that people find confusing. "Only 23?!" or "So if there are 23 people in a room there's a 50% chance that one of them will share a birthday with me?"
I would think that saying "So if there are 23 people in the room there is a 50% chance any 2 of those people share the same birthday" is better.
Edit: I misunderstood, you're saying that other people are misunderstanding. My mistake!
The paradoxy bits of the problem are the things that
people find confusing. "Only 23?!" or "So if there are
23 people in a room there's a 50% chance that one of
them will share a birthday with me?"
In that comment he is saying that people mis-understand the question, and assume that it means that once there are 23 people in the room, then there's a 50:50 chance they will share a birthday with them specifically.And that's exactly the wrong question, as you point out. So when you say:
Is "So if there are 23 people in a room there's a 50% chance
that one of them will share a birthday with me?" correct?
No, that's not correct, but it is what people think they hear, and it's that confusion that makes this whole thing sometimes called a paradox.So let's be clear:
If you're in a room with 22 other people, the chance
that one of them shares a birthday specifically with
you is nowhere near 50%
However, the chance that among the 23 people in the
room there is, somewhere, a shared birthday, is indeed
slightly greater than 50%
And my experience is that it really doesn't matter how carefully you word this, some people simply will not understand it.It is also counterintuitive, because it is unavoidable. Even with a very large hash range, one can force a collision with a relatively small number of samples.
[1]: https://en.wikipedia.org/wiki/Banach%E2%80%93Tarski_paradox
For those interested in another look (basic) at the Birthday Paradox, check out http://alexanderle.com/blog/2011/birthday-paradox.html !
So this is unlikely to be a concern, ever.
But yeah, 160 bits is probably "enough" :-)