Cryptography Introduction (Unfinished)
Cryptography Introduction (Unfinished)
Implementation
Understanding and Implmenting Cryptographic functions in Mathematica
and C++
Travis Melka
B.S. Mathematics, Michigan Technological University∗
[email protected]
February 2019
∗
I am no longer affiliated with Michigan Technological University, and this work is neither
authorized nor approved by Michigan Technological University
0.1 Introduction
This document will pertain to the introduction of cryptography, using both Mathematica
and C++ to implement various cryptographic functions, and will explain the prerequisite
mathematics either in the beginning, or before the topic they require. Question or
suggestions should be emailed to the author at ([email protected]).
This document is free and allowed to be used anywhere, by anyone, but the author
wishes to be notified when such a thing occurs. For example, if a teacher wishes to use
this in a class on Cryptography, that is allowed, but the author wishes to be notified. If
you wish to inquire regarding the original LATEX code- for example, for use an ”Intro-
duction to LATEX class” - the same applies, just inquire at the email above if you need
the original LATEX code.
2
1 Basic Mathematics Used in Cryptography
2. Commutative Property: a + b = b + a.
4. Identity Element: There exists (∃) an element ω ∈ Z such that for any a ∈ Z,
a + ω = a. We denote this element as 0.
These are all the properties we need to define an Abelian group. If we did not have
property 4, the commutative property, we would just have a regular group, an example
of which are matrices over the real numbers, which will be discussed later.
3
1 Basic Mathematics Used in Cryptography
2. Commutative Property: a · b = b · a.
5. Zero Divisors: The only element that exhibits the property that α · β = 0 is 0.
This can also be stated that if α · β = 0, then either α or β is 0.
Proof : Recall that, in the addition section, we defined 0 as the additive identity.
So, 0+a=a. Multiplying this by b, we get, using the distributive property 0·b+a·b =
a · b. Subtracting (adding the inverse of) a · b, we get:
0·b+a·b−a·b=a·b−a·b
0·b=0
Hence 0 · b = 0 ∀ b.
4
1.3 Divisibility
This combination of these two operators in this way is what defines a ring. In fact, it
defines a commutative ring, as both operations possess the commutative property. In
addition, since the multiplication operator possess no zero divisors, we call this special
type of commutative ring an Integral Domain. This is important because we use
integral domains to entertain the concept of divisibility, which will be discussed in the
next section.
1.3 Divisibility
Divisibility will be a very important concept throughout this document, it will be seen in
quite a few cryptographic systems, especially the older ones. Divisibility is quite a simple
concept in and of itself, and can be defined as follows: If {a, b, c} ∈ Z, then a divides b
if we can find an element, c such that a · c = b. Symbolically, we denote this relationship
as a | b. This is obviously true only for some numbers, because some numbers are not
divisible by others. For example, take a = 3 and b = 16. Then @ c ∈ Z such that a·c = b,
so we say that b is not divisible by a, or a does not divide b, symbolically a - b (Note
that @ means ”does not exist”).
Divisibility has several properties we can prove. Here is a list of some of them and
their proofs:
1. All numbers divide both 0 and themselves: ∀a ∈ Z, a | 0 and a | a. In addition,
1 | a ∀a.
Proof : Recall that a · 0 = 0 by the definition in section 1.1.2. We now need to find
a number, η, such that a · η = 0. In fact, as we stated in 1.1.2, we know that η = 0,
so a | 0. For the second part, we need find a number b such that a · b = a. Recall
that in section 1.1.2, we also dealt with this case, and defined b as the identity
element 1, so there exists a number η such that a · η = a, where η = 1. Therefore
a | a, and so does 1.
2. If a | b and b | c, then a | c.
Proof : If a | b, then ∃ α such that a · α = b. Similarly, if b | c, then ∃ β such that
b · β = c. Substituting the expression for a into b, we get:
b·β =c
(a · α)β = c
a·α·β =c
By the properties discussed in 1.1.2, ∃ γ such that γ = α · β, so a · γ = c, meaning
we have shown what we wanted to show, that a | c.
α · b + β · c = α · ·a · `1 + β · a · `2 = a · (α · `1 + β · `2 )
5
1 Basic Mathematics Used in Cryptography
4. If a | c or a | b, then a | b · c.
PROOF: If a | c, then a · j = c. Multiplying both sides by b, we get
a·j·b=c·b
a·ω =c·b
As multiplication is closed.
PROOF: Suppose the above theorem is false. This implies ∃ ω ∈ Z such that ω is
not a product of primes, and is > 1. This means ω is not prime (otherwise it would be
a factor of itself), so ω is composite. This means ∃ a, b ∈ Z such that ω = a · b. But
neither a nor b are prime, so have their own factorization into primes. This means that
ω IS a factor of primes, So our assumption that the theorem is false is not correct. So
the theorem must be true.
To prove uniqueness of this factorization, however, we require a lemma which will be
proven in the near future.
6
1.4 Prime Numbers
Where j, z > 0, and ai 6= bj ∀ i, j. But by the lemma, since ai | n, it must divide one
of the bj . This can only occur if ∃ η such thataη = bj . This contradicts the assumption
that ai 6= bj ∀ i, j, hence the two factorizations must be the same. This means that such
a factorization is unique.
1.4.2 The Prime Number Theorem and the Prime Counting Function
It was shown in the time of the Ancient Greeks, said to have been proven by Euclid
(though this isn’t known for certain, he could have gotten his arguments from someone
before him), but how many are there up to a certain number, say x? A theorem proven
in 1896, independently by Hadamard and Poussin, answers that question
Prime Number Theorem: Let π(x) denote the number of primes less than x (The
”prime counting function”). Then, we can prove the following relation:
x
π(x) ∼
ln (x)
7
1 Basic Mathematics Used in Cryptography
gcd(a, b) = {k ∈ Z : k | a ∧ k | b ∧ k ≥ j ∀ j | a ∧ j | b}
We call two numbers relatively prime if their gcd is 1. There are several ways to find
the gcd of two numbers, but we will talk about two main ways.
1. Factor the number into it’s prime factorization, Qkandj look for Qkthe highest number
that occurs in both factorizations. So, if β = i=0 ai , ω = i=0 bi , then we look α
for the largest aji ·aks ·... in β that also occurs in ω (afi ·aks ·... = b`i ·bqt ·..., where f, `, t, q
do not necessarily equal ji , αw ). For example, 300 = 22 · 3 · 52 , and 720 = 24 ∗ 32 ∗ 5.
The largest number that divides both of these is 22 ·3·5 = 60, since it contains all of
the prime factors that occur in both numbers. Hence, gcd(300, 720) = 22 · 3 · 5 = 60
2. First, we will need the following Lemma, which is proven in the next section:
∀z ∈ Z, ∃{k, q, r} ∈ Z, with{k, q} ≤ z such that z = k · q + r. Using this, we can
use a process called the The Euclidean Algorithm to factorize the number. This
process is much faster than finding the prime factorizaiton for very large integers.
2. Find the euclidean division of a. That is, fine {qk , rk } such that a = qk · b + rk
a = q1 · b + r1
b = q2 · r1 + r2
r1 = q3 · r2 + r3
...
rk−1 = qk+1 · rk + rk+1
rk = qk+2 · rk+1
This leads us to the conclusion that gcd(a, b) = rk
8
1.4 Prime Numbers
For a example done by hand, we will use the numbers we used before, 300 and 720.
300 = 2 · 120 + 60
120 = 2 · 60
So, as we found before, gcd(300, 720) = 60.
Note that, this works because gcd(a, b) = gcd(a − b, b) = gcd(b, a − b). We can
prove this. Let d = gcd(a, b), So d | a, d | b, so a = α · d, and b = β · d. Hence,
a − b = α · d − β · d = d · (α − β), so d | (a − b). Let e = gcd(b, a − b), then e | b and
e | (a − b). Similarly, b − (a − b) = e(ω − η) = −a. so e | a, and e | b. This implies that
d ≥ e, because both are a common factor, and d = gcd(a, b). Since d | (a − b) and d ≥ e,
then, since e = gcd(b, a − b), if d > e then d would be the gcd(b, a − b) and not e, so d
must equal e.
There is another way to code this with recursion, utlizing modular arithmetic, which
will be introduced in the next section. However, without recursion, we can do this in a
simple loop. For now, just think of a%b as the remainder of ab (It basically is, but we
will dicuss this in detail in the next section).
The actual calculation of the gcd is the final WHILE loop, the rest is just taking care
of the cases that could affect the program- such as a < b and a < 0 || b < 0. This is
9
1 Basic Mathematics Used in Cryptography
In addition, we could also apply the recusrive function method in Mathematica, and it
would be similar to the C++ code above.
Bezout’s Identity
Now, let r be the smallest non-negative element of S. This means ∃ Γ such that
r = A − Γq, rearranged to A = r + Γq. If r is the smallest non-negative element of the
set S, this means that all other elements of the set are at least q units away from r. This
means that r < q, and since r is non-negative, 0 < r < q. We can do this for any A ∈ Z
we want to, as we didn’t specify what integer A was. Hence we have shown that such
an r exists, independent of A.
PROOF OF UNIQUENESS: Now, all that’s left to show is that this is the only
r that satisfies the above conditions. Suppose another set of {q, r} exists that satisfies
A = k · q + r. Let’s denote these new q and r as Q and R. So we have
A = k · q + r, A = k · Q + R
r−R=k·Q−k·q
10
1.5 Congruence Relations and Modular Arithmetic
r − R = k · (Q − q)
Since r and R are both between 0 and k by definition, their difference, r − R, is as well.
This implies the following: k > r − R = k · (Q − q) ≥ k which gives us the absurd
statement, k > k, which is obviously false, except for one case: if r − R = Q − q = 0,
then we have that k > 0 = 0 ≥ k → k ≥ k which is true. r = R and q = Q follows from
this statement. So, we have shown that ∃ ∀ A ∈ Z a pair of integers {q, r} exist such
that A = k · q + r and that these {q, r} are unique when 0 < r < A.
We can view this as the foundation of discussing Congruence relations and modular
arithmetic. If we can write A = k · q + r, r 6= 0, then we can define the congruence
relation between A and q as
A ≡ r mod q
Another way to say this is that we define A ≡ r mod n if n | A − r. These are
equivalent statements,because n | A − r → A − r = k · n for some k. Rearranged,
A = k · n + r, which is the form of breaking down A we just discussed above. Note that
if A ≡ 0 mod n, then n | A − 0 → n | A, meaning A is a multiple of n.
11