Skip to content
Extended Euclidean & Modular Inverse
step 1/7

Reading — step 1 of 7

Read

~1 min readNumber Theory

Extended Euclidean & Modular Inverse

To find d such that e * d ≡ 1 (mod φ(n)) (the RSA private exponent), we need the modular inverse.

Extended Euclidean Algorithm finds gcd(a, b) AND coefficients x, y such that a*x + b*y = gcd(a, b):

python

If gcd(a, m) = 1, then a*x + m*y = 1, which means a*x ≡ 1 (mod m). So x mod m is the modular inverse.

python

Example: inverse of 3 mod 11:

3 * 4 = 12 ≡ 1 (mod 11). So 3^-1 ≡ 4 (mod 11).

This is exactly how RSA computes the private key d:

d = modinv(e, φ(n))

Where φ is Euler's totient and e is the public exponent.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…