Modular Arithmetic

Ref. Chapter 8 INTRODUCTION TO MODERN CRYPTOGRAPHY by Jonathan Katz and Yehuda Lindellbar

Prime numbers and factorization

Let us consider $\mathbb Z$ the set of integers and $\mathbb N$ the set of natural numbers.

Definition ($a|b$) : $a \in \mathbb Z$ divides $b \in \mathbb Z$ if there exists an integer $c$ such that $ac=b$. If $a$ is positive, we say that $a$ is a divisor of $b$.

Definition: An integer $p\ne\pm 1$ is a prime if whenever $p|ab$, then $p|a$ or $p|b$.

Proposition 1: Let $p\in \mathbb Z $ and $p \ne \pm 1$. $p$ is a prime if and only if it has only two divisors, i.e. 1 and $p$.

Theorem (Fundamental theorem of arithmetic/Unique factorization): Every integer $n>1$ can be decomposed uniquely (up to permutation) as a product of prime factors. More precisely, there exist unique $p_1<\dots<p_k$ prime integers and $e_1,\dots e_k\in\mathbb N^+$ such that $$ n= \prod_{j=1}^k p_j^{e_j}$$

We call this decomposition the factorization of $n$.

Theorem 2: Let $a \in \mathbb Z$ and $b \in \mathbb N$. There exists unique integers $q,r$ such that $a=qb+r$ and $0\le r< b$.

We call $r$ the reminder of the division of $a$ by $b$.

CompFact: There exist an algorithm to compute $r,q$ in polyonomial time (Polynomial what??).

Example

Definition: Let $a,b \in \mathbb Z$. The greatest common divisor of $a$ and $b$ is $$gcd(a,b)=\max \{c \in \mathbb Z ~:~ c|a~~~ and ~~~ c|b \}.$$

Definition: Let $a,b \in \mathbb Z$. If $gcd(a,b)=1$ we say that they are coprime.

CompFact: The Euclidian algorithm computes the gcd in polyonomial time (Polynomial what??). We will see this algorithm.

CompOpenQuest: Can we compute the factorization?? Ideas??

Modular arithmetic

Warm up: Classify the following numbers according to the reminder of their division by 5: 36, 44, -45, -38, -14, -50, 35, -49, -34, -12, 43,37, 24, -37, 40, -11, 7, 38.

Let us consider $a,r,N\in \mathbb Z$ and $N>1$. Suppose $r$ is the reminder of the division of $a$ by $N$. We define $$[a \mod N]=r.$$ r is also called class of a modulo N.

The reduction mod N is the mapping $a \rightarrow [a \mod N]$. Notice that $$ [* \mod N ]: \mathbb Z \rightarrow \{0,1,\dots,N-1\}\subseteq\mathbb Z$$

Definition: $a$ and $b$ are congruent modulo N if and only if $[a \mod N]=[b \mod N]$. In such a case we write $a =b \mod N$.

Proposition 2: $a=b\mod N \iff a-b=0 \mod N \iff N|(a-b)$

Exercise. Prove that the congruence modulo N is an equivalence relation.

Example

Compute 674 and -674 modulo 7.

$-674=-679 +5 = - 97\cdot7 + 5= 5 \mod 7$

Indeed, congruence modulo $N$ commutes with addition and multiplication! Suppose $\bar a=[a \mod N]$ and $\bar b=[b \mod N]$.

  1. $$ a+ b = \bar a +\bar b \mod N$$
  2. $$ a \cdot b = \bar a \cdot \bar b \mod N$$ Exercise. Poof.

Example

This is not true for the 'usual' division!

Inverse mod N

Definition: An integer $a\in \mathbb Z$ is invertible modulo N if there exits $y\in \mathbb Z$ such that $ a\cdot y =1 \mod N$.

Attention false friend: $ab = ac \mod N$ does NOT imply $b = c \mod N$!

If $y,y'\in \mathbb Z$ are such that $ a\cdot y= a\cdot y' =1 \mod N$, then $ y=y'\mod N$ (this is not obvious!!). Therefore, we can define $a^{-1}=[y \mod N ] $ the inverse of $a$ modulo $ N $.

Definition: Let $b$ be invertible modulo $N$. We define $[a/b \mod N]= [a\cdot b^{-1} \mod N]$.

Proposition 3: Let $a,b,c\in \mathbb Z$ and $a$ be invertible modulo $N$. Then $$ ab = ac \mod N \Rightarrow b = c \mod N.$$

CompFact: We can test in poly time if an integer is invertible mod $N$.

Proposition 4: Let $b,N \in \mathbb Z$ and $N>1$ and $b\ge 1$. Then $b$ is invertible modulo $N$ if and only if $gcd(b,N)=1$.

Lemma GCD: Let $a,b \in \mathbb N$. Then there exist $X,Y\in \mathbb Z$ such that $aX+bY=gcd(a,b)$ and $gcd(a,b)$ is the smallest integer that can be expressed in this way.

Example

Compute if possible the inverse of 13 and 36 mod 6.

Since $13 =1 \mod 6$, the inverse of 13 modulo 6 is 1. While 34 is not invertible.

Example

We want to compute now $[ 9 / 40 \mod 11]$. First we check if 40 is invertible!

First we check if 40 is invertible, and in such a case we have to find its inverse.

Finally, we compute $[ 9 \cdot 8 \mod 11]$.

CompFact: Using the Extended euclidian algorithm we can compute the "division" in polyonomial time (Polynomial what??).

Computational aspects of Modular Arithmetic

Modular operations

The reduction mod N $a \rightarrow [a \mod N]$

Euclidean algorithm

To check if $a$ is invertible we want to use Propostion 4. So we have to compute the GCD.

Euclidean algorithm:

Extended Euclidean algorithm:

Remark: Notice that at each step $r_i=aX_i+bY_i$.

Compute inverse and division mod $N$

Since $xgcd$ returns $X,Y$ such that $aX+NY=1$, we have that $a^{-1}=X \mod N$