Modular exponentiation
Modular exponentiation is exponentiation performed over a modulus. 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. 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, . From the definition of division, it follows that . For example, given , and , dividing by leaves a remainder of . 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). More... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
Modular exponentiation is exponentiation performed over a modulus. 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. 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, . From the definition of division, it follows that . For example, given , and , dividing by leaves a remainder of . 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). More precisely: , where and . Modular exponentiation is efficient to compute, even for very large integers. On the other hand, computing the modular discrete logarithm – that is, finding the exponent when given , , and – is believed to be difficult. This one-way function behavior makes modular exponentiation a candidate for use in cryptographic algorithms. 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. can take the value 0 or 1 for any such that . By definition, . 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 . 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. (This makes the next working bit of the binary exponent exponent , where the least-significant bit is exponent 0 ). The first line of code simply carries out the multiplication in \prod_{i=0}^{n-1} b^{a_i 2^i}\pmod m . If is zero, no code executes since this effectively multiplies the running total by one. If instead is one, the variable base (containing the value of the original base) is simply multiplied in. In this example, the base is raised to the exponent . The exponent is 1101 in binary. There are four binary digits, so the loop executes four times, with values , and . 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 . Step 1) bit 1 is 1, so set R \leftarrow R \cdot x \text{ }(= b^1) ; set x \leftarrow x^2 \text{ }(= b^2) . Step 2) bit 2 is 0, so do not reset ; set x \leftarrow x^2 \text{ }(= b^4) . Step 3) bit 3 is 1, so set R \leftarrow R \cdot x \text{ }(= b^5) ; set x \leftarrow x^2 \text{ }(= b^8) . 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 . We are done: is now b^{13} . Here is the above calculation, where we compute to the power , performed modulo 497. Initialize: R \leftarrow 1 \, ( = b^0) and x \leftarrow b = 4 . 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} . 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} . 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} . 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. The running time of this algorithm is exponent . When working with large values of exponent , this offers a substantial speed benefit over the previous two algorithms, whose time is exponent . For example, if the exponent was 2 20 = 1048576, this algorithm would have 20 steps instead of 1048576 steps. 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) . 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}) . A recursive algorithm for ModExp(A, b, c) = , where is a square matrix. 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 . 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.
Cite this
"Modular exponentiation". 1771773523. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1339838574 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy