Cryptography And Cryptanalysis In Matlab Creating And Programming Advanced Algorithms 1st Edition Marius Iulian Mihailescu instant download
Cryptography And Cryptanalysis In Matlab Creating And Programming Advanced Algorithms 1st Edition Marius Iulian Mihailescu instant download
https://ebookbell.com/product/cryptography-and-cryptanalysis-in-
matlab-creating-and-programming-advanced-algorithms-1st-edition-
marius-iulian-mihailescu-34607982
https://ebookbell.com/product/cryptography-and-cryptanalysis-in-
java-2nd-edition-2nd-edition-stefania-loredana-nita-58449812
https://ebookbell.com/product/cryptography-and-cryptanalysis-in-java-
creating-and-programming-advanced-algorithms-with-java-se-17-lts-and-
jakarta-ee-10-1st-edition-stefania-loredana-nita-42712576
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
with-c20-marius-iulian-mihailescu-46619890
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
with-c23-creating-and-programming-advanced-algorithms-2nd-edition-
marius-iulian-mihailescu-50581684
Pro Cryptography And Cryptanalysis Creating Advanced Algorithms With C
And Net 1st Edition Marius Iulian Mihailescu
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
creating-advanced-algorithms-with-c-and-net-1st-edition-marius-iulian-
mihailescu-52878520
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
creating-advanced-algorithms-with-c-and-net-1st-edition-marius-iulian-
mihailescu-stefania-loredana-nita-15202074
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
with-c23-creating-and-programming-advanced-algorithms-2nd-edition-
marius-iulian-mihailescu-50594372
https://ebookbell.com/product/pro-cryptography-and-cryptanalysis-
with-c23-creating-and-programming-advanced-algorithms-2nd-edition-2nd-
edition-marius-iulian-mihailescu-50569844
https://ebookbell.com/product/history-of-cryptography-and-
cryptanalysis-codes-ciphers-and-their-algorithms-1st-edition-john-f-
dooley-7322434
Cryptography
and Cryptanalysis
in MATLAB
Creating and Programming
Advanced Algorithms
—
Marius Iulian Mihailescu
Stefania Loredana Nita
Cryptography and
Cryptanalysis in MATLAB
Creating and Programming
Advanced Algorithms
v
Table of Contents
vi
Table of Contents
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 80
References���������������������������������������������������������������������������������������������������������������������������������� 80
vii
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 191
ix
About the Authors
Marius Iulian Mihailescu, PhD is an Associate Professor (Senior Lecturer) at the
Spiru Haret University of Bucharest, Romania. He is also the CEO of Dapyx Solution
Ltd., a company based in Bucharest, Romania that is focused on information security-
and cryptography-related research projects. He is a lead guest editor for applied
cryptography journals and a reviewer of multiple publications with information
security and cryptography profiles. He has authored and co-authored more than 30
articles in conference proceedings, 25 articles in journals, and four books. For more
than six years, he has served as a lecturer at well-known national and international
universities (University of Bucharest, Titu Maiorescu University, Kadir Has University,
Istanbul, Turkey). He has taught courses on programming languages (C#, Java, C++, and
Haskell) and object-oriented system analysis and design with UML, graphs, databases,
cryptography, and information security. He served for three years as an IT Officer at
Royal Caribbean Cruises Ltd., where he dealt with IT infrastructure, data security, and
satellite communications systems. He earned his PhD in 2014 with a thesis on applied
cryptography over biometrics data. He holds two MSc degrees, in information security
and software engineering.
Stefania Loredana Nita, PhD is a software developer and researcher at the Institute for
Computers of the Romanian Academy. She earned her PhD with a thesis on advanced
cryptographic schemes using searchable encryption and homomorphic encryption.
At the Institute for Computers, she focuses on research and development projects that
involve searchable encryption, homomorphic encryption, cloud computing security, the
Internet of Things, and big data. She has served for more than two years as an assistant
lecturer at the University of Bucharest, where she teaches advanced programming
techniques, simulation methods, and operating systems. She has authored and co-
authored more than 25 papers for conferences and journals and has co-authored four
books. She holds an MSc in software engineering and two BSc degrees in computer
science and mathematics.
xi
About the Technical Reviewer
Irfan Turk, PhD, is a math and computer programming
instructor and has worked at universities, high schools, and
educational institutions for about 15 years. He concentrated
on applied mathematics while earning his PhD degree. Dr.
Turk finished the computer science track requirements of
his MSc degree while a student at the University of Texas
at Arlington. He is the author of the Python Programming:
for Engineers and Scientists and MATLAB Programming:
for Beginners and Professionals books. Dr. Turk’s research
interests include, but are not limited to, numerical solutions
of differential equations, scientific computing, mathematical
modeling, and programming in MATLAB and Python.
xiii
CHAPTER 1
Getting Started in
Cryptography and
Cryptanalysis
Due to the most recent attacks, the electronic communications and transaction require to
strengthen their practical security techniques related to digital signature in such way that
law enforcement agencies are able to recognize and trust them.
In the last few months alone, as we were writing this book, there were countless
news stories detailing attacks that compromised the private data of millions of users
on the Internet or dark web. Advanced technologies—such as the Internet of Things,
fog computing, edge computing, smart vehicles, drones, smart houses, and many other
complex software applications (desktop/web/mobile)—are evolving so fast that it is a
challenge to keep up with their security requirements. For example, looking at the CVE
platform1, we can see a total of 152,984 records (vulnerabilities) and exposed users. By
examining how these vulnerabilities occurred, developers can identify efficient ways to
protect users and customers against malicious hackers.
A security solution is successful when a user’s machine (computer or other
electronic device) and the networks with which it communicates do not expose the data
flowing between them. Because of the increasing speed and complexity of computing
technology (e.g., quantum computers), the mission of modern cryptography (and we are
not referring to quantum cryptography) is at a very challenging point in time.
1
CVE - https://cve.mitre.org/
1
© Marius Iulian Mihailescu and Stefania Loredana Nita 2021
M. I. Mihailescu and S. L. Nita, Cryptography and Cryptanalysis in MATLAB,
https://doi.org/10.1007/978-1-4842-7334-0_1
Chapter 1 Getting Started in Cryptography and Cryptanalysis
Keeping knowledge secure is one of the most important aspects to consider when
designing and implementing complex systems. Information falling into the wrong hands
can result in huge financial losses or, in extreme cases, even loss of lives. Cryptography can
be used to encode private information in such a way that nobody should be able to decode
it without the necessary privileges. However, cryptography algorithms, have been broken
after hackers found a flaw in their design. When enough computing power is applied (for
example quantum computers) to break an encoded message or cryptography algorithm
(such as RSA), it is only a matter of time until the encryption or algorithm will be broken.
The current work represents a continuation of previous works, such as [2] and [3],
dedicated to applied cryptography using MATLAB environment for developing
cryptography algorithms in a more related scientific manner. The book represents
an advanced work. It provides a comprehensive view of the most important topics in
information security, cryptography, and cryptanalysis. The book can be used in many areas
by multiple professionals, including security experts, military experts and researchers,
ethical hackers, teachers in academia, researchers, software developers, and software
engineers. If you need security and cryptographic solutions in a real business software
environment, this book represents a very good starting point, together with [1] and [2]. The
book will serve very useful for students (undergraduate- and graduate-level, master degree,
professional, and academic doctoral degree students), business analysts, and many others.
C
ryptography and Cryptanalysis
There are three main concepts to keep in mind when dealing with information security
and data protection. Those concepts are cryptology, cryptography, and cryptanalysis.
• “Cryptology is defined as the science or art of secret writings; the
main goal is to protect and defend the secrecy and confidentiality of
the information with the help of cryptographic algorithms.” [2, 3].
2
Chapter 1 Getting Started in Cryptography and Cryptanalysis
The book provides a deep examination of all three concepts from the practical side
to the theoretical side. It illustrates how a theoretical algorithm should be analyzed for
implementation.
B
ook Structure
The book is divided into 14 chapters meant to cover the most important practical
and theoretical aspects of modern cryptography and cryptanalysis. The chapters
are structured in such a manner that they cover the key elements, from theoretical
cryptography to applied cryptography, especially when implementing the algorithms
in MATLAB.
Each chapter is structured into two main parts—the mathematical background,
which provides the notions required in the implementation process, and the
implementation, which contains examples of how it should be done. The chapter goals
are as follows:
3
Chapter 1 Getting Started in Cryptography and Cryptanalysis
C
onclusion
In this first chapter, we discussed the objectives of this book, based on addressing
the practical aspects of cryptography and information security. Due to the increasing
number of requirements for developing secure software applications and using
advanced information technologies, they have a deep impact on our lives every day.
The goal of this book is translate the most important theoretical cryptography
algorithms and mechanism to practice using one of the most powerful technologies in
research, MATLAB.
5
Chapter 1 Getting Started in Cryptography and Cryptanalysis
References
[1] Modern Cryptography Applied Mathematics for Encryption and
Information Security, William Easttom, Springer, 2021.
6
CHAPTER 2
MATLAB Cryptography
Functions
One of the most important elements in cryptography is the bit, the foundation of
digital information. Working with bits requires different logical operations, such as OR,
AND, or XOR. MATLAB implements these operators automatically, as well as some
types of conversions to bits, as you will see in this chapter. Other important aspects of
cryptography are arrays or matrices. These have many applications. For example, the
elements of a finite group are stored in an array, and the key of an encryption system
uses matrices to be generated. In this chapter, you will see some of the most important
functions that work with bits and matrices.
In MATLAB, there are several functions already implemented that work directly
with bits to implement the main operations on bits (bitwise operations). Examples of
such functions are bitset, bitget, bitshift, bitcmp, bitor, bitand, bitxor,
and swapbytes [1]. They all take numeric values as input. These functions are defined as
follows:
• Bitand: Returns the result of the AND operation between the bits.
7
© Marius Iulian Mihailescu and Stefania Loredana Nita 2021
M. I. Mihailescu and S. L. Nita, Cryptography and Cryptanalysis in MATLAB,
https://doi.org/10.1007/978-1-4842-7334-0_2
Chapter 2 MATLAB Cryptography Functions
• Bitxor: Returns the result of the XOR operation between the bits.
Listing 2-1 presents the applications of these functions; the result is shown in
Figure 2-1.
1
2 clc;
3 clear all;
4
5 no1 = 25;
6 bin1 = dec2bin(no1);
7 fprintf("The binary value of %d is: %s. \n", no1, bin1);
8 no2 = -65;
9 bin1 = dec2bin(no2);
10 fprintf("The binary value of %d is: %s. \n", no2, bin1);
11 fprintf("\n");
12
13 no3 = 1739;
14 set_res = bitset(no3, 5);
15 fprintf("Changing the second bit of %d to 1 results in: %d. \n", no3,
16 set_res);
17 fprintf("\n");
18
19 get_res = bitget(no3,3);
20 fprintf("The binary value of %d is %s and the 3rd bit is %d. \n", no3,
21 dec2bin(no1), get_res);
22 fprintf("\n");
23
24 no4 = round(2000 +(10000-2000)*rand(1,1));
25 shift_res = bitshift(no4, 5);
26 fprintf("Shifting 5 bits to left of %d (%s) results in %d (%s). \n",
27 no4, dec2bin(no4), shift_res, dec2bin(shift_res));
28 shift_res2 = bitshift(no4, -5);
8
Chapter 2 MATLAB Cryptography Functions
9
Chapter 2 MATLAB Cryptography Functions
We started with the clc; and clear all; commands, which are used to clear the
Command Window, in order to remove all items from the workspace and release the
corresponding occupied memory. MATLAB provides different types of conversion
functions. One of these functions is dec2bin, which converts a decimal number into
its corresponding binary representation. Another example of a conversion function
is dec2hex, which converts a decimal number into its hexadecimal representation. A
complete list of conversion functions can be found at [2].
Note that the code in Listing 2-1 uses the rand function for no4 and no5, so every time
the program runs, different results will be obtained. This function, without parameters,
generates a random number in the (0, 1) interval. However, Listing 2-1 uses rand(1,1),
which generates a 1 × 1 matrix , which actually means one element. In general,
rand(a,b) generates a a × b matrix, with elements in the (0, 1) interval. Note also that
no4 and no5 are not in the interval (0, 1) and we used a formula to generate them. The
general formula is
x y x rand 1, 1
to generate a number in the interval (x, y). As we used x = 2000 and y = 10000, no4
and no5 were generated in this interval. The values for no4 and no5 are integer values
because we used the round function to round the result.
10
Chapter 2 MATLAB Cryptography Functions
Next, we provide some examples that involve matrices and arrays. These are very
useful in different kinds of cryptography applications, for example, to generate keys or to
test different properties of the key, such as its length. Listing 2-2 presents some examples
implemented in MATLAB.
1
2 clc;
3 clear all;
4
5 fprintf("Generating matrix... \n");
6 min_lim = 100;
7 max_lim = 1000;
8 v = round(min_lim +(max_lim-
9 min_lim)*rand(2,10));
10 disp(v);
11
12 fprintf("The length of v is %d. \n",
13 length(v));
14 fprintf("The size of v is %d. \n",
15 size(v));
16
17 fprintf("v has %d elements. \n", numel(v));
18 fprintf("\n");
19 fprintf("Sorting v... \n");
20 disp(sort(v));
21
22 fprintf("Sorting rows of v... \n");
23 disp(sortrows(v));
24
25 fprintf("Flipping v... \n");
26 disp(flip(v));
27
28 fprintf("Reshaping v... \n");
29 v2 = reshape(v, [5,4]);
11
Chapter 2 MATLAB Cryptography Functions
30 disp(v2);
fprintf("Replicating v...\n");
v3 = repmat(v,2);
disp(v3);
In line 7 of Listing 2-2, we generated a matrix with 2 rows and 10 columns, with
random integer numbers in the [100, 1000] interval. In line 10, we printed the length of
the matrix. The length is given by the longest sub-array in an array. As a matrix can be
seen as an array of arrays, the result of this example is 10, because each line contains
10 elements. Therefore, the size of the matrix is 10. If v would have been composed of
sub-arrays with different lengths, the length of v would have been equal to the number of
elements of the longest sub-array. Next, the size of the matrix gives the number of rows
and columns, while the numel function gives the total number of elements in the matrix.
The sort function sorts the elements of each column of the matrix, while sortrows sorts
12
Chapter 2 MATLAB Cryptography Functions
the elements of each row of the matrix. The flip function flips the elements between
the columns of the matrix. Next, reshape generates another matrix with a total number
of elements that’s the same as the original matrix, but with a different number of rows
and columns. Finally, repmat replicates the matrix by the given number of times, in our
example, two times. Note that all these functions can be applied on arrays too.
The following functions are representative of different operations in cryptography,
including functions such as eig (see Figure 2-3), poly (see Figure 2-4), and roots (see
Figure 2-5).
13
Chapter 2 MATLAB Cryptography Functions
Figure 2-4. Computing the characteristic polynomial from the eigen_value values
14
Chapter 2 MATLAB Cryptography Functions
Conclusion
This chapter presented the main functions that work directly on bits and explained how
to work with characteristics of matrices and arrays. These functions, matrices, and arrays
are very useful in implementing cryptographic algorithms.
15
Chapter 2 MATLAB Cryptography Functions
R
eferences
[1] Bitwise Operations – Functions: https://www.
mathworks.com/help/matlab/referencelist.
html?type=function&category=bit-wise-operations&s_
tid=CRUX_topnav
16
CHAPTER 3
Conversions Used in
MATLAB for Cryptography
The goal of this chapter is to provide a quick overview of the main conversion
mechanisms for numbers and strings that are specific to the cryptography field.
Representing numbers and binary strings in different bases is one of the most critical
steps in cryptography. This chapter covers the main steps and discusses how they are
done properly in MATLAB.
A common example used in cryptographic operations is the process of converting
integers into lowercase and uppercase strings. Listing 3-1 and Figure 3-1 show an
example of converting integers into lowercase strings.
In Line 1 of Listing 3-1, we define the signature of the function. The call of the
function receives an integer array with a length between 0 and 25 (see Line 2). If the size
is not between those values, an error is thrown, informing the user about the size that
should be provided (see Line 3). Line 6 shows the conversion process provided by the
char function, which is a built-in MATLAB function.
17
© Marius Iulian Mihailescu and Stefania Loredana Nita 2021
M. I. Mihailescu and S. L. Nita, Cryptography and Cryptanalysis in MATLAB,
https://doi.org/10.1007/978-1-4842-7334-0_3
Chapter 3 Conversions Used in MATLAB for Cryptography
In Listing 3-2 and Figure 3-2 we have a similar example; the difference is that the
integers are converted into uppercase letters.
Listing 3-3 and Figure 3-3 show another example. This time the process is reversed—
the program converts from letters to integers. The process is similar and the signature of
the function is declared in the same way as in the examples in Listings 3-1 and 3-2.
18
Chapter 3 Conversions Used in MATLAB for Cryptography
In Line 3 of Listing 3-3, there is the condition that the string array characters must be
situated between a and z. If another character is encountered within the string, an error
message is thrown (see Line 4). In Line 7, the conversion is performed and returned to
the user.
In Listing 3-4 and Figure 3-4, we have another example of converting a lowercase
string (including the spaces between words) to an array of integers. In Line 1, we have
the signature of the function; the parameter is represented by the string that the user will
19
Chapter 3 Conversions Used in MATLAB for Cryptography
enter (see Figure 3-4). In Line 3, we loop through each character of the string and run the
tests provided for each character, then convert them properly (see Lines 4, 5, and 7).
20
Chapter 3 Conversions Used in MATLAB for Cryptography
The example in Listing 3-6 generates random numbers. This example can be used to
implement pseudo-random number generators. The implementation provided in
Listing 3-6 is quite straightforward and simple to follow. The signature of the function
receives three parameters—the minimum and maximum values represent the number of
digits, and the length determines how long the random number will be (see Figure 3-6).
21
Chapter 3 Conversions Used in MATLAB for Cryptography
C
onclusion
This chapter discussed several basic examples that illustrate different conversion
scenarios. These examples are very useful for implementing cryptographic algorithms.
They are one of the simplest and most reliable ways of implementing such conversion
methods.
R
eferences
[1] Turk, Irfan. Practical MATLAB: With Modeling, Simulation,
and Processing Projects. Apress, 2019. DOI.org (Crossref ),
doi:10.1007/978-1-4842-5281-9.
[3] Hunt, Brian R., et al. A Guide to MATLAB: For Beginners and
Experienced Users. Cambridge University Press, 2001.
23
CHAPTER 4
Basic Arithmetic
Foundations
In cryptography, the Integer Factorization Problem (IFP) has significant importance
because many cryptosystems with public keys ground their security on the hardness
assumption of it. For example, RSA Laboratories launched many competitions that
targeted IFPs [1]. The IFP problem consists of writing an arbitrary integer as a product of
powers of prime numbers. Note that every integer number can be written as a product of
prime numbers and that product is unique. At the same time, IFP is interesting because
it combines elements of number theory with elements of complexity theory.
However, to factorize an integer number, it is enough to find an algorithm that
defines the given integer as a product of two integers (greater than one). This problem
is known as splitting and if such an algorithm existed, it would be applied recursively
until the complete factorization of any integer is found. This is of interest for odd integers
only, because factoring an even integer leads to dividing by two until an odd integer is
obtained, which is then split on its turn.
The purpose of this chapter is to provide a quick overview of the main tools used to
deal with numbers and their basic operations in cryptography.
Another important tool in cryptography are big integers, which are numbers with
hundreds or thousands of digits. For these, the IFP becomes interesting, because there
are no efficient solutions, except for the following situations [2]:
25
© Marius Iulian Mihailescu and Stefania Loredana Nita 2021
M. I. Mihailescu and S. L. Nita, Cryptography and Cryptanalysis in MATLAB,
https://doi.org/10.1007/978-1-4842-7334-0_4
Chapter 4 Basic Arithmetic Foundations
• Special integers. The integer contains factors that have properties that
enable efficient use.
The Exhaustive Search, which is the most inefficient algorithm, divides n into
successive integer values in the interval 2, n with the purpose of finding a factor. The
complexity for the Exhaustive Search is O n , which is unpractical for big integers with
hundreds or thousands of bits.
This chapter presents the Euclid algorithm and the Extended Euclidean algorithm.
First, let’s establish some guidelines. Note that we briefly present the main ideas that
will be used in this chapter, similarly with [2] and [6]. A comprehensive source about the
number theory used in cryptography is [2]. [3]-[5] are also good references.
(a) a = b · q + r
(b) 0 ≤ r < b
26
Chapter 4 Basic Arithmetic Foundations
The notation for g is g = gcd (a, b), or simply, g = (a, b). If (a, b) = 1, then a and b are
called coprime numbers. Note that coprime numbers are also called relatively prime
numbers.
A notion related to the gcd is the least common multiple between to integer numbers
a and b. It is denoted as lcm(a, b), or simply [a, b] and represents the least natural
number that is divisible with a and b.
Euclid’s Algorithm
Let a, b ∈ ℤ, a ≥ b > 0 and the initialization notation a = r−1, b = r0. By applying Euclid’s
division lemma repeatedly, this will be achieved
rj 1 rj q j 1 rj 1 ,0 rj 1 rj
for all values 0 ≤ j < n, where n is the first prime number with the property rn + 1 = 0. Then
(a, b) = rn.
The factorization based on Euclid’s algorithm supposes to construct the product
k
Pk pi of the first k prime numbers and then compute gcd(n, Pk), where n is
i 1
the numbers that should be factorized. This solution is not more efficient than the
Exhaustive Search, and also requires memory space for storing the products—space
that is not available when working with big integers. However, Euclid’s algorithm has an
advantage that allows it to be used in practice. Tables with products of the above form
can be computed for reasonable values apriori knowing the number n, which can be
used to compute the small factors of any integer that is desired to be factored.
27
Chapter 4 Basic Arithmetic Foundations
si si 2 qn i 2 si 1 ,1 i n 1,
then g = sn + 1a + snb.
Another important topic used in cryptography is the congruence in the ring (ℤn, + , ·),
where ℤn = {0, …, n − 1}. Let a, b ∈ ℤ, n ∈ ℕ∗; a is congruent with b modulo n if n divides
(a − b), and it is denoted a ≡ b (mod n). Recall that all number theory used in cryptography
is explained in detail in [2]; of interest here is how the modular inverse is computed.
Let a ∈ ℤ, n ∈ ℕ∗. The multiplicative (or modular) inverse of a (modulo n) is the
positive integer x < n with ax ≡ 1(mod n) and it is denoted x = a−1. The inverse of a
number in ℤn can be computed using the Extended Euclidean algorithm. Considering
n prime and b ∈ ℤn, the Extended Euclidean algorithm is applied for the pair (b, n) and
it is found in the value g = sp + 1b + spn, where p is the first positive index with rp + 1 = 0. But
g = 1, because n is prime, and if this relation is taken as a congruence modulo n, then it is
achieved 1 ≡ sp + 1 · b (mod n). Therefore, sp + 1 = b−1(mod n).
P
ractical Implementations
This section covers the importance of arithmetic foundations in cryptography. We point
out some of the most useful algorithms, such as the Extended Euclidean algorithm and
Exhaustive Search.
the main mechanisms behind the functions and to provide a starting point for readers,
with the goal of developing personal functions or improving them for their projects.
Figure 4-1. Output of the Extended Euclidian algorithm from Listing 4-1
prime_ factor=factor(500)
If, for example, we need to compute the product of the prime factors from the prime_
factor array, we can use the prod(prime_factor) function. See Figure 4-3 for the output.
30
Chapter 4 Basic Arithmetic Foundations
31
Chapter 4 Basic Arithmetic Foundations
C
onclusion
This chapter covered some of the most important tools for dealing with basic arithmetic
operations, such as the Extended Euclidean algorithm, Exhaustive Search, computing
prime factors and their products, and computing the modular inverse of a number. We
provide the main theoretical concepts as a starting point in the field of number theory.
The set of references can be used to complete your knowledge about number theory.
R
eferences
[1] RSA Factoring Challenge, https://wikimili.com/en/
RSA_Factoring_Challenge
32
Chapter 4 Basic Arithmetic Foundations
33
CHAPTER 5
Number Theory
In Chapter 4, we applied number theory to cryptography. Many techniques and tools
in cryptography are based on primality, factorization, and radomness, and we have
already seen how Euclid’s algorithm works. In this chapter, we present in more depth the
concepts and ideas behind them, as well as more advanced techniques that deal with
characteristics of integer numbers.
This chapter is more technical and contains more mathematics. The definitions and
results presented in this chapter are compiled from resources [1]-[5].
P
rimality and Factorization
We have mentioned the term “prime number” or “prime” a lot in this book. Recall that
a prime number is a positive integer that has no divisors except for 1 and the number
itself. But how can this be “translated” into a mathematical definition? Mathematically,
the definition of a prime number can be one of the following two, discussed next.
P
rime Numbers
Let p ∈ ℕ, p > 1. The number p is called prime if:
(a) (∀)n ∈ ℕ, n ∣ p ⇒ n = 1 or n = p.
These definitions are equivalent. The first definition (a) says that for any positive
integer n that divides p, n is 1 or p. In other words, the only divisors of a prime number
are 1 and the number itself. The second definition (b) says that a prime number divides
a product of two positive integers, and the prime number is one of the two numbers.
This is a little more difficult to understand, and it can be seen as related to integer
35
© Marius Iulian Mihailescu and Stefania Loredana Nita 2021
M. I. Mihailescu and S. L. Nita, Cryptography and Cryptanalysis in MATLAB,
https://doi.org/10.1007/978-1-4842-7334-0_5
Chapter 5 Number Theory
factorization. The lowest prime number and the only even prime number is p = 2.
Two integers a, b for which gcd(a, b) = 1 (where gcd is the greatest common divisor) are
called coprime or relatively primes.
A naïve verification for the primality of a positive integer p is to test whether p is
divisible by one of the integers 2,, p . If one number from this set is a divisor of p,
then p is not a prime. Listing 5-1 presents this implementation in MATLAB; the result
can be seen in Figure 5-1.
36
Chapter 5 Number Theory
27 end
28 end
29 end
In the code in Listing 5-1, the user is asked to enter a number (Line 4). The number
is verified in certain ways, because it has to meet certain requirements: to be greater
than 2 and to be an integer (see Lines 5-13). Checking the primality of a real number, for
example, does not make sense. If everything is alright, the is_prime function is called
on the number. It is very simple. It just verifies whether the number has divisors in the
interval [2, sqrt(number)]. If a divisor is found, the for loop is interrupted and we can say
for sure that the number is not a prime. Otherwise, if the for loop reaches sqrt(number)
and no divisor is found, the number is a prime.
Indeed, 789532547 is not prime, because its first divisor (greater than 2) is 17. On the
other hand, the primality for the number 2147483647 was proved by Euler in 1772 [9].
This number has the following form:
2147483647 2 31 1
It also is a special number called a Mersenne prime. AMersenne prime has the form
Mn = 2n − 1, n ∈ ℕ, n > 1.
37
Chapter 5 Number Theory
There are other, faster techniques for testing the primality, which we will discuss in
this chapter. However, there is no known function (and therefore algorithm) that can
construct distinct prime numbers.
Regarding prime numbers, there are several conjectures formulated:
Further, we try to find how dense the set of prime numbers is within the set of natural
numbers, or, in other words, the prime number distribution. To do this, we need to
define the function π(x):
In other words, the function counts the number of primes less than or equal to a
given number x.
x
x
ln x
x
lim 1.
x x / ln x
i.e.
In other words, the theorem says that π(x) is approximated by the number x/ ln (x),
where ln(x) is the natural logarithm, and the approximation becomes more accurate as
x increases and approaches ∞. This is also known as the de la Vallee Poussin/Hadamard
Theorem (1896), or the conjecture Gauss/Legendre (approx. 1790). Therefore, the prime
numbers are distributed asymptotically. This is useful in applications in which we want
to know the number of primes lower than a given value.
Next, we discuss congruences and begin by providing the definition of congruency.
38
Chapter 5 Number Theory
C
ongruency
Let n, a, b ∈ ℤ and n ≥ 2. We say that a is congruent to b modulo n if n ∣ (a − b).
This definition says that a is congruent to b modulo n if the remainders from the
Euclidean algorithm (see Chapter 4) for the division between (a and n) and (b and n) are
the same. This is denoted with a ≡ b(mod n). Sometimes this relationship can be stated
as b is a residue of a modulo n. This gives the famous sets denoted with ℤn, which are
congruence classes. The set ℤn is defined as ℤn = {0, 1, …, n − 1}. It can be shown that
n = / n ,
where nℤ = {…, −1 ⋅ n, 0 ⋅ n, 1 ⋅ n, …}
The tuple (ℤn, + , ⋅) forms a ring structure, called the ring of integers modulo n.
I nverse
In the previous chapter, we computed the modular inverse of a number modulo another
number. Here is the formal definition for the inverse of an element modulo n:
Let n ∈ ℕ, n ≥ 2 and an element a ∈ ℤn. The inverse of a modulo n is the element
b ∈ ℤn such that ab ≡ 1(mod n).
Note that it exists, the inverse is unique, and it is usually denoted with a−1. A very
useful relation is given by the following theorem:
Let n ∈ ℤ and a ∈ ℤn. We say that a is an invertible element in ℤn iff gcd(a, n) = 1.
Based on this theorem, we computed the modular inverse in the previous chapter.
x a mod m , x b mod n .
39
Chapter 5 Number Theory
P
rimality Tests
We have already seen that in cryptography, prime numbers play a crucial role. Now that
we’ve had a brief introduction to elements of the number theory, we can check whether
a number is a prime or a composite. Besides the naïve verification that we implemented
previously, there are many more primality tests. If the number is a composite, the next
interesting information is its factorization form in prime factors. Primality can be tested
using one of the following primality tests:
• Wilson
• Quadratic congruences
• Little Fermat
• Converse Fermat
• Miller-Rabin
• Frobenius
• AKS (Agrawal-Kayal-Saxena)
• Baillie-PSW
These tests verify whether the number is a composite rather than verifying its
primality 100%. The most commonly used tests are Fermat, Miller-Rabin, and Solovay–
Strassen, which we will implement later.
40
Chapter 5 Number Theory
In the code in Listing 5-2, we made the same verifications for the number that the
user introduced. The is_prime function tests whether the number is a prime. This time
we initialize the variable check with 0 because we begin with the presumption that
the number is not a prime. If the condition is met, check becomes 1 and the number
is a prime. Note in this function that we used sym, because the result becomes quite
41
Discovering Diverse Content Through
Random Scribd Documents
“Sure!” said Chas. “And when the coach and the captain didn’t
give a dinner in your honour and ask you to accept a place on the
team and give them the benefit of your advice as to running same
you got peeved. That’s just what I’d have done if I’d been you, you
see, so I know. If it was me I’d have either gone to the coach and
made a big kick and told him how good I was or else I’d have gone
out and played so hard that they’d have either had to take me on or
chuck me to save the lives of the others! But you, being Haughty
Harold, just froze them with a glance—which same they didn’t
happen to see—and went your way. And it’s a rotten way, too.
Because it won’t get you anywhere. Driscoll won’t fall for you until
you show something and you won’t show anything until Driscoll pats
you on the back. Say, I’m talking a whole lot! What time is it? And
you’ve got some digging to do! I’ll beat it. Think over my words of
wisdom, Foster, and drop around tonight and hear more. I’ve got a
plan, old chap. I’m in 16 Goss; first floor, on the right. Bye-bye!”
And before Myron could agree or refuse the invitation Cummins
had hurried to the door and was clattering downstairs. Myron went
to the window and, in somewhat of a daze, watched Cummins
emerge below and disappear under the trees. Then he sat himself
down on the window-seat, plunged both hands into trousers pockets
and frowned intently at his shoes. He didn’t get much studying done
that hour.
CHAPTER XV
THE PLAN
The team left for North Lebron at eleven o’clock the next
forenoon. The town that had the honour of containing Musket Hill
Academy was not so far away in distance, but those who had
arranged the train service had not consulted the Parkinson School
Football Team, and as a result of this oversight there was an hour
and a half to be spent at a junction that boasted, besides a decrepit
station, only a blacksmith’s shop, a general store and eight assorted
dwellings. Myron knew that there were eight dwellings because he
counted them twice. There wasn’t much of anything else to do.
He was not journeying to North Lebron in any official capacity, for
his name had not been amongst those announced yesterday by
Manager Farnsworth. He was going along, with some sixty other
“fans,” mostly because Chas Cummins had insisted on his doing so.
Privately, he had entertained the thought up to an hour after
breakfast that, not having been invited to attend the contest as a
member of the team, it would be the part of dignity to remain away.
But Chas wasn’t greatly concerned with dignity, and he had a
masterful way with him, and the result was that at a little before
nine o’clock Myron was in possession of the knowledge that he was
going to North Lebron at eleven-four.
At twelve he was seated on an edge of the platform at the
junction, juggling three pebbles in his hand and boredly wondering
what it would be like to have to live in the fifth dwelling; the one
with the blue-green blinds and the sagging porch and the discarded
wagon-seat serving as a porch settle. The day was positively hot for
October and few of the travellers had elected to remain inside the
coaches. Some of the school fellows were adorning the platform, like
Myron, others were strolling about the adjacent landscape in search
of adventures, and a merry handful were exercising the baggage
truck up and down the planks to the restrained displeasure of the
sad-looking station agent. Coming over, Myron had shared a seat
with a stranger, a lad of fourteen or so, and had managed to pass
the time in conversation on various subjects, but now the youngster
had disappeared and no one else appeared to care about taking his
place. Joe and Chas were with the football crowd in the forward car,
and Myron had seen neither of them to speak to since leaving
Warne. Andrew Merriman had not been able to come. In
consequence, Myron had no one to talk to and was fast reaching the
decision that he would have had more pleasure had he remained at
home. Even the assurance that he was irreproachably arrayed in a
suit of cool grey flannel, with a cap to match, a cream-coloured shirt
and patriotic brown tie and stockings didn’t mitigate his boredom. Of
late he had been deriving less satisfaction than of yore from his
attire. Somehow, whether his tie and stockings matched or whether
his trousers were smoothly pressed seemed of less consequence to
him. Several times of late he had forgotten his scarf-pin!
His discontented musings were interrupted by the arrival beside
him of a youth of perhaps nineteen. Myron had glimpsed him once
on the train and been struck by his good looks and by the good taste
of his attire. He wore blue serge, but it was serge of an excellent
quality and cut to perfection. And there was a knowing touch to the
paler blue scarf with its modest moonstone pin and something
pleasantly exceptional in the shape of the soft collar. Myron felt a
kindred interest in the tall, good-looking youth, and determined to
speak to him. But the stranger forestalled him, for, as soon as he
had seated himself nearby on the platform edge, he turned, glancing
at Myron and remarked: “Hot, isn’t it?”
The stranger’s tone held just the correct mixture of cordiality and
restraint. Myron, agreeing, felt flattered that the well-dressed youth
had singled him out. The weather, as a subject of conversation, soon
failed, but there were plenty of other things to discuss, and at the
end of ten minutes the two were getting on famously. The stranger
managed to inform Myron without appearing to do so that he was
interested in a sporting goods house in New Haven, that he had
been in Hartford on business and that, having nothing better to do
today, he had decided to run over to North Lebron and see the game
between Musket Hill and Parkinson. “I fancy you’re a Parkinson
fellow?” he said questioningly. And when Myron acknowledged the
fact: “A fine school, I’ve heard. I’ve never been there. Warne’s off
my territory. I’ve been thinking, though, that some day I’d run over
and see if I could do any business there. I suppose you chaps buy
most of your athletic supplies in New York.”
“I think so. There’s one store in Warne that carries a pretty fair
line of goods, though.”
“I think I’ll have to try your town. Parkinson’s rather a big place,
isn’t it?”
“We have over five hundred fellows this year.”
“Is that so? Why, there ought to be some business there for my
house. I suppose you chaps go in for most everything: football,
baseball, hockey, tennis? How about track athletics?”
“There’s a track team,” answered Myron, “but this is my first year
and I don’t know much about it yet.”
“I see.” The other looked appraisingly and, Myron thought, even
admiringly over his new acquaintance. “I say, you look as if you
ought to be playing football yourself, old man. Or is baseball your
game?”
“Football, but I’m not on the first. It’s hard work breaking in at
Parkinson.”
“Good guess of mine, wasn’t it?” laughed the other. “Thought you
had the build for a good football man. I meet a good many of them,
you see. How’s this team you’ve got ahead there? Going to lick
Musket Hill this afternoon?”
“I don’t know. I hope so. I have an idea that our coach rather
expects a hard game, though. I’ve heard that Musket Hill is further
along than we are.”
“Those fellows play good football,” said the stranger. “I’ve seen
them in action once or twice. I hope you chaps get away with the
game, but my opinion is that you’ll have to go some to do it. Got
some good men on your team?”
Myron was quite willing to sing the praises of Parkinson, and
during the ensuing half-hour the stranger was treated to quite a
fund of information regarding the school, the football team and
Myron Warrenton Foster. Football, though, seemed to interest the
tall youth most of all, and several times Myron was turned back to
that subject by polite questions. When the train from the south
pulled in the two were still conversing and it was but natural that
they should share a seat for the remainder of the journey. The
stranger could talk interestingly himself and the last part of the trip
was occupied with absorbing and even startling adventures met with
by him in his business trips. More than once Myron’s credulity was
severely taxed, but a glance at the narrator’s frank and pleasing
countenance dispelled all suspicions of mendacity. Myron found this
chance acquaintance so interesting that he rather hoped they might
witness the game together, but when North Lebron was reached the
stranger announced that he had one or two errands to attend to
before going up to the field.
The stranger was treated to quite a fund of
information
ebookbell.com