# The Diffie-Hellman Key Exchange

The Diffie-Hellman Key Exchange allows two strangers who have no prior knowledge about each other to securely establish a secret key that they can use to communicate securely even if all of their messages are being intercepted by an eavesdropper.

In this article, we will learn about the Diffie-Hellman Key Exchange and the Miller-Rabin Primality Test. We will describe the six steps of the Diffie-Hellman Key Exchange at a high level and then work through a brief example of the Key Exchange. We will then describe the five steps of the Miller-Rabin Primality Test and work through a brief example of the algorithm. Next, we will apply what we have learned by creating a Python program that successfully implements the Diffie-Hellman Key Exchange.

# Prerequisite Knowledge:

This article assumes that the reader understands concepts such as modular arithmetic and sets, and has a general understanding of Python or a similar programming language. In addition, the following list will be useful when going through this article:

- A pseudoprime is an integer that is probably prime.
- A group is a set with an operation that combines two operands.
- A group’s order is its cardinality. In other words, it describes how many elements it contains.
- The
is denoted by*multiplicative group modulo p for a prime p***(ℤ***. It is the set of order*/p*ℤ)with elements*(p-1)*under the operation multiplication modulo p.*{1,2,…,p-1}* - A generator of the group
**(ℤ*** has order*/p*ℤ)*(p-1) modulo q.* - The order of any integer
that is a generator of*g***(ℤ*** is the smallest positive*/p*ℤ)such that*y**g^y = 1 (mod p).*

# The Diffie-Hellman Key Exchange:

The Diffie-Hellman Key Exchange involves the following six steps:

- Together, PersonA and PersonB choose a very large pseudoprime
with*p*also being pseudoprime. Since the prime factorization of (*(p-1)/2*is*p-1)*, then any generator of*2((p-1)/2)***(ℤ*** will have the order of a divisor of*/p*ℤ)That is, each element of*p-1.***(ℤ*** has either order of*/p*ℤ)or*1, 2, (p-1)/2,*. Next, PersonA and PersonB choose an integer*p-1*such that*g*generates*g***(ℤ****/p*ℤ)**.**The process of finding athat generates*g***(ℤ*** involves finding an element of order*/p*ℤ). If*p-1*has order*2*, then*(p-1)/2*has order*g = -2**(p-1)*. It is not necessary that*modulo p*generates*g***(ℤ***, but for security reasons, this is the approach that we will use. If soundness, and not security, is the reader’s greatest objective, then picking a random*/p*ℤ)such that*g*will suffice.*1≤g≤p* - PersonA secretly chooses an integer
that they don’t tell anyone.*n* - PersonB secretly chooses an integer
that they don’t tell anyone.*m* - PersonA computes
and tells PersonB the resulting integer.*g^n (mod p)* - PersonB computes
and tells PersonA the resulting integer.*g^m (mod p)* - Both PersonA and PersonB are now able to compute the shared secret key denoted by
:*s*

# A Brief Example of the Diffie-Hellman Key Exchange:

Here we will work through an example of the Diffie-Hellman Key Exchange where ** p **is a 29-digit integer.

- Let
and*p = 74,406,850,220,820,509,198,642,540,543***g =**. Note that*74,406,850,220,820,509,198,642,540,541*and*p*are prime and*(p-1)/2*is a generator of*g***(ℤ****/p*ℤ)**.** - Let
*n =**7,160,624,108.* - Let
*m = 8,460,590,791.* - So,
*g^n (mod p) = 47,826,684,805,755,333,836,688,086,773.* - So,
*g^m (mod p) = 1,783,357,463,939,102,724,432,550,267.* - Therefore,
*s = 9,896,557,491,816,756,391,504,944,966.*

# Miller-Rabin Primality Test:

Up to this point, we should have a good understanding of the Diffie-Hellman Key Exchange, but how the heck are we supposed to find a 29-digit ** p **that works? Enter the Miller-Rabin Primality Test. This algorithm will tell us whether an integer is pseudoprime or composite. Simply go through the following four steps until a 29-digit pseudoprime is found.

- Let
be a random 29-digit integer.*n* - Compute the unique integers
and*m*such that*k*is odd and*m**n-1 = m(2^k).* - Let
be a random integer such that*a**1<a<n.* - Let
and*b = a^m (mod n)*If*1≤r≤(k-1).*or*b ≡ ±1 (mod n)*for any*b^(2^r)≡-1(mod n)*, then*r*is pseudoprime. Otherwise,*n*is composite.*n*

If we want to use a pseudoprime, ** n**, in the Diffie-Hellman Key Exchange, then we need to check if

**is also pseudoprime. We can check this by simply using a slight variation of the Miller-Rabin Primality Test which is shown below.**

*(n-1)/2*- Let
*x = (n-1)/2.* - Compute the unique integers
and*m*such that*k*is odd and*m**x-1 = m(2^k).* - Let
be a random integer such that*a**1<a<x.* - Let
and*b = a^m (mod x)*If*1≤r≤(k-1).*or*b ≡ ±1 (mod x)*, then*b^(2^r)≡-1(mod x)*is pseudoprime. Otherwise,*x*is composite.*x*

# A Brief Example of the Miller-Rabin Primality Test:

Here we will work through an example of the Miller-Rabin Primality Test where ** n **and

**are shown to be pseudoprime.**

*(n-1)/2*## Part 1: Find if *n Is Pseudoprime or Composite*

- Let
*n = 24,645,693,377,521,872,504,085,245,467.* - Solving for the unique integers
and*m*such that*k*we find that*n-1 = m(2^k),*and*m = 12,322,846,688,760,936,252,042,622,733*.*k = 1* - Let
*a = 8,625,146,190,036,362,446,236,062,645.* - Therefore,
*b =*Since*a^m (mod n) = 1.*, we can say that*1 modulo n = b modulo n*Therefore,*b ≡ ±1 (mod n).*is pseudoprime.*n*

## Part 2: Find if (n-1)/2 Is Pseudoprime or Composite

- Let
*x = (n-1)/2 = 12,322,846,688,760,936,252,042,622,733.* - Solving for the unique integers
and*m*such that*k*we find that*x-1 = m(2^k),*and*m = 3,080,711,672,190,234,063,010,655,683*.*k = 2* - Let
*a = 4,658,103,515,624,502,847,038,292,640.* - Therefore,
*b =*Since*a^m (mod x) = 1.*, we can say that*1 modulo x = b modulo x*Therefore,*b ≡ ±1 (mod x).*is pseudoprime.*x=(n-1)/2*

# Bringing It All Together: An Example in Python of the Diffie-Hellman Key Exchange Which Utilizes the Miller-Rabin Primality Test:

We will now implement the aforementioned key exchange and algorithm in Python.

## The Miller-Rabin Primality Test in Python

Below is a brief description of each function in the proceeding code which will return a pseudoprime ** p **such that

**is also pseudoprime when the**

*(p-1)/2***function is called.**

*chooseP()*: This is step 4 of the Miller-Rabin Primality Test. We first set*powerTests(a, m, n, k)*equal to*b*by utilizing the Python*a^m (mod n)*function. From here, we realize that if*pow(base, exponent, modulus)*, then*b ≡ ±1 (mod n)*equals*b*or*1*. If this is the case, then*n-1*is pseudoprime and we return*n*to indicate this finding. Otherwise, if the pseudoprimality of*True*has not been determined yet, then we iterate over all possible values of*n*and check if*r*by utilizing the Python*b^(2^r)≡-1(mod n)*function. Note that if this is the case, then*pow(base, exponent, modulus)*So, if*b^(2^r) = n-1.*for any*b^(2^r)≡-1(mod n)*, then*r*is pseudoprime and we return*n*to indicate this finding. Otherwise, we return*True*.*False*: This is step 2 of the Miller-Rabin Primality Test. We start by initializing*computeMandK(n)*and*m*such that*k*and*m = n-1*. Note that with these initial values of*k = 0*and*m*, the equation*k*is satisfied. However,*n-1 = m(2^k)*may not be odd, so we enter a while loop and update the values of*m*and*m*until*k*is odd. Note that by adding*m*to*1*and by dividing*k*by*m*on each loop iteration, the equation*2*continues to be satisfied.*n-1 = m(2^k)*: This function is used to choose*generateRandomInt(lowerBound, upperBound)*and*p*, thus it is steps 1 and 3 of the Miller-Rabin Primality Test. Returns a random integer*a*such that*Z**lowerBound≤Z≤upperBound.*: Continues to perform the Miller-Rabin Primality Test on random 29-digit integers until a pseudoprime is found.*MRPrimalityTest()*: Performs the Miller-Rabin Primality Test on*isPrime(n)**(p-1)/2.*: This is the main function from which all other functions stem from. It ensures that a pseudoprime is found that can be used for the Diffie-Hellman Key Exchange. That is,*chooseP()*and*p*are pseudoprime. It also ensures that*(p-1)/2*has order*2**(p-1)/2.*

## Simulating PersonA and PersonB With a Person Object:

To implement the remaining steps of the Diffie-Hellman Key Exchange, we need to somehow represent PersonA and PersonB so that tasks such as choosing secret integers ** n **and

**, computing and sharing**

*m***and**

*g^n (mod p)***, and computing the shared secret key**

*g^m (mod p)***are possible. We will achieve this by creating a Person class that will be used to create the objects personA and personB. Here is a brief description of the data members and methods of the Person class:**

*s***Data Members:**

: The agreed upon pseudoprime which was found using the Miller-Rabin Primality Test.*p*: The agreed upon integer which has the same order as*g***(ℤ*** and*/p*ℤ)*1<g<p.*: For personA, this is*_secretInt*and for personB this is*n**m**.*: The integer that they share with the other person.*intToShare*: The integer that was shared by the other person.*intFromOtherPerson*: The shared secret key*_sharedSecretKey*.*s*

**Methods:**

: Initialize all data members.*__init__(self, p, g)*: Secretly choose a random integer. For personA, this is*chooseSecretInt(self)*and for personB, this is*n**m.*: Takes the integer that the other person shared as a parameter and sets the data member*setIntFromOtherPerson(self, intFromOtherPerson)*equal to this parameter.*intFromOtherPerson*: Helper function for*computeIntToShare(self)*Computes and returns*sendInt(self).*by using the Python*g^(_secretInt) (mod p)*function.*pow(g, _secretInt, p)*: Returns the integer to share with the other person.*sendInt(self)*: Computes the shared secret key and stores it in its*computeSharedSecretKey(self)*data member.*_sharedSecretKey*

## Performing the Diffie-Hellman Key Exchange:

Below is a working example of the Diffie-Hellman Key Exchange that utilizes all of the code that we have previously talked about. The program imports the Person class and the chooseP() function from MillerRabinPrimalityTest.py. The program then chooses ** p **and

**according to the Diffie-Hellman Key Exchange requirements. PersonA and PersonB are then created and they each choose a secret key. Each person then sends and receives a computed integer corresponding to steps 4 and 5 of the Diffie-Hellman Key Exchange. Finally, each person computes**

*g***and the result is printed to the console.**

*s*A sample output of the preceding program shows that our implementation of the Diffie-Hellman Key Exchange was successful!

Agreed upon p = 52715485256194169359137162299

Agreed upon g = 52715485256194169359137162297

PersonA secretly computes g^n (mod p) = 40768582547499338665116868709

PersonB secretly computes g^m (mod p) = 43084512410369395738880134222

PersonA computes s = 44150840871111848693028463930

PersonB computes s = 44150840871111848693028463930

# References:

Stein, William. Elementary Number Theory: Primes, Congruences, and Secrets. 2017.