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 readJump to the quiz ↓

    Primes are the building blocks, and every integer factors into them in exactly one way

    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. This page covers the tools for all three. Primes come first: n=pqn = pq only resists factoring because pp and qq are hard to find.

    A prime number has exactly two positive divisors, 11 and itself. It cannot be written as a product of two smaller positive integers. 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.

    Try it — worked example

    Write 360 as a product of primes.

    Show answer and steps

    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.

    Fermat’s little theorem makes powers predictable, and square-and-multiply makes them cheap

    Fermat’s little theorem states that for a prime pp and any integer aa not divisible by pp, ap−1≡1(modp).a^{p-1} \equiv 1 \pmod{p}. Raising aa to one less than the prime always lands on 11, modulo that prime. This is why aφ(n)a^{\varphi(n)} lands back on 11 in the generalisation below, which is the fact RSA decryption depends on.

    Formula

    Fermat's little theorem

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

    Try it — worked example

    Check Fermat’s little theorem for p = 7, a = 3: what is 3^6 mod 7?

    Show answer and steps

    Answer3^6 = 1 (mod 7)

    Take p=7p = 7, a=3a = 3. Fermat predicts 37−1=36≡1(mod7)3^{7-1} = 3^6 \equiv 1 \pmod{7}.

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

    Computing 363^6 directly and then reducing works for small numbers, but the source also shows a second route that reduces at every step: 3 mod 7=33 \bmod 7 = 3, 32 mod 7=9 mod 7=23^2 \bmod 7 = 9 \bmod 7 = 2, and then 36=32×32×32≡2×2×2=8≡1(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 is the habit that matters: it keeps every intermediate value small and generalises to exponents far too large to compute directly.

    At the size RSA and Diffie-Hellman use, computing aba^b in full before reducing is not an option, because 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.

    Try it — worked example

    Use square-and-multiply to compute 5^117 mod 19.

    Show answer and steps

    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. 51 mod 19=55^1 \bmod 19 = 5.
    3. 52≡(51)2=25 mod 19=65^2 \equiv (5^1)^2 = 25 \bmod 19 = 6.
    4. 54≡(52)2=62=36 mod 19=175^4 \equiv (5^2)^2 = 6^2 = 36 \bmod 19 = 17.
    5. 58≡(54)2=172=289 mod 19=45^8 \equiv (5^4)^2 = 17^2 = 289 \bmod 19 = 4 (since 19×15=28519 \times 15 = 285).
    6. 516≡(58)2=42=16 mod 19=165^{16} \equiv (5^8)^2 = 4^2 = 16 \bmod 19 = 16.
    7. 532≡(516)2=162=256 mod 19=95^{32} \equiv (5^{16})^2 = 16^2 = 256 \bmod 19 = 9 (since 19×13=24719 \times 13 = 247).
    8. 564≡(532)2=92=81 mod 19=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: 5117≡51×54×516×532×564≡5×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. 61200 mod 1961200 \bmod 19: 19×3221=6119919 \times 3221 = 61199, so 61200 mod 19=161200 \bmod 19 = 1. Thus 5117≡1(mod19)5^{117} \equiv 1 \pmod{19}.

    That worked example builds the powers of two from the bottom up and multiplies together the ones the binary expansion calls for. The tracer below runs the same idea from the top bit down: one running accumulator, squared every step, with 55 multiplied in whenever the current bit is 11. Type base 55, exponent 117117, modulus 1919 into it to 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 b−1b - 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(log⁡b)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.

    The totient counts the numbers coprime to n, and for a product of two primes it is (p-1)(q-1)

    Fermat’s little theorem only counts, implicitly, the p−1p - 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 p−1p - 1 is coprime to pp, so φ(p)=p−1\varphi(p) = p - 1.

    Try it — worked example

    Find φ(37).

    Show answer and steps

    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.

    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).

    Try it — worked example

    Find φ(35) by listing the numbers that are not coprime to 35.

    Show answer and steps

    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 34−6−4=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, multiples of 55 and 77 included, and then states “there are 24 numbers on the list.” The printed list is just the full run of consecutive integers, not the coprime set. The same happens for φ(21)\varphi(21) below. In both cases the final count is correct; only the printed enumeration is wrong. The coprime lists on this page are the ones that belong to φ(35)\varphi(35) and φ(21)\varphi(21).

    Formula

    Totient of a product of two primes

    φ(pq)=φ(p)×φ(q)=(p−1)(q−1)\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.

    Try it — worked example

    Find φ(21) using the product-of-two-primes rule.

    Show answer and steps

    Answerphi(21) = 12

    1. 21=3×721 = 3 \times 7, both prime.
    2. φ(21)=φ(3)×φ(7)=(3−1)(7−1)=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 (p−1)(q−1)(p-1)(q-1) identity is faster and safer than listing and counting. RSA key generation never enumerates anything: it computes φ(n)=(p−1)(q−1)\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.

    Euler’s theorem extends Fermat’s to any modulus coprime to the base

    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)+1≡a(modn)a^{\varphi(n)+1} \equiv a \pmod{n}. Setting n=pn = p prime recovers Fermat’s theorem exactly, since φ(p)=p−1\varphi(p) = p - 1.

    Try it — worked example

    Check Euler’s theorem for a = 3, n = 5: what is 3^φ(5) mod 5?

    Show answer and steps

    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. 81 mod 581 \bmod 5: 5×16=805 \times 16 = 80, so 81 mod 5=181 \bmod 5 = 1.
    4. 3φ(5)=34≡1(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 p−1p-1 is automatically coprime to pp, and φ(p)=p−1\varphi(p) = p - 1. Substituting recovers ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p} exactly: Fermat’s statement, with no extra assumption needed beyond nn being prime.

    Check yourself