Learning Cryptography, Part 2: Diffie-Hellman Key Exchange, Discrete Log Problem & Cyclic Groups

Kerman Kohli
Jul 28, 2019 · 5 min read
Image for post
Image for post
This is Episode 2 in the Learning Cryptography series.


Diffie-Hellman Key Exchange is used for various public key/private key encryption schemes. Security assumptions about the key exchange protocol are guaranteed through the difficulty of breaking the discrete log problem. We will talk about how we can generate discrete logarithmic problems through generator points of polynomials of extension fields (covered in the previous article).

If you haven’t read the previous article about finite fields, I’d highly recommend you to do so before reading this article!


Loopring’s “Learning Cryptography” series hopes to educate the community about this fascinating field. This series will begin from the basics, and work its way up to the advanced tools that make our scalable v3 DEX protocol — which utilizes zero-knowledge proofs — possible.

Diffie-Hellman Key Exchange

Suppose Bob wanted to communicate with Alice in a secure way. To keep things simple, they could have a shared secret between them which could both agree on and encrypt all their messages with that secret. However, there are two problems with this scheme:

  1. How do they communicate the secret in a secret way?
  2. How is key abuse prevented? eg. Alice and Bob may share the keys with 3rd parties.

What we’re describing here is a symmetric key encryption scheme. The trade-offs we described above don’t really cut it for adversarial environments such as crypto. So what’s the solution? Asymmetric key encryption schemes.

Diffie-Hellman is an asymmetric key exchange protocol where each party has its own public key and private key.


  • Alice has her private key a and public key A and Bob has his private key b and public key B;
  • We have two variables which are known as public parameters: p (some large prime number) and 𝛂 (some integer);
  • private keys a and b are randomly selected numbers from a finite field having p-1 elements;
Image for post
Image for post
Diffie-Hellman Key Exchange

To send a message to Bob, Alice would:

  1. Compute her public key A through the equation A=𝛂^a mod p. 𝛂 is our public variable integer, the exponent is a (Alice’s private key);
  2. Alice then sends her public key A to Bob. You might be thinking that couldn’t Bob rework the equation to obtain Alice’s private key, rest assured this isn’t possible and we’ll explain why later on;
  3. Bob computes his public key B through the equation B=𝛂^b mod p. 𝛂 is our public variable integer, the exponent is b (Bob’s private key) ;
  4. Bob sends his public keyB to Alice as well;
  5. Now, Alice has a key K defined as B^a and Bob also has the same key K defined as A^b;
  6. Alice and Bob can securely chat with each other by encrypting all their messages x (secret message) with K generating ciphertexts c (garbled mess).

Point 4 probably threw you off. How can B^a and A^b be the same as you ask? Let’s break down the math:

  • A^b can also be rewritten as (α^a)^b
Image for post
Image for post
Edit: “A” should be labeled “Alice’s public key”, not private key.
  • B^a is actually the same as(𝛂^b)^a as shown in step 3
Image for post
Image for post
  • (𝛂^a)^b is the same as (α^b)^a
Image for post
Image for post

Since each party can independently generate (𝛂^b)^a or (𝛂^a)^b without knowing the other party’s private key, they can independently decipher the cipher-text c (garbled mess) to find out x (secret message).

There’s still one unanswered question: if Alice gives Bob A which is defined as A=𝛂^a mod p, couldn’t Bob find out what a is since he already knowsA, p and 𝛂? The answer is: not until quantum computers come out.

Discrete Log Problem

To understand the discrete logarithm problem, let’s try to solve a simple equation:19683 = 3^n.

If you opened up your calculator you can solve this by simply entering log(19683)/log(3) which gives us 9 — not too hard at all. However, if we said that this equation was the following instead, you’d have a much harder time solving it:

g^y = x mod p, where g, y, p, and x are certain prime numbers and y is unknown.

Cryptography as we know it today relies on the difficulty of solving this equation (which is near impossible). Achieving computation difficulty for the discrete logarithm problem relies on choosing large and difficult prime numbers. I’ll go deeper into this voodoo magic down the line.

Cyclic Groups

Groups in cryptography refer to a set of elements that are all strongly related to each other. An example would be Z*5={1, 2, 3, 4}. However, what makes them special is if you perform the following operations you’ll always end up with elements inside the group:

2^1=2   mod 5 = 2
2^2=4 mod 5 = 4
2^3=8 mod 5 = 3
2^4=16 mod 5 = 1
2^5=32 mod 5 = 2
2^6=64 mod 5 = 4
2^7=128 mod 5 = 3

In this case, we refer to 2 as a generator point inside the cyclic group Z*5. 3 is another generator point for this cyclic group. In order for a group to cyclic, it must have at least one generator point. All finite fields with multiplicative groups are cyclic.

Going back to our equation g^y = x mod p, the problem is only difficult if:

  • g is a base generator in some cyclic group p
  • p is a prime number that has the true security, of some number n, n / 2 bits (compared to RSA which needs thousands of bits for the same level of security)


Hopefully, by now you understand what Diffie-Hellman key exchange is, how it relates to the discrete logarithm problem and why we need cyclic groups to guarantee the security of it all. Stay tuned for the next article in the series!

About Loopring

Loopring is a decentralized exchange protocol utilizing zkSNARKs to bring highly scalable, non-custodial trading to the masses. You can sign up for their bi-weekly update, and learn more at:

⭑ Twitter: twitter.com/loopringorg
⭑ Reddit: reddit.com/r/loopringorg
⭑ Telegram: t.me/loopring_en & t.me/loopringfans (Chinese)
⭑ Discord: discord.gg/KkYccYp
⭑ GitHub: https://github.com/Loopring
⭑ Kakao: open.kakao.com/o/gJbSZdF (Korean)

Loopring Protocol

Loopring Official Blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store