Algorithm Search⌘K

Proofs: MurmurHash

These are the human proofs of MurmurHash, at full length.

MurmurHash3_x86_32 is built from bijections of the state full

This proof shows: correctness, termination.

Notation. All values are 32-bit words, that is elements of {0,…,232−1}\{0, \ldots, 2^{32} - 1\}, with arithmetic modulo 2322^{32}. For a word kk, let MIX(k)\mathrm{MIX}(k) and FMIX(k)\mathrm{FMIX}(k) be the values returned by lines L12 to L13 and L14 to L18. For a block kk, let Bk(h)=ROTL(h⊕MIX(k),13)⋅5+0xe6546b64B_k(h) = \mathrm{ROTL}(h \oplus \mathrm{MIX}(k), 13) \cdot 5 + \mathtt{0xe6546b64} be the effect of lines L4 and L5 on the state.

Lemma 1 (termination and the computed value). MURMUR3-32 stops on every input. If the input has the full blocks k0,…,kq−1k_0, \ldots, k_{q-1} with q=⌊n/4⌋q = \lfloor n/4 \rfloor and the tail word kqk_q of line L7, it returns FMIX(T(Bkq−1(⋯Bk0(s)⋯ ))⊕n)\mathrm{FMIX}(T(B_{k_{q-1}}(\cdots B_{k_0}(s) \cdots)) \oplus n), where ss is the seed, T(h)=h⊕MIX(kq)T(h) = h \oplus \mathrm{MIX}(k_q) if n>4qn > 4q and T(h)=hT(h) = h otherwise.

Proof. The loop of line L2 makes qq passes and every other line runs once, so the procedure stops. Line L3 forms kjk_j from bytes 4j4j to 4j+34j + 3, and lines L4 and L5 replace hh by Bkj(h)B_{k_j}(h); by induction on jj, after jj passes h=Bkj−1(⋯Bk0(s)⋯ )h = B_{k_{j-1}}(\cdots B_{k_0}(s) \cdots). Lines L6 to L9 apply TT, line L10 XORs nn, and line L11 applies FMIX. □\square

Lemma 2 (the basic bijections). Each of the following maps of 32-bit words is a bijection: (a) x↦x⊕cx \mapsto x \oplus c for a constant cc; (b) x↦ROTL(x,r)x \mapsto \mathrm{ROTL}(x, r); (c) x↦cxx \mapsto c x for an odd constant cc; (d) x↦x+cx \mapsto x + c; (e) x↦x⊕(x≫r)x \mapsto x \oplus (x \gg r) for 1≤r≤311 \le r \le 31.

Proof. (a) is its own inverse. (b) has the inverse ROTL(⋅,32−r)\mathrm{ROTL}(\cdot, 32 - r). (c): an odd cc is coprime to 2322^{32}, so it has an inverse c−1c^{-1} modulo 2322^{32}, and y↦c−1yy \mapsto c^{-1} y inverts the map. (d) has the inverse y↦y−cy \mapsto y - c. (e): the set is finite, so it suffices to show injectivity. Let y=x⊕(x≫r)y = x \oplus (x \gg r). Bits 3131 down to 32−r32 - r of x≫rx \gg r are 0, so these bits of yy equal those of xx. For a bit i<32−ri < 32 - r, bit ii of yy is bit ii of xx XOR bit i+ri + r of xx. Going from bit 31 down to bit 0, each bit of xx is determined by yy and the higher bits of xx, which are already known. So yy determines xx. □\square

Lemma 3. MIX and FMIX are bijections, and for every block kk, BkB_k is a bijection.

Proof. MIX is (c) with c1c_1, then (b), then (c) with c2c_2; c1c_1 and c2c_2 are odd. FMIX is (e) with r=16r = 16, (c) with f1f_1, (e) with r=13r = 13, (c) with f2f_2 and (e) with r=16r = 16; f1f_1 and f2f_2 are odd. BkB_k is (a) with c=MIX(k)c = \mathrm{MIX}(k), (b), (c) with c=5c = 5 and (d). Compositions of bijections are bijections (Lemma 2). □\square

Lemma 4 (seeds). For a fixed input, the map from the seed to the hash is a bijection. So distinct seeds give distinct hashes of one input.

Proof. By Lemma 1, the hash is FMIX(T(Bkq−1∘⋯∘Bk0(s))⊕n)\mathrm{FMIX}(T(B_{k_{q-1}} \circ \cdots \circ B_{k_0}(s)) \oplus n). For a fixed input, TT is the identity or (a), and ⊕n\oplus n is (a). By Lemma 3, the map s↦s \mapsto hash is a composition of bijections. □\square

Lemma 5 (one changed block). Let x≠yx \ne y be inputs of the same length nn that differ only within one full block, or only within the tail. Then they have different hashes for every seed.

Proof. The two inputs have the same blocks except one, with words k≠k′k \ne k'. If it is the full block jj, the states before it are equal, say hh. MIX is injective (Lemma 3), so h⊕MIX(k)≠h⊕MIX(k′)h \oplus \mathrm{MIX}(k) \ne h \oplus \mathrm{MIX}(k'), and the rest of BB is injective, so the states after the block differ. The remaining steps are the same bijections for both inputs, since they have the same later blocks, the same tail and the same nn; so the hashes differ. If it is the tail, the tail words differ, the states before line L9 are equal, and the same argument applies to line L9 followed by lines L10 and L11. □\square

Lemma 6 (collisions for every seed). Let x1,x2,y1,y2x_1, x_2, y_1, y_2 be blocks with MIX(y1)=MIX(x1)⊕218\mathrm{MIX}(y_1) = \mathrm{MIX}(x_1) \oplus 2^{18} and MIX(y2)=MIX(x2)⊕231\mathrm{MIX}(y_2) = \mathrm{MIX}(x_2) \oplus 2^{31}, and let zz be any byte string. Then x1x2zx_1 x_2 z and y1y2zy_1 y_2 z have equal hashes for every seed.

Proof. Such y1,y2y_1, y_2 exist because MIX is a bijection (Lemma 3). Let hh be the state before the first block; it is the seed. After line L4, the states are u=h⊕MIX(x1)u = h \oplus \mathrm{MIX}(x_1) and u⊕218u \oplus 2^{18}. ROTL by 13 gives vv and v⊕231v \oplus 2^{31}. XOR with 2312^{31} is the same as adding 2312^{31} modulo 2322^{32}, since it changes only the top bit. So the states after line L5 are 5v+a5v + a and 5(v+231)+a=5v+a+5⋅231=5v+a+2315(v + 2^{31}) + a = 5v + a + 5 \cdot 2^{31} = 5v + a + 2^{31}, where a=0xe6546b64a = \mathtt{0xe6546b64}, because 4⋅231≡04 \cdot 2^{31} \equiv 0. Writing w=5v+aw = 5v + a, they are ww and w⊕231w \oplus 2^{31}. After line L4 on the second block, they are w⊕MIX(x2)w \oplus \mathrm{MIX}(x_2) and w⊕231⊕MIX(x2)⊕231=w⊕MIX(x2)w \oplus 2^{31} \oplus \mathrm{MIX}(x_2) \oplus 2^{31} = w \oplus \mathrm{MIX}(x_2), which are equal. From here the two inputs have the same bytes zz and the same length, so by Lemma 1 they reach the same hash. □\square

Theorem. MURMUR3-32 stops on every input and returns the value of Lemma 1. Its block mix and finalizer are bijections; for a fixed input, distinct seeds give distinct hashes; equal-length inputs that differ in one block never collide; and the pairs of Lemma 6 collide for every seed.

Proof. Lemmas 1, 3, 4, 5 and 6. ■\blacksquare

Remark (what is not proved). The avalanche behaviour and the spread of hash values over ordinary inputs are measured properties, not theorems. Lemma 6 shows that no bound on the probability of a collision over a random seed can hold for all fixed pairs of inputs.

Back to MurmurHash