World Library  
Flag as Inappropriate
Email this Article

Rolling hash

Article Id: WHEBN0004071549
Reproduction Date:

Title: Rolling hash  
Author: World Heritage Encyclopedia
Language: English
Subject: Rabin fingerprint, Rsync, Hash functions, Data comparison, Tabulation hashing
Publisher: World Heritage Encyclopedia

Rolling hash

A rolling hash 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 rsync program which uses a checksum based on Mark Adler's adler-32 as its rolling hash.

Another application is the Low Bandwidth Network Filesystem (LBFS), which uses a Rabin 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.


  • Rabin-Karp rolling hash 1
  • Content based slicing using Rabin-Karp hash 2
  • Cyclic polynomial 3
  • Computational complexity 4
  • Software 5
  • See also 6
  • External links 7
  • Footnotes 8

Rabin-Karp rolling hash

The Rabin-Karp string search algorithm is normally used with a very simple rolling hash function that only uses multiplications and additions:

H = c_1 a^{k-1} + c_2 a^{k-2} + c_3 a^{k-3} + ... + c_k a^{0} where a is a constant and c_1, ..., c_k are the input characters.

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

Content based slicing using Rabin-Karp 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 and a simple byte addition at the front of the file would cause all the fixed size windows to become updated, while in reality, only the first ‘chunk’ has been modified.

The simplest approach to calculate the dynamic chunks is to calculate the rolling hash and if it matches a pattern (like the lower N bits are all zeroes) then it’s a chunk boundary. This approach will ensure that any change in the file will only affect its current and possibly the next chunk, but nothing else.

When the boundaries are known, the chunks need to be compared by their hash values to detect which one was modified and needs transfer across the network.[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 h from characters to integers in the interval [0,2^L). This hash function might be simply an array or a hash table mapping characters to random integers. Let the function s be a cyclic binary rotation (or barrel shift): it rotates the bits by 1 to the left, pushing the latest bit in the first position. E.g., s(10011)=00111. Let \oplus be the bit-wise exclusive or. The hash values are defined as

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)

where the multiplications by powers of two can be implemented by binary shifts. The result is a number in [0,2^L).

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

H\leftarrow s(H) \oplus s^{k}(h( c_1 )) \oplus h(c_{k+1})

where c_{k+1} is the new character.

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

Computational complexity

All rolling hash functions are linear in the number of characters, but their complexity with respect to the length of the window (k) varies. Rabin-Karp rolling hash requires the multiplications of two k-bit numbers, integer multiplication is in O(k \log k 2^{O(\log^*k)}).[4] Hashing ngrams by cyclic polynomials can be done in linear time.[1]


  • ngramhashing is a Free software C++ implementation of several rolling hash functions
  • rollinghashjava is an Apache licensed Java implementation of rolling hash functions

See also

External links

  • MIT 6.006: Introduction to Algorithms 2011- Lecture Notes - Rolling Hash


  1. ^ a b c 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. ^ Horvath, Adam (October 24, 2012). "Rabin Karp rolling hash - dynamic sized chunks based on hashed content". 
  3. ^ Jonathan D. Cohen, Recursive Hashing Functions for n-Grams, ACM Trans. Inf. Syst. 15 (3), 1997
  4. ^ M. Fürer, Faster integer multiplication, in: STOC ’07, 2007, pp. 57–66.
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.