{
"cells": [
{
"cell_type": "markdown",
"id": "9b40912a",
"metadata": {},
"source": [
"# The Modular Inverse\n",
"\n",
"This is the third post in my short series on the RSA algorithm.\n",
"We met most of the cast of characters used in the algorithm in the [last post](https://blog.wificidr.net/posts/prime-numbers-the-extended-euclidean-algorithm-and-the-gcd/).\n",
"Prime numbers, Euclid's algorithm, Bézout's identity, and of course, one of Euler's identities come together to form the foundation of our work.\n",
"The last detail is the modular inverse.\n",
"Conceptually, the idea is straightforward, but I feel many articles on RSA don't give it the attention it deserves.\n",
"\n",
"## Identities\n",
"\n",
"Many operations paired with sets have an element known as the identity element.\n",
"This should be a familiar idea even if you haven't seen the formal definition.\n",
"\n",
"- 0 is the identity of the set $\\mathbb{R}$ and the operation of addition. 42 + 0 = 42\n",
"- 1 is the identity of the set $\\mathbb{R}$ and the operation of multiplication. 42 * 1 = 42\n",
"- \\[\\] is the identity of list concatenation. \\[40, 41, 42\\] + \\[\\] = \\[40, 41, 42\\]\n",
"\n",
"Can you think of other examples?\n",
"What about the identity of a 3x3 matrix?\n",
"\n",
"$$\\begin{bmatrix}\n",
"42 & 99 & 2\\\\\n",
"0 & -35 & 12\\\\\n",
"4 & 2 & 8\n",
"\\end{bmatrix} \\cdot\n",
"\\begin{bmatrix}\n",
"1 & 0 & 0\\\\\n",
"0 & 1 & 0\\\\\n",
"0 & 0 & 1\n",
"\\end{bmatrix} =\n",
"\\begin{bmatrix}\n",
"42 & 99 & 2\\\\\n",
"0 & -35 & 12\\\\\n",
"4 & 2 & 8\n",
"\\end{bmatrix}$$\n",
"\n",
"## Inverses\n",
"\n",
"Informally, and inverse simply \"undoes\" an operation.\n",
"Specifically, we are interested in an element that gives us back the identity.\n",
"\n",
"- $42 + -42 = 0$\n",
"- $42 \\cdot \\frac{1}{42} = 1$\n",
"\n",
"This works for matrices as well, but keep in mind that not all matrices have an inverse!\n",
"\n",
"$$\\begin{bmatrix}\n",
"42 & 99 & 2\\\\\n",
"0 & -35 & 12\\\\\n",
"4 & 2 & 8\n",
"\\end{bmatrix} \\cdot\n",
"\\begin{bmatrix}\n",
"\\frac{38}{967} & \\frac{197}{1934} & \\frac{-629}{3868}\\\\\n",
"\\frac{-6}{967} & \\frac{-41}{967} & \\frac{63}{967}\\\\\n",
"\\frac{-35}{1934} & \\frac{-39}{967} & \\frac{735}{3868}\n",
"\\end{bmatrix} =\n",
"\\begin{bmatrix}\n",
"1 & 0 & 0\\\\\n",
"0 & 1 & 0\\\\\n",
"0 & 0 & 1\n",
"\\end{bmatrix}$$\n",
"\n",
"### Put the modular in the inverse\n",
"\n",
"Consider a multiplication table for $\\pmod 9$ (excluding 0).\n",
"\n",
"$$\\begin{array}{c|c|c|c|c|c|c|c|c|}\n",
"\t & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\\\\n",
"\\hline\n",
"\t1 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\\\\n",
"\\hline\n",
"\t2 & 2 & 4 & 6 & 8 & 1 & 3 & 5 & 7 \\\\\n",
"\\hline\n",
"\t3 & 3 & 6 & 0 & 3 & 6 & 0 & 3 & 6 \\\\\n",
"\\hline\n",
"\t4 & 4 & 8 & 3 & 7 & 2 & 6 & 1 & 5 \\\\\n",
"\\hline\n",
"\t5 & 5 & 1 & 6 & 2 & 7 & 3 & 8 & 4 \\\\\n",
"\\hline\n",
"\t6 & 6 & 3 & 0 & 6 & 3 & 0 & 6 & 3 \\\\\n",
"\\hline\n",
"\t7 & 7 & 5 & 3 & 1 & 8 & 6 & 4 & 2 \\\\\n",
"\\hline\n",
"\t8 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 \\\\\n",
"\\hline\n",
"\\end{array}$$\n",
"\n",
"A quick inspection will reveal that 1 is indeed the identity element for integers and the multiplication operation.\n",
"Our intuition is preserved! 😅\n",
"As I noted earlier, not all matrices have an inverse.\n",
"Likewise, not all integers have a modular inverse.\n",
"Inspect the table closely; can you figure out which numbers **do not** have an inverse and why?\n",
"\n",
"The integers < 9 that are coprime with 9 and their *modular inverse*:\n",
"- $8 * 8 \\pmod{9} = 1$, 8 is the modular inverse of of 8 in $\\pmod{9}$\n",
"- $7 * 4 \\pmod{9} = 1$, 4 is the modular inverse of of 7 in $\\pmod{9}$\n",
"- $5 * 2 \\pmod{9} = 1$, 2 is the modular inverse of of 5 in $\\pmod{9}$\n",
"- $4 * 7 \\pmod{9} = 1$, 7 is the modular inverse of of 4 in $\\pmod{9}$\n",
"- $2 * 5 \\pmod{9} = 1$, 5 is the modular inverse of of 2 in $\\pmod{9}$\n",
"- $1 * 1 \\pmod{9} = 1$, 1 is the modular inverse of of 1 in $\\pmod{9}$\n",
"\n",
"#### Seeking intuition\n",
"\n",
"Given two integers $A$ and $B$, $A$ has a modular inverse in $\\pmod{B}$ *if and only if A is coprime with B*.\n",
"\n",
"Here is how I think about it.\n",
"Consider $4 \\pmod{9}$.\n",
"You can start at **any** number and count by multiples of 4.\n",
"In $\\pmod{9}$, you are guaranteed to hit 1 at some point!\n",
"\n",
"- Start with 30\n",
"- $30 \\pmod{9} = 3$\n",
"- count by 4's\n",
"- $34 \\pmod{9} = 7$\n",
"- $38 \\pmod{9} = 2$\n",
"- $42 \\pmod{9} = 6$\n",
"- $46 \\pmod{9} = 1$\n",
"\n",
"Can you think of a way to determine the number of steps needed?\n",
"I covered that in the last post, and it involves Euclid's algorithm and Bézout's identity.\n",
"\n",
"## Revisiting Bézout and Euclid\n",
"\n",
"Bézout's identity states:\n",
"- If $a$ and $b$ are positive integers, then there exist integers $k$ and $l$ such that $\\gcd(a,b)=ak+bl$\n",
"\n",
"If $a$ and $b$ are chosen so that their $\\gcd = 1$, then we want to find some integers $k$ and $l$ where $1=ak+bl$.\n",
"This is done using Euclid's algorithm as described in the [previous post](https://blog.wificidr.net/posts/prime-numbers-the-extended-euclidean-algorithm-and-the-gcd/).\n",
"\n",
"## Conclusion\n",
"\n",
"I hope this brief discussion clears some of the ideas around the modular inverse.\n",
"What we are trying to accomplish is given some value, scale it up in a modular space, then be able to get back to where we started.\n",
"Kind of sounds like an encryption scheme, doesn't it? 😁\n",
"In my next post in this series, I will finally describe the actual RSA algorithm and how all of these pieces fit together."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"nikola": {
"category": "",
"date": "2023-09-20 14:46:01-05:00",
"description": "",
"link": "",
"slug": "the-modular-inverse",
"tags": "math,rsa",
"title": "The Modular Inverse",
"type": "text"
}
},
"nbformat": 4,
"nbformat_minor": 5
}