# $k$-wise independence

Recall the definition of independence between events:

 Definition (Independent events) Events $\mathcal{E}_1, \mathcal{E}_2, \ldots, \mathcal{E}_n$ are mutually independent if, for any subset $I\subseteq\{1,2,\ldots,n\}$, \begin{align} \Pr\left[\bigwedge_{i\in I}\mathcal{E}_i\right] &= \prod_{i\in I}\Pr[\mathcal{E}_i]. \end{align}

Similarly, we can define independence between random variables:

 Definition (Independent variables) Random variables $X_1, X_2, \ldots, X_n$ are mutually independent if, for any subset $I\subseteq\{1,2,\ldots,n\}$ and any values $x_i$, where $i\in I$, \begin{align} \Pr\left[\bigwedge_{i\in I}(X_i=x_i)\right] &= \prod_{i\in I}\Pr[X_i=x_i]. \end{align}

Mutual independence is an ideal condition of independence. The limited notion of independence is usually defined by the k-wise independence.

 Definition (k-wise Independenc) 1. Events $\mathcal{E}_1, \mathcal{E}_2, \ldots, \mathcal{E}_n$ are k-wise independent if, for any subset $I\subseteq\{1,2,\ldots,n\}$ with $|I|\le k$ \begin{align} \Pr\left[\bigwedge_{i\in I}\mathcal{E}_i\right] &= \prod_{i\in I}\Pr[\mathcal{E}_i]. \end{align} 2. Random variables $X_1, X_2, \ldots, X_n$ are k-wise independent if, for any subset $I\subseteq\{1,2,\ldots,n\}$ with $|I|\le k$ and any values $x_i$, where $i\in I$, \begin{align} \Pr\left[\bigwedge_{i\in I}(X_i=x_i)\right] &= \prod_{i\in I}\Pr[X_i=x_i]. \end{align}

A very common case is pairwise independence, i.e. the 2-wise independence.

 Definition (pairwise Independent random variables) Random variables $X_1, X_2, \ldots, X_n$ are pairwise independent if, for any $X_i,X_j$ where $i\neq j$ and any values $a,b$ \begin{align} \Pr\left[X_i=a\wedge X_j=b\right] &= \Pr[X_i=a]\cdot\Pr[X_j=b]. \end{align}

Note that the definition of k-wise independence is hereditary:

• If $X_1, X_2, \ldots, X_n$ are k-wise independent, then they are also $\ell$-wise independent for any $\ell\lt k$.
• If $X_1, X_2, \ldots, X_n$ are NOT k-wise independent, then they cannot be $\ell$-wise independent for any $\ell\gt k$.

## Pairwise Independent Bits

Suppose we have $m$ mutually independent and uniform random bits $X_1,\ldots, X_m$. We are going to extract $n=2^m-1$ pairwise independent bits from these $m$ mutually independent bits.

Enumerate all the nonempty subsets of $\{1,2,\ldots,m\}$ in some order. Let $S_j$ be the $j$th subset. Let

$Y_j=\bigoplus_{i\in S_j} X_i,$

where $\oplus$ is the exclusive-or, whose truth table is as follows.

 $a$ $b$ $a$$\oplus$$b$ 0 0 0 0 1 1 1 0 1 1 1 0

There are $n=2^m-1$ such $Y_j$, because there are $2^m-1$ nonempty subsets of $\{1,2,\ldots,m\}$. An equivalent definition of $Y_j$ is

$Y_j=\left(\sum_{i\in S_j}X_i\right)\bmod 2$.

Sometimes, $Y_j$ is called the parity of the bits in $S_j$.

We claim that $Y_j$ are pairwise independent and uniform.

 Theorem For any $Y_j$ and any $b\in\{0,1\}$, \begin{align} \Pr\left[Y_j=b\right] &= \frac{1}{2}. \end{align} For any $Y_j,Y_\ell$ that $j\neq\ell$ and any $a,b\in\{0,1\}$, \begin{align} \Pr\left[Y_j=a\wedge Y_\ell=b\right] &= \frac{1}{4}. \end{align}

The proof is left for your exercise.

Therefore, we extract exponentially many pairwise independent uniform random bits from a sequence of mutually independent uniform random bits.

Note that $Y_j$ are not 3-wise independent. For example, consider the subsets $S_1=\{1\},S_2=\{2\},S_3=\{1,2\}$ and the corresponding random bits $Y_1,Y_2,Y_3$. Any two of $Y_1,Y_2,Y_3$ would decide the value of the third one.

## Pairwise Independent Variables

We now consider constructing pairwise independent random variables ranging over $[p]=\{0,1,2,\ldots,p-1\}$ for some prime $p$. Unlike the above construction, now we only need two independent random sources $X_0,X_1$, which are uniformly and independently distributed over $[p]$.

Let $Y_0,Y_1,\ldots, Y_{p-1}$ be defined as:

\begin{align} Y_i=(X_0+i\cdot X_1)\bmod p &\quad \mbox{for }i\in[p]. \end{align}
 Theorem The random variables $Y_0,Y_1,\ldots, Y_{p-1}$ are pairwise independent uniform random variables over $[p]$.
Proof.
 We first show that $Y_i$ are uniform. That is, we will show that for any $i,a\in[p]$, \begin{align} \Pr\left[(X_0+i\cdot X_1)\bmod p=a\right] &= \frac{1}{p}. \end{align} Due to the law of total probability, \begin{align} \Pr\left[(X_0+i\cdot X_1)\bmod p=a\right] &= \sum_{j\in[p]}\Pr[X_1=j]\cdot\Pr\left[(X_0+ij)\bmod p=a\right]\\ &=\frac{1}{p}\sum_{j\in[p]}\Pr\left[X_0\equiv(a-ij)\pmod{p}\right]. \end{align} For prime $p$, for any $i,j,a\in[p]$, there is exact one value in $[p]$ of $X_0$ satisfying $X_0\equiv(a-ij)\pmod{p}$. Thus, $\Pr\left[X_0\equiv(a-ij)\pmod{p}\right]=1/p$ and the above probability is $\frac{1}{p}$. We then show that $Y_i$ are pairwise independent, i.e. we will show that for any $Y_i,Y_j$ that $i\neq j$ and any $a,b\in[p]$, \begin{align} \Pr\left[Y_i=a\wedge Y_j=b\right] &= \frac{1}{p^2}. \end{align} The event $Y_i=a\wedge Y_j=b$ is equivalent to that $\begin{cases} (X_0+iX_1)\equiv a\pmod{p}\\ (X_0+jX_1)\equiv b\pmod{p} \end{cases}$ Due to the Chinese remainder theorem, there exists a unique solution of $X_0$ and $X_1$ in $[p]$ to the above linear congruential system. Thus the probability of the event is $\frac{1}{p^2}$.
$\square$

# Universal Hashing

Hashing is one of the oldest tools in Computer Science. Knuth's memorandum in 1963 on analysis of hash tables is now considered to be the birth of the area of analysis of algorithms.

• Knuth. Notes on "open" addressing, July 22 1963. Unpublished memorandum.

The idea of hashing is simple: an unknown set $S$ of $n$ data items (or keys) are drawn from a large universe $U=[N]$ where $N\gg n$; in order to store $S$ in a table of $M$ entries (slots), we assume a consistent mapping (called a hash function) from the universe $U$ to a small range $[M]$.

This idea seems clever: we use a consistent mapping to deal with an arbitrary unknown data set. However, there is a fundamental flaw for hashing.

• For sufficiently large universe ($N\gt M(n-1)$), for any function, there exists a bad data set $S$, such that all items in $S$ are mapped to the same entry in the table.

A simple use of pigeonhole principle can prove the above statement.

To overcome this situation, randomization is introduced into hashing. We assume that the hash function is a random mapping from $[N]$ to $[M]$. In order to ease the analysis, the following ideal assumption is used:

Simple Uniform Hash Assumption (SUHA or UHA, a.k.a. the random oracle model):

A uniform random function $h:[N]\rightarrow[M]$ is available and the computation of $h$ is efficient.

## Families of universal hash functions

The assumption of completely random function simplifies the analysis. However, in practice, truly uniform random hash function is extremely expensive to compute and store. Thus, this simple assumption can hardly represent the reality.

There are two approaches for implementing practical hash functions. One is to use ad hoc implementations and wish they may work. The other approach is to construct class of hash functions which are efficient to compute and store but with weaker randomness guarantees, and then analyze the applications of hash functions based on this weaker assumption of randomness.

This route was took by Carter and Wegman in 1977 while they introduced universal families of hash functions.

 Definition (universal hash families) Let $[N]$ be a universe with $N\ge M$. A family of hash functions $\mathcal{H}$ from $[N]$ to $[M]$ is said to be $k$-universal if, for any items $x_1,x_2,\ldots,x_k\in [N]$ and for a hash function $h$ chosen uniformly at random from $\mathcal{H}$, we have $\Pr[h(x_1)=h(x_2)=\cdots=h(x_k)]\le\frac{1}{M^{k-1}}.$ A family of hash functions $\mathcal{H}$ from $[N]$ to $[M]$ is said to be strongly $k$-universal if, for any items $x_1,x_2,\ldots,x_k\in [N]$, any values $y_1,y_2,\ldots,y_k\in[M]$, and for a hash function $h$ chosen uniformly at random from $\mathcal{H}$, we have $\Pr[h(x_1)=y_1\wedge h(x_2)=y_2 \wedge \cdots \wedge h(x_k)=y_k]=\frac{1}{M^{k}}.$

In particular, for a 2-universal family $\mathcal{H}$, for any elements $x_1,x_2\in[N]$, a uniform random $h\in\mathcal{H}$ has

$\Pr[h(x_1)=h(x_2)]\le\frac{1}{M}.$

For a strongly 2-universal family $\mathcal{H}$, for any elements $x_1,x_2\in[N]$ and any values $y_1,y_2\in[M]$, a uniform random $h\in\mathcal{H}$ has

$\Pr[h(x_1)=y_1\wedge h(x_2)=y_2]=\frac{1}{M^2}.$

This behavior is exactly the same as uniform random hash functions on any pair of inputs. For this reason, a strongly 2-universal hash family are also called pairwise independent hash functions.

## 2-universal hash families

The construction of pairwise independent random variables via modulo a prime introduced in Section 1 already provides a way of constructing a strongly 2-universal hash family.

Let $p$ be a prime. The function $h_{a,b}:[p]\rightarrow [p]$ is defined by

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

and the family is

$\mathcal{H}=\{h_{a,b}\mid a,b\in[p]\}.$
 Lemma $\mathcal{H}$ is strongly 2-universal.
Proof.
 In Section 1, we have proved the pairwise independence of the sequence of $(a i+b)\bmod p$, for $i=0,1,\ldots, p-1$, which directly implies that $\mathcal{H}$ is strongly 2-universal.
$\square$
The original construction of Carter-Wegman

What if we want to have hash functions from $[N]$ to $[M]$ for non-prime $N$ and $M$? Carter and Wegman developed the following method.

Suppose that the universe is $[N]$, and the functions map $[N]$ to $[M]$, where $N\ge M$. For some prime $p\ge N$, let

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

and the family

$\mathcal{H}=\{h_{a,b}\mid 1\le a\le p-1, b\in[p]\}.$

Note that unlike the first construction, now $a\neq 0$.

 Lemma (Carter-Wegman) $\mathcal{H}$ is 2-universal.
Proof.
 Due to the definition of $\mathcal{H}$, there are $p(p-1)$ many different hash functions in $\mathcal{H}$, because each hash function in $\mathcal{H}$ corresponds to a pair of $1\le a\le p-1$ and $b\in[p]$. We only need to count for any particular pair of $x_1,x_2\in[N]$ that $x_1\neq x_2$, the number of hash functions that $h(x_1)=h(x_2)$. We first note that for any $x_1\neq x_2$, $a x_1+b\not\equiv a x_2+b \pmod p$. This is because $a x_1+b\equiv a x_2+b \pmod p$ would imply that $a(x_1-x_2)\equiv 0\pmod p$, which can never happen since $1\le a\le p-1$ and $x_1\neq x_2$ (note that $x_1,x_2\in[N]$ for an $N\le p$). Therefore, we can assume that $(a x_1+b)\bmod p=u$ and $(a x_2+b)\bmod p=v$ for $u\neq v$. Due to the Chinese remainder theorem, for any $x_1,x_2\in[N]$ that $x_1\neq x_2$, for any $u,v\in[p]$ that $u\neq v$, there is exact one solution to $(a,b)$ satisfying: $\begin{cases} a x_1+b \equiv u \pmod p\\ a x_2+b \equiv v \pmod p. \end{cases}$ After modulo $M$, every $u\in[p]$ has at most $\lceil p/M\rceil -1$ many $v\in[p]$ that $v\neq u$ but $v\equiv u\pmod M$. Therefore, for every pair of $x_1,x_2\in[N]$ that $x_1\neq x_2$, there exist at most $p(\lceil p/M\rceil -1)\le p(p-1)/M$ pairs of $1\le a\le p-1$ and $b\in[p]$ such that $((ax_1+b)\bmod p)\bmod M=((ax_2+b)\bmod p)\bmod M$, which means there are at most $p(p-1)/M$ many hash functions $h\in\mathcal{H}$ having $h(x_1)=h(x_2)$ for $x_1\neq x_2$. For $h$ uniformly chosen from $\mathcal{H}$, for any $x_1\neq x_2$, $\Pr[h(x_1)=h(x_2)]\le \frac{p(p-1)/M}{p(p-1)}=\frac{1}{M}.$ We prove that $\mathcal{H}$ is 2-universal.
$\square$
A construction used in practice

The main issue of Carter-Wegman construction is the efficiency. The mod operation is very slow, and has been so for more than 30 years.

The following construction is due to Dietzfelbinger et al. It was published in 1997 and has been practically used in various applications of universal hashing.

The family of hash functions is from $[2^u]$ to $[2^v]$. With a binary representation, the functions map binary strings of length $u$ to binary strings of length $v$. Let

$h_{a}(x)=\left\lfloor\frac{a\cdot x\bmod 2^u}{2^{u-v}}\right\rfloor,$

and the family

$\mathcal{H}=\{h_{a}\mid a\in[2^v]\mbox{ and }a\mbox{ is odd}\}.$

This family of hash functions does not exactly meet the requirement of 2-universal family. However, Dietzfelbinger et al proved that $\mathcal{H}$ is close to a 2-universal family. Specifically, for any input values $x_1,x_2\in[2^u]$, for a uniformly random $h\in\mathcal{H}$,

$\Pr[h(x_1)=h(x_2)]\le\frac{1}{2^{v-1}}.$

So $\mathcal{H}$ is within an approximation ratio of 2 to being 2-universal. The proof uses the fact that odd numbers are relative prime to a power of 2.

The function is extremely simple to compute in c language. We exploit that C-multiplication (*) of unsigned u-bit numbers is done $\bmod 2^u$, and have a one-line C-code for computing the hash function:

h_a(x) = (a*x)>>(u-v)


The bit-wise shifting is a lot faster than modular. It explains the popularity of this scheme in practice than the original Carter-Wegman construction.