kartikeya sharma
AI Verification · Applied Cryptography · AI Safety

Why Computing g To The x Is Easy

Friday, August 28, 2026

CryptographyBitcoin

Last week, we left off talking about this thing called the discrete log problem, DLP.

f(x) = gx mod p

p is a large prime, and g is a number that comes from our Zp∗ group.

The group we are working in

Zp∗ is a set. It goes from one all the way up to p − 1. If I say something like Z7∗, it's going to be 1, 2, 3, 4, 5, 6. Instead of us calling it Zp∗ every time, we're going to call this a multiplicative group, because that's what it is.

We can't just call any set a group. For a set to be a group, it needs to satisfy four conditions.

  1. The first is closure, which means if we choose two elements in the group, a · b is also in Zp∗. For example, 2 · 6 = 12, and now if we mod this by 7, 12 mod 7 is 5. You can also verify that 5 is in Z7∗.
  2. The second is associativity. If we choose a, b and c in Zp∗, it should follow the associative property: (a · b) · c = a · (b · c).
  3. The third property that a group has to have is the identity element, which pretty much means that if we choose any integer a in Zp∗, a · e and e · a should both equal a, where e is the identity element. In the case of Zp∗, the identity element is 1, because a · 1 = 1 · a = a. If we multiply it by 0, we would not get a. So 0 is not the identity element of Zp∗. It's only 1.
  4. The fourth thing is the inverse element. If we choose a in Zp∗, there should be an inverse in Zp∗ such that a · a−1 = a−1 · a = 1, the identity element. Suppose a = 2. What can we multiply 2 with to get 1 mod 7? 2 · 2 = 4. 2 · 3 = 6. 2 · 4 = 8, and 8 mod 7 is 1. So the inverse of 2 is 4.

These four properties must be true for each integer in Zp∗, and that's what we call a group. Basically, what it's saying is we can do multiplication in the set without things breaking.

The generator

We can call a group cyclic if there exists a generator g in that group. It's not exactly a random number. It's a special number, where if we raise it to the powers 1 up to p − 1, we get the set back. The value you will get is all the elements in the original Zp∗. This is all mod p.

In Zp∗, we already know, through proofs that have already been proven, that there is at least one generator that generates the entire group.

For Z7∗, the generator is three. Let's see what happens when we multiply this generator by itself p − 1 times: g1, g2, g3, g4, g5 and g6. We have to do all of these multiplications mod p.

31 = 3    32 = 9 ≡ 2    33 = 27 ≡ 6    34 ≡ 4    35 ≡ 5    36 ≡ 1    (mod 7)

What we notice now is that we have one, two, three, four, five, and six. All the elements appear only once. So there are no duplicates. There are no collisions, in other words.

Someone asked how I knew the generator is three. To be honest, I'm not sure. I think for small numbers, it's easy. But for large numbers, I'm not sure how the generator is picked. My guess is there's some trial and error algorithm, or there's a math proof where you could do it very quickly.

While you were talking, I just tried g = 2, and we already found a duplicate. 21 = 2, and 22 = 4. Then 23 = 8, and 8 mod 7 is 1. And 24 = 16, and 16 mod 7 is 2, which we have already seen. So this is not a generator. I should also specify: generate the entire group once.

So which numbers generate the whole group, and how many of them are there? Pick a prime, then a base.

Prime p
Base g (filled chips are generators)
–
–elements reached
–powers before it repeats
–generators in this group
–
Each cell is gk mod p for k = 1 up to p − 1. Once a power comes back to 1 the list starts over, so everything after that is a duplicate and is shown faded.

For p = 7 only 3 and 5 generate the group. The guess in the session is how it is done in practice: you try candidates, and the test is cheap, because g is a generator exactly when g(p−1)/q is not 1 for every prime q that divides p − 1.

Everyone is using a fixed generator when wallet addresses are being generated. As far as I know, there's a specific number that everyone uses as a generator. If you were to change that number, then you could have one private key mapped to two public keys, or something like that. So that's why the generator is a fixed, global and public variable. Everyone knows this, even attackers.

The hard direction

We said that doing this calculation, gx mod p, is very easy. What is hard is when you're given f(x), which is the answer to this, and you're given the generator, which is also public, and you're given the prime value, finding the x.

given y, g, p:   find x such that   y ≡ gx (mod p)

By the way, I'm using three lines here. That's just a way of showing it's congruent. 27 was the same as 6 mod 7.

Go back to the simple example. Given this number 4, and also g = 3 and p = 7, finding x is hard. As the person who calculated this equation, I know x is four. But what the attacker sees is not that top part of the screen. They're only given this.

Obviously, for small groups like this, you could just brute force it. You could just run this function seven times, and you will find it. But where p values are very large, it is very hard to find that x, and that is actually what's called the discrete log problem. This is why we also call this a one-way function, or a candidate one-way function, because we assume that it's secure.

DLP assumption: no PPT algorithm computes x = logg y (mod p) with non-negligible probability

People have tried to disprove it. People have tried to create algorithms to break it, to find that x. But the closest people have come is sub-exponential. You can bypass this type of attack by increasing your security parameter.

Bitcoin is not using this equation. Some of it has to do with the fact that people can break it in sub-exponential time. To bypass, you increase key size, and that increase leads to more storage costs. Things are just now larger for no reason.

The easy direction

Was anyone able to figure out why this calculation is easy? It doesn't matter what p is. Imagine p is 256 bits, 512 bits, 1024 bits, whatever. By the way, x is your private key here, your secret key, because the attacker is trying to guess this x.

Someone suggested building it up one step at a time: g2 is g · g, g3 is g2 · g, and so on. You're getting the point, but there are still some things we can optimize. The question I'll ask is, why do we even care about g3? If we've already calculated g2, why not just skip to g4, which is g2 · g2? That saves us one multiplication. So I do g2, then I calculate g4, then g8, then g16.

We don't need to take every single step. We can just start hopping, in a way. It's much easier if I just square this, because I'm moving closer to my number this way, if I do repeated squaring, versus if I only add 1 each time. It's called exponentiation by squaring.

If we have, say, x = 5, what we should do first is convert this to the binary representation. 20, 21, 22: that's 1, 2, 4. 4 + 1 = 5, so it's going to be 1, 0, 1. That's the binary representation of 5. We know that this number is something we need, and this number is something we need. We do not need this one. This is not part of the active bits, we could say.

Let's just take p = 7. 31 = 3 mod 7. For the next value, we square this: 32 = 9, which is 2 mod 7. For 34, we just have to square that again, because we know that the last value is 32. So we just have to do 22, which is equal to 4 mod 7.

So now we have these values: 3, 2, and 4. If I take my bit representation of the secret value, 1, 0, 1, I know that this is an active bit, so we take this 3. This is an inactive bit, so it's 0. And this is an active bit, so we take the 4. Now we have to multiply these, 4 · 3 only. We're not taking the 0.

35 ≡ 4 × 3 = 12 ≡ 5 (mod 7)

That is the answer for g5 mod 7, instead of doing 3 · 3 · 3 · 3 · 3. That's 243, I calculated it, which is 238 with a remainder of 5. So if you check it, it's the same answer.

Someone asked what the active bits mean. If you change this to 1, then what are you actually doing? 5 in binary is 1, 0, 1, but if you take this as an active bit too, this becomes 4 + 2 + 1, which is 7. So then you're actually finding it for x = 7.

Here is the same board for any exponent. Which squares get multiplied in, and how many multiplications does it take compared with multiplying g by itself x times?

Prime p  (g is 3)
–
–multiplications, by squaring
–multiplications, one at a time
–
Each row squares the row above it. Rows whose bit is 1 are multiplied into the answer. The count by squaring is one multiplication per row after the first, plus one for each extra active bit. The check at the bottom recomputes the answer the slow way.

How much work is that?

Multiplying one at a time first. The time complexity of this is easy to guess: 2n, where n is the number of bits in x. As the value of our input increases, the number of computations we have to do increases exponentially.

Now repeated squaring. In the worst case, if we think of the value 1, 1, 1, everything is active. So this is n, and then you would have to multiply each active bit, so that would be another n, which adds up to 2n, which, if you drop the constant, is n.

This is also something I learned later on. If we convert a number into its binary representation, we can convert a calculation like this from an exponential time complexity algorithm to a linear complexity algorithm, just by converting it to binary and then doing that repeated squaring.

What does that difference look like at the size of a real key? Slide the number of bits in x.

one at a time
by squaring
–multiplications, one at a time
–multiplications, by squaring
–
Worst case for an n-bit exponent with every bit active: 2n − 2 multiplications one at a time, against 2n − 2 by squaring. The bars are on a log scale, so each step to the right is a doubling.

Someone pointed out that since g is fixed, the table of squares could be computed once and stored. Yeah, of course. And then that would just be even easier. The whole point is that this should be an easy equation to compute. If you store these values somewhere, it just becomes easier, and now you have more space complexity. That's the trade-off.

This is very fast anyway. It doesn't grow exponentially with the size of the input. That's the main point. But this DLP assumption is saying that reversing that whole thing is extremely hard.

Where this goes next

What Bitcoin uses is the elliptic curve discrete log problem. Bitcoin has its own equation, and they have a generator as well, a generator G, which is a point on this curve. The public key and secret key, at least the raw parts, are derived from this curve using a generator, which is well known. It's a public parameter. We'll talk about this next week.

← Back to all articles