Collision on self made hashing function I am currently designing a hashing function based on a mathematical formular to calculate the size of an arc on a circle with the radius 1.

From that I made a small hashing function which works kind of like this:

f(n) = n² * π / 180


Then I only take the decimal places and convert them to hex. I remove the first three letters / numbers and take the next three places.

I then tried to find collisions by brute force because it would not take too long.
I started with 1.

f(1) = 553


Then I tried to find collisions and I found the following:

123, 4755, 8313


I expected the last two ones because they exceed the range of the hash-length.
The first one however was unexpected and I simply cannot explain it. Is it because of bad chance or is it because of the function?

(I know about the other flaws of this design)

I greatly appreciate any answer which helps me in this endeavour.
(Actually, this is about second-preimage resistance, not collision resistance)


  I expected the last two ones because they exceed the range of the hash-length. The first one however was unexpected and I simply cannot explain it. Is it because of bad chance or is it because of the function?


Actually, even for a good hash function, that has a relatively good chance at happening.

Let us assume that our hash function $F$ with an $n$-bit output is "good", that is, it acts like a totally random function.

Then, we evaluate $F(1)$ and get a specific value $z$.

Then, we evaluate $F(x)$ for $1 &lt; x &lt; 2^n$, and see if $z$ happens to occur a second time.

As $F(x)$ is random, then for any particular $x$, it will happen to be the value $z$ with probability $2^{-n}$; that is, it will be a value other than $z$ with probability $1 - 2^{-n}$

So, for $F(x)$ never to be $z$ for any of those $2^n - 2$ values of $x$, each probability is independent, and so the total probability is the product of the individual probabilities, namely, $(1 - 2^{-n})^{2^n-2} \approx 1/e \approx 0.368$ (where $e$ is Euler's constant 2.718281828459...)

That is, there is an about 63% chance of $F(1)$ appearing at least once somewhere else, even if you limit the range of the input to the hash-length.