<?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=Modular_exponentiation</id>
	<title>Modular exponentiation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Modular_exponentiation"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Modular_exponentiation&amp;action=history"/>
	<updated>2026-05-15T09:08:38Z</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=Modular_exponentiation&amp;diff=5946&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;'''Modular}} mod ''m''}} of the original base) is simply multiplied in.  In this example, the base &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; is raised to the exponent &amp;lt;tt&amp;gt;e = 13&amp;lt;/tt&amp;gt;...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Modular_exponentiation&amp;diff=5946&amp;oldid=prev"/>
		<updated>2019-06-16T12:22:47Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Modular}} mod &amp;#039;&amp;#039;m&amp;#039;&amp;#039;}} of the original base) is simply multiplied in.  In this example, the base &amp;lt;tt&amp;gt;b&amp;lt;/tt&amp;gt; is raised to the exponent &amp;lt;tt&amp;gt;e = 13&amp;lt;/tt&amp;gt;...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;'''Modular}} mod ''m''}} of the original base) is simply multiplied in.&lt;br /&gt;
&lt;br /&gt;
In this example, the base &amp;amp;lt;tt&amp;amp;gt;b&amp;amp;lt;/tt&amp;amp;gt; is raised to the exponent &amp;amp;lt;tt&amp;amp;gt;e = 13&amp;amp;lt;/tt&amp;amp;gt;.&lt;br /&gt;
The exponent is 1101 in binary. There are four binary digits, so the loop executes four times.&lt;br /&gt;
The bits in right-to-left order are 1, 0, 1, 1.&lt;br /&gt;
&lt;br /&gt;
First, initialize the result &amp;amp;lt;math&amp;amp;gt;R&amp;amp;lt;/math&amp;amp;gt; to 1 and preserve the value of &amp;amp;lt;tt&amp;amp;gt;b&amp;amp;lt;/tt&amp;amp;gt; in the variable &amp;amp;lt;tt&amp;amp;gt;x&amp;amp;lt;/tt&amp;amp;gt;:&lt;br /&gt;
: &amp;amp;lt;math&amp;amp;gt; R \leftarrow 1 \, ( = b^0) \text{ and } x \leftarrow b &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 1) bit 1 is 1, so set &amp;amp;lt;math&amp;amp;gt; R \leftarrow R \cdot x \text{ }(= b^1) &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^2) &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 2) bit 2 is 0, so do not reset &amp;amp;lt;tt&amp;amp;gt;R&amp;amp;lt;/tt&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^4) &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 3) bit 3 is 1, so set &amp;amp;lt;math&amp;amp;gt; R \leftarrow R \cdot x \text{ }(= b^5) &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^8) &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 4) bit 4 is 1, so set &amp;amp;lt;math&amp;amp;gt; R \leftarrow R \cdot x \text{ }(= b^{13}) &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
:: This is the last step so we don't need to square &amp;amp;lt;tt&amp;amp;gt;x&amp;amp;lt;/tt&amp;amp;gt;.&lt;br /&gt;
We are done: &amp;amp;lt;tt&amp;amp;gt;R&amp;amp;lt;/tt&amp;amp;gt; is now &amp;amp;lt;math&amp;amp;gt;b^{13}&amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Here is the above calculation, where we compute &amp;amp;lt;tt&amp;amp;gt;b = 4&amp;amp;lt;/tt&amp;amp;gt; to the power &amp;amp;lt;tt&amp;amp;gt;e = 13&amp;amp;lt;/tt&amp;amp;gt;, performed modulo 497.&lt;br /&gt;
&lt;br /&gt;
Initialize:&lt;br /&gt;
: &amp;amp;lt;math&amp;amp;gt; R \leftarrow 1 \, ( = b^0) &amp;amp;lt;/math&amp;amp;gt; and &amp;amp;lt;math&amp;amp;gt; x \leftarrow b = 4 &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 1) bit 1 is 1, so set &amp;amp;lt;math&amp;amp;gt;R \leftarrow R \cdot 4 \pmod {497} \equiv 4 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^2) \equiv 4^2 \equiv 16 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 2) bit 2 is 0, so do not reset &amp;amp;lt;tt&amp;amp;gt;R&amp;amp;lt;/tt&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^4) \equiv 16^2 \pmod{497} \equiv 256 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 3) bit 3 is 1, so set &amp;amp;lt;math&amp;amp;gt; R \leftarrow R \cdot x \text{ }(= b^5) \equiv 4 \cdot 256 \pmod{497} \equiv 30 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
:: set &amp;amp;lt;math&amp;amp;gt; x \leftarrow x^2 \text{ }(= b^8) \equiv 256^2 \pmod{497} \equiv 429 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 4) bit 4 is 1, so set &amp;amp;lt;math&amp;amp;gt; R \leftarrow R \cdot x \text{ }(= b^{13}) \equiv 30 \cdot 429 \pmod{497} \equiv 445 \pmod{497} &amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
&lt;br /&gt;
We are done: &amp;amp;lt;tt&amp;amp;gt;R&amp;amp;lt;/tt&amp;amp;gt; is now &amp;amp;lt;math&amp;amp;gt;4^{13} \pmod{497} \equiv 445 \pmod{497}&amp;amp;lt;/math&amp;amp;gt;, the same result obtained in the previous algorithms.&lt;br /&gt;
&lt;br /&gt;
The running time of this algorithm is &amp;amp;lt;tt&amp;amp;gt;exponent&amp;amp;lt;/tt&amp;amp;gt;. When working with large values of &amp;amp;lt;tt&amp;amp;gt;exponent&amp;amp;lt;/tt&amp;amp;gt;, this offers a substantial speed benefit over the previous two algorithms, whose time is &amp;amp;lt;tt&amp;amp;gt;exponent&amp;amp;lt;/tt&amp;amp;gt;. For example, if the exponent was 2&amp;amp;lt;sup&amp;amp;gt;20&amp;amp;lt;/sup&amp;amp;gt; = 1048576, this algorithm would have 20 steps instead of 1048576 steps.&lt;br /&gt;
&lt;br /&gt;
==Left-to-right binary method==&lt;br /&gt;
We can also use the bits of the exponent in left to right order. In practice, we would usually want the result modulo some modulus &amp;amp;lt;tt&amp;amp;gt;m&amp;amp;lt;/tt&amp;amp;gt;. In that case, we would reduce each multiplication result &amp;amp;lt;tt&amp;amp;gt;(mod m)&amp;amp;lt;/tt&amp;amp;gt; before proceeding. For simplicity, the modulus calculation is omitted here. This example shows how to compute &amp;amp;lt;math&amp;amp;gt;b^{13}&amp;amp;lt;/math&amp;amp;gt; using left to right binary exponentiation. The exponent is 1101 in binary; there are 4 bits, so there are 4 iterations.&lt;br /&gt;
&lt;br /&gt;
Initialize the result to 1: &amp;amp;lt;math&amp;amp;gt;r \leftarrow 1 \, ( = b^0)&amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
: Step 1) &amp;amp;lt;math&amp;amp;gt;r \leftarrow r^2 \, ( = b^0)&amp;amp;lt;/math&amp;amp;gt;; bit 1 = 1, so compute &amp;amp;lt;math&amp;amp;gt;r \leftarrow r \cdot b \,( = b^1)&amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
: Step 2) &amp;amp;lt;math&amp;amp;gt;r \leftarrow r^2 \, ( = b^2)&amp;amp;lt;/math&amp;amp;gt;; bit 2 = 1, so compute &amp;amp;lt;math&amp;amp;gt;r \leftarrow r \cdot b \, ( = b^3)&amp;amp;lt;/math&amp;amp;gt;;&lt;br /&gt;
: Step 3) &amp;amp;lt;math&amp;amp;gt;r \leftarrow r^2 \, ( = b^6)&amp;amp;lt;/math&amp;amp;gt;; bit 3 = 0, so we are done with this step;&lt;br /&gt;
: Step 4) &amp;amp;lt;math&amp;amp;gt;r \leftarrow r^2 \, ( = b^{12})&amp;amp;lt;/math&amp;amp;gt;; bit 4 = 1, so compute &amp;amp;lt;math&amp;amp;gt;r \leftarrow r \cdot b \, ( = b^{13})&amp;amp;lt;/math&amp;amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Minimum multiplications===&lt;br /&gt;
In ''The Art of Computer Programming, Vol. 2, Seminumerical Algorithms'', page 463, Professor Knuth notes that contrary to some assertions, this method does ''not'' always give the minimum possible number of multiplications. The smallest counterexample is for a power of 15, when the binary method needs six multiplications, yet forming ''y = x&amp;amp;lt;sup&amp;amp;gt;3&amp;amp;lt;/sup&amp;amp;gt;'' in two multiplications and then ''x&amp;amp;lt;sup&amp;amp;gt;15&amp;amp;lt;/sup&amp;amp;gt; = y&amp;amp;lt;sup&amp;amp;gt;5&amp;amp;lt;/sup&amp;amp;gt;'' in three more, achieves the desired result with only five multiplications. However, many pages follow describing how such sequences might be contrived in general.&lt;br /&gt;
&lt;br /&gt;
==Matrices==&lt;br /&gt;
The [[Fibonacci numbers]] and [[Perrin numbers]] modulo can be computed efficiently by computing for a certain and a certain matrix . The above methods adapt easily to this application. This can be used for [[Fibonacci number#Primality testing|primality testing]] of large numbers , for example.&lt;br /&gt;
&lt;br /&gt;
===Pseudocode===&lt;br /&gt;
A recursive algorithm for &amp;amp;lt;tt&amp;amp;gt;ModExp(A, b, c)&amp;amp;lt;/tt&amp;amp;gt; = , where is a square matrix.&lt;br /&gt;
 '''function''' Matrix_ModExp(Matrix A, int b, int c)&lt;br /&gt;
 '''if''' (b == 0):&lt;br /&gt;
 '''return''' I // The identity matrix&lt;br /&gt;
 '''if''' (b '''mod''' 2 == 1):&lt;br /&gt;
 '''return''' (A * Matrix_ModExp(A, b - 1, c)) '''mod''' c &lt;br /&gt;
 Matrix D := Matrix_ModExp(A, b / 2, c)&lt;br /&gt;
 '''return''' (D * D) '''mod''' c&lt;br /&gt;
&lt;br /&gt;
==Finite cyclic groups==&lt;br /&gt;
[[Diffie–Hellman key exchange]] uses exponentiation in finite cyclic groups. The above methods for modular matrix exponentiation clearly extend to this context. The modular matrix multiplication is simply replaced everywhere by the group multiplication .&lt;br /&gt;
&lt;br /&gt;
==Reversible and quantum modular exponentiation==&lt;br /&gt;
&lt;br /&gt;
In [[quantum computing]], modular exponentiation appears as the bottleneck of [[Shor's algorithm]], where it must be computed by a circuit consisting of [[reversible computing|reversible gates]], which can be further broken down into [[quantum gate]]s appropriate for a specific physical device. Furthermore, in Shor's algorithm it is possible to know the base and the modulus of exponentiation at every call, which enables various circuit optimizations.&lt;br /&gt;
&lt;br /&gt;
==In programming languages==&lt;br /&gt;
Because modular exponentiation is an important operation in computer science, and there are efficient algorithms (see above) that are much faster than simply exponentiating and then taking the remainder, many programming languages and arbitrary-precision integer libraries have a dedicated function to perform modular exponentiation:&lt;br /&gt;
* [[Python (programming language)|Python]]'s built-in &amp;amp;lt;tt&amp;amp;gt;pow()&amp;amp;lt;/tt&amp;amp;gt; (exponentiation) function [https://docs.python.org/library/functions.html#pow] takes an optional third argument which is the number to modulo by&lt;br /&gt;
* [[.NET Framework]]'s &amp;amp;lt;tt&amp;amp;gt;BigInteger&amp;amp;lt;/tt&amp;amp;gt; class has a &amp;amp;lt;tt&amp;amp;gt;[http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.modpow%28v=vs.100%29.aspx#pow ModPow()]&amp;amp;lt;/tt&amp;amp;gt; method to perform modular exponentiation&lt;br /&gt;
* [[Java (programming language)|Java]]'s &amp;amp;lt;tt&amp;amp;gt;java.math.BigInteger&amp;amp;lt;/tt&amp;amp;gt; class has a method to perform modular exponentiation&lt;br /&gt;
* [[Perl]]'s &amp;amp;lt;tt&amp;amp;gt;Math::BigInt&amp;amp;lt;/tt&amp;amp;gt; module has a &amp;amp;lt;tt&amp;amp;gt;bmodpow()&amp;amp;lt;/tt&amp;amp;gt; method [http://perldoc.perl.org/Math/BigInt.html#bmodpow%28%29] to perform modular exponentiation&lt;br /&gt;
* [[Perl 6]] has a built-in routine &amp;amp;lt;tt&amp;amp;gt;expmod&amp;amp;lt;/tt&amp;amp;gt;.&lt;br /&gt;
* [[Go (programming language)|Go]]'s &amp;amp;lt;tt&amp;amp;gt;big.Int&amp;amp;lt;/tt&amp;amp;gt; type contains an &amp;amp;lt;tt&amp;amp;gt;Exp()&amp;amp;lt;/tt&amp;amp;gt; (exponentiation) method [http://golang.org/pkg/big/#Int.Exp] whose third parameter, if non-nil, is the number to modulo by&lt;br /&gt;
* [[PHP]]'s BC Math library has a &amp;amp;lt;tt&amp;amp;gt;bcpowmod()&amp;amp;lt;/tt&amp;amp;gt; function [http://www.php.net/manual/en/function.bcpowmod.php] to perform modular exponentiation&lt;br /&gt;
* The [[GNU Multiple Precision Arithmetic Library]] (GMP) library contains a &amp;amp;lt;tt&amp;amp;gt;mpz_powm()&amp;amp;lt;/tt&amp;amp;gt; function [http://gmplib.org/manual/Integer-Exponentiation.html] to perform modular exponentiation&lt;br /&gt;
* Custom Function &amp;amp;lt;tt&amp;amp;gt;[http://www.briandunning.com/cf/1482 @PowerMod()]&amp;amp;lt;/tt&amp;amp;gt; for [[FileMaker|FileMaker Pro]] (with 1024-bit [[RSA (algorithm)|RSA]] encryption example)&lt;br /&gt;
* [[Ruby (programming language)|Ruby]]'s &amp;amp;lt;tt&amp;amp;gt;openssl&amp;amp;lt;/tt&amp;amp;gt; package has the &amp;amp;lt;tt&amp;amp;gt;OpenSSL::BN#mod_exp&amp;amp;lt;/tt&amp;amp;gt; method [http://ruby-doc.org/stdlib-trunk/libdoc/openssl/rdoc/OpenSSL/BN.html#method-i-mod_exp] to perform modular exponentiation.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Montgomery reduction]], for calculating the remainder when the modulus is very large.&lt;br /&gt;
* [[Kochanski multiplication]], serializable method for calculating the remainder when the modulus is very large&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
&lt;br /&gt;
[http://wikipedia.org/ http://wikipedia.org/]&lt;br /&gt;
[[Category:Cryptographic algorithms]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>