Cryptography Intermediate

Modular Arithmetic Toolkit

The arithmetic underneath RSA, Diffie-Hellman and every classical cipher on this site. Six calculators, each showing the full working rather than just an answer — because in cryptography the algorithm is the point.

Euclid and Bézout
Square-and-multiply trace
Euler φ, primality, CRT
Live
Choose a Calculation

Finds gcd(a, b) and integers x, y with ax + by = gcd(a, b).

Finds a−1 with a · a−1 ≡ 1 (mod m). Exists exactly when gcd(a, m) = 1.

Square-and-multiply: a 2048-bit exponent takes about 2048 squarings, not 22048 multiplications. This is what makes RSA possible.

Factors n, tests primality, and computes φ(n) — the count of integers below n coprime to it. RSA's private key depends on φ(n).

Solves a system of congruences with pairwise coprime moduli. RSA decryption is roughly four times faster done this way.

Result
Answer
—
Full Working
The Six Routines Cryptography Runs On
a^-1 a = 1 mod m a^phi(n) = 1 mod n c = m^e mod n

Euclidean algorithm. Repeated division to find gcd(a, b). It is astonishingly fast — the number of steps is proportional to the number of digits, not the size of the numbers.

Extended Euclid. The same divisions run backwards give x and y with ax + by = gcd(a, b). Setting gcd = 1 yields the modular inverse, which is how RSA computes the private exponent d from e.

Square-and-multiply. Computing me mod n by repeated squaring turns an impossible calculation into a few thousand multiplications. Without it, public-key cryptography would not exist.

Euler's φ. For a prime p, φ(p) = p − 1; for n = pq, φ(n) = (p−1)(q−1). Euler's theorem then says aφ(n) ≡ 1, and RSA is built directly on that.

Every one of these is polynomial time. RSA's security rests on the one operation that is NOT known to be fast: factoring n back into p and q.
Why the Working Matters

The answers here are easy to get from any calculator. The algorithms are the content, for two reasons:

  • Cost is the whole story. Modular exponentiation is fast and factoring is slow, and that single gap is what a public-key system is built in. If you cannot see why one is cheap, you cannot see why the other being expensive matters.
  • The extended Euclidean algorithm reappears everywhere. It computes RSA's d, the affine cipher's a−1, the Hill cipher's matrix inverse, and half the steps of elliptic curve arithmetic.
  • These routines are where implementations leak. A square-and-multiply that branches on the secret exponent leaks it through timing — which is a real attack, not a theoretical one.
Try 2^1234 mod 789 in the modular power tab. Eleven squarings, and the trace shows every one.
Put It Into Practice

The tool shows the mechanism — the slides show why it is built that way.

Number theory that finally has a purpose

gcd, modular inverses and Euler's theorem stop being abstract the moment you see RSA built out of them. One-on-one tutoring makes that connection explicit.

Book a Free Consultation →