Friday, December 26, 2025

Short intro to Modular exponentiation

In the world of computer science, especially in cryptography, we often need to calculate extremely large numbers. If you have ever wondered how your browser stays secure or how RSA encryption works, you have already encountered modular exponentiation.

In simple terms, modular exponentiation is the process of computing the remainder when an integer bb (the base) is raised to the power ee (the exponent) and divided by a positive integer mm (the modulus). Mathematically, this is written as

c=be(modm)

At first glance, this looks like a problem for a standard power function. However, consider trying to compute

5100(mod7)5^{100} \pmod{7}

Standard power functions face two major obstacles. First, there is the issue of overflow. The number

51005^{100}

has more than 7070 digits, and most standard programming data types, such as a 6464-bit integer, will overflow or lose accuracy long before reaching this size. Second, computing the full value of such a massive exponent just to extract a small remainder is an enormous waste of computation. This is where binary exponentiation, also known as the square-and-multiply method, becomes essential.

Instead of multiplying the base bb exactly ee times, we exploit properties of exponents. The key identities are

If e is even, then be=(b2)e/2\text{If } e \text{ is even, then } b^e = (b^2)^{e/2}

and

If e is odd, then be=b(b2)(e1)/2\text{If } e \text{ is odd, then } b^e = b \cdot (b^2)^{(e-1)/2}

By repeatedly squaring the base and reducing modulo mm at each step, we reduce the number of operations from linear time

O(e)

to logarithmic time

O(loge)

This makes it feasible to compute modular powers even when the exponent ee has hundreds or thousands of digits.

Let us work through a concrete example:

313(mod7)

First, express the exponent in binary. The number 13 written in base 2 is

110121101_2

which corresponds to the decomposition

13=8+4+113 = 8 + 4 + 1

This allows us to rewrite the exponentiation as

313=3834313^{13} = 3^8 \cdot 3^4 \cdot 3^1

Next, we compute successive squares, taking the modulo 7 at every step:

31(mod7)=33^1 \pmod{7} = 3
32(mod7)=9(mod7)=23^2 \pmod{7} = 9 \pmod{7} = 2
34(mod7)=22(mod7)=43^4 \pmod{7} = 2^2 \pmod{7} = 4
38(mod7)=42(mod7)=16(mod7)=23^8 \pmod{7} = 4^2 \pmod{7} = 16 \pmod{7} = 2

Now we multiply the relevant terms:

383431243=24(mod7)3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \cdot 4 \cdot 3 = 24 \pmod{7}

Finally, we compute the remainder:

24(mod7)=324 \pmod{7} = 3

So the final result is

313(mod7)=33^{13} \pmod{7} = 3

By reducing modulo mm at every step, the intermediate values remain small and never grow large enough to overflow memory or slow down computation.

Modular exponentiation forms the backbone of modern digital security. In the Diffie–Hellman key exchange, it enables two parties to establish a shared secret over an insecure channel. In RSA encryption, security relies on the fact that modular exponentiation is computationally easy, while reversing the process (computing discrete logarithms) is believed to be computationally infeasible.

No comments:

Post a Comment

A Course Correction

  I want to record a small but important update about the direction of this blog. Over the past few weeks, I’ve been working through Freder...