CSEC3616Cybersecurity Engineering

    Primes, Fermat, Euler and fast modular exponentiation

    The fundamental theorem of arithmetic, Fermat's little theorem, square-and-multiply exponentiation worked in full on 5^117 mod 19, and Euler's totient function and theorem.

    • State the fundamental theorem of arithmetic and factor an integer into its prime powers.
    • Apply Fermat's little theorem and verify 3^6 = 1 (mod 7) by direct computation.
    • Compute a modular exponentiation by binary decomposition, reproducing every squaring in 5^117 mod 19.
    • Compute Euler's totient function directly and via the phi(pq) = (p-1)(q-1) identity, and state Euler's theorem.

    22 min read

    Intuition

    RSA key generation needs two large primes, a count of how many numbers are coprime to their product, and an exponent computed against that count. That is three separate jobs, and this page covers the tools for all three. Primes are the raw material: n=pqn = pq only resists factoring because pp and qq are hard to find. Fermat’s and Euler’s theorems are what make modular exponentiation predictable instead of chaotic. They are the reason aφ(n)a^{\varphi(n)} always lands back on 11, which is exactly the fact RSA decryption depends on. And fast exponentiation is what makes any of this usable at real key sizes: an RSA exponent can run to hundreds of digits, and multiplying a number by itself that many times, one step at a time, is not something a computer finishes in a human lifetime. Binary decomposition turns that into a few hundred squarings instead.

    Mechanism

    A prime number has exactly two positive divisors, 11 and itself. It cannot be written as a product of two smaller positive integers. This makes primes the multiplicative building blocks of every integer, a fact strong enough to have its own theorem.

    The fundamental theorem of arithmetic states that any integer a>1a > 1 factors into primes in exactly one way: a=p1a1×p2a2××ptat,p1<p2<<pt prime.a = p_1^{a_1} \times p_2^{a_2} \times \ldots \times p_t^{a_t}, \qquad p_1 < p_2 < \ldots < p_t \text{ prime.} Uniqueness is the load-bearing part: there is no second, different set of prime powers that also multiplies out to aa.

    Worked example

    Answer360 = 2^3 x 3^2 x 5

    1. 360=8×45=23×45360 = 8 \times 45 = 2^3 \times 45.
    2. 45=9×5=32×545 = 9 \times 5 = 3^2 \times 5.
    3. Combining: 360=23×32×5360 = 2^3 \times 3^2 \times 5. Check: 8×9×5=3608 \times 9 \times 5 = 360.

    Pitfall

    The Week 4 number-theory supplement’s own second example, factoring 3636, prints 36=22×3336 = 2^2 \times 3^3. That is wrong: 22×33=4×27=1082^2 \times 3^3 = 4 \times 27 = 108, not 3636. The correct factorisation is 36=22×3236 = 2^2 \times 3^2 (4×9=364 \times 9 = 36); the exponent on 33 is 22, not 33. The 360360 example above is printed correctly in the source and needs no correction.

    Mechanism

    Fermat’s little theorem states that for a prime pp and any integer aa not divisible by pp, ap11(modp).a^{p-1} \equiv 1 \pmod{p}. Raising aa to one less than the prime always lands on 11, modulo that prime.

    Worked example

    Answer3^6 = 1 (mod 7)

    Take p=7p = 7, a=3a = 3. Fermat predicts 371=361(mod7)3^{7-1} = 3^6 \equiv 1 \pmod{7}.

    1. 36=7293^6 = 729.
    2. 729mod7729 \bmod 7: 7×104=7287 \times 104 = 728, and 729728=1729 - 728 = 1. So 729mod7=1729 \bmod 7 = 1.
    3. 361(mod7)3^6 \equiv 1 \pmod{7}, confirming the theorem.

    Mechanism

    Computing 363^6 directly and then reducing works fine for small numbers, but the source also shows a second route that reduces at every step instead of at the end: 3mod7=33 \bmod 7 = 3, 32mod7=9mod7=23^2 \bmod 7 = 9 \bmod 7 = 2, and then 36=32×32×322×2×2=81(mod7).3^6 = 3^2 \times 3^2 \times 3^2 \equiv 2 \times 2 \times 2 = 8 \equiv 1 \pmod{7}. Both routes agree. The second one is the more important habit: it keeps every intermediate value small, and it generalises to exponents far too large to compute directly. That generalisation is square-and-multiply.

    Mechanism

    For a modulus and exponent the size RSA and Diffie-Hellman actually use, computing aba^b in full before reducing is not an option: the intermediate number would run to thousands of digits. Square-and-multiply fixes this by writing the exponent bb in binary and building the answer from repeated squarings of aa, mod nn, at every step, combining only the squarings that correspond to a 11 bit.

    Worked example

    Answer5^117 mod 19 = 1

    1. Write 117117 in binary: 117=11101012117 = 1110101_2, i.e. 117=20+22+24+25+26=1+4+16+32+64117 = 2^0 + 2^2 + 2^4 + 2^5 + 2^6 = 1 + 4 + 16 + 32 + 64. So 5117=51×54×516×532×5645^{117} = 5^1 \times 5^4 \times 5^{16} \times 5^{32} \times 5^{64}.
    2. 51mod19=55^1 \bmod 19 = 5.
    3. 52(51)2=25mod19=65^2 \equiv (5^1)^2 = 25 \bmod 19 = 6.
    4. 54(52)2=62=36mod19=175^4 \equiv (5^2)^2 = 6^2 = 36 \bmod 19 = 17.
    5. 58(54)2=172=289mod19=45^8 \equiv (5^4)^2 = 17^2 = 289 \bmod 19 = 4 (since 19×15=28519 \times 15 = 285).
    6. 516(58)2=42=16mod19=165^{16} \equiv (5^8)^2 = 4^2 = 16 \bmod 19 = 16.
    7. 532(516)2=162=256mod19=95^{32} \equiv (5^{16})^2 = 16^2 = 256 \bmod 19 = 9 (since 19×13=24719 \times 13 = 247).
    8. 564(532)2=92=81mod19=55^{64} \equiv (5^{32})^2 = 9^2 = 81 \bmod 19 = 5 (since 19×4=7619 \times 4 = 76).
    9. Combine the terms needed for 117=1+4+16+32+64117 = 1 + 4 + 16 + 32 + 64: 511751×54×516×532×5645×17×16×9×5(mod19)5^{117} \equiv 5^1 \times 5^4 \times 5^{16} \times 5^{32} \times 5^{64} \equiv 5 \times 17 \times 16 \times 9 \times 5 \pmod{19}.
    10. 5×17×16×9×5=612005 \times 17 \times 16 \times 9 \times 5 = 61200. 61200mod1961200 \bmod 19: 19×3221=6119919 \times 3221 = 61199, so 61200mod19=161200 \bmod 19 = 1. Thus 51171(mod19)5^{117} \equiv 1 \pmod{19}.

    Aside

    This reaches the answer by computing powers of two from the bottom up (51,52,54,,5645^1, 5^2, 5^4, \ldots, 5^{64}) and multiplying together the ones the binary expansion calls for. The tracer below implements the same idea the other way round, from the top bit down: it keeps a single running accumulator, squaring it every step and multiplying in 55 whenever the current bit is 11. Both are square-and-multiply; they only differ in which end of the exponent they start from. Type base 55, exponent 117117, modulus 1919 into the tracer to see the second route reach the same answer.

    Modular exponentiation tracer

    Trace square-and-multiply bit by bit: the exponent in binary, the squaring step, the conditional multiply, and the accumulator.

    ibitsquaremultiplyaccumulator

    Exam detail

    The point of this method is the operation count. Multiplying aa by itself b1b - 1 times is O(b)O(b) multiplications, infeasible once bb has hundreds of digits. Square-and-multiply needs one squaring per bit of bb and one extra multiply per 11 bit, so the cost is O(logb)O(\log b) modular multiplications: for a 2048-bit RSA exponent, roughly 2048 squarings instead of 220482^{2048} multiplications. Diffie-Hellman key exchange, covered in Diffie-Hellman key exchange, runs on exactly this operation.

    Formula

    Fermat's little theorem

    ap11(modp)a^{p-1} \equiv 1 \pmod{p}
    pp
    a prime number
    aa
    any integer not divisible by p

    Mechanism

    Fermat’s little theorem only counts, implicitly, the p1p - 1 nonzero residues mod a prime pp. Euler’s totient function φ(n)\varphi(n) generalises that count to any modulus: φ(n)\varphi(n) is the number of positive integers less than nn that are relatively prime to nn. When n=pn = p is prime, every integer from 11 to p1p - 1 is coprime to pp, so φ(p)=p1\varphi(p) = p - 1.

    Worked example

    Answerphi(37) = 36

    1. 3737 is prime.
    2. Every integer from 11 through 3636 shares no factor with a prime, so all 3636 of them are coprime to 3737.
    3. φ(37)=36\varphi(37) = 36.

    Mechanism

    For nn that is not prime, the direct method is to list every integer below nn and remove the ones that share a factor with nn. The supplement demonstrates this on φ(35)\varphi(35).

    Worked example

    Answerphi(35) = 24

    1. 35=5×735 = 5 \times 7, so an integer from 11 to 3434 fails to be coprime to 3535 exactly when it is a multiple of 55 or of 77.
    2. Multiples of 55 below 3535: 5,10,15,20,25,305, 10, 15, 20, 25, 30, six numbers.
    3. Multiples of 77 below 3535: 7,14,21,287, 14, 21, 28, four numbers. (3535 itself is excluded by “below 35”.)
    4. No overlap between the two lists, since lcm(5,7)=35\mathrm{lcm}(5, 7) = 35 is not below 3535. So 3464=2434 - 6 - 4 = 24 integers remain.
    5. The coprime set is {1,2,3,4,6,8,9,11,12,13,16,17,18,19,22,23,24,26,27,29,31,32,33,34}\{1, 2, 3, 4, 6, 8, 9, 11, 12, 13, 16, 17, 18, 19, 22, 23, 24, 26, 27, 29, 31, 32, 33, 34\}, 2424 numbers. φ(35)=24\varphi(35) = 24.

    Pitfall

    The Week 4 supplement’s own worked version of this example lists every integer from 11 through 3434, all 3434 of them, multiples of 55 and 77 included, and then states “there are 24 numbers on the list.” The printed list does not match that claim: it is not the coprime set, just the full run of consecutive integers. The same pattern repeats for φ(21)\varphi(21) below. In both cases the final count is correct; only the printed enumeration is wrong. The coprime lists above are the ones that actually belong to φ(35)\varphi(35) and φ(21)\varphi(21).

    Formula

    Totient of a product of two primes

    φ(pq)=φ(p)×φ(q)=(p1)(q1)\varphi(pq) = \varphi(p) \times \varphi(q) = (p-1)(q-1)
    p,qp, q
    two distinct primes
    n=pqn = pq
    their product

    This is the identity RSA key generation runs directly, and the reason listing every integer below n is never actually necessary when n's factorisation is known.

    Worked example

    Answerphi(21) = 12

    1. 21=3×721 = 3 \times 7, both prime.
    2. φ(21)=φ(3)×φ(7)=(31)(71)=2×6=12\varphi(21) = \varphi(3) \times \varphi(7) = (3-1)(7-1) = 2 \times 6 = 12.
    3. Direct check: the integers from 11 to 2020 coprime to 2121 (excluding multiples of 33 and of 77) are {1,2,4,5,8,10,11,13,16,17,19,20}\{1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20\}, 1212 numbers, agreeing with the formula.

    Exam detail

    Whenever nn‘s prime factorisation is already known, the (p1)(q1)(p-1)(q-1) identity is always faster and safer than listing and counting. RSA key generation never enumerates anything: it computes φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1) directly from the two primes it just generated. See extended Euclid and modular inverses for how φ(n)\varphi(n) feeds into finding the private exponent dd.

    Mechanism

    Euler’s theorem generalises Fermat’s little theorem from a prime modulus to any modulus, provided the base and modulus are coprime: aφ(n)1(modn),gcd(a,n)=1.a^{\varphi(n)} \equiv 1 \pmod{n}, \qquad \gcd(a, n) = 1. An equivalent form is aφ(n)+1a(modn)a^{\varphi(n)+1} \equiv a \pmod{n}. Setting n=pn = p prime recovers Fermat’s theorem exactly, since φ(p)=p1\varphi(p) = p - 1.

    Worked example

    Answer3^4 = 1 (mod 5)

    Take a=3a = 3, n=5n = 5. φ(5)=4\varphi(5) = 4, since 55 is prime.

    1. gcd(3,5)=1\gcd(3, 5) = 1, so Euler’s theorem applies.
    2. 34=813^4 = 81.
    3. 81mod581 \bmod 5: 5×16=805 \times 16 = 80, so 81mod5=181 \bmod 5 = 1.
    4. 3φ(5)=341(mod5)3^{\varphi(5)} = 3^4 \equiv 1 \pmod{5}, as the theorem predicts.

    Recall

    Why does Fermat's little theorem count as a special case of Euler's theorem, rather than a separate result?

    Euler’s theorem says aφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n} for any aa coprime to nn. When nn is a prime pp, every integer from 11 to p1p-1 is automatically coprime to pp, and φ(p)=p1\varphi(p) = p - 1. Substituting recovers ap11(modp)a^{p-1} \equiv 1 \pmod{p} exactly: Fermat’s statement, with no extra assumption needed beyond nn being prime.