# CSE 311 Lecture 12: Modular Arithmetic and Applications

## Topics

Modular arithmetic basics
Review of Lecture 11.
Modular arithmetic properties
Congruence, addition, multiplication, proofs.
Modular arithmetic and integer representations
Unsigned, sign-magnitude, and two’s complement representation.
Applications of modular arithmetic
Hashing, pseudo-random numbers, ciphers.

$\newcommand{rule}[2]{\begin{array}{c}#1\\ \hline \therefore #2\end{array}}$ $\newcommand{ruletemplate}[2]{\begin{array}{c}#1\\ \hline #2\end{array}}$ $\newcommand{hilite}[2]{\color{#1}{#2}}$ $\newcommand{arbitrary}[1]{\color{SteelBlue}{#1}}$ $\newcommand{specific}[1]{\color{MediumSeaGreen}{#1}}$ $\newcommand{qed}{\square}$ $\newcommand{\Z}{\mathbb{Z}}$ $\newcommand{\mod}[2]{#1\ \text{mod}\ #2}$ $\renewcommand{\div}[2]{#1\ \text{div}\ #2}$ $\newcommand{\congruent}[3]{#1\equiv #2\,(\text{mod}\,#3)}$

## Modular arithmetic basics

Review of Lecture 11.

### Key definition: divisibility

Definition: $a$ divides $b$, written as $a \vert b$.
For $a\in\Z, b\in\Z$, $a \vert b \leftrightarrow \exists k\in\Z. b = ka$.

We also say that $b$ is divisible by $a$ when $a \vert b$.

### Key theorem: division theorem

Division theorem
For $a\in\Z, d\in\Z$ with $d > 0$,
there exist unique integers $q, r$ with $0 \leq r < d$
such that $a = dq+r$.

That is, if we divide $a$ by $d$, we get a unique

• quotient $q = \div{a}{d}$ and
• non-negative remainder $r = \mod{a}{d}$.

So, $a = d(\div{a}{d}) + (\mod{a}{d})$.

## Modular arithmetic properties

Congruence, addition, multiplication, proofs.

### Congruence modulo a positive integer

Definition: $a$ is congruent to $b$ modulo $m$, written as $\congruent{a}{b}{m}$
For $a,b,m\in\Z$ with $m>0$, $\congruent{a}{b}{m} \leftrightarrow m \vert (a-b)$

We read “$\congruent{a}{b}{m}$” as “$a$ is congruent to $b$ modulo $m$”, which means $m \vert (a-b)$.

So, “congruence modulo $m$” is a predicate on integers, written using the notation “$\congruent{}{}{m}$”.

### Congruence and equality

Congruence property
Let $a, b, m \in \Z$ with $m>0$.
Then, $\congruent{a}{b}{m}$ if and only if $\mod{a}{m} = \mod{b}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$. Then $m \vert a-b$ by definition of congruence. So $a-b = km$ for some $k\in\Z$ by definition of divides. Therefore, $a = b + km$. By the division theorem, we can write $a = qm + r$ where $r = \mod{a}{m}$. Combining this with $a = b + km$, we have $b + km = qm + r$, so $b = (q-k)m + r$. By the uniqueness condition of the division theorem, $r = \mod{b}{m}$, so we have $\mod{a}{m} = r = \mod{b}{m}$.
Suppose that $\mod{a}{m} = \mod{b}{m}$. By the division theorem, $a = mq + (\mod{a}{m})$ and $b = ms + (\mod{b}{m})$ for some $q,s\in\Z$. Then, $a - b = (mq + (\mod{a}{m})) - (ms + (\mod{b}{m}))$ $= m(q-s) + (\mod{a}{m}-\mod{b}{m})$ $= m(q-s)$, since $\mod{a}{m} = \mod{b}{m}$. Therefore, $m \vert (a-b)$ and so $\congruent{a}{b}{m}$.

### The $\mod{}{m}\$ function vs the $\congruent{}{\!}{m}$ predicate

The $\mod{}{m}$ function takes any $a\in\Z$ and maps it to a remainder $\mod{a}{m}\in\{0, 1, \ldots, m-1\}$.

In other words, $\mod{}{m}$ places all integers that have the same remainder modulo $m$ into the same “group” (a.k.a. “congruence class”).

The $\congruent{}{\!}{m}$ predicate compares $a,b\in\Z$ and returns true if and only if $a$ and $b$ are in the same group according to the $\mod{}{m}$ function.

### Modular addition property

Let $m$ be a positive integer ($m \in \Z$ with $m>0$).
If $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$, then $\congruent{a+c}{b+d}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$. By definition of congruence, there are $k$ and $j$ such that $a - b = km$ and $c - d = jm$. Adding these equations together, we get $(a+c)-(b+d) = m(j+k)$. Reapplying the definition of congruence, we get that $\congruent{(a+c)}{(b+d)}{m}$.

### Modular multiplication property

Modular multiplication property
Let $m$ be a positive integer ($m \in \Z$ with $m>0$).
If $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$, then $\congruent{ac}{bd}{m}$.
Proof:
Suppose that $\congruent{a}{b}{m}$ and $\congruent{c}{d}{m}$. By definition of congruence, there are $k$ and $j$ such that $a - b = km$ and $c - d = jm$. So, $a = km + b$ and $c = jm + b$. Multiplying these equations together, we get $ac = (km + b)(jm + d) = kjm^2 + kmd + bjm + bd$. Rearranging gives us $ac - bd = m(kjm + kd + bj)$. Reapplying the definition of congruence, we get that $\congruent{ac}{bd}{m}$.

### Example: a proof using modular arithmetic

Let $n\in\Z$, and prove that $\congruent{n^2}{0}{4}$ or $\congruent{n^2}{1}{4}$.

Let’s look at a few examples:
$0^2 = \congruent{ 0 }{ 0 }{4}$
$1^2 = \congruent{ 1 }{ 1 }{4}$
$2^2 = \congruent{ 4 }{ 0 }{4}$
$3^2 = \congruent{ 9 }{ 1 }{4}$
$4^2 = \congruent{ 16 }{ 0 }{4}$
It looks like
$\congruent{n}{0}{2} \rightarrow \congruent{n^2}{0}{4}$
$\congruent{n}{1}{2} \rightarrow \congruent{n^2}{1}{4}$
Proof by cases:
Case 1 ($n$ is even). Suppose $\congruent{n}{0}{2}$. Then $n=2k$ for some integer $k$. So $n^2 = (2k)^2 = 4k^2$. Therefore, by definition of congruence, $\congruent{n^2}{0}{4}$.
Case 2 ($n$ is odd). Suppose $\congruent{n}{1}{2}$. Then $n=2k+1$ for some integer $k$. So $n^2 = (2k+1)^2 = 4k^2 + 4k + 1$ $=$ $4(k^2 + k) + 1$. Therefore, by definition of congruence, $\congruent{n^2}{1}{4}$.

## Modular arithmetic and integer representations

Unsigned, sign-magnitude, and two’s complement representation.

### Unsigned integer representation

Represent integer $x$ as a sum of $n$ powers of 2:
If $x = \sum_{i=0}^{n-1} b_i2^i$ where each $b_i\in\{0,1\}$,
then the representation is $b_{n-1}\ldots b_2 b_1 b_0$.
Examples:
$99 = 64 + 32 + 2 + 1$
$18 = 16 + 2$
So for $n = 8$:
$99 = 0110\ 0011$
$18 = 0001\ 0010$

This works for unsigned integers. How do we represented signed integers?

### Sign-magnitude integer representation

If $-2^{n-1} < x < 2^{n-1}$, represent $x$ with $n$ bits as follows:
Use the first bit as the sign (0 for positive and 1 for negative), and
the remaining $n-1$ bits as the (unsigned) value.
Examples:
$99 = 64 + 32 + 2 + 1$
$18 = 16 + 2$
So for $n = 8$:
$\ \ \,99 = 0110\ 0011$
$-18 = 1001\ 0010$
$\ \ \,81 = 0101\ 0001$

The problem with this representation is that our standard arithmetic algorithms no longer work, e.g., adding the representation of -18 and 99 doesn’t give the representation of 81.

### Two’s complement integer representation

Represent $x$ with $n$ bits as follows:
If $0 \leq x < 2^{n-1}$, use the $n$-bit unsigned representation of $x$.
If $-2^{n-1} \leq x < 0$, use the $n$-bit unsigned representation of $2^n - |x|$.
Key property:
Two’s complement representation of any number $y$ is equivalent to $\mod{y}{2^n}$ so arithmetic works $\mod{}{2^n}$.
Examples:
$99 = 64 + 32 + 2 + 1$
$18 = 16 + 2$
$2^8 - 18 = 256 - 18 = 238 = 128 + 64 + 32 + 8 + 4 + 2$
$81 = 64 + 16 + 1$
So for $n = 8$:
$\ \ \,99 = 0110\ 0011$
$-18 = 1110\ 1110$
$\ \ \,81 = 0101\ 0001$

### Computing the two’s complement representation

For $-2^{n-1} \leq x < 0$, $x$ is represented using the $n$-bit unsigned representation of $2^n - |x|$. To compute this value:

• Compute the $n$-bit unsigned representation of $|x|$.
• Flip the bits of $|x|$ to get the representation of $2^n-1-|x|$.
• Add 1 to get $2^n - |x|$.
• This works because $x+\overline{x}$ is all 1s, which represents $2^n-1$. So $\overline{x} = 2^n - 1 - x$ and $\overline{x} + 1 = 2^n - x$.
Example: -18 in 8-bit two’s complement
18 in 8-bit unsigned: $0001\ 0010$
Flip the bits: $1110\ 1101$
Add 1: $1110\ 1110$

## Applications of modular arithmetic

Hashing, pseudo-random numbers, ciphers.

### Hashing

Problem:
We want to map a small number of data values from a large domain $\{0, 1, \ldots, M-1\}$ into a small set of locations $\{0, 1, \ldots, n-1\}$ to be able to quickly check if a value is present.
Solution:
Compute $\text{hash}(x) = \mod{x}{p}$ for a prime $p$ close to $n$.
Or, compute $\text{hash}(x) = \mod{ax+b}{p}$ for a prime $p$ close to $n$.
This approach depends on all of the bits of data the data.
Helps avoid collisions due to similar values.
But need to manage them if they occur.

### Pseudo-random number generation

Linear Congruential method
$x_{n+1} = \mod{(ax_n + c)}{m}$

Choose $x_0$ randomly and $a, c, m$ carefully to produce a sequence of $x_n$’s.

Example
$a = 1103515245, c = 12345, m = 2^{31}$ from BSD
$x_0 = 311$
$x_1 = 1743353508, x_2 = 1197845517, x_3 = 1069836226, \ldots$

### Simple ciphers

Ceasar or shift cipher
Treat letters as numbers: A = 0, B = 1, …
$f(p) = \mod{(p + k)}{26}$
$f^{-1}(p) = \mod{(p - k)}{26}$
More general version
$f(p) = \mod{(ap + b)}{26}$
$f^{-1}(p) = \mod{(a^{-1}(p - b))}{26}$

## Summary

Modular arithmetic is arithmetic over a finite domain.
Key notions are divisibility and congruence modulo $m$.
Thanks to addition and multiplication properties, modular arithmetic supports familiar algebraic manipulations such as adding and multiplying together $\congruent{\,}{\!}{m}$ equations.
Modular arithmetic is the basis of computing.
Used with two’s complement representation to implement computer arithmetic.
Also used in hashing, pseudo-random number generation, and cryptography.