A rolling hash (also known as recursive hashing or rolling checksum) is a
hash function where the input is hashed in a window that moves through the input.
A few hash functions allow a rolling hash to be computed very quickly—the new hash value is rapidly calculated given only the old hash value, the old value removed from the window, and the new value added to the window—similar to the way a
moving average function can be computed much more quickly than other low-pass filters.
One of the main applications is the
Rabin–Karp string search algorithm, which uses the rolling hash described below. Another popular application is the
rsync program, which uses a checksum based on Mark Adler's
adler-32 as its rolling hash. Low Bandwidth Network Filesystem (LBFS) uses a
Rabin fingerprint The Rabin fingerprinting scheme is a method for implementing fingerprints using polynomials over a finite field. It was proposed by Michael O. Rabin.
Scheme
Given an ''n''-bit message ''m''0,...,''m''n-1, we view it as a polynomial of degree ''n''- ...
as its rolling hash. FastCDC (Fast Content-Defined Chunking) uses a compute-efficient Gear fingerprint as its rolling hash.
At best, rolling hash values are
pairwise independent In probability theory, a pairwise independent collection of random variables is a set of random variables any two of which are independent. Any collection of mutually independent random variables is pairwise independent, but some pairwise indepen ...
[Daniel Lemire, Owen Kaser: Recursive ''n''-gram hashing is pairwise independent, at best, Computer Speech & Language 24 (4), pages 698–710, 2010. arXiv:0705.4676.] or strongly
universal
Universal is the adjective for universe.
Universal may also refer to:
Companies
* NBCUniversal, a media and entertainment company
** Universal Animation Studios, an American Animation studio, and a subsidiary of NBCUniversal
** Universal TV, a t ...
. They cannot be
3-wise independent, for example.
Polynomial rolling hash
The
Rabin–Karp string search algorithm is often explained using a rolling hash function that only uses multiplications and additions:
:
,
where
is a constant, and
are the input characters (but this function is not a
Rabin fingerprint The Rabin fingerprinting scheme is a method for implementing fingerprints using polynomials over a finite field. It was proposed by Michael O. Rabin.
Scheme
Given an ''n''-bit message ''m''0,...,''m''n-1, we view it as a polynomial of degree ''n''- ...
, see below).
In order to avoid manipulating huge
values, all math is done
modulo
In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the '' modulus'' of the operation).
Given two positive numbers and , modulo (often abbreviated as ) is t ...
. The choice of
and
is critical to get good hashing; in particular, the modulus
is typically a prime number. See
linear congruential generator for more discussion.
Removing and adding characters simply involves adding or subtracting the first or last term. Shifting all characters by one position to the left requires multiplying the entire sum
by
. Shifting all characters by one position to the right requires dividing the entire sum
by
. Note that in modulo arithmetic,
can be chosen to have a
multiplicative inverse
In mathematics, a multiplicative inverse or reciprocal for a number ''x'', denoted by 1/''x'' or ''x''−1, is a number which when Multiplication, multiplied by ''x'' yields the multiplicative identity, 1. The multiplicative inverse of a rat ...
by which
can be multiplied to get the result of the division without actually performing a division.
Rabin fingerprint
The
Rabin fingerprint The Rabin fingerprinting scheme is a method for implementing fingerprints using polynomials over a finite field. It was proposed by Michael O. Rabin.
Scheme
Given an ''n''-bit message ''m''0,...,''m''n-1, we view it as a polynomial of degree ''n''- ...
is another hash, which also interprets the input as a polynomial, but over the
Galois field GF(2)
(also denoted \mathbb F_2, or \mathbb Z/2\mathbb Z) is the finite field of two elements (GF is the initialism of ''Galois field'', another name for finite fields). Notations and \mathbb Z_2 may be encountered although they can be confused with ...
. Instead of seeing the input as a polynomial of bytes, it is seen as a polynomial of bits, and all arithmetic is done in GF(2) (similarly to
CRC32
A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to digital data. Blocks of data entering these systems get a short ''check value'' attached, based on t ...
). The hash is the result of the division of that polynomial by an irreducible polynomial over GF(2). It is possible to update a Rabin fingerprint using only the entering and the leaving byte, making it effectively a rolling hash.
Because it shares the same author as the Rabin–Karp string search algorithm, which is often explained with another, simpler rolling hash, and because this simpler rolling hash is also a polynomial, both rolling hashes are often mistaken for each other. The backup softwar
resticuses a Rabin fingerprint for splitting files, with blob size varying between and .
Cyclic polynomial
Hashing by cyclic polynomial—sometimes called Buzhash—is also simple, but it has the benefit of avoiding multiplications, using
barrel shifts instead. It is a form of
tabulation hashing
In computer science, tabulation hashing is a method for constructing universal families of hash functions by combining table lookup with exclusive or operations. It was first studied in the form of Zobrist hashing for computer games; later work b ...
: it presumes that there is some hash function
from characters to integers in the interval
. This hash function might be simply an array or a hash table mapping characters to random integers. Let the function
be a cyclic binary rotation (or
circular shift): it rotates the bits by 1 to the left, pushing the latest bit in the first position. E.g.,
. Let
be the bitwise
exclusive or. The hash values are defined as
:
where the multiplications by powers of two can be implemented by binary shifts. The result is a number in