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 , with arithmetic modulo . For a word , let and be the values returned by lines L12 to L13 and L14 to L18. For a block , let 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 with and the tail word of line L7, it returns , where is the seed, if and otherwise.
Proof. The loop of line L2 makes passes and every other line runs once, so the procedure stops. Line L3 forms from bytes to , and lines L4 and L5 replace by ; by induction on , after passes . Lines L6 to L9 apply , line L10 XORs , and line L11 applies FMIX.
Lemma 2 (the basic bijections). Each of the following maps of 32-bit words is a bijection: (a) for a constant ; (b) ; (c) for an odd constant ; (d) ; (e) for .
Proof. (a) is its own inverse. (b) has the inverse . (c): an odd is coprime to , so it has an inverse modulo , and inverts the map. (d) has the inverse . (e): the set is finite, so it suffices to show injectivity. Let . Bits down to of are 0, so these bits of equal those of . For a bit , bit of is bit of XOR bit of . Going from bit 31 down to bit 0, each bit of is determined by and the higher bits of , which are already known. So determines .
Lemma 3. MIX and FMIX are bijections, and for every block , is a bijection.
Proof. MIX is (c) with , then (b), then (c) with ; and are odd. FMIX is (e) with , (c) with , (e) with , (c) with and (e) with ; and are odd. is (a) with , (b), (c) with and (d). Compositions of bijections are bijections (Lemma 2).
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 . For a fixed input, is the identity or (a), and is (a). By Lemma 3, the map hash is a composition of bijections.
Lemma 5 (one changed block). Let be inputs of the same length 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 . If it is the full block , the states before it are equal, say . MIX is injective (Lemma 3), so , and the rest of 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 ; 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.
Lemma 6 (collisions for every seed). Let be blocks with and , and let be any byte string. Then and have equal hashes for every seed.
Proof. Such exist because MIX is a bijection (Lemma 3). Let be the state before the first block; it is the seed. After line L4, the states are and . ROTL by 13 gives and . XOR with is the same as adding modulo , since it changes only the top bit. So the states after line L5 are and , where , because . Writing , they are and . After line L4 on the second block, they are and , which are equal. From here the two inputs have the same bytes and the same length, so by Lemma 1 they reach the same hash.
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.
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.