encyclopegia.org

Rolling hash

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.[src] 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.[src] One of the main applications is the Rabin–Karp string search algorithm, which uses the rolling hash described below.[src] Another popular application is the rsync program, which uses a checksum based on Mark Adler's adler-32 as its rolling hash.[src] Low Bandwidth Network Filesystem (LBFS) uses a Rabin fingerprint as its rolling hash.[src] FastCDC (Fast Content-Defined Chunking) uses a compute-efficient Gear fingerprint as its rolling hash.[src] At best, rolling hash values are pairwise independent or strongly universal.[src] They cannot be 3-wise independent, for example.[src]

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.[src] 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.[src] One of the main applications is the Rabin–Karp string search algorithm, which uses the rolling hash described below.[src] Another popular application is the rsync program, which uses a checksum based on Mark Adler's adler-32 as its rolling hash.[src] Low Bandwidth Network Filesystem (LBFS) uses a Rabin fingerprint as its rolling hash.[src] FastCDC (Fast Content-Defined Chunking) uses a compute-efficient Gear fingerprint as its rolling hash.[src] At best, rolling hash values are pairwise independent or strongly universal.[src] They cannot be 3-wise independent, for example.[src] H = c_1 a^{k-1} + c_2 a^{k-2} + c_3 a^{k-3} + ...[src] + c_k a^{0} , where a is a constant, and c_1, ..., c_k are the input characters (but this function is not a Rabin fingerprint, see below).[src] In order to avoid manipulating huge H values, all math is done modulo n .[src] The choice of a and n is critical to get good hashing; in particular, the modulus n is typically a prime number.[src] See linear congruential generator for more discussion.[src] Removing and adding characters simply involves adding or subtracting the first or last term.[src] Shifting all characters by one position to the left requires multiplying the entire sum H by a .[src] Shifting all characters by one position to the right requires dividing the entire sum H by a .[src] 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.[src] Hashing by cyclic polynomial &mdash;sometimes called Buzhash &mdash;is also simple and it has the benefit of avoiding multiplications, using circular shift instead.[src] It is a form of tabulation hashing: it presumes that there is some substitution function s from characters to integers in the interval [0,2^L) , essentially a lookup table (each of the 32 bit-positions of the values of s should be balanced, i.e.[src] have as many 1s as there are 0s).[src] Let the function \operatorname{rol} be bitwise rotation.[src] E.g., \operatorname{rol}(101) = 011 .[src] Let \oplus be the bitwise exclusive or.[src] Let c_i be the i-th byte in a stream, and w be the window size in use.[src] We precalculate s' for removing the contribution of a byte that is out of the window: s'[i] = \operatorname{rol}(s[i], w) The hash values is defined as the following recurrence relation: H_i = \begin{cases} 0 & \text{if } i = 0 \\ \operatorname{rol}(H_{i-1}, 1) \oplus s[c_i] & \text{if } i \le w \\ \operatorname{rol}(H_{i-1}, 1) \oplus s[c_i] \oplus s'[c_{i-w}] & \text{if } i > w \end{cases} All values of H are within the interval [0,2^L) .[src] This recurrence relation describes the way to compute the hash in a rolling fashion: Hash is initialized at 0.[src] When the window is being filled, adding a character involves left-rotating the old hash by one position and XORing the new s[c_i] .[src] When the window is full, the same procedure is used to add a character, followed by removal of the contribution of the out-of-window character by XORing s'[c_{i-w}] .[src] The value of H is neither formally uniform nor 2-universal, despite its empirical uniformity.[src] However, it can be made formally uniform and pairwise independent if only a consecutive L-w+1 bits are taken from the hash.[src] In practice, this can be a bitwise AND (masking) operation: H'_i = H_i \mathbin{\&} (1 \ll (w-1) - 1) , where \mathbin{\&} is a bitwise AND and \ll is a left-shift.[src] In addition, the authors of borg note that w should not be a multiple of 2&thinsp;L if a seed is used to modify s[] by XORing each value with the seed.[src] H_0 = 0 H_i = (H_{i-1} \ll 1) + s[f[i]] Compared to the cyclic polynomial hash, the left-rotate operation is replaced by left-shift, which removes the need to remove the contribution of an out-of-window byte.[src] Xor is also replaced by addition.[src] Chunking is done based on the top bits of H.[src] This type of chunking generates results comparable with the Rabin fingerprint in one-third of the time.[src] However, in practice the distribution of split-sizes were wider than Rabin, making deduplication results about 1% poorer for the typical cases and 6% poor for the worst case.[src] Using the bottom bits is also acceptable but reduces the effective sliding-window size.[src] The largest effective size appears when the mask samples a number of non-consecutive bits from a relatively wide "span" of H.[src] A simple approach to making dynamic chunks is to calculate a rolling hash, and if the hash value matches an arbitrary pattern (e.g.[src] all zeroes) in the lower N bits (with a probability of {1 \over 2^n} , given the hash has a uniform probability distribution) then it’s chosen to be a chunk boundary.[src] Each chunk will thus have an average size of 2^n bytes.[src] This approach ensures that unmodified data (more than a window size away from the changes) will have the same boundaries.[src] Once the boundaries are known, the chunks need to be compared by cryptographic hash value to detect changes.[src] Such content-defined chunking (CDC) or content-based chunking (CBC) is often used for data deduplication.[src] The hash function used can be any rolling-hash algorithm.[src] Some examples are: Chunking on the least-significant bits An unweighted sum (rsyncrypto) Rabin-Karp polynomial rolling hash Rabin fingerprint, as used in the backup software restic (with blob size varying between and and 64-byte window).[src] Chunking on any consecutive bits Cyclic polynomial (buzhash), as used in the backup software Borg.[src] Borg provides a customizable chunk size range for splitting file streams, based on varying the N, by default between and ).[src] It uses a 4095-byte window and non-balanced substitution function.[src] Chunking on any collection of bits A gear hash.[src] Gear-based CDC has been successively optimized, yielding FastCDC, RapidCDC, and QuickCDC.[src] Content-based chunking has the advantage that in most cases, local changes to a file only affects the chunk it is in and possibly the next chunk, but no other chunk.[src] Different choices of algorithms and hashes offer different strengths of this guarantee.[src] H(n) = \sum_{i = n - 8195}^{n} c_i \mod 4096 where c_i is byte i of the file, H(n) is a "hash value" consisting of the bottom 12 bits of the sum of 8196 consecutive bytes ending with byte n .[src] 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.[src] Because of the properties of modulo arithmetic, the storage required is only 12 bits.[src] For every n where H(n) == 0 , these programs cut the file between n and n+1 .[src] Mask &larr; 0x0000d93003530000 // 13 bits set -> desired average size is 2^13 bytes = 8 KB fp &larr; 0 // uint64 i &larr; 0 // buffer size is less than minimum chunk size if n ≤ MinSize then return n if n ≥ MaxSize then n &larr; MaxSize // Skip the first MinSize bytes, and kickstart the hash while i < MinSize do fp &larr; (fp << 1 ) + Gear[src[i]] i &larr; i + 1 while i < n do fp &larr; (fp << 1 ) + Gear[src[i]] if !(fp & Mask) then return i i &larr; i + 1 return i Where the Gear array is equivalent to the s table above.[src] An advanced version uses two different masks derived from the above Mask, one with 15 set bits and the other with 11 set bits.[src] The former is used when i < 8 KB; afterwards the latter takes over.[src] This tightens the chunk-size distribution around the desired average and improves deduplication.[src] ==See also==[src]

71 sentences · 71 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Rolling hash." encyclopegia.org, 2026, https://encyclopegia.org/wiki/rolling-hash. Accessed 25 November 2026.

Chicago

"Rolling hash." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/rolling-hash.

APA

encyclopegia.org. (2026). Rolling hash. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/rolling-hash

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Hash Chain

article · references available

Rolling Stone

stub · references available

Cooper's Hill Cheese-Rolling and Wake

stub · references available

London Underground rolling stock

stub · references available

Operation Rolling Thunder

stub · references available

The Rolling Stone Album Guide

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1348578201 · 0.5 · snapshot enwiki-001348578201

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.