# Cryptography/Mathematical Background

See Talk page for other suggestions.

## Introduction

Modern public-key (asymmetric) cryptography is based upon a branch of mathematics known as number theory, which is concerned solely with the solution of equations that yield only integer results. These type of equations are known as diophantine equations, named after the Greek mathematician Diophantos of Alexandria (ca. 200 CE) from his book Arithmetica that addresses problems requiring such integral solutions.
One of the oldest diophantine problems is known as the Pythagorean problem, which gives the length of one side of a right triangle when supplied with the lengths of the other two side, according to the equation
    $a^{2}+b^{2}=c^{2}\$
where $c\$ is the length of the hypotenuse. While two sides may be known to be integral values, the resultant third side may well be irrational. The solution to the Pythagorean problem is not beyond the scope, but is beyond the purpose of this chapter. Therefore, example integral solutions (known as Pythagorean triplets) will simply be presented here. It is left as an exercise for the reader to find additional solutions, either by brute-force or derivation.
| $a\$ | $b\$ | $c\$ |
| 3 | 4 | 5 |
| 5 | 12 | 13 |
| 7 | 24 | 25 |
| 8 | 15 | 17 |

## Prime Numbers
### Description

Asymmetric key algorithms rely heavily on the use of prime numbers, usually exceedingly long primes, for their operation. By definition, prime numbers are divisible only by themselves and 1. In other words, letting the symbol | denote divisibility (i.e. - $a|b$ means "$b$ divides into $a$"), a prime number strictly adheres to the following mathematical definition
    $p\$ | $b\$ Where $b=1\$ or $p\$ only
The Fundamental Theorem of Arithmetic states that all integers can be decomposed into a unique prime factorization. Any integer greater than 1 is considered either prime or composite. A composite number is composed of more than one prime factor
    $c\$ | $b\$ where ultimately $b=p_{0}^{e_{0}}p_{1}^{e_{1}}\cdot \cdot \cdot p_{n}^{e_{n}}\$
in which $p_{n}\$ is a unique prime number and $e_{n}\$ is the exponent.

#### Numerical Examples

As can be seen, according to this systematic decomposition, each factorization is unique.
In order to deterministically verify whether an integer $a\$ is prime or composite, only the primes $p\leq {\sqrt {c}}\$ need be examined. This type of systematic, thorough examination is known as a brute-force approach. Primes and composites are noteworthy in the study of cryptography since, in general, a public key is a composite number which is the product of two or more primes. One (or more) of these primes may constitute the private key.
There are several types and categories of prime numbers, three of which are of importance to cryptography and will be discussed here briefly.

### Fermat Primes

Fermat numbers take the following form
    $F_{n}=2^{2^{n}}+1\$
If F$_n$ is prime, then it is called a Fermat prime.

#### Numerical Examples

The only Fermat numbers known to be prime are $F_{0}-F_{4}\$. Moreover, the primality of all Fermat numbers was disproven by Euler, who showed that $F_{5}=641\cdot 6,700,417$.

### Mersenne Primes

Mersenne primes - another type of formulaic prime generation - follow the form
$M_{p}=2^{p}-1\$
where $p\$ is a prime number. The Wolfram Alpha engine reports Mersenne Primes, an example input request being "4th Mersenne Prime".

#### Numerical Examples

The first four Mersenne primes are as follows
Numbers of the form M$_p$ = 2$^p$ without the primality requirement are called Mersenne numbers. Not all Mersenne numbers are prime, e.g. M$_{11}$ = 2$^{11}$−1 = 2047 = 23 · 89.

### Coprimes (Relatively Prime Numbers)

Two numbers are said to be coprime if the largest integer that divides evenly into both of them is 1. Mathematically, this is written
    $\gcd(a,b)=1\$
where $\gcd \$ is the greatest common divisor. Two rules can be derived from the above definition
    If $ab\$ | $c\$ and $\gcd(b,c)=1\$, then $a\$ | $c\$
    If $ab=c^{2}\$ with $\gcd(a,b)=1\$, then both $a\$ and $b\$ are squares, i.e. - $a=a_{0}^{2}\$, $b=b_{0}^{2}\$

### The Prime Number Theorem

The Prime Number Theorem estimates the probability that any integer, chosen randomly will be prime. The estimate is given below, with $\pi (x)\$ defined as the number of primes $\leq x\$
    $\pi (x)\approx {\frac {x}{\ln x}}\$
$\pi (x)\$ is asymptotic to ${\frac {x}{\ln x}}\$, that is to say $\quad \lim _{x\to \infty }{\frac {\pi (x)}{\frac {x}{\ln x}}}=1\$. What this means is that generally, a randomly chosen number is prime with the approximate probability ${\tfrac {1}{\ln x}}\$.

## The Euclidean Algorithm
### Introduction

The Euclidean Algorithm is used to discover the greatest common divisor of two integers. In cryptography, it is most often used to determine if two integers are coprime, i.e. - $\gcd(a,b)=1\$.
In order to find $\gcd(a,b)\$ where $a>b\$ efficiently when working with very large numbers, as with cryptosystems, a method exists to do so. The Euclidean algorithm operates as follows - First, divide $a\$ by $b\$, writing the quotient $q_{1}\$, and the remainder $r_{1}\$. Note this can be written in equation form as $a=q_{1}b+r_{1}\$. Next perform the same operation using $b\$ in $a\$'s place: $b=q_{2}r_{1}+r_{2}\$. Continue with this pattern until the final remainder is zero. Numerical examples and a formal algorithm follow which should make this inherent pattern clear.

### Mathematical Description

When $r_{n}=0\$, stop with $\gcd(a,b)=r_{n-1}\$.

### Numerical Examples

Example 1 - To find gcd(17,043,12,660)
gcd (17,043,12,660) = 3
Example 2 - To find gcd(2,008,1,963)
gcd (2,008,1963) = 1 Note: the two number are coprime.

### Algorithmic Representation
## The Extended Euclidean Algorithm

In order to solve the type of equations represented by Bézout's identity, as shown below
    $au+bv=\gcd(a,b)\$
where $a\$, $b\$, $u\$, and $v\$ are integers, it is often useful to use the extended Euclidean algorithm. Equations of the form above occur in public key encryption algorithms such as RSA (Rivest-Shamir-Adleman) in the form $ed+w(p-1)(q-1)=1\$ where $\gcd(e,(p-1)(q-1))=1\$. There are two methods in which to implement the extended Euclidean algorithm; the iterative method and the recursive method.
As an example, we shall solve an RSA key generation problem with e = 2$^{16}$ + 1, p = 3,217, q = 1,279. Thus, 62,537d + 51,456w = 1.

### Methods
#### The Iterative Method

This method computes expressions of the form $r_{i}=ax_{i}+by_{i}$ for the remainder in each step $i$ of the Euclidean algorithm. Each modulus can be written in terms of the previous two remainders and their whole quotient as follows:
    $r_{i}=r_{i-2}-\left\lfloor {\frac {r_{i-2}}{r_{i-1}}}\right\rfloor \cdot r_{i-1}$
By substitution, this gives:
    $r_{i}=(ax_{i-2}+by_{i-2})-\left\lfloor {\frac {r_{i-2}}{r_{i-1}}}\right\rfloor \cdot (ax_{i-1}+by_{i-1})$
    $r_{i}=a(x_{i-2}-\left\lfloor {\frac {r_{i-2}}{r_{i-1}}}\right\rfloor \cdot x_{i-1})+b(y_{i-2}-\left\lfloor {\frac {r_{i-2}}{r_{i-1}}}\right\rfloor \cdot y_{i-1})$
The first two values are the initial arguments to the algorithm:
    $r_{1}=a=a(1)+b(0)\$
    $r_{2}=b=a(0)+b(1)\$
The expression for the last non-zero remainder gives the desired results since this method computes every remainder in terms of a and b, as desired.

##### Example

| Step | Quotient | Remainder | Substitute | Combine terms |
| 1 | | 4,110,048 = a | | 4,110,048 = 1a + 0b |
| 2 | | 65,537 = b | | 65,537 = 0a + 1b |
| 3 | 62 | 46,754 = 4,110,048 - 65,537 $\cdot$ 62 | 46,754 = (1a + 0b) - (0a + 1b) $\cdot$ 62 | 46,754 = 1a - 62b |
| 4 | 1 | 18,783 = 65,537 - 46,754 $\cdot$ 1 | 18,783 = (0a + 1b) - (1a - 62b) $\cdot$ 1 | 18,783 = -1a + 63b |
| 5 | 2 | 9,188 = 46,754 - 18,783 $\cdot$ 2 | 9,188 = (1a - 62b) - (-1a + 62b) $\cdot$ 2 | 9,188 = 3a - 188b |
| 6 | 2 | 407 = 18,783 - 9,188 $\cdot$ 2 | 407 = (-1a + 63b) - (3a - 188b) $\cdot$ 2 | 407 = -7a + 439b |
| 7 | 22 | 234 = 9,188 - 407 $\cdot$ 22 | 234 = (3a - 188b) - (-7a + 439b) $\cdot$ 22 | 234 = 157a - 9,846b |
| 8 | 1 | 173 = 407 - 234 $\cdot$ 1 | 173 = (-7a + 439b) - (157a - 9,846b) $\cdot$ 1 | 173 = -164a + 10,285b |
| 9 | 1 | 61 = 234 - 173 $\cdot$ 1 | 61 = (157a - 9,846b) - (-164a + 10,285b) $\cdot$ 1 | 61 = 321a + 20,131b |
| 10 | 2 | 51 = 173 - 61 $\cdot$ 2 | 51 = (-164a + 10,285b) - (321a +20,131b) $\cdot$ 2 | 51 = -806a + 50,547b |
| 11 | 1 | 10 = 61 - 51 $\cdot$ 1 | 61 = (321a +20,131b) - (-806a + 50,547b) $\cdot$ 1 | 10 = 1,127a - 70,678b |
| 12 | 5 | 1 = 51 -10 $\cdot$ 5 | 1 = (-806a + 50,547b) - (1,127a - 70,678b) $\cdot$ 5 | 1 = -6,441a + 403,937b |
| 13 | 10 | 0 | End of algorithm | End of algorithm |
Putting the equation in its original form $ed+w(p-1)(q-1)=1\$ yields $(65,537)(403,937)+(-6,441)(3,217-1)(1,279-1)=1\$, it is shown that $d=403,937\$ and $w=-6,441\$. During the process of key generation for RSA encryption, the value for w is discarded, and d is retained as the value of the private key In this case

#### The Recursive Method

This is a direct method for solving Diophantine equations of the form $au+bv=\gcd(a,b)\$. Using this method, the dividend and the divisor are reduced over a series of steps. At the last step, a trivial value is substituted into the equation, and is then worked backward until the solution is obtained.

##### Example

Using the previous RSA vales of $(p-1)(p-1)=4,110,048\$ and $e=2^{16}+1=65,537\$
| Euclidean Expansion | Collect Terms | | Substitute | Retrograde Substitution | Solve For d$_x$ |
| 4,110,048 | w$_0$ | + 65,537d$_0$ = 1 | | | |
| (62 $\cdot$ 65,537 + 46,754) | w$_0$ | + 65,537d$_0$ = 1 | | |
| 65,537 | (62w$_0$ + d$_0$) | + 46,754w$_0$ = 1 | w$_1$ = 62w$_0$ + d$_0$ | 4,595 = (62)(-6441) + d$_0$ | d$_0$ = 403,937 |
| 65,537 | w$_1$ | + 46,754d$_1$ = 1 | d$_1$ = w$_0$ | | w$_1$ = -6,441 |
| (1 $\cdot$ 46,754 + 18,783) | w$_1$ | + 46,754d$_1$ = 1 | | |
| 46,754 | (w$_1$ + d$_1$) | + 18,783w$_1$ = 1 | w$_2$ = w$_1$ + d$_1$ | -1,846 = 4,595 + d$_1$ | d$_1$ = -6,441 |
| 46,754 | w$_2$ | + 18,783d$_2$ = 1 | d$_2$ = w$_1$ | | |
| (2 $\cdot$ 18,783 + 9,188) | w$_2$ | + 18,783d$_2$ = 1 | | | |
| 18,783 | (2w$_2$ + d$_2$) | + 9,188w$_2$ = 1 | w$_3$ = 2w$_2$ + d$_2$ | 903 = (2)(-1,846) + d$_2$ | d$_2$ = 4,595 |
| 18,783 | w$_3$ | + 9,188d$_3$ = 1 | d$_3$ = w$_2$ | | |
| (2 $\cdot$ 9,188 + 407) | w$_3$ | + 9,188d$_3$ = 1 | | |
| 9,188 | (2w$_3$ + d$_3$) | + 407w$_3$ = 1 | w$_4$ = 2w$_3$ + d$_3$ | -40 = (2)(903) + d$_3$ | d$_3$ = -1846 |
| 9,188 | w$_4$ | + 407d$_4$ = 1 | d$_4$ = w$_3$ | | |
| (22 $\cdot$ 407 + 234) | w$_4$ | + 407d$_4$ = 1 | | | |
| 407 | (22w$_4$ + d$_4$) | + 234w$_4$ = 1 | w$_5$ = 22w$_4$ +d$_4$ | 23 = (22)(-40) + d$_4$ | d$_4$ = 903 |
| 407 | w$_5$ | + 234d$_5$ = 1 | d$_5$ = w$_4$ | | |
| (1 $\cdot$ 234 + 173) | w$_5$ | + 234d$_5$ = 1 | | | |
| 234 | (w$_5$ + d$_5$) | + 173w$_5$ = 1 | w$_6$ = w$_5$ +d$_5$ | -17 = 23 + d$_5$ | d$_5$ = -40 |
| 234 | w$_6$ | + 173d$_6$ = 1 | d$_6$ = w$_5$ | | |
| (1 $\cdot$ 173 + 61) | w$_6$ | + 173d$_6$ = 1 | | | |
| 173 | (w$_6$ + d$_6$) | + 61w$_6$ = 1 | w$_7$ = w$_6$ +d$_6$ | 6 = -17 + d$_6$ | d$_6$ = 23 |
| 173 | w$_7$ | + 61d$_7$ = 1 | d$_7$ = w$_6$ | | |
| (2 $\cdot$ 61 + 51) | w$_7$ | + 61d$_7$ = 1 | | | |
| 61 | (2w$_7$ + d$_7$) | + 51w$_7$ = 1 | w$_8$ = 2w$_7$ +d$_7$ | -5 = (2)(6) + d$_7$ | d$_7$ = -17 |
| 61 | w$_8$ | + 51d$_8$ = 1 | d$_8$ = w$_7$ | | |
| (1 $\cdot$ 51 + 10) | w$_8$ | + 51d$_8$ = 1 | | | |
| 51 | (w$_8$ + d$_8$) | + 10w$_8$ = 1 | w$_9$ = w$_8$ +d$_8$ | 1 = -5 + d$_8$ | d$_8$ = 6 |
| 51 | w$_9$ | + 10d$_9$ = 1 | d$_9$ = w$_8$ | | |
| (5 $\cdot$ 10 + 1) | w$_9$ | + 10d$_9$ = 1 | | | |
| 10 | (5w$_9$ + d$_9$) | + 1w$_9$ = 1 | w$_{10}$ = 5w$_9$ +d$_9$ | 0 = (5)(1) + d$_9$ | d$_9$ = -5 |
| 10 | w$_{10}$ | + 1d$_{10}$ = 1 | d$_{10}$ = w$_9$ | | |
| (1 $\cdot$ 10 + 0) | w$_{10}$ | + 1d$_{10}$ = 1 | | | |
| 1 | (10w$_{10}$ + d$_{10}$) | + 0w$_{10}$ = 1 | w$_{11}$ = 10w$_{10}$ +d$_{10}$ | 1 = (10)(0) + d$_{10}$ | d$_{10}$ = 1 |
| 1 | w$_{11}$ | + 0d$_{11}$ = 1 | d$_{11}$ = w$_{10}$ | w$_{11}$ = 1, d$_{11}$ = 0 | |

## Euler's Totient Function

Significant in cryptography, the totient function (sometimes known as the phi function) is defined as the number of nonnegative integers $a\$ less than $n\$ that are coprime to $n\$. Mathematically, this is represented as
    $\phi (n)=\left|{\bigg \0\leq a\leq n|\gcd(a,n)=1{\bigg \}}\right|$
Which immediately suggests that for any prime $p\$
    $\phi (p)=p-1\$
The totient function for any exponentiated prime is calculated as follows
    $\phi (p^{\alpha })\$
    $=p^{\alpha }-p^{\alpha -1}\$
    $=p^{\alpha }\left(1-{\tfrac {1}{p}}\right)\$
The Euler totient function is also multiplicative
    $\phi (ab)=\phi (a)\phi (b)\$
where $\gcd(a,b)=1\$

## Finite Fields and Generators

A field is simply a set $\mathbb {F}$ which contains numerical elements that are subject to the familiar addition and multiplication operations. Several different types of fields exist; for example, $\mathbb {R}$, the field of real numbers, and $\mathbb {Q}$, the field of rational numbers, or $\mathbb {C}$, the field of complex numbers. A generic field is usually denoted $\mathbb {F}$.

### Finite Fields

Cryptography utilizes primarily finite fields, nearly exclusively composed of integers. The most notable exception to this are the Gaussian numbers of the form $a+bi\$ which are complex numbers with integer real and imaginary parts. Finite fields are defined as follows
    $\left(\mathbb {Z} /n\mathbb {Z} \right)=\mathbb {Z} _{n}\$ The set of integers modulo $n\$
    $\left(\mathbb {Z} /p\mathbb {Z} \right)=\mathbb {Z} _{p}\$ The set of integers modulo a prime $p\$
Since cryptography is concerned with the solution of diophantine equations, the finite fields utilized are primarily integer based, and are denoted by the symbol for the field of integers, $\mathbb {Z}$.
A finite field $\mathbb {F} _{n}\$ contains exactly $n\$ elements, of which there are $n-1\$ nonzero elements. An extension of $\mathbb {Z} _{n}\$ is the multiplicative group of $\mathbb {Z} _{n}\$, written $\left(\mathbb {Z} /n\mathbb {Z} \right)^{*}=\mathbb {Z} _{n}^{*}\$, and consisting of the following elements
    $a\in \mathbb {Z} _{n}^{*}\$ such that $\gcd(a,n)=1\$
in other words, $\mathbb {Z} _{n}^{*}\$ contains the elements coprime to $n\$
Finite fields form an abelian group with respect to multiplication, defined by the following properties
A subscript following the symbol for the field represents the set of integers modulo $n\$, and these integers run from $0\$ to $n-1\$ as represented by the example below
    $\mathbb {Z} _{12}=\{0,1,2,3,4,5,6,7,8,9,10,11\}\$
The multiplicative order of $\mathbb {Z} _{n}$ is represented $\mathbb {Z} _{n}^{*}$ and consists of all elements $a\in \mathbb {Z} _{n}$ such that $\gcd(a,n)=1\$. An example for $\mathbb {Z} _{12}$ is given below
    $\mathbb {Z} _{12}^{*}=\{1,5,7,11\}\$
If $p\$ is prime, the set $\mathbb {Z} _{p}^{*}$ consists of all integers $a\$ such that $1\leq a\leq p\$. For example
| Composite n | Prime p |
| $\mathbb {Z} _{9}=\{0,1,2,3,4,5,6,7,8\}$ | $\mathbb {Z} _{11}=\{0,1,2,3,4,5,6,7,8,9,10\}$ |
| $\mathbb {Z} _{9}^{*}=\{1,2,4,5,7,8\}$ | $\mathbb {Z} _{11}^{*}=\{1,2,3,4,5,6,7,8,9,10\}$ |

### Generators

Every finite field has a generator. A generator $g\$ is capable of generating all of the elements in the set $\mathbb {Z} _{n}$ by exponentiating the generator $g\,{\bmod {\,}}n\$. Assuming $g\$ is a generator of $\mathbb {Z} _{n}^{*}$, then $\mathbb {Z} _{n}^{*}$ contains the elements $g^{i}\,{\bmod {\,}}n\$ for the range $0\leq i\leq \phi (n)-1$. If $\mathbb {Z} _{n}^{*}$ has a generator, then $\mathbb {Z} _{n}$ is said to be cyclic.
The total number of generators is given by
    $\phi \left(\phi \left(n\right)\right)$

#### Examples
## Congruences
### Description

Number theory contains an algebraic system of its own called the theory of congruences. The mathematical notion of congruences was introduced by Karl Friedrich Gauss in Disquisitiones (1801).

### Definition

If $a\$ and $b\$ are two integers, and their difference is evenly divisible by $m\$, this can be written with the notation
    $\left(a-b\right)|m\$
This is expressed by the notation for a congruence
    $a\equiv b\,{\bmod {\,}}m$
where the divisor $m\$ is called the modulus of congruence. $a\equiv b\,{\bmod {\,}}m$ can equivalently be written as
    $a-b=mk\$
where $k\$ is an integer.
Note in the examples that for all cases in which $a\equiv 0\,{\bmod {\,}}m$, it is shown that $a|m\$. with this in mind, note that
$a\equiv 0\,{\bmod {\,}}2$ Represents that $a\$ is an even number.
$a\equiv 1\,{\bmod {\,}}2$ Represents that $a\$ is an odd number.

#### Examples

| $a\equiv b\,{\bmod {\,}}m$ | $a-b=mk\$ |
| $14\equiv 5\,{\bmod {\,}}3$ | $14-5=3\cdot 3\$ |
| $-13\equiv 7\,{\bmod {\,}}4$ | $(-13)-7=4\cdot (-5)\$ |
| $90\equiv 0\,{\bmod {\,}}18$ | $90-0=18\cdot 5\$ |

### Properties of Congruences

All congruences (with fixed $m\$) have the following properties in common
    $a\equiv a\,{\bmod {\,}}m$
    $a\equiv b\,{\bmod {\,}}m$ if and only if $b\equiv a\,{\bmod {\,}}m$
    If $a\equiv b\,{\bmod {\,}}m$ and $b\equiv c\,{\bmod {\,}}m$ then $a\equiv c\,{\bmod {\,}}m$
    Given $a\equiv a\,{\bmod {\,}}m$ there exists a unique $b\$ such that $0\leq b\leq m-1\$
These properties represent an equivalence class, meaning that any integer is congruent modulo $m\$ to one specific integer in the finite field $\mathbb {Z} _{m}$.

### Congruences as Remainders

If the modulus of an integer $m>2\$, then for every integer $a\$
    $a=mk+r\,\left(r\in \mathbb {Z} _{m}\right)$
which can be understood to mean $r\$ is the remainder of $a\$ divided by $m\$, or as a congruence
    $a\equiv r\,{\bmod {\,}}m$
Two numbers that are incongruent modulo $m\$ must have different remainders. Therefore, it can be seen that any congruence $a\equiv b\,{\bmod {\,}}m$ holds if and only if $a\$ and $b\$ are integers which have the same remainder when divided by $m\$.

#### Example
### The Algebra of Congruences

Suppose for this section we have two congruences, $a\equiv b\,{\bmod {\,}}m$ and $c\equiv d\,{\bmod {\,}}m$. These congruences can be added or subtracted in the following manner
    $a+c\equiv b+d\,{\bmod {\,}}m$
    $a-c\equiv b-d\,{\bmod {\,}}m$
If these two congruences are multiplied together, the following congruence is obtained
    $ac\equiv bd\,{\bmod {\,}}m$
or the special case where $c=d\$
    $ac\equiv bc\,{\bmod {\,}}m$
Note: The above does not mean that there exists a division operation for congruences. The only possibility for simplifying the above is if and only if $c\$ and $m\$ are coprime. Mathematically, this is represented as
    $ac\equiv bc\,{\bmod {\,}}m$ implies that $a\equiv b\,{\bmod {\,}}m$ if and only if $\gcd \left(c,m\right)=1$
The set of equivalence classes defined above form a commutative ring, meaning the residue classes can be added, subtracted and multiplied, and that the operations are associative, commutative and have additive inverses.

### Reducing Modulo m

Often, it is necessary to perform an operation on a congruence $a\equiv b\,{\bmod {\,}}m$ where $b>m\$, when what is desired is a new integer $d\$ such that $0\leq d\leq m-1\$ with the resultant $d\$ being the least nonnegative residue modulo m of the congruence. Reducing a congruence modulo $m\$ is based on the properties of congruences and is often required during exponentiation of a congruence.

#### Example

Note that $4\$ is the least nonnegative residue modulo $5\$

### Exponentiation

Assume you begin with $a\equiv b\,{\bmod {\,}}m$. Upon multiplying this congruence by itself the result is $a^{2}\equiv b^{2}\,{\bmod {\,}}m$. Generalizing this result and assuming $n\$ is a positive integer
    $a^{n}\equiv b^{n}\,{\bmod {\,}}m$

#### Example
### Repeated Squaring Method

Sometimes it is useful to know the least nonnegative residue modulo $m\$ of a number which has been exponentiated as $a^{2}\equiv \,{\bmod {\,}}m$. In order to find this number, we may use the repeated squaring method which works as follows:

#### Example
### Inverse of a Congruence
#### Description

While finding the correct symmetric or asymmetric keys is required to encrypt a plaintext message, calculating the inverse of these keys is essential to successfully decrypt the resultant ciphertext. This can be seen in cryptosystems Ranging from a simple affine transformation
    $C\equiv aP+b\,{\bmod {\,}}N$
Where
    $P\equiv a^{-1}C+b^{-1}\,{\bmod {\,}}N$
To RSA public key encryption, where one of the deciphering (private) keys is
    $d_{A}=e_{A}^{-1}\,{\bmod {\,}}\phi \left(n_{A}\right)$

#### Definition

For the elements $a\in \mathbb {Z} _{m}$ where $\gcd \left(a,m\right)=1$, there exists $b\in \mathbb {Z} _{m}$ such that $ab\equiv 1\,{\bmod {\,}}m$. Thus, $b\$ is said to be the inverse of $a\$, denoted $a^{-n}\,{\bmod {\,}}m$ where $n\$ is the $n^{th}\$ power of the integer $b\$ for which $ab\equiv 1\,{\bmod {\,}}m$.

##### Example
### Fermat's Little Theorem
#### Definition

Where $p\$ is defined as prime, any integer will satisfy the following relation:
    $a^{p}\equiv a\,{\bmod {\,}}p$

#### Example

When $a=2\$ and $p=19\$
    $2^{2}\equiv 23\,{\bmod {\,}}19$
    $2^{4}\equiv 529\equiv 16\,{\bmod {\,}}19$
    $2^{8}\equiv 256\equiv 9\,{\bmod {\,}}19$
    $2^{16}\equiv 81\equiv 5\,{\bmod {\,}}19$
    $16+2+1=19\$ implies that $5\cdot 23\cdot 2=230\equiv 2\,{\bmod {\,}}19$

#### Conditions and Corollaries

An additional condition states that if $a\$ is not divisible by $p\$, the following equation holds
    $a^{p-1}\equiv 1\,{\bmod {\,}}p$
Fermat's Little Theorem also has a corollary, which states that if $a\$ is not divisible by $p\$ and $n\equiv m\,{\bmod {\,}}\left(p-1\right)$ then
    $a^{n}\equiv a^{m}\,{\bmod {\,}}p$

#### Euler's Generalization

If $\gcd \left(a,m\right)=1\$, then $a^{\phi \left(m\right)}\equiv 1\,{\bmod {\,}}m$

### Chinese Remainder Theorem

If one wants to solve a system of congruences with different moduli, it is possible to do so as follows:
    $x\equiv a_{1}\,{\bmod {\,}}m_{1}$
    $x\equiv a_{2}\,{\bmod {\,}}m_{2}$
    $\cdots$
    $x\equiv a_{k}\,{\bmod {\,}}m_{k}$
A simultaneous solution $x\$ exists if and only if $\gcd \left(m_{i},m_{j}\right)=1$ with $\left(i\neq j\right)\$, and any two solutions are congruent to one another modulo $M=m_{1}m_{2}\ldots m_{k}\$.
The steps for finding the simultaneous solution using the Chinese Remainder theorem are as follows:
    1. Compute $M\$
    2. Compute $M_{i}=M/m_{i}\$ for each of the different $i\$'s
    3. Find the inverse $N\$ of $M_{i}\,{\bmod {\,}}m_{i}$ for each $i\$ using the Extended Euclidean algorithm
    4. Multiply out $a_{i}M_{i}N_{i}\$ for each $i\$
    5. Sum all $a_{i}M_{i}N_{i}\$
    6. Compute $\sum _{i=1}^{k}a_{i}M_{i}N_{i}\,{\bmod {\,}}M$ to obtain the least nonnegative residue

#### Example
## Quadratic Residues

If $p\$ is prime and $>2\$, examining the nonzero elements of $\mathbb {Z} _{p}=\{1,2,\ldots ,p-1\}$, it is sometimes important to know which of these are squares. If for some $a\in \mathbb {Z} _{p}^{*}$, there exists a square such that $b^{2}=a\$. Then all squares for $\mathbb {Z} _{p}^{*}$ can be calculated by $b^{2}\,{\bmod {\,}}p$ where $b=1,2,\ldots ,\left(p-1\right)/2\$. $a\in \mathbb {Z} _{n}^{*}$ is a quadratic residue modulo $n\$ if there exists an $x\in \mathbb {Z} _{n}^{*}$ such that $a\equiv x^{2}\,{\bmod {\,}}n$. If no such $x\$ exists, then $a\$ is a quadratic non-residue modulo $n\$. $a\$is a quadratic residue modulo a prime $p\$ if and only if $a^{\tfrac {p-1}{2}}\,\mod \,p=1$.

### Example

The values above are quadratic residues. The remaining (in this example) 9 values are known as quadratic nonresidues. the complete listing is given below.

### Legendre Symbol

The Legendre symbol denotes whether or not $a\$ is a quadratic residue modulo the prime $p\$ and is only defined for primes $p\$ and integers $a\$. The Legendre of $a\$ with respect to $p\$ is represented by the symbol $L\left({\tfrac {a}{p}}\right)$. Note that this does not mean $a\$ divided by $p\$. $L\left({\tfrac {a}{p}}\right)$ has one of three values: $0,1,-1\$.
$L\left({\tfrac {a}{p}}\right){\begin{cases}0,&{\mbox{if }}p{\mbox{ divides }}a{\mbox{ evenly}}\\1,&{\mbox{if }}a{\mbox{ is a quadratic residue modulo }}p\\-1,&{\mbox{if }}a{\mbox{ is a quadratic nonresidue modulo }}p\end{cases}}$

### Jacobi Symbol

The Jacobi symbol applies to all odd numbers $n>3\$ where $n=p_{1}^{e_{1}}p_{2}^{e_{2}}\ldots p_{m}^{e_{m}}\$, then:
    $J\left({\tfrac {a}{n}}\right)=L\left({\tfrac {a}{p_{1}}}\right)^{e_{1}}L\left({\tfrac {a}{p_{2}}}\right)^{e_{2}}\ldots L\left({\tfrac {a}{p_{m}}}\right)^{e_{m}}$
If $n\$ is prime, then the Jacobi symbol equals the Legendre symbol (which is the basis for the Solovay-Strassen primality test).

## Primality Testing
### Description

In cryptography, using an algorithm to quickly and efficiently test whether a given number is prime is extremely important to the success of the cryptosystem. Several methods of primality testing exist (Fermat or Solovay-Strassen methods, for example), but the algorithm to be used for discussion in this section will be the Miller-Rabin (or Rabin-Miller) primality test. In its current form, the Miller-Rabin test is an unconditional probabilistic (Monte Carlo) algorithm. It will be shown how to convert Miller-Rabin into a deterministic (Las Vegas) algorithm.

### Pseudoprimes

Remember that if $p\$ is prime and $gcd\left(b,m\right)=1$, Fermat's Little Theorem states:
    $a^{p-1}\equiv 1\,{\bmod {\,}}p$
However, there are cases where $p\$ can meet the above conditions and be nonprime. These classes of numbers are known as pseudoprimes.
$m\$ is a pseudoprime to the base $a\$, with $gcd\left(a,m\right)=1$ if and only if the least positive power of $a\$ that is congruent to $1{\bmod {\,}}p$ evenly divides $p-1\$.
If Fermat's Little Theorem holds for any $p\$ that is an odd composite integer, then $p\$ is referred to as a pseudoprime. This forms the basis of primality testing. By testing different $a\$'s, we can probabilistically become more certain of the primality of the number in question.
The following three conditions apply to odd composite integers:
    I. If the least positive power of $a\$ which is congruent to $1\,{\bmod {\,}}n$ and divides $n-1\$ which is the order of $a\$ in $\mathbb {Z} _{n}^{*}$, then $n\$ is a pseudoprime.
    II. If $n\$ is a pseudoprime to base $a_{1}\$ and $a_{2}\$, then $n\$ is also a pseudoprime to $a_{1}a_{2}\,{\bmod {\,}}n$ and $a_{1}a_{2}^{-1}\,{\bmod {\,}}n$.
    III. If $n\$ fails $a^{p-1}\equiv 1\,{\bmod {\,}}p$, for any single base $a\in \mathbb {Z} _{p}^{*}$, then $n\$ fails $a^{p-1}\equiv 1\,{\bmod {\,}}p$ for at least half the bases $a\in \mathbb {Z} _{p}^{*}$.
An odd composite integer for which $a^{p-1}\equiv 1\,{\bmod {\,}}p$ holds for every $a\in \mathbb {Z} _{p}^{*}$ is known as a Carmichael Number.