Proofs: FNV hash
These are the human proofs of FNV hash, at full length.
FNV computes its recurrence, and its steps are bijections of the state full
This proof shows: correctness, termination.
Notation. Fix a width and write , and . All states are in . For a byte , let be one step of FNV-1a and one step of FNV-1. For a byte string , let , with the identity, so that the FNV-1a hash of is . The XOR of a state with a byte stays below because .
Lemma 1 (the procedure computes the recurrence). FNV-1A stops after passes of its loop and returns .
Proof. The loop at line L2 runs for , so it makes passes and stops. Let be the value of after passes. Then by line L1. Lines L3 and L4 replace by , so . By induction on , , and line L5 returns .
Lemma 2 (each step is a bijection). For every byte , and are bijections of .
Proof. The map is its own inverse on , so it is a bijection. is odd and is a power of 2, so and has an inverse modulo ; the map has the inverse . A composition of bijections is a bijection.
Lemma 3 (suffix extension). For all byte strings , the FNV-1a hashes satisfy if and only if . The same holds for FNV-1.
Proof. By Lemma 1, , and likewise . By Lemma 2, is a composition of bijections, so it is injective, and holds exactly when . For FNV-1, use in place of .
Lemma 4 (one changed byte). If and have the same length and differ in exactly one position, then , for FNV-1a and for FNV-1.
Proof. Write and with bytes . The state after is the same value for both. For FNV-1a, , and multiplication by modulo is injective (Lemma 2), so . For FNV-1, , so . The remaining steps for form an injective map by Lemma 2, so the final states differ.
Lemma 5 (low bits). For , depends only on and on the values . Bit 0 of equals bit 0 of XOR the parity of the number of indices with odd.
Proof. Bit of is bit of XOR bit of , so . Since divides , . So is a function of and . By induction on the steps, as in Lemma 1, the state after bytes modulo is a function of and . For : is odd, so multiplication by keeps bit 0, and the XOR with flips bit 0 exactly when is odd. So bit 0 of the final state is bit 0 of , flipped once for each odd byte.
Theorem. FNV-1A stops on every input and returns the FNV-1a hash defined by the recurrence. The hash satisfies the suffix extension property, never maps two strings of one length that differ in one byte to one value, and has the low-bit property of Lemma 5.
Proof. Termination and the returned value are Lemma 1. The other claims are Lemmas 3, 4 and 5.
Remark (what is not proved). No statement here bounds the number or the probability of collisions. FNV has no random parameter, and Lemma 3 shows that a single collision, which exists by the pigeonhole principle among any inputs, extends to infinitely many.