Modular exponentiation
Modular exponentiation is exponentiation performed over a modulus.[src] It is useful in computer science, especially in the field of public-key cryptography, where it is used in both Diffie–Hellman key exchange and RSA public/private keys.[src] Modular exponentiation is the remainder when an integer (the base) is raised to the power (the exponent), and divided by a positive integer (the modulus); that is, .[src] From the definition of division, it follows that .[src] For example, given , and , dividing by leaves a remainder of .[src] When and are relatively prime, one can also allow the exponent to be negative by finding the multiplicative inverse of modulo (for instance by using extended Euclidean algorithm).[src] More precisely: : , where and .[src] Modular exponentiation is efficient to compute, even for very large integers.[src] On the other hand, computing the modular discrete logarithm – that is, finding the exponent when given , , and – is believed to be difficult.[src] This one-way function behavior makes modular exponentiation a candidate for use in cryptographic algorithms.[src]
Modular exponentiation is exponentiation performed over a modulus.[src] It is useful in computer science, especially in the field of public-key cryptography, where it is used in both Diffie–Hellman key exchange and RSA public/private keys.[src] Modular exponentiation is the remainder when an integer (the base) is raised to the power (the exponent), and divided by a positive integer (the modulus); that is, .[src] From the definition of division, it follows that .[src] For example, given , and , dividing by leaves a remainder of .[src] When and are relatively prime, one can also allow the exponent to be negative by finding the multiplicative inverse of modulo (for instance by using extended Euclidean algorithm).[src] More precisely: , where and .[src] Modular exponentiation is efficient to compute, even for very large integers.[src] On the other hand, computing the modular discrete logarithm – that is, finding the exponent when given , , and – is believed to be difficult.[src] This one-way function behavior makes modular exponentiation a candidate for use in cryptographic algorithms.[src] if modulus = 1 then return 0 c := 1 for e_prime = 0 to exponent-1 do c := (c * base) mod modulus return c e = \sum_{i=0}^{n-1} a_i 2^i In such notation, the length of is bits.[src] can take the value 0 or 1 for any such that .[src] By definition, .[src] The value can then be written as: b^e = b^{\left( \sum_{i=0}^{n-1} a_i 2^i \right)} = \prod_{i=0}^{n-1} b^{a_i 2^i} The solution is therefore: c \equiv \prod_{i=0}^{n-1} b^{a_i 2^i} \pmod m if modulus = 1 then return 0 Assert :: (modulus - 1) * (modulus - 1) does not overflow base result := 1 base := base mod modulus while exponent > 0 do if (exponent mod 2 == 1) then result := (result * base) mod modulus exponent := exponent >> 1 base := (base * base) mod modulus return result Note that upon entering the loop for the first time, the code variable base is equivalent to .[src] However, the repeated squaring in the third line of code ensures that at the completion of every loop, the variable base is equivalent to , where is the number of times the loop has been iterated.[src] (This makes the next working bit of the binary exponent exponent , where the least-significant bit is exponent 0 ).[src] The first line of code simply carries out the multiplication in \prod_{i=0}^{n-1} b^{a_i 2^i}\pmod m .[src] If is zero, no code executes since this effectively multiplies the running total by one.[src] If instead is one, the variable base (containing the value of the original base) is simply multiplied in.[src] In this example, the base is raised to the exponent .[src] The exponent is 1101 in binary.[src] There are four binary digits, so the loop executes four times, with values , and .[src] First, initialize the result R to 1 and preserve the value of in the variable : R \leftarrow 1 \, ( = b^0) \text{ and } x \leftarrow b .[src] Step 1) bit 1 is 1, so set R \leftarrow R \cdot x \text{ }(= b^1) ; set x \leftarrow x^2 \text{ }(= b^2) .[src] Step 2) bit 2 is 0, so do not reset ; set x \leftarrow x^2 \text{ }(= b^4) .[src] Step 3) bit 3 is 1, so set R \leftarrow R \cdot x \text{ }(= b^5) ; set x \leftarrow x^2 \text{ }(= b^8) .[src] Step 4) bit 4 is 1, so set R \leftarrow R \cdot x \text{ }(= b^{13}) ; This is the last step so we don't need to square .[src] We are done: is now b^{13} .[src] Here is the above calculation, where we compute to the power , performed modulo 497.[src] Initialize: R \leftarrow 1 \, ( = b^0) and x \leftarrow b = 4 .[src] Step 1) bit 1 is 1, so set R \leftarrow R \cdot 4 \equiv 4 \pmod{497} ; set x \leftarrow x^2 \text{ }(= b^2) \equiv 4^2 \equiv 16 \pmod{497} .[src] Step 2) bit 2 is 0, so do not reset ; set x \leftarrow x^2 \text{ }(= b^4) \equiv 16^2\equiv 256 \pmod{497} .[src] Step 3) bit 3 is 1, so set R \leftarrow R \cdot x \text{ }(= b^5) \equiv 4 \cdot 256 \equiv 30 \pmod{497} ; set x \leftarrow x^2 \text{ }(= b^8) \equiv 256^2 \equiv 429 \pmod{497} .[src] Step 4) bit 4 is 1, so set R \leftarrow R \cdot x \text{ }(= b^{13}) \equiv 30 \cdot 429 \equiv 445 \pmod{497} ; We are done: is now 4^{13} \equiv 445 \pmod{497} , the same result obtained in the previous algorithms.[src] The running time of this algorithm is exponent .[src] When working with large values of exponent , this offers a substantial speed benefit over the previous two algorithms, whose time is exponent .[src] For example, if the exponent was 2 20 = 1048576, this algorithm would have 20 steps instead of 1048576 steps.[src] if m == 1 then return 0 end local r = 1 b = b % m while e > 0 do if e % 2 == 1 then r = (r*b) % m end b = (b*b) % m e = e >> 1 --use 'e = math.floor(e / 2)' on Lua 5.2 or older end return r end Initialize the result to 1: r \leftarrow 1 \, ( = b^0) .[src] Step 1) r \leftarrow r^2 \, ( = b^0) ; bit 1 = 1, so compute r \leftarrow r \cdot b \,( = b^1) ; Step 2) r \leftarrow r^2 \, ( = b^2) ; bit 2 = 1, so compute r \leftarrow r \cdot b \, ( = b^3) ; Step 3) r \leftarrow r^2 \, ( = b^6) ; bit 3 = 0, so we are done with this step; Step 4) r \leftarrow r^2 \, ( = b^{12}) ; bit 4 = 1, so compute r \leftarrow r \cdot b \, ( = b^{13}) .[src] A recursive algorithm for ModExp(A, b, c) = , where is a square matrix.[src] function Matrix_ModExp(Matrix A, int b, int c) is if b == 0 then return I // The identity matrix if (b mod 2 == 1) then return (A * Matrix_ModExp(A, b - 1, c)) mod c Matrix D := Matrix_ModExp(A, b / 2, c) return (D * D) mod c .NET Framework's BigInteger class has a ModPow() method to perform modular exponentiation Java's java.math.BigInteger class has a method to perform modular exponentiation MATLAB's powermod function from Symbolic Math Toolbox Wolfram Language has the PowerMod function Perl's Math::BigInt module has a bmodpow() method to perform modular exponentiation Raku has a built-in routine expmod .[src] Go's big.Int type contains an Exp() (exponentiation) method [http://golang.org/pkg/big/#Int.Exp whose third parameter, if non-nil, is the modulus PHP's BC Math library has a bcpowmod() function to perform modular exponentiation The GNU Multiple Precision Arithmetic Library (GMP) library contains a mpz_powm() function [http://gmplib.org/manual/Integer-Exponentiation.html to perform modular exponentiation Custom Function @PowerMod() for FileMaker Pro (with 1024-bit RSA encryption example) Ruby's openssl package has the OpenSSL::BN#mod_exp method [https://docs.ruby-lang.org/en/master/OpenSSL/BN.html#method-i-mod_exp] to perform modular exponentiation.[src]
52 sentences · 52 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Modular exponentiation." encyclopegia.org, 2026, https://encyclopegia.org/wiki/modular-exponentiation. Accessed 04 December 2026.
Chicago
"Modular exponentiation." encyclopegia.org. Accessed December 4, 2026. https://encyclopegia.org/wiki/modular-exponentiation.
APA
encyclopegia.org. (2026). Modular exponentiation. Retrieved December 4, 2026, from https://encyclopegia.org/wiki/modular-exponentiation
Accessed December 4, 2026 · CC BY-SA 4.0 · citation page · JSON