Part A: a small modulus has few values
With there are only 101 fingerprints, so among 102 words two must collide. A search over
a few hundred words finds a pair, for example aaah and aaca.
Part B: a random base is a random point of a polynomial
The difference of two fingerprints is a polynomial in the base:
. The letter differences
lie between and , so for two different words this polynomial is not zero mod 101, and a
nonzero polynomial of degree at most 2 has at most 2 roots mod a prime. So no pair collides for
more than 2 of the 101 bases. To reach 2, choose the roots first: ,
so the letter differences are , for example bac and ada.
That is the whole case for Rabin–Karp with a random base: two strings of length collide with
probability at most , so with a large prime a collision is rare whatever strings
the adversary writes.
Part C: without a prime, the roots come back
Mod the argument fails, because is not prime and a polynomial can vanish at
every odd point. Take the Thue–Morse strings: is a, and is followed by
with a and b swapped. The difference between and its complement is, up to sign,
. For odd , the factor is even, and
for is divisible by (since is divisible by 8). So the
product is divisible by , which reaches at
(32 letters), at (128 letters) and at (1024 letters). The
bound is exact for a base that is 3 more than a multiple of 8, so a shorter Thue–Morse string
does not work.
So a fingerprint computed with plain 64-bit arithmetic is broken for every base at once by two
strings of 1024 letters. Programming contests use this as a standard "anti-hash" test.
Going further
- Use a prime modulus and a random base. Then part B's bound holds: collisions have
probability at most . A common choice is , which has fast reduction.
- A fixed, known base can still be attacked. A birthday search over about strings
finds a collision mod , and lattice reduction (LLL) finds collisions far shorter
than either. A secret random base defeats both.
- The same polynomial argument powers
Polynomials in Algorithm Design: a nonzero
polynomial rarely vanishes at a random point.
References
- R. M. Karp, M. O. Rabin. Efficient randomized pattern-matching algorithms. IBM Journal of
Research and Development 31(2), 1987.
- A. K. Lenstra, H. W. Lenstra, L. Lovász. Factoring polynomials with rational coefficients.
Mathematische Annalen 261, 1982.