# Universal hashing

Using universal hashing (in a randomized algorithm or data structure) refers to selecting a hash function at random from a family of hash functions with a certain mathematical property (see definition below). This guarantees a low number of collisions in expectation, even if the data is chosen by an adversary. Many universal families are known (for hashing integers, vectors, strings), and their evaluation is often very efficient. Universal hashing has numerous uses in computer science, for example in implementations of hash tables, randomized algorithms, and cryptography.

## Introduction

Assume we want to map keys from some universe ${\displaystyle U}$ into ${\displaystyle m}$ bins (labelled ${\displaystyle [m]=\{0,\dots ,m-1\}}$). The algorithm will have to handle some data set ${\displaystyle S\subseteq U}$ of ${\displaystyle |S|=n}$ keys, which is not known in advance. Usually, the goal of hashing is to obtain a low number of collisions (keys from ${\displaystyle S}$ that land in the same bin). A deterministic hash function cannot offer any guarantee in an adversarial setting if the size of ${\displaystyle U}$ is greater than ${\displaystyle m^{2}}$, since the adversary may choose ${\displaystyle S}$ to be precisely the preimage of a bin. This means that all data keys land in the same bin, making hashing useless. Furthermore, a deterministic hash function does not allow for rehashing: sometimes the input data turns out to be bad for the hash function (e.g. there are too many collisions), so one would like to change the hash function.

The solution to these problems is to pick a function randomly from a family of hash functions. A family of functions ${\displaystyle H=\{h:U\to [m]\}}$ is called a universal family if, ${\displaystyle \forall x,y\in U,~x\neq y:~~\Pr _{h\in H}[h(x)=h(y)]\leq {\frac {1}{m}}}$.

In other words, any two keys of the universe collide with probability at most ${\displaystyle 1/m}$ when the hash function ${\displaystyle h}$ is drawn randomly from ${\displaystyle H}$. This is exactly the probability of collision we would expect if the hash function assigned truly random hash codes to every key. Sometimes, the definition is relaxed to allow collision probability ${\displaystyle O(1/m)}$. This concept was introduced by Carter and Wegman[1] in 1977, and has found numerous applications in computer science (see, for example [2]). If we have an upper bound of ${\displaystyle \epsilon <1}$ on the collision probability, we say that we have ${\displaystyle \epsilon }$-almost universality.

Many, but not all, universal families have the following stronger uniform difference property:

${\displaystyle \forall x,y\in U,~x\neq y}$, when ${\displaystyle h}$ is drawn randomly from the family ${\displaystyle H}$, the difference ${\displaystyle h(x)-h(y)~{\bmod {~}}m}$ is uniformly distributed in ${\displaystyle [m]}$.

Note that the definition of universality is only concerned with whether ${\displaystyle h(x)-h(y)=0}$, which counts collisions. The uniform difference property is stronger.

(Similarly, a universal family can be XOR universal if ${\displaystyle \forall x,y\in U,~x\neq y}$, the value ${\displaystyle h(x)\oplus h(y)~{\bmod {~}}m}$ is uniformly distributed in ${\displaystyle [m]}$ where ${\displaystyle \oplus }$ is the bitwise exclusive or operation. This is only possible if ${\displaystyle m}$ is a power of two.)

An even stronger condition is pairwise independence: we have this property when ${\displaystyle \forall x,y\in U,~x\neq y}$ we have the probability that ${\displaystyle x,y}$ will hash to any pair of hash values ${\displaystyle z_{1},z_{2}}$ is as if they were perfectly random: ${\displaystyle P(h(x)=z_{1}\land h(y)=z_{2})=1/m^{2}}$. Pairwise independence is sometimes called strong universality.

Another property is uniformity. We say that a family is uniform if all hash values are equally likely: ${\displaystyle P(h(x)=z)=1/m}$ for any hash value ${\displaystyle z}$. Universality does not imply uniformity. However, strong universality does imply uniformity.

Given a family with the uniform distance property, one can produce a pairwise independent or strongly universal hash family by adding a uniformly distributed random constant with values in ${\displaystyle [m]}$ to the hash functions. (Similarly, if ${\displaystyle m}$ is a power of two, we can achieve pairwise independence from an XOR universal hash family by doing an exclusive or with a uniformly distributed random constant.) Since a shift by a constant is sometimes irrelevant in applications (e.g. hash tables), a careful distinction between the uniform distance property and pairwise independent is sometimes not made.[3]

For some applications (such as hash tables), it is important for the least significant bits of the hash values to be also universal. When a family is strongly universal, this is guaranteed: if ${\displaystyle H}$ is a strongly universal family with ${\displaystyle m=2^{L}}$, then the family made of the functions ${\displaystyle h{\bmod {2^{L'}}}}$ for all ${\displaystyle h\in H}$ is also strongly universal for ${\displaystyle L'\leq L}$. Unfortunately, the same is not true of (merely) universal families. For example the family made of the identity function ${\displaystyle h(x)=x}$ is clearly universal, but the family made of the function ${\displaystyle h(x)=x{\bmod {2^{L'}}}}$ fails to be universal.

## Mathematical guarantees

For any fixed set ${\displaystyle S}$ of ${\displaystyle n}$ keys, using a universal family guarantees the following properties.

1. For any fixed ${\displaystyle x}$ in ${\displaystyle S}$, the expected number of keys in the bin ${\displaystyle h(x)}$ is ${\displaystyle n/m}$. When implementing hash tables by chaining, this number is proportional to the expected running time of an operation involving the key ${\displaystyle x}$ (for example a query, insertion or deletion).
2. The expected number of pairs of keys ${\displaystyle x,y}$ in ${\displaystyle S}$ with ${\displaystyle x\neq y}$ that collide (${\displaystyle h(x)=h(y)}$) is bounded above by ${\displaystyle n(n-1)/2m}$, which is of order ${\displaystyle O(n^{2}/m)}$. When the number of bins, ${\displaystyle m}$, is ${\displaystyle O(n)}$, the expected number of collisions is ${\displaystyle O(n)}$. When hashing into ${\displaystyle n^{2}}$ bins, there are no collisions at all with probability at least a half.
3. The expected number of keys in bins with at least ${\displaystyle t}$ keys in them is bounded above by ${\displaystyle 2n/(t-2(n/m)+1)}$.[4] Thus, if the capacity of each bin is capped to three times the average size (${\displaystyle t=3n/m}$), the total number of keys in overflowing bins is at most ${\displaystyle O(m)}$. This only holds with a hash family whose collision probability is bounded above by ${\displaystyle 1/m}$. If a weaker definition is used, bounding it by ${\displaystyle O(1/m)}$, this result is no longer true.[4]

As the above guarantees hold for any fixed set ${\displaystyle S}$, they hold if the data set is chosen by an adversary. However, the adversary has to make this choice before (or independent of) the algorithm's random choice of a hash function. If the adversary can observe the random choice of the algorithm, randomness serves no purpose, and the situation is the same as deterministic hashing.

The second and third guarantee are typically used in conjunction with rehashing. For instance, a randomized algorithm may be prepared to handle some ${\displaystyle O(n)}$ number of collisions. If it observes too many collisions, it chooses another random ${\displaystyle h}$ from the family and repeats. Universality guarantees that the number of repetitions is a geometric random variable.

## Constructions

Since any computer data can be represented as one or more machine words, one generally needs hash functions for three types of domains: machine words ("integers"); fixed-length vectors of machine words; and variable-length vectors ("strings").

### Hashing integers

This section refers to the case of hashing integers that fit in machines words; thus, operations like multiplication, addition, division, etc. are cheap machine-level instructions. Let the universe to be hashed be ${\displaystyle U=\{0,\dots ,u-1\}}$.

The original proposal of Carter and Wegman[1] was to pick a prime ${\displaystyle p\geq u}$ and define

${\displaystyle h_{a,b}(x)=((ax+b)~{\bmod {~}}p)~{\bmod {~}}m}$

where ${\displaystyle a,b}$ are randomly chosen integers modulo ${\displaystyle p}$ with ${\displaystyle a\neq 0}$. Technically, adding ${\displaystyle b}$ is not needed for universality (but it does make the hash function 2-independent). (This is a single iteration of a linear congruential generator).

To see that ${\displaystyle H=\{h_{a,b}\}}$ is a universal family, note that ${\displaystyle h(x)=h(y)}$ only holds when

${\displaystyle ax+b\equiv ay+b+i\cdot m{\pmod {p}}}$

for some integer ${\displaystyle i}$ between ${\displaystyle 0}$ and ${\displaystyle p/m}$. If ${\displaystyle x\neq y}$, their difference, ${\displaystyle x-y}$ is nonzero and has an inverse modulo ${\displaystyle p}$. Solving for ${\displaystyle a}$,

${\displaystyle a\equiv i\cdot m\cdot (x-y)^{-1}{\pmod {p}}}$.

There are ${\displaystyle p-1}$ possible choices for ${\displaystyle a}$ (since ${\displaystyle a=0}$ is excluded) and, varying ${\displaystyle i}$ in the allowed range, ${\displaystyle \lfloor p/m\rfloor }$ possible values for the right hand side. Thus the collision probability is

${\displaystyle \lfloor p/m\rfloor /(p-1)}$

which tends to ${\displaystyle 1/m}$ for large ${\displaystyle p}$ as required. This analysis also shows that ${\displaystyle b}$ does not have to be randomised in order to have universality.

Another way to see ${\displaystyle H}$ is a universal family is via the notion of statistical distance. Write the difference ${\displaystyle h(x)-h(y)}$ as

${\displaystyle h(x)-h(y)\equiv (a(x-y)~{\bmod {~}}p){\pmod {m}}}$.

Since ${\displaystyle x-y}$ is nonzero and ${\displaystyle a}$ is uniformly distributed in ${\displaystyle \{1,\dots ,p\}}$, it follows that ${\displaystyle a(x-y)}$ modulo ${\displaystyle p}$ is also uniformly distributed in ${\displaystyle \{1,\dots ,p\}}$. The distribution of ${\displaystyle (h(x)-h(y))~{\bmod {~}}m}$ is thus almost uniform, up to a difference in probability of ${\displaystyle \pm 1/p}$ between the samples. As a result, the statistical distance to a uniform family is ${\displaystyle O(m/p)}$, which becomes negligible when ${\displaystyle p\gg m}$.

#### Avoiding modular arithmetic

The state of the art for hashing integers is the multiply-shift scheme described by Dietzfelbinger et al. in 1997.[5] By avoiding modular arithmetic, this method is much easier to implement and also runs significantly faster in practice (usually by at least a factor of four[6]). The scheme assumes the number of bins is a power of two, ${\displaystyle m=2^{M}}$. Let ${\displaystyle w}$ be the number of bits in a machine word. Then the hash functions are parametrised over odd positive integers ${\displaystyle a<2^{w}}$ (that fit in a word of ${\displaystyle w}$ bits). To evaluate ${\displaystyle h_{a}(x)}$, multiply ${\displaystyle x}$ by ${\displaystyle a}$ modulo ${\displaystyle 2^{w}}$ and then keep the high order ${\displaystyle M}$ bits as the hash code. In mathematical notation, this is

${\displaystyle h_{a}(x)=(a\cdot x\,\,{\bmod {\,}}2^{w})\,\,{\mathrm {div} }\,\,2^{w-M}}$

and it can be implemented in C-like programming languages by

${\displaystyle h_{a}(x)=}$ (unsigned) (a*x) >> (w-M)

This scheme does not satisfy the uniform difference property and is only ${\displaystyle 2/m}$-almost-universal; for any ${\displaystyle x\neq y}$, ${\displaystyle \Pr\{h_{a}(x)=h_{a}(y)\}\leq 2/m}$.

To understand the behavior of the hash function, notice that, if ${\displaystyle ax{\bmod {2}}^{w}}$ and ${\displaystyle ay{\bmod {2}}^{w}}$ have the same highest-order 'M' bits, then ${\displaystyle a(x-y){\bmod {2}}^{w}}$ has either all 1's or all 0's as its highest order M bits (depending on whether ${\displaystyle ax{\bmod {2}}^{w}}$ or ${\displaystyle ay{\bmod {2}}^{w}}$ is larger. Assume that the least significant set bit of ${\displaystyle x-y}$ appears on position ${\displaystyle w-c}$. Since ${\displaystyle a}$ is a random odd integer and odd integers have inverses in the ring ${\displaystyle Z_{2^{w}}}$, it follows that ${\displaystyle a(x-y){\bmod {2}}^{w}}$ will be uniformly distributed among ${\displaystyle w}$-bit integers with the least significant set bit on position ${\displaystyle w-c}$. The probability that these bits are all 0's or all 1's is therefore at most ${\displaystyle 2/2^{M}=2/m}$. On the other hand, if ${\displaystyle c, then higher-order M bits of ${\displaystyle a(x-y){\bmod {2}}^{w}}$ contain both 0's and 1's, so it is certain that ${\displaystyle h(x)\neq h(y)}$. Finally, if ${\displaystyle c=M}$ then bit ${\displaystyle w-M}$ of ${\displaystyle a(x-y){\bmod {2}}^{w}}$ is 1 and ${\displaystyle h_{a}(x)=h_{a}(y)}$ if and only if bits ${\displaystyle w-1,\ldots ,w-M+1}$ are also 1, which happens with probability ${\displaystyle 1/2^{M-1}=2/m}$.

This analysis is tight, as can be shown with the example ${\displaystyle x=2^{w-M-2}}$ and ${\displaystyle y=3x}$. To obtain a truly 'universal' hash function, one can use the multiply-add-shift scheme

${\displaystyle h_{a,b}(x)=((ax+b){\bmod {2}}^{w})\,{\mathrm {div} }\,2^{w-M}}$

which can be implemented in C-like programming languages by

${\displaystyle h_{a,b}(x)=}$ (unsigned) (a*x+b) >> (w-M)

where ${\displaystyle a}$ is a random odd positive integer with ${\displaystyle a<2^{w}}$ and ${\displaystyle b}$ is a random non-negative integer with ${\displaystyle b<2^{w-M}}$. With these choices of ${\displaystyle a}$ and ${\displaystyle b}$, ${\displaystyle \Pr\{h_{a,b}(x)=h_{a,b}(y)\}\leq 1/m}$ for all ${\displaystyle x\not \equiv y{\pmod {2^{w}}}}$.[7] This differs slightly but importantly from the mistranslation in the English paper.[8]

### Hashing vectors

This section is concerned with hashing a fixed-length vector of machine words. Interpret the input as a vector ${\displaystyle {\bar {x}}=(x_{0},\dots ,x_{k-1})}$ of ${\displaystyle k}$ machine words (integers of ${\displaystyle w}$ bits each). If ${\displaystyle H}$ is a universal family with the uniform difference property, the following family (dating back to Carter and Wegman[1]) also has the uniform difference property (and hence is universal):

${\displaystyle h({\bar {x}})=\left(\sum _{i=0}^{k-1}h_{i}(x_{i})\right)\,{\bmod {~}}m}$, where each ${\displaystyle h_{i}\in H}$ is chosen independently at random.

If ${\displaystyle m}$ is a power of two, one may replace summation by exclusive or.[9]

In practice, if double-precision arithmetic is available, this is instantiated with the multiply-shift hash family of.[10] Initialize the hash function with a vector ${\displaystyle {\bar {a}}=(a_{0},\dots ,a_{k-1})}$ of random odd integers on ${\displaystyle 2w}$ bits each. Then if the number of bins is ${\displaystyle m=2^{M}}$ for ${\displaystyle M\leq w}$:

${\displaystyle h_{\bar {a}}({\bar {x}})=\left({\big (}\sum _{i=0}^{k-1}x_{i}\cdot a_{i}{\big )}~{\bmod {~}}2^{2w}\right)\,\,{\mathrm {div} }\,\,2^{2w-M}}$.

It is possible to halve the number of multiplications, which roughly translates to a two-fold speed-up in practice.[9] Initialize the hash function with a vector ${\displaystyle {\bar {a}}=(a_{0},\dots ,a_{k-1})}$ of random odd integers on ${\displaystyle 2w}$ bits each. The following hash family is universal:[11]

${\displaystyle h_{\bar {a}}({\bar {x}})=\left({\Big (}\sum _{i=0}^{\lceil k/2\rceil }(x_{2i}+a_{2i})\cdot (x_{2i+1}+a_{2i+1}){\Big )}{\bmod {~}}2^{2w}\right)\,\,{\mathrm {div} }\,\,2^{2w-M}}$.

If double-precision operations are not available, one can interpret the input as a vector of half-words (${\displaystyle w/2}$-bit integers). The algorithm will then use ${\displaystyle \lceil k/2\rceil }$ multiplications, where ${\displaystyle k}$ was the number of half-words in the vector. Thus, the algorithm runs at a "rate" of one multiplication per word of input.

The same scheme can also be used for hashing integers, by interpreting their bits as vectors of bytes. In this variant, the vector technique is known as tabulation hashing and it provides a practical alternative to multiplication-based universal hashing schemes.[12]

Strong universality at high speed is also possible.[13] Initialize the hash function with a vector ${\displaystyle {\bar {a}}=(a_{0},\dots ,a_{k})}$ of random integers on ${\displaystyle 2w}$ bits. Compute

${\displaystyle h_{\bar {a}}({\bar {x}})^{\mathrm {strong} }=(a_{0}+\sum _{i=0}^{k}a_{i+1}x_{i}{\bmod {~}}2^{2w})\div 2^{w}}$.

The result is strongly universal on ${\displaystyle w}$ bits. Experimentally, it was found to run at 0.2 CPU cycle per byte on recent Intel processors for ${\displaystyle w=32}$.

### Hashing strings

This refers to hashing a variable-sized vector of machine words. If the length of the string can be bounded by a small number, it is best to use the vector solution from above (conceptually padding the vector with zeros up to the upper bound). The space required is the maximal length of the string, but the time to evaluate ${\displaystyle h(s)}$ is just the length of ${\displaystyle s}$. As long as zeroes are forbidden in the string, the zero-padding can be ignored when evaluating the hash function without affecting universality[9]). Note that if zeroes are allowed in the string, then it might be best to append a fictitious non-zero (e.g., 1) character to all strings prior to padding: this will ensure that universality is not affected.[13]

Now assume we want to hash ${\displaystyle {\bar {x}}=(x_{0},\dots ,x_{\ell })}$, where a good bound on ${\displaystyle \ell }$ is not known a priori. A universal family proposed by [10] treats the string ${\displaystyle x}$ as the coefficients of a polynomial modulo a large prime. If ${\displaystyle x_{i}\in [u]}$, let ${\displaystyle p\geq \max\{u,m\}}$ be a prime and define:

${\displaystyle h_{a}({\bar {x}})=h_{\mathrm {int} }\left({\big (}\sum _{i=0}^{\ell }x_{i}\cdot a^{i}{\big )}{\bmod {~}}p\right)}$, where ${\displaystyle a\in [p]}$ is uniformly random and ${\displaystyle h_{\mathrm {int} }}$ is chosen randomly from a universal family mapping integer domain ${\displaystyle [p]\mapsto [m]}$.

Using properties of modular arithmetic, above can be computed without producing large numbers for large strings as follows:[14]

int hash(String x, int a, int p)
int h=x[0]
for ( i=1 ; i < x.length ; i++)
h = ((h*a) + x[i])) mod p
return h


Consider two strings ${\displaystyle {\bar {x}},{\bar {y}}}$ and let ${\displaystyle \ell }$ be length of the longer one; for the analysis, the shorter string is conceptually padded with zeros up to length ${\displaystyle \ell }$. A collision before applying ${\displaystyle h_{\mathrm {int} }}$ implies that ${\displaystyle a}$ is a root of the polynomial with coefficients ${\displaystyle {\bar {x}}-{\bar {y}}}$. This polynomial has at most ${\displaystyle \ell }$ roots modulo ${\displaystyle p}$, so the collision probability is at most ${\displaystyle \ell /p}$. The probability of collision through the random ${\displaystyle h_{\mathrm {int} }}$ brings the total collision probability to ${\displaystyle {\frac {1}{m}}+{\frac {\ell }{p}}}$. Thus, if the prime ${\displaystyle p}$ is sufficiently large compared to the length of strings hashed, the family is very close to universal (in statistical distance).

To mitigate the computational penalty of modular arithmetic, two tricks are used in practice:[9]

1. One chooses the prime ${\displaystyle p}$ to be close to a power of two, such as a Mersenne prime. This allows arithmetic modulo ${\displaystyle p}$ to be implemented without division (using faster operations like addition and shifts). For instance, on modern architectures one can work with ${\displaystyle p=2^{61}-1}$, while ${\displaystyle x_{i}}$'s are 32-bit values.
2. One can apply vector hashing to blocks. For instance, one applies vector hashing to each 16-word block of the string, and applies string hashing to the ${\displaystyle \lceil k/16\rceil }$ results. Since the slower string hashing is applied on a substantially smaller vector, this will essentially be as fast as vector hashing.

## References

1. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
2. {{#invoke:citation/CS1|citation |CitationClass=book }}
3. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
4. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
5. Template:Cite thesis
6. {{#invoke:citation/CS1|citation |CitationClass=conference }}
7. {{#invoke:citation/CS1|citation |CitationClass=conference }}, section 5.3
8. {{#invoke:citation/CS1|citation |CitationClass=conference }}
9. {{#invoke:citation/CS1|citation |CitationClass=conference }}, Equation 1
10. {{#invoke:citation/CS1|citation |CitationClass=conference }}
11. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
12. Template:Cite web