CSEC3616Cybersecurity Engineering

    The extended Euclidean algorithm and modular inverses

    The extended Euclidean algorithm worked as a full table, then used to compute a modular inverse and to show when one does not exist.

    • Run the extended Euclidean algorithm on two integers and produce every row of the table.
    • Read the Bezout coefficients x and y off the finished table and verify ax + by = gcd(a, b).
    • Compute a modular inverse with the extended Euclidean algorithm, and explain why one can fail to exist.

    18 min read

    Intuition

    The plain Euclidean algorithm on the previous page answers “what is gcd(a,b)\gcd(a, b)?” and stops there. Encryption needs a second, related question answered: given aa and a modulus nn, is there a number that “undoes” multiplication by aa, modulo nn? That number is the modular inverse, and RSA’s private key is computed by finding exactly one. The extended Euclidean algorithm answers both questions in a single pass — it never costs more work than the plain version, and it hands back the modular inverse as a byproduct.

    Mechanism

    For integers aa and bb, the extended Euclidean algorithm finds not just d=gcd(a,b)d = \gcd(a, b) but two integers xx and yy satisfying ax+by=d=gcd(a,b).ax + by = d = \gcd(a, b). It does this by running the same division steps as the plain Euclidean algorithm, while carrying two extra running values, ss and tt, alongside the remainder rr at every row. Each row satisfies r=sa+tbr = sa + tb: the remainder, expressed as a combination of the original aa and bb. The last row before the remainder hits 00 gives dd, xx and yy together.

    Worked example

    Answergcd(240, 46) = 2, x = -9, y = 47

    Start the table with r1=a=240r_{-1} = a = 240 (s=1,t=0s=1, t=0) and r0=b=46r_0 = b = 46 (s=0,t=1s=0, t=1). Each later row divides the previous remainder by the current one and updates ss and tt the same way.

    1. q=240/46=5q = \lfloor 240/46 \rfloor = 5. r=2405×46=10r = 240 - 5 \times 46 = 10. s=15×0=1s = 1 - 5 \times 0 = 1. t=05×1=5t = 0 - 5 \times 1 = -5. Row: q=5,r=10,s=1,t=5q=5, r=10, s=1, t=-5.
    2. q=46/10=4q = \lfloor 46/10 \rfloor = 4. r=464×10=6r = 46 - 4 \times 10 = 6. s=04×1=4s = 0 - 4 \times 1 = -4. t=14×(5)=21t = 1 - 4 \times (-5) = 21. Row: q=4,r=6,s=4,t=21q=4, r=6, s=-4, t=21.
    3. q=10/6=1q = \lfloor 10/6 \rfloor = 1. r=101×6=4r = 10 - 1 \times 6 = 4. s=11×(4)=5s = 1 - 1 \times (-4) = 5. t=51×21=26t = -5 - 1 \times 21 = -26. Row: q=1,r=4,s=5,t=26q=1, r=4, s=5, t=-26.
    4. q=6/4=1q = \lfloor 6/4 \rfloor = 1. r=61×4=2r = 6 - 1 \times 4 = 2. s=41×5=9s = -4 - 1 \times 5 = -9. t=211×(26)=47t = 21 - 1 \times (-26) = 47. Row: q=1,r=2,s=9,t=47q=1, r=2, s=-9, t=47.
    5. q=4/2=2q = \lfloor 4/2 \rfloor = 2. r=42×2=0r = 4 - 2 \times 2 = 0. The remainder has reached 00, so the table stops here.
    6. The last row with a non-zero remainder is row 4: r=2r = 2, s=9s = -9, t=47t = 47. So gcd(240,46)=2\gcd(240, 46) = 2, x=9x = -9, y=47y = 47. Check: 240×(9)+46×47=2160+2162=2240 \times (-9) + 46 \times 47 = -2160 + 2162 = 2.

    Mechanism

    This reproduces the same five division steps the lecture writes out directly — 240=46×5+10240 = 46 \times 5 + 10, 46=10×4+646 = 10 \times 4 + 6, 10=6×1+410 = 6 \times 1 + 4, 6=4×1+26 = 4 \times 1 + 2, 4=2×2+04 = 2 \times 2 + 0 — and reaches the same coefficients the lecture finds by substituting each equation back into the last: 2=46×47240×92 = 46 \times 47 - 240 \times 9, i.e. x=9x = -9, y=47y = 47. The table method and the back-substitution method are the same algorithm; the table just keeps the running ss and tt instead of unwinding the equations afterward.

    Extended Euclid stepper

    Run the extended Euclidean algorithm on two integers and show every row of the table, the Bezout coefficients, and the modular inverse where one exists.

    iqrst

    Mechanism

    For aa coprime to nn (that is, gcd(a,n)=1\gcd(a, n) = 1), the extended Euclidean algorithm on aa and nn gives ax+ny=1ax + ny = 1. Reducing both sides modulo nn makes the nyny term vanish, leaving ax1(modn)ax \equiv 1 \pmod{n} — so xx, reduced into the range [0,n)[0, n), is the modular inverse of aa modulo nn, written a1modna^{-1} \bmod n.

    Worked example

    Answer3^-1 mod 11 = 4

    Run the table on a=3a = 3, b=11b = 11.

    1. q=3/11=0q = \lfloor 3/11 \rfloor = 0. r=30×11=3r = 3 - 0 \times 11 = 3. s=1s = 1, t=0t = 0. Row: q=0,r=3,s=1,t=0q=0, r=3, s=1, t=0. (aa is smaller than bb, so the first quotient is 00 — the algorithm still runs correctly.)
    2. q=11/3=3q = \lfloor 11/3 \rfloor = 3. r=113×3=2r = 11 - 3 \times 3 = 2. s=03×1=3s = 0 - 3 \times 1 = -3. t=13×0=1t = 1 - 3 \times 0 = 1. Row: q=3,r=2,s=3,t=1q=3, r=2, s=-3, t=1.
    3. q=3/2=1q = \lfloor 3/2 \rfloor = 1. r=31×2=1r = 3 - 1 \times 2 = 1. s=11×(3)=4s = 1 - 1 \times (-3) = 4. t=01×1=1t = 0 - 1 \times 1 = -1. Row: q=1,r=1,s=4,t=1q=1, r=1, s=4, t=-1.
    4. q=2/1=2q = \lfloor 2/1 \rfloor = 2. r=22×1=0r = 2 - 2 \times 1 = 0. Remainder is 00, table stops.
    5. Last non-zero remainder: row 3, r=1r = 1, s=4s = 4. Since gcd(3,11)=1\gcd(3, 11) = 1, a modular inverse exists, and it is x=4x = 4. Check: 3×4=121(mod11)3 \times 4 = 12 \equiv 1 \pmod{11}.

    Aside

    This reaches the same coefficient the lecture’s back-substitution gets from the same two division steps, 11=3×3+211 = 3 \times 3 + 2 and 3=1×2+13 = 1 \times 2 + 1: working backward gives 1=3×4111 = 3 \times 4 - 11, so 44 is the coefficient of 33 — the modular inverse. Typing a=3a = 3, b=11b = 11 into the stepper above reproduces this exact table, including the leading q=0q=0 row.

    Mechanism

    Not every aa has an inverse modulo nn. Since ax+ny=gcd(a,n)ax + ny = \gcd(a, n), reducing mod nn only isolates a 11 on the right-hand side when gcd(a,n)=1\gcd(a, n) = 1. If gcd(a,n)>1\gcd(a, n) > 1, no xx satisfies ax1(modn)ax \equiv 1 \pmod{n} — the table still runs, but its final gcd is not 11.

    Worked example

    Answer2x ≡ 1 (mod 10) has no solution

    1. q=2/10=0q = \lfloor 2/10 \rfloor = 0. r=2r = 2. s=1s = 1, t=0t = 0. Row: q=0,r=2,s=1,t=0q=0, r=2, s=1, t=0.
    2. q=10/2=5q = \lfloor 10/2 \rfloor = 5. r=105×2=0r = 10 - 5 \times 2 = 0. Remainder is 00, table stops.
    3. Last non-zero remainder: row 0, r=2r = 2. So gcd(2,10)=2\gcd(2, 10) = 2, not 11. Since 22 and 1010 share the common factor 22, 22 has no modular inverse mod 1010.

    Exam detail

    The lecture’s own check confirms this the direct way: every product 2×kmod102 \times k \bmod 10 for kZ10k \in \mathbb{Z}_{10} is even (0,2,4,6,8,0,2,4,6,80, 2, 4, 6, 8, 0, 2, 4, 6, 8), and 11 is odd, so it never appears. Both arguments — the gcd test and the direct enumeration — agree, and either is an acceptable exam answer.

    Pitfall

    A modular inverse requires gcd(a,n)=1\gcd(a, n) = 1, full stop — not “small,” not “close to coprime.” 22 and 1010 share only the factor 22, and that is already enough to rule out every inverse. Do not confuse this with ordinary division, where every non-zero number has a reciprocal; modular arithmetic on Zn\mathbb{Z}_n only grants that to the elements coprime to nn, which is exactly the property φ(n)\varphi(n) counts, on the next page.

    Formula

    Bezout identity

    ax+by=d=gcd(a,b)ax + by = d = \gcd(a, b)
    a,ba, b
    the two input integers
    dd
    gcd(a, b)
    x,yx, y
    the Bezout coefficients

    Exam detail

    This is the step RSA depends on directly. The private exponent is d=e1modφ(n)d = e^{-1} \bmod \varphi(n) — the modular inverse of the public exponent ee, modulo the totient of the modulus. Key generation runs exactly the algorithm on this page: if gcd(e,φ(n))1\gcd(e, \varphi(n)) \neq 1, there is no valid dd, and ee must be rejected and chosen again. Module 6 relies on this fact without re-deriving it.

    Recall

    Why does the extended Euclidean algorithm always find a modular inverse when one exists, without any extra searching?

    Because it computes xx and yy with ax+ny=gcd(a,n)ax + ny = \gcd(a, n) as a direct side effect of finding the gcd, not by trying candidate inverses one at a time. When gcd(a,n)=1\gcd(a, n) = 1, that same xx, reduced mod nn, already is the inverse — no search step is needed.