Rolling hash

From HandWiki

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; and similar to the way a Zobrist hash can be rapidly updated from the old hash value.

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 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[1] or strongly universal. 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:

[math]\displaystyle{ H = c_1 a^{k-1} + c_2 a^{k-2} + c_3 a^{k-3} + ... + c_k a^{0} }[/math],

where [math]\displaystyle{ a }[/math] is a constant, and [math]\displaystyle{ c_1, ..., c_k }[/math] are the input characters (but this function is not a Rabin fingerprint, see below).

In order to avoid manipulating huge [math]\displaystyle{ H }[/math] values, all math is done modulo [math]\displaystyle{ n }[/math]. The choice of [math]\displaystyle{ a }[/math] and [math]\displaystyle{ n }[/math] is critical to get good hashing; in particular, the modulus [math]\displaystyle{ n }[/math] 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 [math]\displaystyle{ H }[/math] by [math]\displaystyle{ a }[/math]. Shifting all characters by one position to the right requires dividing the entire sum [math]\displaystyle{ H }[/math] by [math]\displaystyle{ a }[/math]. Note that in modulo arithmetic, [math]\displaystyle{ a }[/math] can be chosen to have a multiplicative inverse [math]\displaystyle{ a^{-1} }[/math] by which [math]\displaystyle{ H }[/math] can be multiplied to get the result of the division without actually performing a division.

Rabin fingerprint

The Rabin fingerprint is another hash, which also interprets the input as a polynomial, but over the Galois field GF(2). 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). 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 software restic uses a Rabin fingerprint for splitting files, with blob size varying between 512KiB and 8MiB.[2]

Cyclic polynomial

Hashing by cyclic polynomial[3]—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: it presumes that there is some hash function [math]\displaystyle{ h }[/math] from characters to integers in the interval [math]\displaystyle{ [0,2^L) }[/math]. This hash function might be simply an array or a hash table mapping characters to random integers. Let the function [math]\displaystyle{ s }[/math] 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., [math]\displaystyle{ s(101)=011 }[/math]. Let [math]\displaystyle{ \oplus }[/math] be the bitwise exclusive or. The hash values are defined as

[math]\displaystyle{ H = s^{k-1}(h( c_1 )) \oplus s^{k-2}( h(c_2) ) \oplus \ldots \oplus s( h(c_{k-1}) ) \oplus h(c_k), }[/math]

where the multiplications by powers of two can be implemented by binary shifts. The result is a number in [math]\displaystyle{ [0,2^L) }[/math].

Computing the hash values in a rolling fashion is done as follows. Let [math]\displaystyle{ H }[/math] be the previous hash value. Rotate [math]\displaystyle{ H }[/math] once: [math]\displaystyle{ H \leftarrow s(H) }[/math]. If [math]\displaystyle{ c_1 }[/math] is the character to be removed, rotate it [math]\displaystyle{ k }[/math] times: [math]\displaystyle{ s^{k}(h( c_1 )) }[/math]. Then simply set

[math]\displaystyle{ H \leftarrow s(H) \oplus s^{k}(h( c_1 )) \oplus h(c_{k+1}), }[/math]

where [math]\displaystyle{ c_{k+1} }[/math] is the new character.

Hashing by cyclic polynomials is strongly universal or pairwise independent: simply keep the first [math]\displaystyle{ L-k+1 }[/math] bits. That is, take the result [math]\displaystyle{ H }[/math] and dismiss any [math]\displaystyle{ k-1 }[/math] consecutive bits.[1] In practice, this can be achieved by an integer division [math]\displaystyle{ H \rightarrow H \div 2^{k-1} }[/math].

Content-based slicing using a rolling hash

One of the interesting use cases of the rolling hash function is that it can create dynamic, content-based chunks of a stream or file. This is especially useful when it is required to send only the changed chunks of a large file over a network: a simple byte addition at the front of the file would normally cause all fixed size windows to become updated, while in reality, only the first "chunk" has been modified.[4]

A simple approach to making dynamic chunks is to calculate a rolling hash, and if the hash value matches an arbitrary pattern (e.g. all zeroes) in the lower N bits (with a probability of [math]\displaystyle{ {1 \over 2^n} }[/math], given the hash has a uniform probability distribution) then it’s chosen to be a chunk boundary. Each chunk will thus have an average size of [math]\displaystyle{ 2^n }[/math] bytes. This approach ensures that unmodified data (more than a window size away from the changes) will have the same boundaries.

Once the boundaries are known, the chunks need to be compared by cryptographic hash value to detect changes.[5] The backup software Borg uses the Buzhash algorithm with a customizable chunk size range for splitting file streams.[6]

Such content-defined chunking is often used for data deduplication.[4][6]

Content-based slicing using moving sum

Several programs, including gzip (with the --rsyncable option) and rsyncrypto, do content-based slicing based on this specific (unweighted) moving sum:[7]

[math]\displaystyle{ S(n) = \sum_{i = n - 8195}^{n} c_i, }[/math]
[math]\displaystyle{ H(n) = S(n) \mod 4096, }[/math]

where

  • [math]\displaystyle{ S(n) }[/math] is the sum of 8196 consecutive bytes ending with byte [math]\displaystyle{ n }[/math] (requires 21 bits of storage),
  • [math]\displaystyle{ c_i }[/math] is byte [math]\displaystyle{ i }[/math] of the file,
  • [math]\displaystyle{ H(n) }[/math] is a "hash value" consisting of the bottom 12 bits of [math]\displaystyle{ S(n) }[/math].

Shifting the window by one byte simply involves adding the new character to the sum and subtracting the oldest character (no longer in the window) from the sum.

For every [math]\displaystyle{ n }[/math] where [math]\displaystyle{ H(n) == 0 }[/math], these programs cut the file between [math]\displaystyle{ n }[/math] and [math]\displaystyle{ n+1 }[/math]. This approach will ensure that any change in the file will only affect its current and possibly the next chunk, but no other chunk.

Gear fingerprint and content-based chunking algorithm FastCDC

Chunking is a technique to divide a data stream into a set of blocks, also called chunks. Content-defined chunking (CDC) is a chunking technique in which the division of the data stream is not based on fixed chunk size, as in fixed-size chunking, but on its content.

The Content-Defined Chunking algorithm needs to compute the hash value of a data stream byte by byte and split the data stream into chunks when the hash value meets a predefined value. However, comparing a string byte-by-byte will introduce the heavy computation overhead. FastCDC [8] proposes a new and efficient Content-Defined Chunking approach. It uses a fast rolling Gear hash algorithm,[9] skipping the minimum length, normalizing the chunk-size distribution, and last but not the least, rolling two bytes each time to speed up the CDC algorithm, which can achieve about 10X higher throughput than Rabin-based CDC approach.[10]

The basic version pseudocode is provided as follows:

algorithm FastCDC
    input: data buffer src, 
           data length n, 
    output: cut point i
    
    MinSize ← 2KB     // split minimum chunk size is 2 KB
    MaxSize ← 64KB    // split maximum chunk size is 64 KB
    Mask0x0000d93003530000LL
    fp0
    i0
    
    // buffer size is less than minimum chunk size
    if nMinSize then
        return n
    if nMaxSize then
        nMaxSize
    
    // Skip the first MinSize bytes, and kickstart the hash
    while i < MinSize do
        fp ← (fp << 1 ) + Gear[src[i]]
        ii + 1
     
    while i < n do
        fp ← (fp << 1 ) + Gear[src[i]]
        if !(fp & Mask) then
            return i
        ii + 1
   
    return i

Where Gear array is a pre-calculated hashing array. Here FastCDC uses Gear hashing algorithm which can calculate the rolling hashing results quickly and keep the uniform distribution of the hashing results as Rabin. Compared with the traditional Rabin hashing algorithm, it achieves a much faster speed. Experiments suggest that it can generate nearly the same chunk size distribution in the much shorter time (about 1/10 of rabin-based chunking [10]) when segmenting the data stream.

Computational complexity

All rolling hash functions can be computed in time linear in the number of characters and updated in constant time when characters are shifted by one position. In particular, computing the Rabin-Karp rolling hash of a string of length [math]\displaystyle{ k }[/math] requires [math]\displaystyle{ O(k) }[/math] modular arithmetic operations, and hashing by cyclic polynomials requires [math]\displaystyle{ O(k) }[/math] bitwise exclusive ors and circular shifts.[1]

See also

Footnotes

  1. 1.0 1.1 1.2 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.
  2. "References — restic 0.9.0 documentation" (in en). https://restic.readthedocs.io/en/stable/100_references.html#backups-and-deduplication. 
  3. Jonathan D. Cohen, Recursive Hashing Functions for n-Grams, ACM Trans. Inf. Syst. 15 (3), 1997.
  4. 4.0 4.1 "Foundation - Introducing Content Defined Chunking (CDC)". 2015.
  5. Horvath, Adam (October 24, 2012). "Rabin Karp rolling hash - dynamic sized chunks based on hashed content". http://blog.teamleadnet.com/2012/10/rabin-karp-rolling-hash-dynamic-sized.html. 
  6. 6.0 6.1 "Data structures and file formats — Borg - Deduplicating Archiver 1.1.5 documentation" (in en). https://borgbackup.readthedocs.io/en/stable/internals/data-structures.html#chunker-details. 
  7. "Rsyncrypto Algorithm".
  8. Xia, Wen; Zhou, Yukun; Jiang, Hong; Feng, Dan; Hua, Yu; Hu, Yuchong; Liu, Qing; Zhang, Yucheng (2005). FastCDC: A Fast and Efficient Content-Defined Chunking Approach for Data Deduplication. ISBN 9781931971300. https://www.usenix.org/conference/atc16/technical-sessions/presentation/xia. Retrieved 2020-07-24. 
  9. Xia, Wen; Jiang, Hong; Feng, Dan; Tian, Lei; Fu, Min; Zhou, Yukun (2014). "Ddelta: A deduplication-inspired fast delta compression approach". Performance Evaluation 79: 258–272. doi:10.1016/j.peva.2014.07.016. ISSN 0166-5316. 
  10. 10.0 10.1 Xia, Wen; Zou, Xiangyu; Jiang, Hong; Zhou, Yukun; Liu, Chuanyi; Feng, Dan; Hua, Yu; Hu, Yuchong et al. (2020-06-16). "The Design of Fast Content-Defined Chunking for Data Deduplication Based Storage Systems". IEEE Transactions on Parallel and Distributed Systems 31 (9): 2017–2031. doi:10.1109/TPDS.2020.2984632. https://ieeexplore.ieee.org/document/9055082. Retrieved 2020-07-22. 

External links