{
"cells": [
{
"cell_type": "markdown",
"id": "2473ba79-651d-4710-9299-137ae6cbd8d3",
"metadata": {},
"source": [
"# Foundations of Cryptography\n",
"\n",
"## Modular Arithmetic\n",
"\n",
"A number $x \\bmod N$ is the equivalent of asking for the remainder of $x$ when divided by $N$. \n",
"\n",
"Two integers $a$ and $b$ are said to be **congruent** (or in the same equivalence class) modulo $N$ if they have the same remainder upon division by $N$. In such a case, we say that \n",
"\n",
"$$a \\equiv b \\pmod N$$\n",
"\n",
"
\n",
"\n",
"### Addition in Modular Arithmetic\n",
"\n",
"1. If $a + b = c$, then $a \\pmod N + b \\pmod N \\equiv c \\pmod N$\n",
"\n",
"2. If $a \\equiv b \\pmod N$, then $a + k \\equiv b + k \\pmod N$ for any integer $k$\n",
"\n",
"3. If $a \\equiv b \\pmod N$ and $c \\equiv d \\pmod N$, then $a + c \\equiv b + d \\pmod N$\n",
"\n",
"4. If $a \\equiv b \\pmod N$, then $-a \\equiv -b \\pmod N$\n",
"\n",
"
\n",
"\n",
"### Multiplication in Modular Arithmetic\n",
"\n",
"1. If $a \\cdot b = c$, then $a \\pmod N \\cdot b \\pmod N \\equiv c \\pmod N$\n",
"\n",
"2. If $a \\equiv b \\pmod N$, then $k \\cdot a \\equiv k \\cdot b \\pmod N$ for any integer $k$\n",
"\n",
"3. If $a \\equiv b \\pmod N$ and $c \\equiv d \\pmod N$, then $a \\cdot c \\equiv b \\cdot d \\pmod N$\n",
"\n",
"
\n",
"\n",
"### Exponentiation in Modular Arithmetic\n",
"\n",
"1. If $a \\equiv b \\pmod N$, then $a^k \\equiv b^k \\pmod N$ for any integer $k$\n",
"\n",
"
\n",
"\n",
"### Division in Modular Arithmetic\n",
"\n",
"1. If $\\gcd(k, N) = 1$ and $k \\cdot a \\equiv k \\cdot b \\pmod N$, then $a \\equiv b \\pmod N$\n",
"\n",
"This property is true, because if $k \\cdot (a−b)$ is a multiple of $N$ and $\\gcd(k, N) = 1$, then $N$ must divide $a-b$, or equivalently $a \\equiv b \\pmod N$.\n",
"\n",
"**Example**: Consider $4 \\equiv 8 \\pmod 4$. Note that we cannot simply divide both sides of the equation by $2$, since $2 \\not \\equiv 4 \\pmod 4$.\n",
"\n",
"
\n",
"\n",
"### Multiplicative Inverse in Modular Arithmetic\n",
"\n",
"If $a$ and $N$ are integers such that $\\gcd(a, N) = 1$ (coprime or relatively prime), then there exists an integer $b$ such that $a \\cdot b \\equiv 1 \\pmod N$. $b$ is called the multiplicative inverse of $a \\bmod N$.\n",
"\n",
"**Examples**:\n",
"\n",
"* $2 \\cdot 3 \\equiv 1 \\pmod{5}$\n",
"* $2 \\cdot 6 \\equiv 1 \\pmod{11}$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7e02a076-d72b-40b2-abc2-9305e9b7e4fb",
"metadata": {},
"outputs": [],
"source": [
"# reference: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Pseudocode\n",
"def mod_inv(x, m):\n",
" r0, r1 = x, m\n",
" s0, s1 = 1, 0\n",
" while r1 != 0:\n",
" quotient = r0 // r1\n",
" r0, r1 = r1, r0 - quotient * r1\n",
" s0, s1 = s1, s0 - quotient * s1\n",
" if r0 != 1:\n",
" return None\n",
" return s0 if s0 >= 0 else s0 + m"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "42037c5e-bc7a-4204-8310-45c22e8bfd46",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mod_inv(2, 11)"
]
},
{
"cell_type": "markdown",
"id": "0bf78190-2540-4b3a-b93e-1f925c23df8e",
"metadata": {},
"source": [
"## The XOR Operation\n",
"\n",
"The boolean XOR (exclusive OR) operation form an **abelian group** $(\\,\\{T, F\\}^N, \\,\\oplus\\,)$ over the set of **boolean vectors** of length $N$:\n",
"\n",
"* **Closure**: when you XOR two boolean vectors $A$ and $B$, the result $C$ is also a boolean vector\n",
"\n",
"* **Commutative**: $A \\oplus B = B \\oplus A$\n",
"\n",
"* **Associative**: $A \\oplus (B \\oplus C) = (A \\oplus B) \\oplus C$\n",
"\n",
"* The **identity element** for the XOR operation is $T^N$ with $A \\oplus T^N = T^N \\oplus A = A$\n",
"\n",
"* **Inverse element**: each element is its own inverse, i.e. $A \\oplus A = T^N$\n",
"\n",
"### Isomorphism\n",
"\n",
"Two groups are said to be **isomorphic** if there is a **one-to-one mapping** between the **elements** of the sets that **preserves the operation**.\n",
"\n",
"
\n",
"\n",
"The group $(\\,\\{T, F\\}^N, \\,\\oplus\\,)$ is isomorphic to the group $(\\,\\{0, 1\\}^N, \\, + \\,)$ of **addition modulo 2** over the set of vectors whose **elements** are **integers mod 2**. \n",
"\n",
"→ The isomorphism simply maps $T$ to $1$ and $F$ to $0$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "75d2636e-107c-44d1-8e8d-e0cd936ad869",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0\n",
"1 1\n",
"1 1\n",
"0 0\n"
]
}
],
"source": [
"print((0 + 0) % 2, 0 ^ 0)\n",
"print((1 + 0) % 2, 1 ^ 0)\n",
"print((0 + 1) % 2, 0 ^ 1)\n",
"print((1 + 1) % 2, 1 ^ 1)"
]
},
{
"cell_type": "markdown",
"id": "c8d5f916-5415-410a-991a-b2893ff5a69b",
"metadata": {},
"source": [
"
\n",
"\n",
"The group $(\\,\\{T, F\\}^N, \\,\\oplus\\,)$ is also isomorphic to the group $(\\,\\text{S}^N, \\, \\Delta \\;)$ of **symmetric difference** $\\Delta$ over the **power set** of $N$ elements (set of all possible subsets of $\\text{S}$).\n",
"\n",
"Symmetric difference is a **set** operation that gives the set of elements that are in either of the sets but **not in their intersection**:\n",
"\n",
"$$\n",
"\\begin{align}\n",
"A \\, \\Delta \\, B &= (A / B) \\cup (B / A) \\qquad \\text{or} \\\\[8pt]\n",
"A \\, \\Delta \\, B &= (A \\cup B) / (B \\cap A)\n",
"\\end{align}\n",
"$$\n",
"\n",
"→ The isomorphism maps $T$ to *included in the set* and $F$ to *excluded from the set* for each of the $N$ entries of the Boolean vector.\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "aba84d4b-317e-44a7-9df8-fdc9b89dc6f3",
"metadata": {},
"source": [
"### Properties of the XOR Operation\n",
"\n",
"#### Toggling\n",
"\n",
"The combined value $A \\oplus B$ *remembers* both states, and one state is the key to getting at the other:\n",
"\n",
"$$\n",
"\\begin{align}\n",
"A \\oplus (A \\oplus B) &= (A \\oplus A) \\oplus B \\\\[6pt]\n",
"&= 0 \\oplus B \\\\[6pt]\n",
"&= B\n",
"\\end{align}\n",
"$$\n",
"\n",
"and\n",
"\n",
"$$\n",
"\\begin{align}\n",
"B \\oplus (A \\oplus B) &= B \\oplus (B \\oplus A) \\\\[6pt]\n",
"&= (B \\oplus B) \\oplus A \\\\[6pt]\n",
"&= 0 \\oplus A \\\\[6pt]\n",
"&= A\n",
"\\end{align}\n",
"$$\n",
"\n",
"#### Parity\n",
"\n",
"A powerful interpretation of XOR is in terms of parity, i.e. whether something is odd or even. \n",
"\n",
"For any $n$ bits, $A_1 \\oplus A_2 \\oplus \\ldots \\oplus A_n = 1$ if and only if the number of $1$s is **odd**.\n",
"\n",
"
\n",
"\n",
"An application of XOR’s parity property is **RAID** (Redundant Arrays of Inexpensive Disks) which is a way to recover from hard drive corruption. \n",
"\n",
"If we have $n$ hard drives, we can create an **additional** one $A^*$ which contains the XOR value of all the others:\n",
"\n",
"$$\n",
"A^* = A_1 \\oplus A_2 \\oplus \\ldots \\oplus A_n\n",
"$$\n",
"\n",
"This introduces redundancy: if a failure occurs on one drive, say $A_1$, we can **restore** it from **the others** since:\n",
"\n",
"$$\n",
"\\begin{align}\n",
"A_2 \\oplus \\ldots \\oplus A_n \\oplus A^* &= A_2 \\oplus \\ldots \\oplus A_n \\oplus (A_1 \\oplus A_2 \\oplus \\ldots \\oplus A_n) \\\\[6pt]\n",
"&= A_1 \\oplus (A_2 \\oplus A_2) \\oplus \\ldots \\oplus (A_n \\oplus A_n) \\\\[6pt]\n",
"&= A_1 \\oplus 0 \\oplus \\ldots \\oplus 0 \\\\[6pt]\n",
"&= A_1\n",
"\\end{align}\n",
"$$\n",
"\n",
"(this is the same reasoning used to explain toggling earlier, but applied to $n$ inputs rather than just $2$)\n",
"\n",
"In the (highly unlikely) event that 2 drives fail simultaneously, the above would not be applicable so there would be no way to recover the data."
]
},
{
"cell_type": "markdown",
"id": "12ccdd74-43a0-4b61-98a1-3d4cee456d55",
"metadata": {},
"source": [
"# Cryptography Principles\n",
"\n",
"## Kerckhoffs's Principle\n",
"\n",
" TODO \n",
"\n",
"## Key-Space\n",
"\n",
"The **key space** should be large enough to prevent **brute-force** and exhaustive-search attacks."
]
},
{
"cell_type": "markdown",
"id": "aec82972-a1d6-4590-b43d-360644096210",
"metadata": {},
"source": [
"# Encryption Schemes\n",
"\n",
"