Discrete MathematicsModular Arithmetic and Cryptography5 of 6
Modular Arithmetic and Cryptography · 5
Fermat's Little Theorem, Euler's Theorem and Fast Exponentiation
We know how to reduce numbers modulo n, and now we need a way to compute huge powers like 31000(mod10) without multiplying a thousand times in a row.
1
Fast Exponentiation
To find a remainder like 313(mod10) (the last digit of 313), we can double the exponent at each step by repeatedly squaring, rather than multiplying by 3 one step at a time.
Fast Exponentiation
Text solution
Break the exponent into powers of 2Write 13 in binary to split 313 into a product of powers of 3 whose exponents are powers of 2.
13⟹313=11012=8+4+1=38⋅34⋅31
Square repeatedly modulo 10Double the exponent on each line by squaring the previous remainder and reducing modulo 10.
Multiply the matching piecesSubstitute the reduced remainders for 38,34, and 31 to find the remainder of 313.
313=38⋅34⋅31≡1⋅1⋅3=3(mod10)
So the last digit of 313 is 3.
Every exponent k breaks into a sum of powers of 2 in binary, and multiplying the matching rows from the squaring table modulo n gives 3k(modn) in just a few steps:
Because every exponent breaks into a sum of powers of two, you can build any power of three up to the 63rd just by multiplying the matching rows from a six-row squaring table.
2
Fermat's Little Theorem
In the squaring table for 313(mod10), the remainders hit 1 at 34≡1(mod10) and started repeating. When the modulus is a prime p, we can predict when the powers hit 1 ahead of time: the powers of any integer a not divisible by p are guaranteed to equal 1 at ap−1≡1(modp).
Fermat's Little Theorem
For every non-zero remainder a modulo a prime p, multiplying the p−1 non-zero remainders by a gives back the same remainders in some order, with no 0 and no repeats, so their product cancels on both sides:
Interactive c2
Cancelling the product 1⋅2⋯(p−1) from both sides leaves p−1 copies of a equal to 1 modulo p:
(1⋅2⋯(p−1))⋅ap−1≡1⋅2⋯(p−1)(modp)⟹Fermat’s Little Theoremap−1≡1(modp)(gcd(a,p)=1)
Keep in mind that the powers are guaranteed to equal 1 at p−1, even if they hit 1 earlier: modulo 7,23=8≡1(mod7) hits 1 after just 3 steps, then repeats that 3-step cycle to land back on 1 at 26≡1(mod7).
Let's use 36≡1(mod7) to shrink the exponent in 3100(mod7).
Text solution
Find the exponent that gives 1 modulo 7Since 7 is prime and gcd(3,7)=1, Fermat's Little Theorem guarantees that 36≡1(mod7).
37−1=36≡1(mod7)
Divide the exponent by 6Write 100 as a multiple of 6 plus a remainder so every 36 turns into 1.
100⟹3100=6⋅16+4=(36)16⋅34≡116⋅34=34(mod7)
Reduce the remaining powerCompute 34 and find its remainder modulo 7.
34=81=7⋅11+4≡4(mod7)
So 3100 leaves a remainder of 4 when divided by 7.
3
Proving Divisibility for All Integers
The congruence ap−1≡1(modp) leaves out multiples of p, where 0p−1≡0≡1(modp). When we need to prove a divisibility statement for every integer n at once, we multiply both sides by a so that multiples of p work too.
Proving Divisibility for All Integers
n5−n≡?(mod30)
for every integer n
Multiplying ap−1≡1(modp) by a gives a statement with no exceptions, since 0p≡0(modp) is also true:
ap≡a(modp)for every integer a
Combined with the Chinese Remainder Theorem (which says that pairwise coprime numbers divide a number together if and only if each one divides it separately), this form proves divisibility across all integers without splitting into cases.
Text solution
Factor the modulus into distinct primesSince 30=2⋅3⋅5 is a product of pairwise coprime primes, 30 divides n5−n if and only if 2,3, and 5 each divide n5−n.
30=2⋅3⋅5
Reduce n5−n modulo each prime using np≡n(modp)Replace n5 with n modulo 5,n3 with n modulo 3, and n2 with n modulo 2.
Mod 5:n5−nMod 3:n5−nMod 2:n5−n≡n−n=0(mod5)=n3⋅n2−n≡n⋅n2−n=n3−n≡n−n=0(mod3)≡n−n=0(mod2)
Because n5−n is a multiple of 2,3, and 5 for every integer n, it is divisible by 30 for every integer n.
4
Fermat's Primality Test and Pseudoprimes
Suppose we are given a huge odd number n and want to know whether n is prime without factoring it. We can use Fermat's Little Theorem as a test by computing an−1(modn) with fast exponentiation.
Fermat's Primality Test and Pseudoprimes
Let's compare how the test behaves on n=15 and n=341 using base a=2.
Text solution
Test n=15 by computing 214(mod15)Square 2 repeatedly modulo 15 and multiply the rows for 14=8+4+2.
21222428=2,=4,=16≡1,≡1(mod15)
214=28⋅24⋅22≡1⋅1⋅4=4≡1(mod15)
Test n=341 by computing 2340(mod341)Use 210=1024=341⋅3+1≡1(mod341) to evaluate 2340.
2340=(210)34≡134=1(mod341)
Since 214≡1(mod15), the contrapositive of Fermat's Little Theorem proves 15 is composite without finding any of its factors. Since 2340≡1(mod341), the test is inconclusive for 341. In fact, 341=11⋅31 is a composite number that passes the test for base 2 (called a pseudoprime to base 2), though base 3 catches it because 3340≡56≡1(mod341). Rare composite numbers called Carmichael numbers, like 561, pass the test for every base coprime to n.
5
Euler's Theorem and Euler's Totient Function
Now suppose we already know the modulus n is composite, such as n=10 or n=100, and we want to shrink a huge exponent. We can't cancel the product of all the remainders 1,…,n−1 because numbers that share a factor with n have no modular inverse, so we keep only the remainders that are coprime to n.
Euler's Theorem and Euler's Totient Function
Crossing out the multiples of each prime factor p of n removes p1 of the list at each step, leaving the ϕ(n) coprime remainders. Multiplying these ϕ(n) remainders by a coprime base a just rearranges them:
Crossing out the multiples of each prime factor of 12 shrinks the list by that fraction and leaves the four coprime remainders whose product cancels.
For any modulus n>1, we find ϕ(n) from the distinct prime factors p of n, and use aϕ(n)≡1(modn) whenever gcd(a,n)=1:
When n=p is prime, no number from 1 to p−1 shares a factor with p, so ϕ(p)=p(1−p1)=p−1 and Euler's Theorem becomes Fermat's Little Theorem.
Text solution
Find the last digit of 31000 using ϕ(10)The remainders coprime to 10 are 1,3,7,9, so ϕ(10)=4, and since gcd(3,10)=1,34≡1(mod10).
31000=(34)250≡1250=1(mod10)
Count the remainders coprime to 100Factor 100=22⋅52 to find its distinct prime factors 2 and 5, then multiply 100 by (1−21) and (1−51).
ϕ(100)=100⋅(1−21)(1−51)=100⋅21⋅54=40
So the last digit of 31000 is 1, and any base a coprime to 100 satisfies a40≡1(mod100).
6
Combining Euler's Theorem and Fast Exponentiation
When we want the last two digits of 72026, which means finding 72026(mod100), dividing the exponent by ϕ(100)=40 still leaves a remainder of 26. Whenever the reduced exponent is still too big to compute in one step, use Euler's Theorem first to shrink the exponent and fast exponentiation second to finish the calculation.
Combining Euler's Theorem and Fast Exponentiation
72026≡?(mod100)
Text solution
Shrink the exponent modulo ϕ(100)=40Since gcd(7,100)=1,740≡1(mod100), so divide 2026 by 40 and keep the remainder 26.
To compute a modular power ak(modn), start by checking whether you can shrink the exponent k before you multiply. If gcd(a,n)=1, Euler's Theorem tells us that aϕ(n)≡1(modn), where ϕ(n)=n∏p∣n(1−p1) uses each distinct prime factor p of n once (for a prime modulus p, this is Fermat's Little Theorem, ap−1≡1(modp), as in 36≡1(mod7)). Divide k by ϕ(n) and keep only the remainder of the exponent, as in 72026≡726(mod100) since ϕ(100)=40.
Do not use Euler's or Fermat's Theorem to reduce the exponent if gcd(a,n)=1 or if the exponent k is already smaller than ϕ(n) (such as 513(mod23), where 13<22). In those cases, and whenever reducing modulo ϕ(n) still leaves an exponent like 26 in 726(mod100), use fast exponentiation: split the exponent into powers of 2(13=8+4+1), square and reduce modulo n row by row, and multiply the matching rows. For two special questions, use Fermat's Little Theorem in a slightly different way: to prove a prime p divides an expression for every integer n (including multiples of p), use np≡n(modp), which holds for all integers; and to test if an odd number n is prime, check an−1(modn) for a base a with gcd(a,n)=1. Getting anything other than 1 proves n is composite (214≡4≡1(mod15)), while getting 1 is inconclusive because some composite numbers (2340≡1(mod341)) also give 1.
7
Practice
1
Use fast exponentiation (repeated squaring) to compute 513(mod23).
When moving from 54≡4 to 58, square the remainder to get 42=16; doubling the remainder to 4⋅2=8 gives the wrong answer 22, and adding the remainders 16+4+5=25≡2 instead of multiplying them gives the wrong answer 2.
Since 13 is already smaller than 22, go straight to fast exponentiation: how do you write 13 as a sum of powers of 2?
Write 13=8+4+1. Repeated squaring modulo 23 gives
51525458≡5,=25≡2,≡22=4,≡42=16(mod23)
(Warning: when moving from 54≡4 to 58, square the remainder to get 42=16; doubling the remainder to 4⋅2=8 gives the wrong answer 22, and adding the remainders 16+4+5=25≡2 instead of multiplying them gives the wrong answer 2.) Multiplying the pieces together gives
513=58⋅54⋅51≡16⋅4⋅5=320=23⋅13+21≡21(mod23)
21
2
Use Fermat's Little Theorem to find the remainder when 21000 is divided by 13.
Divide the exponent by p−1=12, not by the modulus 13, which would leave remainder 12 and give the wrong answer 1.Don't stop at the reduced exponent 4.
Since 13 is prime and gcd(2,13)=1, what power of 2 is guaranteed to be 1(mod13)?
Since 13 is prime and gcd(2,13)=1, Fermat's Little Theorem gives 212≡1(mod13). Dividing the exponent 1000 by 12 gives 1000=12⋅83+4 (warning: divide the exponent by p−1=12, not by the modulus 13, which would leave remainder 12 and give the wrong answer 1). Reducing the power gives
21000=(212)83⋅24≡183⋅16≡3(mod13)
(Warning: don't stop at the reduced exponent 4. Remember to evaluate 24=16≡3(mod13).) 3
3
Prove that 42 divides n7−n for every integer n.
Factor 42 into primes, and use the form of Fermat's Little Theorem that holds for all integers, including multiples of p.
Factor 42=2⋅3⋅7. By Fermat's Little Theorem in the form np≡n(modp), which holds for every integer n (warning: do not write np−1≡1(modp) without checking whether p∣n, because that form fails when n≡0(modp)):
Mod 7:n7≡n(mod7), so n7−n≡n−n=0(mod7).
Mod 3:n3≡n(mod3), so n7=(n3)2⋅n≡n2⋅n=n3≡n(mod3), which gives n7−n≡0(mod3).
Mod 2:n2≡n(mod2), so repeated replacements of n2 with n give n7≡n(mod2) and n7−n≡0(mod2).
Since the pairwise coprime primes 2,3, and 7 each divide n7−n, the Chinese Remainder Theorem guarantees that 42 divides n7−n for every integer n.
4
Test n=65 with Fermat's primality test using base a=8 and base a=2 by computing 864(mod65) and 264(mod65) via repeated squaring. What does each result tell you about 65?
Square each base repeatedly up to the 64th power modulo 65; what can you conclude when a64(mod65) equals 1 versus when it does not equal 1?
For base a=8, repeated squaring modulo 65 gives 82=64 and 84=642=4096=65⋅63+1≡1(mod65), so 88,816,832, and 864 are all congruent to 1(mod65). Because the converse of Fermat's Little Theorem is false, this test is inconclusive (65 could be prime or composite; warning: do not conclude from 864≡1(mod65) that 65 is prime).
By the contrapositive of Fermat's Little Theorem, 264≡1(mod65) proves for sure that 65 is composite.
5
Compute ϕ(180), the number of integers from 1 to 180 that are coprime to 180.
Include each distinct prime factor only once in the product. Multiplying by (1−21) and (1−31) twice because of the squares 22 and 32 gives the wrong answer 16.
Find the prime factorization of 180 first; which distinct primes divide 180?
Since 180=22⋅32⋅5, its distinct prime factors are 2,3, and 5. (Warning: include each distinct prime factor only once in the product. Multiplying by (1−21) and (1−31) twice because of the squares 22 and 32 gives the wrong answer 16.) Therefore,
Don't forget the +1 for 31=3 when splitting 27, or you will compute 326≡29(mod100).
Finding the last two digits means working modulo 100: use ϕ(100) to shrink the exponent 2027 first, then break the remaining exponent into powers of 2.
Working modulo 100, since gcd(3,100)=1 and ϕ(100)=40, Euler's Theorem gives 340≡1(mod100) (warning: 100 is composite, so do not use 100−1=99 as the exponent that gives 1: in fact 399≡67≡1(mod100)). Because 2027=40⋅50+27,
32027=(340)50⋅327≡327(mod100)
Using fast exponentiation with 27=16+8+2+1 (warning: don't forget the +1 for 31=3 when splitting 27, or you will compute 326≡29(mod100)):