Search for a tool
Modular Exponentiation

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.

Results

Modular Exponentiation -

Tag(s) : Arithmetics

Share
dCode and more

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!

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Feedback and suggestions are welcome so that dCode offers the best 'Modular Exponentiation' tool for free! Thank you!

# Modular Exponentiation

## a^b mod n Solver

Solver limited to integer solutions < 10000

### What is modular exponentiation? (Definition)

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.

### How to raise to power b modulo n?

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.

### How to calculate the last digits of an exponentiation?

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)

### What is the algorithm of powmod?

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:// pseudocodefunction 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}

### How to calculate a^b mod n by hand?

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.

### How to solve for exponent with base and modulo?

This calculation is known as the discrete logarithm problem. Some solutions can be found by brute force but there is no trivial general solution.

### Why is modular exponentiation limited to integers?

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.

## Source code

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.

## Cite dCode

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-09-10, https://www.dcode.fr/modular-exponentiation

## Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!