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!
Modular exponentiation (also called powmod or modpow in programming) is the calculation of an integer power followed by a reduction modulo a positive integer $ n $.
Mathematically, it involves calculating $ a^b \mod n $, where $ a $ is the base, $ b $ is the exponent, and $ n $ is the modulo (a strictly positive integer).
Modular exponentiation is a fundamental tool in modern cryptography, particularly in public-key systems.
The calculation amounts to finding the remainder of the Euclidean division of $ a^b $ by $ n $.
A direct method is to calculate the full power and then take the remainder modulo $ n $.
Example: Calculate $ 9^{10} \mod 11 $ is calculated $ 9^{10} = 3486784401 $ then $ 3486784401 \mod 11 \equiv 1 $
However, this method quickly becomes impractical because the values of $ a^b $ grow exponentially with $ b $.
In practice, it is preferable to reduce modulo $ n $ at each step of the calculation using the property $ x \equiv y \mod n $, alors $ x \cdot z \equiv y \cdot z \mod n $
This property makes it possible to perform intermediate reductions and avoid the manipulation of gigantic numbers.
The word power indicates the name of the operation, and the word exponent indicates the operand.
Calculating the last $ x $ digits of a number in decimal writing is equivalent to performing a modulo $ 10^x $ calculation.
Example: $ 3^9 = 19683 $ and $ 3^9 \mod 100 = 83 $ (the last 2 digits)
This property is based on the fact that working modulo $ 10^x $ preserves exactly the last $ x $ digits in base 10.
The most commonly used algorithm is modular fast exponentiation, also called the square-and-multiply method. It is based on the binary representation of the exponent $ 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 exponentiation algorithm is the one that requires the fewest steps. The number of steps depends on the bit size of the exponent $ b $.
For a manual calculation, an efficient method is to:
— Decompose the exponent into powers of 2
— Successively calculate the squares modulo $ n $
— Multiply the useful powers (bits equal to $ 1 $)
In practice, for small values of $ a $, $ b $, and $ n $, calculating the power first and then the modulo (using Euclidean division) is more instinctive.
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. Any algorithm for the "Modular Exponentiation" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "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.) or any database download or API access for "Modular Exponentiation" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Modular Exponentiation" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Modular Exponentiation on dCode.fr [online website], retrieved on 2026-04-02,