<?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=Barrett_reduction</id>
	<title>Barrett reduction - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Barrett_reduction"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Barrett_reduction&amp;action=history"/>
	<updated>2026-05-15T09:30:46Z</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=Barrett_reduction&amp;diff=2376&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;In modular arithmetic, '''Barrett reduction''' is a reduction algorithm introduced in 1986 by P.D. Barrett. A naive way of computing  :&lt;math&gt;c = a \,\bmod\, n. \, &lt;/ma...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Barrett_reduction&amp;diff=2376&amp;oldid=prev"/>
		<updated>2019-03-20T04:29:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In &lt;a href=&quot;/index.php?title=Modular_arithmetic&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Modular arithmetic (page does not exist)&quot;&gt;modular arithmetic&lt;/a&gt;, &amp;#039;&amp;#039;&amp;#039;Barrett reduction&amp;#039;&amp;#039;&amp;#039; is a reduction &lt;a href=&quot;/index.php?title=Algorithm&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Algorithm (page does not exist)&quot;&gt;algorithm&lt;/a&gt; introduced in 1986 by P.D. Barrett. A naive way of computing  :&amp;lt;math&amp;gt;c = a \,\bmod\, n. \, &amp;lt;/ma...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[modular arithmetic]], '''Barrett reduction''' is a reduction [[algorithm]] introduced in 1986 by P.D. Barrett. A naive way of computing&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;c = a \,\bmod\, n. \, &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would be to use a fast [[division algorithm]]. Barrett reduction is an algorithm designed to optimize this operation assuming &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is constant, and &amp;lt;math&amp;gt;a&amp;lt;n^2&amp;lt;/math&amp;gt;, replacing divisions by multiplications.&lt;br /&gt;
&lt;br /&gt;
== General idea ==&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;s=1/n&amp;lt;/math&amp;gt; be the inverse of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; as a [[floating point]] number. Then&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;a \,\bmod\, n = a-\lfloor a s\rfloor n &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;math&amp;gt;\lfloor x \rfloor&amp;lt;/math&amp;gt; denotes the [[floor function]]. The result is exact, as long as &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; is computed with sufficient accuracy.&lt;br /&gt;
&lt;br /&gt;
== Single-word Barrett reduction ==&lt;br /&gt;
&lt;br /&gt;
Barrett initially considered an integer version of the above algorithm when the values fit into machine words.&lt;br /&gt;
&lt;br /&gt;
When calculating &amp;lt;math&amp;gt;a \,\bmod\, n&amp;lt;/math&amp;gt; using the method above, but with integers, the obvious analogue would be to use division by &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot;&amp;gt;&lt;br /&gt;
func reduce(a uint) uint {&lt;br /&gt;
 q := a / n // Division implicitly returns the floor of the result.&lt;br /&gt;
 return a - q * n&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, division can be expensive and, in cryptographic settings, may not be a constant-time instruction on some CPUs. Thus Barrett reduction approximates &amp;lt;math&amp;gt;1/n&amp;lt;/math&amp;gt; with a value &amp;lt;math&amp;gt;m/2^k&amp;lt;/math&amp;gt; because division by &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; is just a right-shift and so is cheap.&lt;br /&gt;
&lt;br /&gt;
In order to calculate the best value for &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; given &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; consider:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{1}{n} = \frac{n(\frac{2^k}{n})} = \frac{2^k} = \frac{m}{2^k}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In order for &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; to be an integer, we need to round &amp;lt;math&amp;gt;{2^k}/{n}&amp;lt;/math&amp;gt; somehow. Rounding to the nearest integer will give the best approximation but can result in &amp;lt;math&amp;gt;m/2^k&amp;lt;/math&amp;gt; being larger than &amp;lt;math&amp;gt;1/n&amp;lt;/math&amp;gt;, which can cause underflows. Thus &amp;lt;math&amp;gt;m = \lfloor {2^k}/{n} \rfloor&amp;lt;/math&amp;gt; is generally used.&lt;br /&gt;
&lt;br /&gt;
Thus we can approximate the function above with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot;&amp;gt;&lt;br /&gt;
func reduce(a uint) uint {&lt;br /&gt;
 q := (a * m) &amp;gt;&amp;gt; k // &amp;quot;&amp;gt;&amp;gt; k&amp;quot; denotes bitshift by k.&lt;br /&gt;
 return a - q * n&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, since &amp;lt;math&amp;gt;m/2^k \le 1/n&amp;lt;/math&amp;gt;, the value of &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; in that function can end up being one too small, and thus &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; is only guaranteed to be within &amp;lt;math&amp;gt;[0, 2n)&amp;lt;/math&amp;gt; rather than &amp;lt;math&amp;gt;[0, n)&amp;lt;/math&amp;gt; as is generally required. A conditional subtraction will correct this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot;&amp;gt;&lt;br /&gt;
func reduce(a uint) uint {&lt;br /&gt;
 q := (a * m) &amp;gt;&amp;gt; k&lt;br /&gt;
 a -= q * n&lt;br /&gt;
 if n &amp;lt;= a {&lt;br /&gt;
 a -= n&lt;br /&gt;
 }&lt;br /&gt;
 return a&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since &amp;lt;math&amp;gt;m/2^k&amp;lt;/math&amp;gt; is only an approximation, the valid range of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; needs to be considered. The error of the approximation of &amp;lt;math&amp;gt;1/n&amp;lt;/math&amp;gt; is:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;e = \frac{1}{n} - \frac{m}{2^k}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus the error in the value of &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; is &amp;lt;math&amp;gt;ae&amp;lt;/math&amp;gt;. As long as &amp;lt;math&amp;gt;ae &amp;lt; 1&amp;lt;/math&amp;gt; then the reduction is valid thus &amp;lt;math&amp;gt;a &amp;lt; 1/e&amp;lt;/math&amp;gt;. The reduction function might not immediately give the wrong answer when &amp;lt;math&amp;gt;a \ge 1/e&amp;lt;/math&amp;gt; but the bounds on &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; must be respected to ensure the correct answer in the general case.&lt;br /&gt;
&lt;br /&gt;
By choosing larger values of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, the range of values of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; for which the reduction is valid can be increased, but larger values of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; may cause overflow problems elsewhere.&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
Consider the case of &amp;lt;math&amp;gt;n = 101&amp;lt;/math&amp;gt; when operating with 16-bit integers.&lt;br /&gt;
&lt;br /&gt;
The smallest value of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; that makes sense is &amp;lt;math&amp;gt;k = 7&amp;lt;/math&amp;gt; because if &amp;lt;math&amp;gt;2^k &amp;lt; n&amp;lt;/math&amp;gt; then the reduction will only be valid for values that are already minimal! For a value of seven, &amp;lt;math&amp;gt;m = \lfloor 2^k / n \rfloor = \lfloor 128 / 101 \rfloor = 1&amp;lt;/math&amp;gt;. For a value of eight &amp;lt;math&amp;gt;m = \lfloor 256 / 101 \rfloor = 2&amp;lt;/math&amp;gt;. Thus &amp;lt;math&amp;gt;k = 8&amp;lt;/math&amp;gt; provides no advantage because the approximation of &amp;lt;math&amp;gt;1/101&amp;lt;/math&amp;gt; in that case (&amp;lt;math&amp;gt;2/256&amp;lt;/math&amp;gt;) is exactly the same as &amp;lt;math&amp;gt;1/128&amp;lt;/math&amp;gt;. For &amp;lt;math&amp;gt;k = 9&amp;lt;/math&amp;gt;, we get &amp;lt;math&amp;gt;m = 512 / 101 = 5&amp;lt;/math&amp;gt;, which is a better approximation.&lt;br /&gt;
&lt;br /&gt;
Now we consider the valid input ranges for &amp;lt;math&amp;gt;k = 7&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;k = 9&amp;lt;/math&amp;gt;. In the first case, &amp;lt;math&amp;gt;e = 1/n - m/2^k = 1/101 - 1/128 = 27/12928&amp;lt;/math&amp;gt; so &amp;lt;math&amp;gt;a &amp;lt; 1/e&amp;lt;/math&amp;gt; implies &amp;lt;math&amp;gt;a &amp;lt; 478.81&amp;lt;/math&amp;gt;. Since &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is an integer, effectively the maximum value is 478. (In practice the function happens to work for values up to 504.)&lt;br /&gt;
&lt;br /&gt;
If we take &amp;lt;math&amp;gt;k = 9&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;e = 1/101 - 5/512 = 7/51712&amp;lt;/math&amp;gt; and so the maximum value of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is 7387. (In practice the function happens to work until 7473.)&lt;br /&gt;
&lt;br /&gt;
The next value of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; that results in a better approximation is 13, giving &amp;lt;math&amp;gt;81/8192&amp;lt;/math&amp;gt;. However, note that the intermediate value &amp;lt;math&amp;gt;ax&amp;lt;/math&amp;gt; in the calculation will then overflow an unsigned 16-bit value when &amp;lt;math&amp;gt;810 \le a&amp;lt;/math&amp;gt;, thus &amp;lt;math&amp;gt;k = 7&amp;lt;/math&amp;gt; works better in this situation.&lt;br /&gt;
&lt;br /&gt;
=== Proof for range for a specific k ===&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;k_0&amp;lt;/math&amp;gt; be the smallest integer such that &amp;lt;math&amp;gt;2^{k_0}&amp;gt;n&amp;lt;/math&amp;gt;. Take &amp;lt;math&amp;gt;k_0+1&amp;lt;/math&amp;gt; as a reasonable value for &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; in the above equations. As in the code snippets above, let&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;math&amp;gt;q = \left\lfloor \frac{m a}{2^k} \right\rfloor &amp;lt;/math&amp;gt; and&lt;br /&gt;
* &amp;lt;math&amp;gt;r = a - q n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Because of the [[floor function]], &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; is an integer and &amp;lt;math&amp;gt;r \equiv a \pmod{n}&amp;lt;/math&amp;gt;. Also, if &amp;lt;math&amp;gt;a &amp;lt; 2 ^ k&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;r &amp;lt; 2n&amp;lt;/math&amp;gt;. In that case, writing the snippets above as an expression:&lt;br /&gt;
:&amp;lt;math&amp;gt;a \,\bmod\, n = \begin{cases} r &amp;amp; \text{if } r&amp;lt;n \\ r-n &amp;amp; \text{otherwise} \end{cases}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The proof that &amp;lt;math&amp;gt;r &amp;lt; 2n&amp;lt;/math&amp;gt; follows:&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;math&amp;gt;a &amp;lt; 2 ^ k&amp;lt;/math&amp;gt;, then&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{a}{2 ^ k} \cdot (2 ^ k \mod n) &amp;lt; n&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since &amp;lt;math&amp;gt;n\cdot\frac{m a \mod 2^k}{2^k} &amp;lt; n&amp;lt;/math&amp;gt; regardless of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, it follows that&lt;br /&gt;
:&amp;lt;math&amp;gt; \frac{a\cdot (2 ^ k \mod n)}{2 ^ k} + n\cdot\frac{m a \mod 2^k}{2^k} &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt; a - \left(a - \frac{a\cdot (2 ^ k \mod n)}{2 ^ k}\right) + \frac{(n \cdot (m a \mod 2^k))}{2^k} &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt; a - \left(\left\lfloor\frac{2 ^ k}{n}\right\rfloor \cdot \frac{n a}{2 ^ k}\right) + \frac{(n \cdot (m a \mod 2^k))}{2^k} &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt; a - \frac{m n a}{2 ^ k} + \frac{(n \cdot (m a \mod 2^k))}{2^k} &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt; a - \left(\frac{m a - (m a \mod 2^k)}{2^k}\right)\cdot n &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt; a - \left\lfloor\frac{m a}{2 ^ k}\right\rfloor\cdot n &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
:&amp;lt;math&amp;gt;r &amp;lt; 2n&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Multi-word Barrett reduction ==&lt;br /&gt;
&lt;br /&gt;
Barrett's primary motivation for considering reduction was the implementation of [[RSA (cryptosystem)|RSA]], where the values in question will almost certainly exceed the size of a machine word. In this situation, Barrett provided an algorithm that approximates the single-word version above but for multi-word values. For details see section 14.3.3 of the Handbook of Applied Cryptography.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
[[Montgomery reduction]] is another similar algorithm.&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
&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>