07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Chapter 4 ■ Cryptographic Math and Frequency Analysis

Both Fermat’s and Euler’s theorems play an important role in public-key cryptography,

which will be explored in greater detail in Chapter 8.

In number theory, Euler’s theorem, also known as Euler’s totient theorem or

the Fermat–Euler theorem, states that if n and a are coprime positive integers,

then aφ (n) ≡ 1 mod n where φ(n) is Euler’s totient function. In 1736, Leonhard

Euler published his proof of Fermat’s little theorem, which Fermat had presented

without proof. Subsequently, Euler presented other proofs of the theorem, culminating

with “Euler’s theorem” in his paper of 1763, in which he attempted to

find the smallest exponent for which Fermat’s little theorem was always true.

Euler investigated the properties of numbers; he specifically studied the

distribution of prime numbers. One crucial function he defined is named the

PHI function; the PHI function measures the breakability of a number. Assume

you have the number n; the function calculates the number of integers that are

less than or equal to n and do not share any common factor with n; you see it

in the following notation: ɸ [n]. For example, if you wanted to examine ɸ [8],

you would examine all values from 1 to 8 and count all integers with which 8

does not share a factor greater than 1; the numbers are 1, 3, 5, 7. The function

produces 4. As it turns out, calculating the PHI of a prime number P is simple.

ɸ [P] = P − 1. To calculate ɸ [7], you count all integers except 7 since none of the

integers share a factor with 7; therefore, ɸ [7] = 6. Assume a larger prime such

as 21,377. ɸ [21,377] = 21,376. The equation looks like the following:

ap − 1 ≡ 1 mod p

To take full advantage of this trick, which helps when you explore RSA, you

need to see the general version. If you know the prime factorization of a modulus,

then computing the Euler’s totient works as follows:

ɸ(p 1

K1

... p n Kn ) = ɸ(p 1 K1 ) ... ɸ(p n

Ki − 1 ) where ɸ(p i Ki ) – p i

Ki − 1

= p Ki – 1 ◦ (p i – 1)

We extend the ideas presented in this chapter into some classical number

theory that also happens to be practical for cryptographers. Euler totient functions

offer benefits to speed up modular inverse computations. You also learn why

raising numbers to substantial powers is not expensive when you are working

with modular arithmetic.

For instance:

ɸ(3 ◦ 5 ◦ 23) = ɸ(3) ◦ ɸ(5) ◦ ɸ(23) = 2 ◦ 4 ◦ 22 = 32

Euler’s totient function Φ(n) for an input n is the count of numbers in the

format of {1, 2, 3, 4, 5, n} that are relatively prime to n, i.e., the numbers whose

GCD with n is 1. Examine the following six examples, which calculate the Euler’s

totient function Φ (n) in respect to the inputs 1 through 6. The output will be

the number of positive integers that do not exceed n and also have no common

divisors with n other than the common divisor 1:

Φ (1) = 1

gcd (1, 1) is 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!