Sub topic 9
Getting k hash values from two base hashes
This chapter assumes you know how a Bloom filter inserts and queries items with k hash positions in an m-bit array. It also assumes you are comfortable with modular arithmetic and Python's hashlib.
The cost of k independent hashes
A Bloom filter with k = 7 needs seven hash values for every insert and every lookup. You could run seven different hash functions, or seed one function seven ways. Both approaches multiply the hashing work by k, and hashing is often the slowest part of the operation.
Double hashing avoids this. You compute two base hashes once and combine them to get as many positions as you need.
The formula
Given base hashes h1 and h2 of an item, the i-th position is:
g_i(x) = (h1(x) + i · h2(x)) mod m, for i = 0, 1, ..., k − 1
Position 0 is h1 mod m. Each later position steps forward by h2, wrapping around the array. Kirsch and Mitzenmacher showed that this scheme gives the same false positive rate as k independent hash functions in the asymptotic sense. You pay for two hashes and gain k positions.
Implementation
This function matches the _positions method of the BloomFilter class from earlier in the tutorial. It is written here as a standalone function so you can test it on its own.
import hashlib
def positions(item, k, m):
digest = hashlib.sha256(item.encode("utf-8")).digest()
h1 = int.from_bytes(digest[:8], "big")
h2 = int.from_bytes(digest[8:16], "big") | 1
return [(h1 + i * h2) % m for i in range(k)]
pos = positions("apple", 7, 1_000_003)
print(pos)
print(len(set(pos)), "distinct positions out of", len(pos))A single 32-byte SHA-256 digest supplies both base hashes. The first 8 bytes become h1. The next 8 bytes become h2.
Why h2 is forced to be odd
The expression | 1 sets the lowest bit of h2, so h2 is never even. If h2 were 0, every position would equal h1 mod m, and the item would set only one bit. That would weaken the filter.
The step h2 also interacts with m. Positions cycle after m / gcd(h2, m) steps. When h2 and m share a large factor, the cycle is short and some of the k positions repeat. Repeats mean fewer distinct bits per item, which raises the false positive rate.
- If m is a power of two, an odd h2 always has gcd 1 with m, so
| 1fully fixes the problem. - If m is not a power of two, as with the sizing formula's output,
| 1helps but does not guarantee gcd 1. Repeats are rare when m is large and k is small.
Trade-offs
| Approach | Hash calls per operation | Independence | Notes |
|---|---|---|---|
| k separate hash functions | k | Highest | Slowest, and needs k good functions |
| Seeded single function | k | High | Simple, still k calls |
| Double hashing | 1 digest | Slightly lower | Same error rate in practice |
| Slicing one digest into k parts | 1 digest | High | Limited by digest size |
Double hashing is the usual default. Its weak point is small m or large k, where the cycle and repeat effects above become visible.
Exercise
Let m = 10, h1 = 3, h2 = 5, and k = 4. What are the four positions g0 to g3, and what is wrong with them?
Show answer
g0 = 3, g1 = (3 + 5) mod 10 = 8, g2 = (3 + 10) mod 10 = 3, g3 = (3 + 15) mod 10 = 8. The positions are 3, 8, 3, 8, so only two distinct bits are set. Here gcd(h2, m) = gcd(5, 10) = 5, so the cycle length is 10 / 5 = 2. The item behaves as if k were 2, which raises the false positive rate.