<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Rolling_hash</id>
	<title>Rolling hash - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Rolling_hash"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Rolling_hash&amp;action=history"/>
	<updated>2026-06-13T16:57:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>http://en.zaoniao.it/index.php?title=Rolling_hash&amp;diff=2454&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;&lt;amp/&gt; 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...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Rolling_hash&amp;diff=2454&amp;oldid=prev"/>
		<updated>2019-03-23T07:24:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;amp/&amp;gt; A &amp;#039;&amp;#039;&amp;#039;rolling hash&amp;#039;&amp;#039;&amp;#039; (also known as recursive hashing or rolling checksum) is a &lt;a href=&quot;/Hash_function&quot; class=&quot;mw-redirect&quot; title=&quot;Hash function&quot;&gt;hash function&lt;/a&gt; where the input is hashed in a window that moves through the input.  A...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;amp/&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
At best, rolling hash values are [[pairwise independent]] or strongly [[universal hashing|universal]]. They cannot be [[k-independent hashing|3-wise independent]], for example.&lt;br /&gt;
&lt;br /&gt;
== Rabin–Karp rolling hash ==&lt;br /&gt;
&lt;br /&gt;
The [[Rabin–Karp string search algorithm]] is normally used with a very simple rolling hash function that only uses multiplications and additions:&lt;br /&gt;
:&amp;lt;math&amp;gt;H = c_1 a^{k-1} + c_2 a^{k-2} + c_3 a^{k-3} + ... + c_k a^{0},&amp;lt;/math&amp;gt;&lt;br /&gt;
where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is a constant, and &amp;lt;math&amp;gt;c_1, ..., c_k&amp;lt;/math&amp;gt; are the input characters.&lt;br /&gt;
&lt;br /&gt;
In order to avoid manipulating huge &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; values, all math is done [[modular arithmetic|modulo]] &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. The choice of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is critical to get good hashing; see [[linear congruential generator]] for more discussion.&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; by &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;. Shifting all characters by one position to the right requires dividing the entire sum &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; by &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;. Note that in modulo arithmetic, &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; can be chosen to have a [[modular multiplicative inverse|multiplicative inverse]] &amp;lt;math&amp;gt;a^{-1}&amp;lt;/math&amp;gt; by which &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; can be multiplied to get the result of the division without actually performing a division.&lt;br /&gt;
&lt;br /&gt;
== Content-based slicing using Rabin–Karp hash ==&lt;br /&gt;
 &lt;br /&gt;
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 &amp;quot;chunk&amp;quot; has been modified.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Cyclic polynomial ==&lt;br /&gt;
&lt;br /&gt;
Hashing by cyclic polynomial&amp;amp;mdash;sometimes called Buzhash&amp;amp;mdash;is also simple, but it has the benefit of avoiding multiplications, using [[Barrel shifter|barrel shifts]] instead. It is a form of [[tabulation hashing]]: it presumes that there is some hash function &amp;lt;math&amp;gt;h&amp;lt;/math&amp;gt; from characters to integers in the interval &amp;lt;math&amp;gt;[0,2^L)&amp;lt;/math&amp;gt;. This hash function might be simply an array or a [[hash table]] mapping characters to random integers. Let the function &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; be a cyclic binary rotation (or [[Circular shift|circular shift]]): it rotates the bits by 1 to the left, pushing the latest bit in the first position. E.g., &amp;lt;math&amp;gt;s(10011)=00111&amp;lt;/math&amp;gt;. Let &amp;lt;math&amp;gt;\oplus&amp;lt;/math&amp;gt; be the bitwise [[exclusive or]]. The hash values are defined as&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt; 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),&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the multiplications by powers of two can be implemented by binary shifts. The result is a number in &amp;lt;math&amp;gt;[0,2^L)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Computing the hash values in a rolling fashion is done as follows. Let &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; be the previous hash value. Rotate &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; once: &amp;lt;math&amp;gt;H \leftarrow s(H)&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;c_1&amp;lt;/math&amp;gt; is the character to be removed, rotate it &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; times: &amp;lt;math&amp;gt;s^{k}(h( c_1 ))&amp;lt;/math&amp;gt;. Then simply set &lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;H \leftarrow s(H) \oplus s^{k}(h( c_1 )) \oplus h(c_{k+1}),&amp;lt;/math&amp;gt; &lt;br /&gt;
&lt;br /&gt;
where &amp;lt;math&amp;gt;c_{k+1}&amp;lt;/math&amp;gt; is the new character.&lt;br /&gt;
&lt;br /&gt;
Hashing by cyclic polynomials is strongly universal or pairwise independent: simply keep the first &amp;lt;math&amp;gt;L-k+1&amp;lt;/math&amp;gt; bits. That is, take the result &amp;lt;math&amp;gt;H&amp;lt;/math&amp;gt; and dismiss any &amp;lt;math&amp;gt;k-1&amp;lt;/math&amp;gt; consecutive bits.&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;S(n) = \sum_{i = n - 8195}^{n} c_i,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;H(n) = S(n) \mod 4096,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
* &amp;lt;math&amp;gt;S(n)&amp;lt;/math&amp;gt; is the sum of 8196 consecutive bytes ending with byte &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (requires 21 bits of storage),&lt;br /&gt;
* &amp;lt;math&amp;gt;c_i&amp;lt;/math&amp;gt; is byte &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; of the file,&lt;br /&gt;
* &amp;lt;math&amp;gt;H(n)&amp;lt;/math&amp;gt; is a &amp;quot;hash value&amp;quot; consisting of the bottom 12 bits of &amp;lt;math&amp;gt;S(n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For every &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;H(n) == 0&amp;lt;/math&amp;gt;, these programs cut the file between &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
This approach will ensure that any change in the file will only affect its current and possibly the next chunk, but no other chunk.&lt;br /&gt;
&lt;br /&gt;
== Computational complexity ==&lt;br /&gt;
&lt;br /&gt;
All rolling hash functions are linear in the number of characters, but their complexity with respect to the length of the window (&amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;) varies. Rabin–Karp rolling hash requires the multiplications of two &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;-bit numbers, [[integer multiplication]] is in &amp;lt;math&amp;gt;O(k \log k 2^{O(\log^*k)})&amp;lt;/math&amp;gt;. Hashing [[ngram]]s by cyclic polynomials can be done in linear time.&lt;br /&gt;
&lt;br /&gt;
== Software ==&lt;br /&gt;
*[https://github.com/lemire/rollinghashcpp rollinghashcpp] is a [[free-software]] C++ implementation of several rolling hash functions&lt;br /&gt;
*[https://github.com/lemire/rollinghashjava rollinghashjava] is an Apache-licensed Java implementation of rolling hash functions&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[MinHash]]&lt;br /&gt;
*[[w-shingling]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
*[http://courses.csail.mit.edu/6.006/spring11/rec/rec06.pdf MIT 6.006: Introduction to Algorithms 2011- Lecture Notes - Rolling Hash]&lt;br /&gt;
&lt;br /&gt;
== Footnotes ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
[http://wikipedia.org/ http://wikipedia.org/]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cryptography]]&lt;br /&gt;
[[Category:Cryptographic algorithms]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>