File: crypto/number.tex, r1963
This chapter introduces basic concepts of number theory. These concepts are useful when studying several aspects of cryptography, especially public key cryptosystems.
Presentation slides that accompany this chapter can be downloaded in the following formats: slides only (PDF); slides with notes (PDF, ODP, PPTX).
Definition 5.1 (Divides). divides if for some , where , and are integers. We can also say is a divisor of , or .
Definition 5.2 (Greatest Common Divisor). returns the greatest common divisor of integers and . There are efficient algorithms for finding the gcd, i.e. Euclidean algorithm.
Example 5.2 (Greatest Common Divisor). , since the divisors of 12 are (1, 2, 3, 4, 6, 12) and the divisors of 20 are (1, 2, 4, 5, 10, 20).
Example 5.3 (Relatively Prime). , since the divisors of 7 are (1, 7) and the divisors of 12 are (1, 2, 3, 4, 6, 12). Therefore 7 and 12 are relatively prime to each other.
Exercise 5.1 (Relatively Prime). How many positive integers less than 10 are relatively prime with 10?
Solution 5.1 (Relatively Prime). There are 9 positive integers less than 10, i.e. . For an integer to be relatively prime to 10, then . The divisors of 10 are 1, 2, 5 and 10. As the even integers have a divisor of 2, then they cannot be relatively prime with 10. That leaves 1, 3, 5, 7 and 9. and therefore 5 is not relatively prime with 10. The integers 1, 3, 7 and 9 cannot be divided by 3, 5 or 10, and therefore all have a greatest common divisor with 10 of 1. Hence 1, 3, 7 and 9 are less than 10 and relatively prime with 10. The answer is 4.
Video: Divisibility, Greatest Common Divisor and Relatively Prime (10 min; Apr 2021)
Definition 5.4 (Prime Number). An integer is a prime number if and only if its only divisors are , , and .
Example 5.4 (Prime Number). The divisors of 13 are (1, 13), that is, 1 and itself. Therefore 13 is a prime number. The divisors of 15 are (1, 3, 5, 15). Since the divisors include numbers other than 1 and itself, 15 is not prime.
Credit: Wikipedia, https://en.wikipedia.org/wiki/List_of_prime_numbers, CC BY-SA 3.0
Definition 5.5 (Prime Factors). Any integer can be factored as:
where are prime numbers and where each is a positive integer
Solution 5.2 (Integers as Prime Factors). A naive approach (which works for these small examples) is to check if the number is divisible by primes, in increasing order. For example, is 12870 divisible by 2? Yes, then 2 is a prime factor. Is the result, 6435 divisible by 2? No, then is 6435 divisible by 3? Yes, and so on.
A “cheat” is to use software to find the factors. In Linux command line there is a command called factor.
The answers are:
Definition 5.6 (Prime Factorization Problem). There are no known efficient, non-quantum algorithms that can find the prime factors of a sufficiently large number.
Example 5.6 (Prime Factorization Problem). RSA Challenge involved researchers attempting to factor large numbers. Largest number measured in number of bits or decimal digits. Some records held over time are:
1991: 330 bits or 100 digits
2005: 640 bits or 193 digits
2009: 768 bits or 232 digits
Equivalent of 2000 years on single core 2.2 GHz computer to factor 768 bit
Current algorithms such as RSA rely on numbers of 1024, 2048 and even 4096 bits in length
Video: Prime Numbers and Prime Factorization (11 min; Apr 2021)
Definition 5.7 (Euler’s Totient Function). Euler’s totient function, , is the number of positive integers less than and relatively prime to . Also written as or .
Example 5.7 (Euler’s Totient Function). The integers relatively prime to 10, and less than 10, are: 1, 3, 7, 9. There are 4 such numbers. Therefore .
The integers relatively prime to 11, and less than 11, are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. There are 10 such numbers. Therefore . The property could also be used since 11 is prime.
Since 7 is prime, .
Since , then .
Video: Euler’s Totient Function (8 min; Apr 2021)
Definition 5.9 (Modular arithmetic simple). Modular arithmetic is similar to normal arithmetic (addition, subtraction, multiplication, division) but the answers “wrap around”.
Definition 5.10 (mod operator). If is an integer and is a positive integer, then is defined as the remainder when is divided by . is called the modulus.
Definition 5.11 (Congruent modulo). Two integers and are congruent modulo if . The congruence relation is written as:
When the modulus is known from the context, it may be written simply as a .
Definition 5.12 (Modular arithmetic). Modular arithmetic with modulus performs arithmetic operations within the confines of set .
Example 5.10 (mod in ). Consider the set:
All modular arithmetic operations in mod 7 return answers in .
Definition 5.13 (Modular Addition). Addition in mod is performed as normal addition, with the answer then mod by .
Definition 5.14 (Additive Inverse). is the additive inverse of in mod , if .
For brevity, AI() may be used to indicate the additive inverse of . One property is that all integers have an additive inverse.
Definition 5.15 (Modular Subtraction). Subtraction in mod is performed by addition of the additive inverse of the subtracted operand. This is effectively the same as normal subtraction, with the answer then mod by .
Example 5.13 (Modular Subtraction). For brevity, the modulus is sometimes omitted and is used in replace of . In mod 7:
While the first two examples obviously give answers as we expect from normal subtraction, the third does as well. , and in mod 7, since . Recall .
Video: Modular Addition, Additive Inverse and Modular Subtraction (13 min; Apr 2021)
Definition 5.16 (Modular Multiplication). Modular multiplication is performed as normal multiplication, with the answer then mod by .
Definition 5.17 (Multiplicative Inverse). is a multiplicative inverse of in mod if . For brevity, MI() may be used to indicate the multiplicative inverse of . has a multiplicative inverse in (mod ) if is relatively prime to .
Example 5.15 (Multiplicative Inverse in mod 7). 2 and 7 are relatively prime, therefore 2 has a multiplicative inverse in mod 7.
3 and 7 are relatively prime, therefore 3 has a multiplicative inverse in mod 7.
, meaning 1, 2, 3, 4, 5 and 6 are relatively prime with 7, and therefore all of those numbers have a MI in mod 7.
Example 5.16 (Multiplicative Inverse in mod 8). 3 and 8 are relatively prime, therefore 3 has a multiplicative inverse in mod 8.
4 and 8 are NOT relatively prime, therefore 4 does not have a multiplicative inverse in mod 8. , and therefore only 4 numbers (1, 3, 5, 7) have a MI in mod 8.
Definition 5.18 (Modular Division). Division in mod is performed as modular multiplication of the multiplicative inverse of 2nd operand. Modular division is only possible when the 2nd operand has a multiplicative inverse, otherwise the operation is undefined.
Example 5.17 (Modular Division). In mod 7:
In mod 8:
is undefined, since 4 does not have a multiplicative inverse in mod 8.
Definition 5.19 (Properties of Modular Arithmetic).
Commutative, associative and distributive laws similar to normal arithmetic also hold.
Video: Modular Multiplication, Multiplicative Inverse (6 min; Apr 2021)
Definition 5.20 (Fermat’s Theorem 1). If is prime and is a positive integer not divisible by , then:
There are two forms of Fermat’s theorem—use whichever form is most convenient.
Example 5.18 (Fermat’s theorem). What is mod 43? Since 43 is prime and , this matches Fermat’s Theorem form 1. Therefore the answer is 1.
Example 5.19 (Fermat’s theorem). What is ? Since 163 is prime, this matches Fermat’s Theorem form 2. Therefore the answer is 640, or simplified to .
Note that there are two forms of Euler’s theorem—use the most relevant form.
Example 5.20 (Euler’s theorem). Show that . Since = 41, which is prime, then . As 37 is also prime, 37 and 41 are relatively prime. Therefore Euler’s Theorem form 1 holds.
Example 5.21 (Euler’s theorem). What is ? Factoring 4757 into primes gives . Therefore . Therefore, this follows Euler’s Theorem form 2, giving an answer of 13794.
Video: Fermat’s and Euler’s Theorems (16 min; Apr 2021)
Definition 5.24 (Modular Exponentiation). As exponentiation is just repeated multiplication, modular exponentiation is performed as normal exponentiation with the answer mod by .
Video: Modular Exponentiation (1 min; Apr 2021)
Definition 5.25 (Normal Logarithm). If , then:
read as “the log in base of is index (or exponent) i”.
The above definition is for normal arithmetic, not for modular arithmetic. Logarithm in normal arithmetic is the inverse operation of exponentiation. In modular arithmetic, modular logarithm is more commonly called discrete logarithm. Note we replace with —the reason will become apparent shortly.
Definition 5.26 (Discrete Logarithm). If , then:
A unique exponent can be found if is a primitive root of the prime .
Video: Normal and Discrete Logarithms (3 min; Apr 2021)
Definition 5.27 (Primitive Root). If is a primitive root of prime then are distinct in mod .
The integers with a primitive root are: 2, 4, , where is any odd prime and is a positive integer.
Example 5.23 (Primitive Root). Consider the prime :
Therefore 3 is a primitive root of 7 (but 1 and 2 are not).
From the above table we see 3 and 5 are primitive roots of 7.
Discrete logarithms to the base 3, modulo 7 are distinct since 3 is a primitive root of 7. Discrete logarithms to the base 5, modulo 7 are distinct since 5 is a primitive root of 7.
We see that 3, 5, 6, 7, 10, 11, 12 and 14 are primitive roots of 17.
The discrete logarithm in modulo 17 can be calculated for the 8 primitive roots.
There are several problems in number theory that are considered computationally hard. That means, when sufficiently large numbers are used, solving the problems are practically impossible. These computationally hard problems are used to provide security in cryptographic mechanisms, especially in public key cryptography. Three important problems considered impossible to solve with conventional computers follow.
Definition 5.28 (Hard Problem: Integer Factorisation). If and are unknown primes, given , find and .
Also known as prime factorisation. While someone that knows and can easily calculate , if an attacker knows only they cannot find and .
While it is easy to calculate Euler’s totient of a prime, or of the multiplication of two primes if those primes are known, an attacker cannot calculate Euler’s totient of sufficiently large non-prime number. Solving Euler’s totient of , where , is considered to be harder than integer factorisation.
While modular exponentiation is relatively easy, such as calculating ,
the inverse operation of discrete logarithms is computationally hard. The complexity is considered comparable to that of integer factorisation.
When studying RSA and Diffie-Hellman, you will see how these hard problems in number theory are used to secure ciphers.
Video: Computationally Hard Problems for Cryptography (4 min; Apr 2021)