On this page
Suppose you need 7^560 mod 561. Written out in full, 7^560 has 474 digits, and multiplying by 7 one step at a time takes 559 multiplications. Square and multiply gets the same remainder in 11 multiplications, and no number you handle is ever bigger than 561² = 314,721.
What is modular exponentiation, and why is naive powering hopeless?
Modular exponentiation means finding a^k mod n, the remainder when a^k is divided by n. The size of a^k is not the real problem, because you may reduce after every multiplication: (x × y) mod n equals ((x mod n) × (y mod n)) mod n. The problem is the count. Multiplying by a one step at a time needs k − 1 multiplications, which the Wikipedia article on modular exponentiation writes as O(e) multiplications for exponent e.
In RSA encryption the exponents are huge. NIST's SP 800-57 Part 1 Rev. 5 pairs an RSA modulus of 2048 bits with 112 bits of security, and no longer approves anything weaker for protecting federal information. A decryption exponent can be almost as long as the modulus, up to about 2^2048, a 617-digit number, so one-at-a-time powering could never finish. Square and multiply needs about 2048 squarings and at most 2048 more multiplications.
How does the square-and-multiply method work?
Write k in binary. Squaring doubles the exponent, so squaring a again and again gives a, a², a⁴, a⁸ and so on: a raised to every power of 2. Then a^k is the product of the powers whose binary digit in k is 1.
The Wikipedia article on exponentiation by squaring puts the cost at ⌊log₂ k⌋ squarings and at most ⌊log₂ k⌋ multiplications. The 1978 paper that introduced RSA, by Rivest, Shamir and Adleman, gives the procedure and the bound: computing M^e mod n needs at most 2·log₂(e) multiplications and 2·log₂(e) divisions, the divisions being the reductions mod n. The authors call it "exponentiation by repeated squaring and multiplication" and note that more efficient procedures are known.
Worked example: what is 7^560 mod 561?
First the binary digits: 560 = 512 + 32 + 16, so 560 = 1000110000 in binary. Now square, reducing mod 561 each time:
7^1 ≡ 7
7^2 = 49
7^4 = 49² = 2401 = 4 × 561 + 157, so 157
7^8 = 157² = 24649 = 43 × 561 + 526, so 526
7^16: 526 ≡ −35, and 35² = 1225 = 2 × 561 + 103, so 103
7^32 = 103² = 10609 = 18 × 561 + 511, so 511
7^64: 511 ≡ −50, and 50² = 2500 = 4 × 561 + 256, so 256
7^128 = 256² = 65536 = 116 × 561 + 460, so 460
7^256: 460 ≡ −101, and 101² = 10201 = 18 × 561 + 103, so 103
7^512 = 103² ≡ 511, as in line 6
Now multiply the three lines whose bits are 1: 7^560 = 7^512 × 7^32 × 7^16 ≡ 511 × 511 × 103. Since 511 ≡ −50, 511 × 511 ≡ 2500 ≡ 256, and 256 × 103 = 26368 = 47 × 561 + 1. So 7^560 ≡ 1 (mod 561), after 9 squarings and 2 multiplications.
That answer is a trap. 561 = 3 × 11 × 17 is not prime, yet it passes Fermat's test for base 7; our post on Carmichael numbers and the 561 trap in Fermat's test explains why.
Right-to-left or left-to-right: which should you use?
The table above follows the right-to-left method: you read the bits of k from the last digit, keep a running square, and multiply it into an answer whenever the bit is 1. You can find the bits as you go by halving k.
The left-to-right method, the one printed in the RSA paper, reads bits from the first digit. For each bit, square the running result, then multiply by a if the bit is 1. For 560 the running result goes 7, 49, 157, 526, then 103 × 7 ≡ 160 (that is 7^17), 160² ≡ 355 and 355 × 7 ≡ 241 (7^35), then four squarings: 298, 166, 67, 1.
Choose right-to-left when you want the bits as you go, without writing k in binary first.
Choose left-to-right when a is small, because every multiplication is by a itself, and you store one number instead of two.
Left-to-right also shows something the table hides: 7^280 ≡ 67, and 67² = 4489 = 8 × 561 + 1. A square root of 1 that is neither 1 nor −1 can only exist when n is composite, so this single line proves 561 is not prime.
How can you practise modular exponentiation?
Pick a small case, such as 3^45 mod 7, and write 45 in binary.
Build the squaring table, reducing after every step.
Multiply the rows whose bits are 1 and count your multiplications.
Redo it left to right; both answers must match.
Check it with Fermat's little theorem, which our explainer on Lagrange's theorem and Fermat derives, then try finding the order of each base, as in primitive roots modulo a prime.
If binary or the reduction step still feels shaky, the Fermi Concept Library has a lesson on every concept in mathematics and computing. When you want to test yourself, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning, so you can compare its lines with your own table.
Next time you see a giant power with a small modulus, write the exponent in binary first.



