# The idea of fingerprinting

Suppose we want to compare two items ${\displaystyle Z_{1}}$ and ${\displaystyle Z_{2}}$. Instead of comparing them directly, we compute random fingerprints ${\displaystyle \mathrm {FING} (Z_{1})}$ and ${\displaystyle \mathrm {FING} (Z_{2})}$ and compare these. The fingerprints has the following properties:

• ${\displaystyle \mathrm {FING} (\cdot )}$ is a function, which means that if ${\displaystyle Z_{1}=Z_{2}}$ then ${\displaystyle \mathrm {FING} (Z_{1})=\mathrm {FING} (Z_{2})}$.
• If ${\displaystyle Z_{1}\neq Z_{2}}$ then ${\displaystyle \Pr[\mathrm {FING} (Z_{1})=\mathrm {FING} (Z_{2})]}$ is small.
• It is much more to compute and compare the fingerprints than to compare ${\displaystyle Z_{1}}$ and ${\displaystyle Z_{2}}$ directly.

For Freivald's algorithm, the items to compare are two ${\displaystyle n\times n}$ matrices ${\displaystyle AB}$ and ${\displaystyle C}$, and given an ${\displaystyle n\times n}$ matrix ${\displaystyle M}$, its random fingerprint is computed as ${\displaystyle \mathrm {FING} (M)=Mr}$ for a uniformly random ${\displaystyle r\in \{0,1\}^{n}}$.

For the Schwartz-Zippel algorithm, the items to compare are two polynomials ${\displaystyle P_{1}(x_{1},\ldots ,x_{n})}$ and ${\displaystyle P_{2}(x_{1},\ldots ,x_{n})}$, and given a polynomial ${\displaystyle Q(x_{1},\ldots ,x_{n})}$, its random fingerprint is computed as ${\displaystyle \mathrm {FING} (Q)=Q(r_{1},\ldots ,r_{n})}$ for ${\displaystyle r_{i}}$ chosen independently and uniformly at random from some fixed set ${\displaystyle S}$.

For different problems, we may have different definitions of ${\displaystyle \mathrm {FING} (\cdot )}$.

# Communication complexity

Alice and Bob are two entities. Alice has a private input ${\displaystyle x}$ and Bob has a private input ${\displaystyle y}$. Together they want to compute a function ${\displaystyle f(x,y)}$ by communicating with each other. This is the model of communication complexity introduced by Yao in 1979.

In the communication complexity model, the local computational costs are ignored. The complexity of algorithms (also called communication protocols here) are measured by the number of bits communicated between Alice and Bob.

A basic function is EQ, defined as

${\displaystyle \mathrm {EQ} (x,y)={\begin{cases}1&{\mbox{if }}x=y,\\0&{\mbox{otherwise.}}\end{cases}}}$

This function corresponds to the problem that two far apart entities Alice and Bob, each has a copy of a database (Alice's copy is ${\displaystyle x}$, and Bob's copy is ${\displaystyle y}$), and they want to compare whether their copies of the database are identical.

A trivial way to solve EQ is to let Bob send ${\displaystyle y}$ to Alice. Supposed that ${\displaystyle x,y\in \{0,1\}^{n}}$, this costs ${\displaystyle n}$ bits of communications.

It is known that for deterministic communication protocols, this is the best we can get for computing EQ.

 Theorem (Yao 1979) Any deterministic communication protocol computing EQ on two ${\displaystyle n}$-bit strings costs ${\displaystyle n}$ bits of communication in the worst-case.

This theorem is much more nontrivial to prove than it looks, because Alice and Bob are allowed to interact with each other in arbitrary ways. How to prove such lower bounds is not today's topic.

If the randomness is allowed, we can use the idea of fingerprinting to solve this problem with significantly less communications. The general framework for the algorithm is as follows:

• Alice choose a random fingerprint function ${\displaystyle \mathrm {FING} (\cdot )}$ and compute the fingerprint of her input ${\displaystyle \mathrm {FING} (x)}$;
• Alice sends both the description of ${\displaystyle \mathrm {FING} (\cdot )}$ and the value of ${\displaystyle \mathrm {FING} (x)}$ to Bob;
• Bob computes ${\displaystyle \mathrm {FING} (y)}$ and check whether ${\displaystyle \mathrm {FING} (x)=\mathrm {FING} (y)}$.

So the question is, how to design this random fingerprint function ${\displaystyle \mathrm {FING} (\cdot )}$ to guarantee:

1. A random ${\displaystyle \mathrm {FING} (\cdot )}$ can be described succinctly.
2. The range of ${\displaystyle \mathrm {FING} (\cdot )}$ is small, so the fingerprints are succinct.
3. If ${\displaystyle x\neq y}$, the probability ${\displaystyle \Pr[\mathrm {FING} (x)=\mathrm {FING} (y)]}$ is small.

The fingerprint function we choose is as follows: by treating the input string ${\displaystyle x\in \{0,1\}^{n}}$ as the binary representation of a number, let ${\displaystyle \mathrm {FING} (x)=x{\bmod {p}}}$ for some random prime ${\displaystyle p}$. The prime ${\displaystyle p}$ can uniquely specify a random fingerprint function ${\displaystyle \mathrm {FING} (\cdot )}$, thus can be used as a description of the function, and alos the range of the fingerprints is ${\displaystyle [p]}$, thus we want the prime ${\displaystyle p}$ to be reasonably small, but still has a good chance to distinguish different ${\displaystyle x}$ and ${\displaystyle y}$ after modulo ${\displaystyle p}$.

 A randomized protocol for EQ Alice does: for some parameter ${\displaystyle k}$ (to be specified), choose uniformly at random a prime ${\displaystyle p\in [k]}$; send ${\displaystyle p}$ and ${\displaystyle x{\bmod {p}}}$ to Bob; Upon receiving ${\displaystyle p}$ and ${\displaystyle x{\bmod {p}}}$, Bob does: check whether ${\displaystyle x{\bmod {p}}=y{\bmod {p}}}$.

The number of bits to be communicated is ${\displaystyle O(\log k)}$. We then bound the probability of error ${\displaystyle \Pr[x{\bmod {p}}=y{\bmod {p}}]}$ for ${\displaystyle x\neq y}$, in terms of ${\displaystyle k}$.

Suppose without loss of generality ${\displaystyle x>y}$. Let ${\displaystyle z=x-y}$. Then ${\displaystyle z<2^{n}}$ since ${\displaystyle x,y\in [2^{n}]}$, and ${\displaystyle z\neq 0}$ for ${\displaystyle x\neq y}$. It holds that ${\displaystyle x{\bmod {p}}=y{\bmod {p}}}$ if and only if ${\displaystyle z}$ is dividable by ${\displaystyle p}$. Note that ${\displaystyle z<2^{n}}$ since ${\displaystyle x,y\in [2^{n}]}$. We only need to bound the probability

${\displaystyle \Pr[z{\bmod {p}}=0]}$ for ${\displaystyle 0, where ${\displaystyle p}$ is a random prime chosen from ${\displaystyle [k]}$.

The probability ${\displaystyle \Pr[z{\bmod {p}}=0]}$ is computed directly as

${\displaystyle \Pr[z{\bmod {p}}=0]\leq {\frac {{\mbox{the number of prime divisors of }}z}{{\mbox{the number of primes in }}[k]}}}$.

For the numerator, we have the following lemma.

 Lemma The number of distinct prime divisors of any natural number less than ${\displaystyle 2^{n}}$ is at most ${\displaystyle n}$.
Proof.
 Each prime number is ${\displaystyle \geq 2}$. If an ${\displaystyle N>0}$ has more than ${\displaystyle n}$ distinct prime divisors, then ${\displaystyle N\geq 2^{n}}$.
${\displaystyle \square }$

Due to this lemma, ${\displaystyle z}$ has at most ${\displaystyle n}$ prime divisors.

We then lower bound the number of primes in ${\displaystyle [k]}$. This is given by the celebrated Prime Number Theorem (PNT).

 Prime Number Theorem Let ${\displaystyle \pi (k)}$ denote the number of primes less than ${\displaystyle k}$. Then ${\displaystyle \pi (k)\sim {\frac {k}{\ln k}}}$ as ${\displaystyle k\rightarrow \infty }$.

Therefore, by choosing ${\displaystyle k=tn\ln tn}$ for some ${\displaystyle t}$, we have that for a ${\displaystyle 0, and a random prime ${\displaystyle p\in [k]}$,

${\displaystyle \Pr[z{\bmod {p}}=0]\leq {\frac {n}{\pi (k)}}\sim {\frac {1}{t}}}$.

We can make this error probability polynomially small and the number of bits to be communicated is still ${\displaystyle O(\log k)=O(\log n)}$.

## Application: Randomized pattern matching

Consider the following problem of pattern matching, which has nothing to do with communication complexity.

• Input: a string ${\displaystyle x\in \{0,1\}^{n}}$ and a "pattern" ${\displaystyle y\in \{0,1\}^{m}}$.
• Determine whether the pattern ${\displaystyle y}$ is a contiguous substring of ${\displaystyle x}$. Usually, we are also asked to find the location of the substring.

A naive algorithm trying every possible match runs in ${\displaystyle O(nm)}$ time. The more sophisticated KMP algorithm inspired by automaton theory runs in ${\displaystyle O(n+m)}$ time.

A simple randomized algorithm, due to Karp and Rabin, uses the idea of fingerprinting and also runs in ${\displaystyle O(n+m)}$ time.

Let ${\displaystyle X(j)=x_{j}x_{j+1}\cdots x_{j+m-1}}$ denote the substring of ${\displaystyle x}$ of length ${\displaystyle m}$ starting at position ${\displaystyle j}$.

 Algorithm (Karp-Rabin) pick a random prime ${\displaystyle p\in [k]}$; for ${\displaystyle j=1}$ to ${\displaystyle n-m+1}$ do if ${\displaystyle X(j){\bmod {p}}=y{\bmod {p}}}$ then report a match; return "no match";

So the algorithm just compares the ${\displaystyle \mathrm {FING} (X(j))}$ and ${\displaystyle \mathrm {FING} (y)}$ for every ${\displaystyle j}$, with the same definition of fingerprint function ${\displaystyle \mathrm {FING} (\cdot )}$ as in the communication protocol for EQ.

By the same analysis, by choosing ${\displaystyle k=n^{2}m\ln(n^{2}m)}$, the probability of a single false match is

${\displaystyle \Pr[X(j){\bmod {p}}=y{\bmod {p}}\mid X(j)\neq y]=O\left({\frac {1}{n^{2}}}\right)}$.

By the union bound, the probability that a false match occurs is ${\displaystyle O\left({\frac {1}{n}}\right)}$.

The algorithm runs in linear time if we assume that we can compute ${\displaystyle X(j){\bmod {p}}}$ for each ${\displaystyle j}$ in constant time. This outrageous assumption can be made realistic by the following observation.

 Lemma Let ${\displaystyle \mathrm {FING} (a)=a{\bmod {p}}}$. ${\displaystyle \mathrm {FING} (X(j+1))\equiv 2(\mathrm {FING} (X(j))-2^{m-1}x_{j})+x_{j+m}{\pmod {p}}\,}$.
Proof.
 It holds that ${\displaystyle X(j+1)=2(X(j)-2^{m-1}x_{j})+x_{j+m}\,}$. So the equation holds on the finite field modulo ${\displaystyle p}$.
${\displaystyle \square }$

Due to this lemma, each fingerprint ${\displaystyle \mathrm {FING} (X(j))}$ can be computed in an incremental way, each in constant time. The running time of the algorithm is ${\displaystyle O(n+m)}$.