Algorithm Search⌘K

Proofs: Xor filter

These are the human proofs of Xor filter, at full length.

The false positive rate of an xor filter full

This proof shows: complexity.

Model. Assume ideal hashing: in each try, h0,h1,h2h_0, h_1, h_2 and fp\mathrm{fp} are independent random functions, with hjh_j uniform on block jj and fp\mathrm{fp} uniform on {0,…,2r−1}\{0, \ldots, 2^r - 1\}, and the tries use independent functions.

Theorem 1. For each key x∉Sx \notin S, Pr⁡[QUERY(x)=true]=2−r\Pr[\text{QUERY}(x) = \text{true}] = 2^{-r}.

Proof. Let TT be the try at which BUILD stops. Whether a try succeeds depends only on the cells of the keys of SS in that try, so TT is independent of the values at xx of the functions of all tries. The table BB is a function of the cells and fingerprints of the keys of SS in try TT. Given T=tT = t and all the values on SS, the values h0(x),h1(x),h2(x),fp(x)h_0(x), h_1(x), h_2(x), \mathrm{fp}(x) of try tt are still independent and uniform, since x∉Sx \notin S. So the xor w=B[h0(x)]⊕B[h1(x)]⊕B[h2(x)]w = B[h_0(x)] \oplus B[h_1(x)] \oplus B[h_2(x)] is independent of fp(x)\mathrm{fp}(x), and

Pr⁡[fp(x)=w]=∑vPr⁡[w=v]⋅2−r=2−r.□\Pr[\mathrm{fp}(x) = w] = \sum_v \Pr[w = v] \cdot 2^{-r} = 2^{-r}. \quad \square

Theorem 2 (time and space). QUERY takes Θ(1)\Theta(1) time: four hash values and three cell reads. Each try of BUILD takes Θ(n+ℓ)\Theta(n + \ell) time, which is Θ(n)\Theta(n). The table has 3ℓr3\ell r bits, with 3ℓ≤1.23n+353\ell \le 1.23 n + 35.

Proof. Lines L6 and L7 visit each key and each cell once. In lines L8 and L9 each cell enters the queue at most once at line L7 and once for each time its count falls to 1, which is at most once for each key that uses it, so the queue has at most 3ℓ+3n3\ell + 3n entries in total, and each is handled in constant time. The loop of lines L4 and L5 has nn steps of constant time. The bound on 3ℓ3\ell follows from ℓ≤(1.23n+32)/3+1\ell \le (1.23 n + 32)/3 + 1. With Theorem 2 of the correctness proof, the expected time of BUILD is Θ(n)/p\Theta(n)/p, which is Θ(n)\Theta(n) whenever the success probability pp of a try is bounded below by a constant, as it is at this table size for large nn by the threshold result of Molloy (2005). ■\blacksquare

An xor filter accepts every key of its set full

This proof shows: correctness, invariant, termination.

Let SS be a set of nn distinct keys. For fixed hash functions write E(x)={h0(x),h1(x),h2(x)}E(x) = \{h_0(x), h_1(x), h_2(x)\}, a set of three distinct cells, one in each block.

Lemma 1 (peeling order). If PEEL(S)(S) succeeds, its order (x1,c1),…,(xn,cn)(x_1, c_1), \ldots, (x_n, c_n) lists each key of SS once, and for each tt, ct∈E(xt)c_t \in E(x_t) and ct∉E(xt′)c_t \notin E(x_{t'}) for each t′>tt' > t.

Proof. Call a key remaining while it is not yet in the order. Lines L6 and L9 keep, for each cell cc, the count and the xor of the indices of the remaining keys yy with c∈E(y)c \in E(y): line L6 sets them for all keys, and line L9 removes a key from its three cells when it leaves. When line L8 takes a cell cc with count 1, exactly one remaining key uses cc, and the xor is its index. That key xtx_t is appended and stops being remaining. So ct∈E(xt)c_t \in E(x_t), each key is appended at most once, and the keys xt′x_{t'} with t′>tt' > t were remaining at that time and are not xtx_t, so they do not use ctc_t. Success at line L10 means all nn keys are listed. □\square

Lemma 2. In the loop of lines L4 and L5, the cell ctc_t is written only at step tt, and after step tt no cell of E(xt)E(x_t) is written.

Proof. Step t′t' writes only ct′c_{t'}. For t′>tt' > t, ct∉E(xt′)c_t \notin E(x_{t'}) by Lemma 1, while ct′∈E(xt′)c_{t'} \in E(x_{t'}), so ct′≠ctc_{t'} \ne c_t. The steps after step tt are the steps t′<tt' < t, and they write ct′c_{t'}, which is not in E(xt)E(x_t) by Lemma 1 applied to t′t' and the later index tt. □\square

Theorem 1 (invariant (X) and no false negatives). After BUILD, B[h0(x)]⊕B[h1(x)]⊕B[h2(x)]=fp(x)B[h_0(x)] \oplus B[h_1(x)] \oplus B[h_2(x)] = \mathrm{fp}(x) for each x∈Sx \in S, so QUERY(x)(x) returns true.

Proof. Take x=xtx = x_t, and let u,vu, v be the two cells of E(xt)E(x_t) other than ctc_t. Line L5 sets B[ct]=fp(xt)⊕B[u]⊕B[v]B[c_t] = \mathrm{fp}(x_t) \oplus B[u] \oplus B[v], so right after step tt, B[ct]⊕B[u]⊕B[v]=fp(xt)B[c_t] \oplus B[u] \oplus B[v] = \mathrm{fp}(x_t), since a⊕a=0a \oplus a = 0. By Lemma 2 none of the three cells changes after step tt. Line L11 then computes the same xor. □\square

Theorem 2 (termination). Under ideal hashing, with a fresh independent choice of hash functions in each try, BUILD ends with probability 1, and the number of tries has a geometric distribution.

Proof. Each try succeeds with the same probability pp, independently. It remains to show p>0p > 0, by one choice of the cells that peels. Since 3ℓ≥1.23n+323\ell \ge 1.23 n + 32, we have 3(ℓ−1)≥n3(\ell - 1) \ge n, so we can give each key xx a private cell π(x)\pi(x), all distinct, using at most ℓ−1\ell - 1 cells of each block, and keep one spare cell zjz_j in each block jj. Let the other two cells of xx be the spare cells of the two other blocks. Then each π(x)\pi(x) has count 1 and is in the first queue, and peeling removes the keys one after another, so it succeeds. This choice of cells has positive probability under ideal hashing. Each try is O(n)O(n) steps, since each key leaves the queue at most once. ■\blacksquare

Back to Xor filter