Number Theory and its Application to Cryptography, Combinatorics, Relations
For a comprehensive subject on number theory the reader is referred to the book [Hardy and Wright(1975)].
Elementary number theory
Prime factorisation
Fundamental theorem of arithmetic
Fundamental theorem of arithmetic - in number theory
Every integer
is either prime itself or the product of prime numbers, where this product is unique, up to the order of the factors. In other words

where
is the
-th prime number arising in the product generating
and
is its multiplicity.
Prime factorisation algorithm
The prime factorisation is an iterative algorithm to determine the prime factors of a given number. The algorithm is based on the Fundamental theorem of arithmetic. The prime factors are determined by performing iterative division by prime numbers in increasing order.
Example Prime factorisation of

Prime factorisation is believed to be difficult to perform practically for large number since its time complexity is NP, i.e. superpolynomial (= not bounded above by any polynomial).
Congruence
Modulo operator
Definition - modulo operator
The
the remainder after dividing
by
.
It follows

Operator identities of modulo operator are given as
- Distributivity - addition
![{\displaystyle (a+b)\mod n=[(a\mod n)+(b\mod n)]\mod n}](https://wikimedia.org/api/rest_v1/media/math/render/svg/b84aa6919c308ae1fb583abf7b300858bee64871)
- Distributivity - multiplication
![{\displaystyle ab\mod n=[(a\mod n)*(b\mod n)]\mod n}](https://wikimedia.org/api/rest_v1/media/math/render/svg/19e56fcbad152388c6710c96440aa873dba6a802)
- Distributivity - power

These identities can be proved directly from the definition of the modulo operator. Due to these three identities one can think in modular arithmetic including only addition, multiplication and power, like in regular integer arithmetic without modulo.
Greatest common divisor
Divisability
Definition - divisability
is dividable by a if and only if there exists an integer
for which
. This is denoted by
. The number
is called the divisor of
.
Greatest common divisor operator
Definition - greatest common divisor, gcd() operator The greatest common division of the integers
and
,
is defined by

An alternative notation of

is

.
The operator characteristics of the
operator are given by
- Reflexivity:

is neutral element with respect to the operator
. In other words 
- The following relation holds

Proof.
.
Let
be, where
. It follows that
, since otherwise
and due to expression of
also
were dividable by
which would lead to
. Then
, where the last step comes from
. Thus
, due to
.
Congruence
Definition - relatively prime (also called as coprime)
The integers
and
are relatively prime if and only if
.
Definition - congruence
The congruence is a relation and refers to a base number
. The integers
and
are in congruence relation if and only if
. The congruence id denoted by
.
Note that
implies
. Congruence is an equivalence relation - partitioning the set
to
disjunct subsets.
Being an equivalence relation, congruence have the following properties:

The operator identities of the congruence refer to given
and
and can be given as
CO1. Adding a constant

CO2. Multiplication by constant

CO3. Addition of congruences

CO4. Multiplication of congruences

CO5. Division by constant

The first three identities can be proved directly from the definitions of the modulo and congruence operators. The last identity follows from the general identity for division by constant:

where

.
Proof:
![{\displaystyle {\begin{aligned}&\alpha *a\equiv \alpha *b\mod m\Leftrightarrow m|[\alpha *(b-a)]\Leftrightarrow \\&[m/d]|[\alpha /d*(b-a)]\Leftrightarrow [m/d]|[(b-a)],\end{aligned}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e897823e0b6c98283b97b2ee6f55b80509871cfd)
since

. Then
![{\textstyle [m/d]|[(b-a)]\Leftrightarrow a\equiv b\mod (m/d)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/3aae9a3d271f24073e20b91f5dbf8ed0e5dfcc1a)
.
Congruence class
Definition - Congruence class modulo
(also called residue class modulo
)
Congruence class modulo
is any of the disjunct subsets resulted by partitioning the set
by congruence as equivalence relation. In other words congruence class modulo
is a set
for any
. For a specific
the congruence class modulo
is denoted by
.
The set of all congruence classes modulo
is denoted by
. Thus the number of elements in this set is
, and this set can be given as

The set of congruence classes relative prime to
is denoted by
. For any
representing an element of
holds
.
Example

If m is prime then number of elements in
=
.
Diophantic equations
Euclidean algorithm
The Euclidean algorithm is the standard way of solving the equation of the form

where
Theoretic basics
Bézout’s identity
The greatest common divisor
of two integers
and
can be represented as a linear sum of the original two numbers
and
. In other words

Theorem
The set of all integer linear combinations of the nonzero integer
and
equals the set of integer muliples of
, i.e:

Proof:
- Step 1: direction
- follows from definition of 
- Step 2: direction
- follows from Bézout’s identity 
Corollary 1
The equation

has a solution if and only if

.
Proof: It follows directly from the above theorem.
Notes:
- Case 1: If
then the equation has always a solution.
- Case 2: If
then this case can be fallbacked to Case 1: 
Corollary 2
The equation

has always a solution.
Proof: It follows directly from Corollary 1.
Solution of equation
The solution of the equation

consists of two steps as
- determination of
- by means of base variant of Euclidean algorithms and
- determination of
and
- by means of the extended variant of the algorithm.
Euclidean algorithm - base variant
The idea of the base variant of Euclidean algorithms is the recursive application of
until
.
Example - determination of
:

The Pseudo code of determination gcd(a,b) (as well as storing the coefficients q[i]-s) by means of the base variant of the Euclidean algorithm can be given as
![{\displaystyle {\begin{aligned}&h=a;\mathrm {~the~higher~value~} \\&s=b\mathrm {~the~smaller~value~} \\&i=0;\\&while(s>0)\mathrm {~until~} s\mathrm {~reaches~} 0\\&\{\\&\mathrm {\ \ } q[i++]=h\div s;\\&\mathrm {\ \ } t=s;\\&\mathrm {\ \ } s=h\%s;\mathrm {~next~} s\\&\mathrm {\ \ } h=t;\mathrm {~next~} h\\&\}\\&n=i-1;\\&gcd=h;\\\end{aligned}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/24ba1be60c3a1642184650b10990e9144cdf718a)
Euclidean algorithm - extended variant
The extended Euclidean algorithm is used to determine the unknowns
and
after carrying out the base variant of the Euclidean algorithm. This is a necessary previous step, since the extended Euclidean algorithm uses the quotient and remainders computed in the course of executing the steps of the base variant of Euclidean algorithms. The idea of extended Euclidean algorithm is a recursive application of backward substitution based on the steps of the base variant of Euclidean algorithms. This is shown in the next example.
Example - determination of
and
:

The time complexity
The time complexity of both the base and extended variant of Euclidean algorithm is
, where
is the number of digits in the smaller number among
and
.
Fermat–Euler theorem and little Fermat theorem)
Euler’s phi function
Definition - 
Euler’s phi function,
, also called as Euler’s totient function, is defined as the number of integers
in the range
, for which
and
are relatively primes. In other words

where

stands for the cardinality of the set

. Note that

never includes

, since

.

Example - Computation of

The properties of
can be given as
- If p is prime then
.
- If (m,n)=1 then
.
- If p and q are prime then

Proof. This property follows from properties 2. and 1.
- In general
can be expressed by the prime factorisation of
as
, where
.
The order of congruence classes relative prime to
,
is exactly
. If m prime then this order is
.
Fermat–Euler theorem
Fermat–Euler theorem - also called as Euler’s theorem.
If
and
are relatively prime then

Fermat’s little theorem
Fermat’s little theorem - If
is prime and
and
are relatively prime then

Fermat’s little theorem is a special case Fermat–Euler theorem for the case where
is prime.
Modular multiplicative inverse and its computation
Residue systems modulo 
Definition - Complete system of residues modulo m
The complete system of residues modulo
is any set of
integers so that each element comes from a different congruence class modulo
.
Definition - Least residue system modulo 
Least residue system modulo
is the set 0,1,..., m-1.
Definition - Reduced residue system modulo 
Reduced residue system modulo
is a set obtained from complete system of residues modulo
by deleting all elements being not coprime with
.
Modular multiplicative inverse of an integer
Definition - Modular multiplicative inverse of an integer 
The modular multiplicative inverse of an integer
, is the integer x that is given by

Observe, that not every elements of a complete system of residues modulo
has modular multiplicative inverse !
Theorem
An integer
element of complete system of residues modulo
has modular multiplicative inverse if and only if
and
are relatively prime, in other words if
.
Proof:

The modular multiplicative inverse of integer
is denoted by
. This can be explained by dividing the relation the relation
formally by
. With this notation holds the relation

If exists, the modular multiplicative inverse of an integer
is determined uniquely
Corollary
If
is prime then each of the elements
of the least residue system modulo
has modular multiplicative inverse.
Proof: Each element of
and
are relatively primes.
Computation of the modular multiplicative inverse
The modular multiplicative inverse of an integer
can be computed on the following ways.
Modular exponentiation by squaring
Modular exponentiation is raising an integer to a higher power modulo
, in other words computing
. Exponentiation by squaring is an efficient computation of modular exponentiation. The idea of exponentiation by squaring is to compute
by the help of successive squares of
. This can be implemented as follows.
- Rearrange
by using the binary representation of

Only those terms of the product must be computed, for which
, since for
the term
becomes
.
The terms with
becomes powers of
as
.
- Computation of the powers
successively by applying 
- Computate each term with mod
.
Example - Exponentiation by squaring

Conclude that instead of 52 multiplications only 5 squaring and 3 multiplications were needed.
Discrete logarithm
Primitive root modulo m
Definition - primitive root modulo 
The number
is primitive root modulo
if for every integer
being coprime to
(i.e.
) there is an integer
for which
.
Interpretation
All elements of the set of congruence classes coprime to
,
can be generated by power of
modulo
. Therefore
is also called as generator of the set of congruence classes coprime to
and
must be coprime to
.
Definition - Multiplicative order of primitive root modulo 
The multiplicative order of primitive root modulo
is the lowest power of a which is congruent to
modulo
.
Statement The multiplicative order of primitive root modulo
is
.
This is because
- this power of a is congruent to
modulo
, since
due to Euler’s theorem and
- this is the lowest power as the other lower powers are needed to generate the other elements of the set
.
It follows that if
is prime then the multiplicative order
.
Note that not all elements of
are primitive root modulo m ! However the non-primitive root modulo
elements are cyclic generator of a subset of
. The non-primitive root elements of
have also multiplicative order, which is can be however less then
.
Example -
prime

Discrete logarithm to the base
modulo 
Definition - Discrete logarithm (also called index)
Discrete logarithm, also called index When
then the
is called as discrete logarithm of the integer
to the base
modulo
. Here
is primitive root modulo
, and
is an element of
, i.e.
coprime to
.
The practical importance of the discrete logarithm lies in its property that computation of discrete logarithm is believed to be difficult to perform, especially for some specific groups.
Application to cryptography
RSA cryptography
RSA is a cryptosystem, called after its developers: Rivest, Shamir and Adleman. The principle of the key generation can be described by means of the following steps.
- Step 1. Select large primes
and
, e.g. each of them with size
bits.
- Step 2. Compute

- Step 3. Compute

- Step 4. Select a random integer
, such that
is coprime to 
- Step 5. Determine an integer
, such that
, i.e.
is modular multiplicative inverse of
.
The integer
can be computed
- either by using the extended Euclidean algorithm
- or by the help of Euler’s theorem and using exponentiation by squaring.
The keys are called as
- private key = d
- public keys = e, n
Secret parameters of the key generation:
.
The usage of RSA for encryption can be explained as
- Notation:
= message,
= chipertext - both in form natural numbers
- Condition:
- needed for the correct decryption, see below.
- The encryption end decryption processes
- The encryption

- The decryption

The correctness of the RSA cryptographic algorithm can be shows by showing

Proof: Using the way of generation of
we have

By using it, the decrypted chipertext

can be rearranged as
![{\displaystyle {\begin{aligned}m'&=c^{d}\mod n=(m^{e})^{d}\mod n=m^{e*d}\mod n=m^{1+k*\phi (n)}\mod n=\\&=[(m^{1}\mod n)*((m^{\phi (n)})^{k}\mod n)]\mod n=\\&\mathrm {\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ } \uparrow \mathrm {~condition~} m<n\\&=[m*((m^{\phi (n)}\mod n)^{k})\mod n]\mod n=\\&\mathrm {\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ } \uparrow \mathrm {~Euler's~theorem~} \\&=(m*1)\mod n=m\\&\mathrm {\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ } \uparrow \mathrm {~condition~} m<n.\end{aligned}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/3d32e74ca0efa4f2ec340e516385f1ad6ae49cf7)
Note, that proof of correctness is also possible by using Fermat’s little theorem utilizing the fact that
, i.e. multiplication of two primes.
RSA can be used both for
- encryption and
- authentication,
in both cases including key distribution. Proof of correctness for using RSA for authentication can be shown similar to that one provided for the case of using it for encryption.
The secrecy of RSA is based on the computational difficulty of prime factorization.
If n could be factorized on efficient way then the secrecy of RSA would be broken !!
Diffie–Hellman key exchange
Diffie–Hellman key exchange is a protocol for secure key distribution. The principle of the solution is to generate the common secret key without exchanging the key itself. This is achieved on the way that each party shares only a partial info on the key to be generated.
The Diffie-Hellman key establishment protocol can be described by the following steps.
- Step 1. The parties A and B agree on a prime
and a natural number
, which is a primitive root modulo
.
- Step 2. Each party selects a secret number
and
, and computes the power
and
, respectively.
- Step 3. Each party sends
and
to the other party.
- Each party computes the common secret key
based on the received numbers
and
as
and
.
Keys arising in the key exchange protocol are classified as
- Public keys:
and 
- Private keys:
and 
The operation way of the Diffie-Hellman key exchange protocol is shown in Figure 1.
The equality of the keys generated on the different sides can be shown as follows.

The secrecy of Diffie-Hellman key exchange protocol is based on the computational difficulty of the discrete logarithms, more precisely on determining
from
or
from
in the knowledge of
and
or
and Fehler beim Parsen (MathML mit SVG- oder PNG-Rückgriff (empfohlen für moderne Browser und Barrierefreiheitswerkzeuge): Ungültige Antwort („Math extension cannot connect to Restbase.“) von Server „https://wikimedia.org/api/rest_v1/“:): {\textstyle p}
.
Combinatorics
For an introduction to basics of combinatorics see Permutation and Combination.
Relations
For an introduction and overview on mathematical relations see Relation.