The Multiplicative Inverse and Euclid's Algorithm: How to "Divide" in Cryptography

Welcome back to izytech.dev! In the previous article, we looked at the beating heart of cryptography: modular arithmetic. We saw how computers use the math of "remainders" to keep numbers small and calculations fast. But we ended that lesson with a big roadblock: in this circular world, division doesn't exist.

So if we can't divide a number to decrypt a message, how do we go back? The answer lies in one of the most fascinating ideas in number theory: the Multiplicative Inverse, along with an algorithm that's thousands of years old and still lets us calculate it today.

The Illusion of Division

Think about regular math for a second. Dividing a number by 5 is exactly the same as multiplying it by 1/5 (its reciprocal). In cryptography, we use this exact same logic, but since we can't work with decimals or fractions, we need to find a whole number that behaves just like that reciprocal.

This special number is called the multiplicative inverse (often written with a -1 exponent, like x-1). By definition, the multiplicative inverse of a number is the value that, when multiplied by the original number inside a specific modulus, gives us the multiplicative identity, which is 1.

There's just one catch: not every number has a multiplicative inverse!

The Golden Rule: Being "Coprime"

For a number to have an inverse in a given modular system, it has to meet one strict condition: the number and the modulus need to be coprime (also called relatively prime). This means they can't share any prime factors. In mathematical terms, their Greatest Common Divisor (GCD) has to be exactly 1.

If the GCD isn't 1, the multiplicative inverse simply doesn't exist, and the operation fails. In cryptography, quickly figuring out whether two massive numbers (hundreds of digits long) are coprime is basically a matter of life or death for the algorithm.

Euclid's Algorithm: A Legacy Thousands of Years Old

To find out if two numbers are coprime, we need their GCD. Luckily, one of the most efficient ways to get it is also one of the oldest methods ever discovered: Euclid's Algorithm.

Instead of breaking huge numbers down into prime factors (something that would take modern computers thousands of years), Euclid's algorithm relies on a clever, lightning-fast trick based on remainders:

  • Take two numbers, x and y.
  • The Greatest Common Divisor of x and y is exactly the same as the Greatest Common Divisor of y and the remainder of dividing x by y (let's call it r).
  • As a formula: GCD(x, y) = GCD(y, r).

By repeatedly swapping the larger number with the remainder, the problem shrinks at an incredible pace, until the remainder finally hits zero. The last non-zero remainder we get is our GCD!

Why Does This Matter So Much?

It's pretty amazing to think that the security protocols protecting our credit cards and emails online today trace back to calculations first worked out in ancient Greece. Those early mathematicians didn't have computers, so they had to come up with incredibly smart ways to do things by hand. And it's that same hand-calculation efficiency that makes their algorithms so perfectly suited to run on today's machines.

To sum up: today we learned that to "divide" we need multiplicative inverses, that these only exist when the GCD is 1, and that Euclid's Algorithm lets us check this condition in a fraction of a second. In the next lesson, we'll take the final step: we'll adapt this ancient algorithm (into the Extended Euclidean Algorithm) so that instead of just telling us whether the inverse exists, it actually calculates it for us. Stay tuned!

References and Study Material

  • Coursera: Mathematical Foundations of Cryptography (Lesson 3: Multiplicative Inverses and Euclid's Algorithm).
  • Daniele Venturi (2012). Crittografia nel Paese delle Meraviglie. Springer. (Introductory chapters on modular algebra).
  • Carlo Casolo. Appunti di Teoria Elementare dei Numeri. University of Florence. (For the full proofs behind the Euclidean algorithm).

Comments

Popular posts from this blog

How to Install Ubuntu 24.04 WSL on Windows 10 and 11

Introduction to Rust: Writing Your First "Hello, World!"

Getting Started with Cargo in Rust: A Beginner-Friendly Guide