Tool to compute modular power. Modular Exponentiation (or power modulo) is the result of the calculus a^b mod n. It is often used in informatics and cryptography.
Modular Exponentiation - dCode
Tag(s) : Arithmetics
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
Solver limited to integer solutions < 10000
Modular exponentiation (or powmod, or modpow) is a calculation on integers composed of a power followed by a modulo. This type of calculation is widely used in modern cryptography.
A direct method is to calculate the value of the power then to extract the modulo from it (the remainder in division by n).
Example: Computing $ 9^{10} \mod 11 $ it's calculating $ 9^{10} = 3486784401 $ then $ 3486784401 \mod 11 \equiv 1 $
In practice, the numbers generated by the powers are gigantic, and mathematicians and computer scientists use simplifications, especially fast exponentiation.
The word power indicates the name of the operation, and exponent to indicate the operand.
Calculating the last $ x $ digits of $ a^b $ is equivalent to calculating $ a^b \mod n $ with $ n = 10^x $ (the number $ 1 $ followed by $ x $ zeros)
Example: $ 3^9 = 19683 $ and $ 3^9 \mod 100 = 83 $ (the last 2 digits)
There are several algorithms, but the most efficient one, called (modular) fast exponentiation, uses a property on the binary writing of $ e $.
Writing $ e=\sum_{i=0}^{m-1}a_{i}2^{i} $ over $ m $ bits with $ a_i $ the binary values (0 or 1) in writing in base 2 of $ e $ (with $ a_{m-1} = 1 $)
Then $ b^e $ can be written $$ b^e = b^{\left( \sum_{i=0}^{n-1} a_i \cdot 2^i \right)} = \prod_{i=0}^{n-1} \left( b^{2^i} \right)^{a_i} $$
And so $$ b^e \mod n \equiv \prod_{i=0}^{n-1} \left( b^{2^i} \right)^{a_i} \mod n $$
Here is the implementation of fast modular exponentiation in pseudocode:// pseudocode
function powmod(base b, exponent e, modulus m) {
r = 1
b = b % m
if (b == 0) return 0
while (e > 0) {
if (e % 2) r = (r * b) % m
e = e >> 1
b = (b ** 2) % m
}
return r
}
In theory, the fast powmod algorithm (above) is also the one with the fewest steps. It needs $ m $ steps, with $ m $ the size in bits of the number $ b $ in binary.
In practice, for small values of $ a $, $ b $ and $ n $ calculating the power then the modulo (with Euclidean division) is more instinctive.
This calculation is known as the discrete logarithm problem. Some solutions can be found by brute force but there is no trivial general solution.
Calculus uses exponent and modulus that are generally defined over the natural number domain set N. It is possible to use rational numbers but it is not handled here.
dCode retains ownership of the "Modular Exponentiation" source code. Except explicit open source licence (indicated Creative Commons / free), the "Modular Exponentiation" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "Modular Exponentiation" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "Modular Exponentiation" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.
The copy-paste of the page "Modular Exponentiation" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
Modular Exponentiation on dCode.fr [online website], retrieved on 2024-11-21,