Zero-Knowledge Protocols
local graph
Chapter 5 — Zero-Knowledge Protocols
Section titled “Chapter 5 — Zero-Knowledge Protocols”As we have already seen with the digital signature, the authentication problem is one of the most important, complicated, and intriguing challenges that cryptography is going to face in the near future. Imagine that you want to identify yourself to someone who doesn’t know you online. First, you will be asked to provide your name, surname, and address; going deeper, you will be asked for your social security number and other sensitive data that identifies you. Of course, you know that exposing such data via the internet can be very dangerous because someone might steal your private information and use it for nefarious purposes.
Hackers know everything about their victims’ lives – finances, assets owned, and even credit card numbers. If a hacker knows your identity, they can easily find out about most of your digital life.
In another case, you might have read a news story where a gang of thieves planted a fake ATM in a commercial center. Each time a person inserted a card and entered their PIN, a computer recorded this information and the ATM refused the operation. Once the information had been collected from the cards and the PINs stolen, the hackers could then clone the cards, reproducing them along with their PINs, and subsequently be able to withdraw money at an actual ATM.
How is it possible to block this kind of scam? There are many situations in which sensitive information, such as passwords and other private information, is required. If a hacker obtains this information linked to a person or a machine, they can easily steal identities and wreak havoc for their victims.
One way to solve these kinds of problems is by not revealing any sensitive information, such as your name, surname, address, social security number, or PIN, but this is not always possible. Another way is to avoid exposing private information by giving proof of knowledge.
Proof of knowledge is a way to prove that you know something without revealing that information. In practice, proof of knowledge allows the user to identify themselves without entering a PIN, a password, or any other sensitive information. These cryptographic protocols are called Zero-Knowledge Protocols (ZKPs), which we are going to study in this chapter.
In this chapter, we are going to cover the following topics:
- The framework and logical basis of Zero-Knowledge Protocols
- Non-interactive and interactive ZKPs (the Schnorr protocol), with examples and possible attacks on them
- One-round ZKPs
- Introduction to zk-SNARKs
- ZK13 and the zero-authentication protocol
Now that you have been introduced to the world of ZKPs and know what they are used for, it’s time to go deeper to analyze the main scenarios and protocols used in cryptography.
The main scenario of a ZKP – the digital cave
Section titled “The main scenario of a ZKP – the digital cave”Imagine this fantastic scenario: Peggy has to demonstrate to Victor that she is able to open a locked door in the middle of Ali Baba’s cave, a cave with only one entrance/exit that can be reached from two directions, as you can see in the following figure. I suppose you have noticed that I have changed the names of the two actors, Peggy and Victor, from the usual Alice and Bob, just because here a verification is due, and the names Peggy and Victor match better with the first letters of prover (P) and verifier (V).

Figure 5.1 – Ali Baba’s cave
Figure 5.1: Ali Baba’s cave
This example involves Ali Baba’s cave revisited in modern times, in which the door in the middle of the cave is locked through a secret electronic combination that’s strong enough to prevent the entry of anyone who doesn’t know the secret combination.
Now, suppose that Peggy must prove to Victor that she knows the combination to unlock the door without revealing the numbers to him.
So then, the challenge for Peggy is not to reveal the combination of the door to Victor, as Peggy is not sure whether Victor knows it and she doesn’t want to give Victor any information about the combination. She just needs to demonstrate to Victor that she can exit from the opposite side of the cave.
Expressed differently, ZKP is a challenge where the answer is not revealing the exact information required but simply proving to be able to solve the underlying problem. Indeed, the natural way to demonstrate knowing something is to reveal it, and the verification just comes naturally. However, by taking this approach of just demonstrating what you know so directly, you could reveal more information than required. With a ZKP, Peggy can avoid the issue of revealing the digital door’s combination, and at the same time, Victor can be sure (even if he doesn’t know the combination) that Peggy knows the combination if he sees Peggy coming out of the cave from the opposite side.
There are many ways to implement a ZKP because, as you can imagine, there are many scenarios in which such verification could be required.
For example, you can think of Peggy as a human and Victor as a machine (an ATM or server). Peggy has to identify herself to the machine, but she doesn’t want to reveal any sensitive data, such as her name or surname. She just has to prove to the machine that she is really who she is supposed to be. The aim, in this case, is to avoid revealing Peggy’s identity. ZKPs can be applied here. Another use case where ZKPs can be applied is the authentication of virtual machines in a computer network. We will cover this use case in Chapter 8, Homomorphic Encryption and Crypto Search Engine, where we will use ZKPs to protect against man-in-the-middle attacks.
Now, we will delve deeper into the applications of ZKPs, starting with analyzing non-interactive protocols used to prove statements.
Non-interactive ZKPs
Section titled “Non-interactive ZKPs”The protocol we are going to analyze in this section is a non-interactive ZKP. This means that the prover has to demonstrate the statement, assuming that the verifier does not know the solution (the content of the statement) and that the verification is made without any exchange of information from the verifier.
The scheme could be summed up as follows:

Let’s take this problem into consideration: Peggy states to know [m], encrypted with RSA, as follows:

Keep in mind that Peggy doesn’t want to reveal the content [m] to Victor.
Here, (N, c, e) are public parameters, and [m] is secret.
Important Note
Remember, I always denote the secret elements of the functions with the […] symbols.
In order to demonstrate the statement to Victor, the following protocol is executed:
- Peggy chooses a random integer, [r 1] (which she keeps secret), and calculates the inverse of r 1 (represented as INV[r 1]) multiplied by [m] (modulo N):

Peggy keeps r 2 secret.
- Peggy calculates (x 1) and (x 2), as follows:


Peggy sends x 1 and x 2 to Victor.
- Finally, Victor verifies the following:

It is supposed here that if Victor can verify step 3,
, then Peggy really should know [m].
As you can see, Peggy wants to demonstrate to Victor that she effectively knows the message [m] without revealing it. Remember that, in this case, Peggy ignores whether Victor knows [m] or not. In fact, using this protocol, it is irrelevant if Victor actually knows [m] or not.
So the underlying challenge implicit in this statement is for Peggy to be able to solve the RSA problem without revealing [m].
Indeed, it’s supposed that if Peggy knows [m] (hidden in the cryptogram (c)), she can also calculate the function
; otherwise, she will not be able to do that.
Another important consideration is the following: if (c) is a big number, it is supposed that it should be hard to know x 1 and x 2 (the two factors of (c)) without knowing [m]. It could be considered the same degree of computational difficulty to factoring (N).
As you can see in the preceding function, (r 2) is calculated using [m] in the following equation:

So the final effect of the multiplication between
(as you can see in the following demonstration) will be to eliminate (r 1) and leave
, which is equal to (c).
Even if Victor doesn’t know [m], he can believe what Peggy states (to know [m]) because she has demonstrated she can factorize (c).
RSA is supported by the factorization problem (as we saw in Chapter 3, Asymmetric Encryption Algorithms); here, the function is as follows:

This states that it is computationally hard to find two numbers
that factorize (c).
Important Note
I will prove that an attack on this protocol exists that avoids the factorization problem in order to trick Victor, which we will experiment with later in this chapter.
Let’s see with a numerical example how this protocol works before going deeper to analyze it:
- m = 88
- N = 2430101
- e = 9007
- me ≡ c (mod N)
- 889007 ≡ 160613 (mod 2430101)
Now, let’s start the protocol of verification.
Peggy chooses a random number:

- Step 1: Peggy calculates r 2:

First, Peggy calculates the [Inv(r 1)] function (the inverse value of r 1 (mod N)), and then she multiplies it by [m]:




So Peggy gets r 2:

Then, Peggy calculates x 1 and x 2:






Finally, Peggy sends x 1, x 2 (1587671, 374578) to Victor.
- Step 2: Victor can verify the following:



Let’s see why this protocol is mathematically correct.
Demonstration of a non-interactive ZKP
Section titled “Demonstration of a non-interactive ZKP”We have to demonstrate the following:

Where
, we can substitute in the previous function
with
so that we get the following:

Substituting r 2 into the equation, we have the following:

Going by the modular power’s properties (collecting together the two factors I have highlighted), we have the following:

So eliminating
and
from the final equation will leave only the m e remaining in the second stage of the equation, and the result will be as follows:

As you know, since the beginning of this demonstration, (
) is just the RSA encryption of the secret message [m], which is equal to the cryptogram (c); that is why
. That’s just what we wanted to demonstrate.
This protocol has an important characteristic – it’s executed in only two steps:
- Peggy calculates the parameters
- Victor verifies the correctness
Crucially, this avoids any interaction between Peggy and Victor.
The next section will show how we can attack an RSA ZKP.
Demonstrating an attack on an RSA ZKP
Section titled “Demonstrating an attack on an RSA ZKP”If you have stayed with me until this point, I’m hoping you will follow me further on this journey so that I can give you a demonstration of using a protocol to trick the verifier.
Note that I created this attack at the end of 2018. Over time, we have since seen more demonstratable attacks on a ZKP.
The goal of this attack is to demonstrate that Eve (the attacker) can calculate two fake numbers (x 1, x 2) that prove to factorize (c), even if Eve effectively doesn’t know [m].
Let’s explore how this attack works and what effects are produced:
- Eve (the attacker) picks up a random number, [r], and calculates the following:

(e) is the public parameter of RSA (as we saw in the RSA section of Chapter 3, Asymmetric Encryption Algorithms), so (e) is known by everyone. By means of this function, Eve can extract (v1).
- In parallel, Eve calculates the following:

- The parameters (e, c) are known. This, just like in step 1, is an inverse multiplication (modulo N). The scope of this operation is to obtain [x]. Then, using [x], Eve multiplies [x] by [r], yielding (v2):

Eve sends (v1, v2) to Victor, who can verify the following:

Eve can impersonate Peggy, and she can claim to know [m] even if she doesn’t know it!
Numerical example:
r = 39 is the secret number chosen by Eve.
(N, c, e) are the same public parameters of the previous example (N = 2430101, c = 160613, e = 9007):
- Step 1: Eve calculates v1:



Eve performs v2.
The next operation is to obtain the inverse of (e) with respect to (c).
, obtaining (x) in inverse modular multiplication:


Then, using x, Eve gets v2, performing the following operation:
x * r ≡ v2 (mod N), obtaining v2:

After having gained v2, Eve sends (v1 = 1557988; v2 = 1480556) to Victor.
- Step 2: The verification stage.
Finally, Victor can verify the following:


The attack was successful!
Important Note
Peggy herself could be the primary actor of this trick if she doesn’t know [m], but she wishes to convince Victor of it.
This attack works because (c) contains [m], and I don’t need to demonstrate showing the value of [m]. This protocol isn’t required to show [m] or its hash*, [H(m)],* because Peggy doesn’t want to reveal any information about [m] to Victor. Remember that this is not an authentication protocol; it’s a proof of statement (or knowledge) that Peggy knows [m].
For completeness, also note that (c) is not a product of two big prime numbers such as N is, as you have probably already noticed in the example. So the logical basis of this protocol is weak: finding two numbers whose product is (c) would be not so difficult, even if (c) is a large number.
To use a hypothetical example, imagine a scenario where there are two countries: (A) has to demonstrate to (B) that it holds the formula for an atomic bomb. Using this ZKP, (A) could claim to know [m] (the formula of the atomic bomb) without really knowing it.
This attack could be prevented under certain conditions, one of which is the following.
If Victor already knows [m], then he can require Peggy to send him a hash of the message, H[m]. Victor can then verify whether (x 1 and x 2) are the correct values, and he will accept or deny the verification, based on the correspondence of the hash value with [m].
In this case, the problem is that the aim of this protocol was not to prove something that was already known but to prove something independently, regardless of whether or not it was known.
This last point is very important because if Victor knows [m], then this protocol works; if Victor doesn’t know [m], this protocol fails.
To prevent problems such as this, we have to switch to an interactive protocol, as we will see in the next section.
Schnorr’s interactive ZKP
Section titled “Schnorr’s interactive ZKP”The protocol that we saw in the previous section is a non-interactive protocol, where Peggy and Victor don’t interact with each other but there is simply a commitment between them. The commitment is that Peggy shows Victor that she knows the message [m] without revealing anything about it. Thus, she tries to demonstrate to Victor that she can overcome the RSA problem (or another hard mathematical problem) as proof of her honesty. However, we have also seen that this protocol can be bypassed using a mathematical trick.
Let’s see whether the following interactive ZKP is more robust and can prevent possibly devastating attacks.
We always have Peggy and Victor as our main actors. So let’s assume the following:
- p is a big prime number.
- g is the generator of (Zp).
- is the public parameter of Peggy.
- (p, g, B) are public parameters.
- [a] is the secret number object of the commitment.
Peggy claims that she knows *[a];*let’s say that [a] is the password to unlock a certain amount of money in a wallet. In order to demonstrate the claim, Peggy and Victor apply the following interactive protocol:
- Step 1: Peggy chooses a random integer, [k], where 1 ≤ k < p-1. This is the basis of modular math.
She performs the following calculation:

Peggy sends (V) to Victor.
- Step 2: Victor chooses a random integer, (r), where 1 ≤ r < p-1.
Victor sends (r) to Peggy.
- Step 3: Peggy calculates as follows:

Peggy sends (w) to Victor.
- Step 4: Finally, Victor verifies as follows:

If that is true, Victor should be convinced that Peggy knows [a].
Let’s see why the protocol should work and the reason why the last function (V) states that Peggy really knows [a] (the commitment).
First of all, I will show why the protocol is mathematically true, and then I will give a numerical example of this protocol.
A demonstration of an interactive ZKP
Section titled “A demonstration of an interactive ZKP”Recall the following instructions:



Now, we substitute all the past equations inside the last verification of step 3:


Substituting the functions in (V), the equation becomes the following:

For the properties of exponential factors, we have the following:


Simplifying [-ar] with [+ar], we get back the following:

That’s what we wanted to demonstrate.
Let’s do a numerical example to better visualize how this interactive ZKP works.
Numerical example:
Let’s assume the following parameters:
- p = 1987
- a = 17
- g = 3
(p = 1987 and g = 3) are public parameters.
[a] = 17 is the secret number that Peggy claims to know:

(B) is the public key of Peggy, given by the following:

Peggy picks up a random number, [k] = 67, and she calculates (V):

Peggy sends (V) to Victor.
Victor picks up a random number (r = 37) and sends it to Peggy, who can calculate the following:



Peggy sends (w = 1424).
Finally, Victor now verifies whether (V) = 1753 corresponds to the following:



In fact, it does correspond.
Now, we analyze the reason why this protocol states that by knowing [a] automatically, Peggy can convince Victor. Let’s use an example to better understand the problem.
This protocol can be used as an authentication scheme in which, for example, Victor is a bank that holds the public parameter (B) of Peggy (a client of the bank). The secret number [a] could be Peggy’s secret code (PIN). In order to gain access to her online account, Peggy has to demonstrate that she knows [a].
In another use case, we could have Victor as a central unit computational power (server) and Peggy as a user who wants to connect to the server using an insecure line, or again (as we will see later), Peggy could be another server, too.
The point of using a ZKP is to avoid Peggy revealing her sensitive data to the public. So the underlying problem she has to demonstrate to Victor consists of solving a challenge in which she can demonstrate that she knows the discrete logarithm of (B).
As we saw in Chapter 3, Asymmetric Encryption Algorithms, knowing (B) and (g) is not enough to compute [a] in this function:

This is because we are operating in modular functions.
Of course, Peggy needs to know [a] if she wants to compute the verification function, (w):

There is no way to trick Victor, who, furthermore, sent (r) to Peggy, which is used in the last verification function together with (v) and (B), along with (r), to ensure that Peggy cannot bluff:

So I hope I have convinced you that there is no way for Peggy to trick Victor in this case.
A challenge for a disruptive attack on an interactive ZKP
Section titled “A challenge for a disruptive attack on an interactive ZKP”Now that we have seen that Peggy can’t trick Victor, let’s consider an attack against this protocol that I created in late 2018 and see whether it works or not.
Here, the scope for an attacker (Eve) is to provide a final proof of verification without knowing the secret number, [a].
- Step 1: Peggy chooses a random integer [k] where 1 ≤ k < p-1.
Then, she calculates the following:

It’s when Peggy sends (V) to Victor that Eve can try to launch a man-in-the-middle attack.
Peggy sends (V) to Victor.
- Step 2: Victor chooses a random integer, (r), where 1 ≤ r < p-1.
Eve injects
, where [k1] is a number invented by Eve that substitutes [k].
Eve sends (V1) to Victor, substituting her value for Peggy’s result. After receiving (r) from Victor, Eve calculates (V1) as follows:

This is the path to the attack:
- If you can exclude (r) from the final verification function, (V1), then you have reached the goal.
- Essentially, the attacker should find a value for (v1), as follows:

Here are some things to note:
- Remember that you don’t have to implement (v1) in the same way the preceding function (v) did, but you are free to give (v1) any value.
- Remember that the earliest point of attack is substituting (V) with (V1), but that is not mandatory. In this case, the warning is that as you don’t know (r) when you have delivered (V1) to Victor, this parameter can no longer be changed.
- Good luck! If you are able to find a way to trick the Schnorr interactive protocol, please let me know when you have arrived at a conclusion, and you will get a cryptographer researcher position.
The preceding analyzed interactive protocol suffers from another problem. Let’s imagine that two people live in different time zones, such as Europe and Australia. If one person is ON, the other person is probably OFF because they are sleeping. In that case, this isn’t probably the most appropriate protocol to use.
This protocol doesn’t fit well with this kind of purpose, such as cryptocurrency transactions. Most cryptocurrency protocols use zero-knowledge algorithms to anonymize data inside their architecture structures. Now that we know how to implement such a protocol, we can explore zk-SNARKs.
One-round ZKP
Section titled “One-round ZKP”In this section, we’ll explore a little-known ZKP composed of only one round of encryption that was presented by two researchers, Sultan Almuhammadi and Clifford Neuman, from the University of Southern California. It purports to give proof of knowledge for a challenge in just one round. The paper, Security and privacy using one-round zero-knowledge proofs (2005), states the following: “The proposed approach creates new protocols that allow the prover to prove knowledge of a secret without revealing it.”
The researchers also proved that a non-interactive ZKP is more efficient in terms of computational and communications costs because it saves execution time and reduces latency in communication.
ZKPs are used in many fields of information technology, such as e-commerce applications, smart cards, digital cash, anonymous communication, and electronic voting. Almuhammadi and Neuman sought to satisfy the requirements of ZKPs but in just one round, eliminating any iterative mathematical scheme that would entail high computation and communication costs.
So let’s dive deep to analyze this one-round ZKP and see how it works.
Let’s say that Peggy wants to demonstrate to Victor that she knows a discrete logarithm (we’ll be focusing on a discrete logarithm, but the protocol can work for other problems); in order to do this, Peggy has to demonstrate that she knows [x], as follows:

Victor launches a challenge (c) to verify whether Peggy really knows [x]. He picks up a random [y] and calculates the following function:

Victor sends (c) to Peggy. She inserts the parameter [x] on (c), computing (r) as follows:

Peggy sends (r) to Victor, who can verify the following:

Finally, Victor accepts the verification if (r) corresponds to
.
This protocol looks very simple and straightforward; you may recall some of it from the Diffie-Hellman (D-H) algorithm we covered in Chapter 3, Asymmetric Encryption Algorithms. It is based on the computational difficulty of calculating the discrete logarithm, as you have seen in previous cases. But to help you better understand the operations, I will show how it works mathematically and demonstrate a numerical example in the next section.
How it works mathematically
Section titled “How it works mathematically”The first question is, why are the parameters (r and V) mathematically identical?
Here, you can find the answer:


As you can see, 
Numerical example
Section titled “Numerical example”Let’s look at a numerical example:
- p = 2741
- g = 7
x = 88 is the secret number that Peggy has to demonstrate she knows.
The statement is as follows:



Victor chooses a random number:

Victor calculates the following:



Peggy, after having received (c), calculates the following:



Peggy sends (r) to Victor, who can verify the following:



Finally, it is verified!
As you can see, we have proved the one-round ZKP with the help of a numerical example. In the next section, we will examine some notes that further demonstrate the strong similarity of this protocol with the D-H algorithm.
Notes on the one-round protocol
Section titled “Notes on the one-round protocol”Having analyzed this protocol, you may have noticed that it is similar to the D-H exchange. Undoubtedly, the authors of the one-round ZKP were well aware of that. Still, even though the aim of the one-round ZKP is different from that of D-H, I will compare the two algorithms so that you can see what similarities there are.
In the second part of the analysis, we will see how efficient this protocol is. Indeed, with only two steps, Peggy can demonstrate to Victor that the statement [x] is valid.
Now, we can reassemble the one-round protocol using the following method:
- Step 1: Peggy:

That is the same in D-H, as follows:

- Step 2: Victor:

That is the same in D-H, as follows:

- Step 3: Peggy:

In D-H, this becomes the shared key H:

- Step 4: Victor:

In D-H, this again becomes the shared key H:

So [H] is the shared private key that “Alice and Bob” (here, Peggy and Victor) use to compute in D-H. Here, it is just [r = H] that gives the proof to Victor.
So we can certainly say that Sultan and Clifford’s protocol is identical to D-H, as discussed in Chapter 3, Asymmetric Encryption Algorithms.
This protocol undoubtedly verifies that Peggy knows [x]. She can demonstrate it to Victor even if Victor doesn’t know [x]. That is the exciting point, and the innovation of this protocol: even if Victor doesn’t know [x], by using this protocol, he can be confident that Peggy knows it. In other words, what the authors of this protocol did was apply the D-H protocol to the ZKP use case.
If you look at the simplified version of the protocol shown below, you will get an even better understanding of the steps required. There are only two, essentially:
- Initialization of the parameters for Peggy is g, b, p, and x.
- Victor generates a random y.
Step 1: Victor sends the following to Peggy:

Step 2: Peggy sends the following to Victor:

Instantly, Victor can verify the following:

As you can see, there are only two steps required to perform this protocol and verify the statement [x] through Victor’s validation of the parameter (r).
This protocol inspired me to build a new protocol, which we will explore in the next section. My research has allowed me to reduce the number of steps to one.
An introduction to zk-SNARKs – spooky moon math
Section titled “An introduction to zk-SNARKs – spooky moon math”Now, we are approaching a new kind of protocol called zk-SNARKs – a kind of non-interactive ZKP that is a little bit complicated, which is also known as spooky moon math. In the next section, you will see interesting new attack possibilities.
Non-interactive zero-knowledge proofs, also known as zk-SNARKs or zk-STARKs, are types of ZKPs that require no interaction between the prover and verifier, like the first protocol we saw in this chapter. In this section, we are going to focus on zk-SNARKs.
The name zk-SNARK stands for Zero-Knowledge Succinct Non-Interactive Argument of Knowledge. So we are facing off with schemes that need only one interaction between the prover and the verifier.
Indeed, zk-SNARKs are very much appreciated for their ability to anonymize transactions and identify users in cryptocurrency schemes, as we will see in this section.
zk-SNARKs have been adopted in the blockchain as a scope of authentication to create consensus.
The use of zk-SNARKs in a blockchain is important, as we will see later, for the use of smart contracts. As you may know, a smart contract is an escrow of cryptocurrency, activated following the completion of an agreed execution.
Since smart contracts and blockchains are not a part of this book, I will show just a limited example of how zk-SNARKs work in a cryptocurrency environment, as it will be useful to understand.
For example, suppose Peggy makes a payment in Ethereum to execute a smart contract with Victor. In that case, both Peggy and Victor want to be sure that the execution of the smart contract (for Peggy) and the payment received (for Victor) are completed successfully. However, many details inherent to the smart contract will not be revealed. So the role that zk-SNARKs play is fundamental to covering these secrets and executing smart contracts. In order to work, the protocol has to be fast, secure, and easy to implement.
As we have already seen, you will notice that this is just what the purpose of a ZKP is – to ease navigation in an untrustworthy environment. Here, we are talking about blockchains and virtual payments, but essentially, the process is similar.
So in this environment, zk-SNARKs keep secrets by protecting the steps involved in a smart contract and, at the same time, proving that all these steps have been executed. This way, they protect the privacy of people and companies.
Remember that (not because you have to be super-skeptical, but because you should be realistic) this statement is true under determinate conditions, which I will try to explain as follows:
- The proof given by the prover holds the same computational degree of difficulty as the underlying algorithm chosen as proof of knowledge.
- There is no mathematical way to trick the verifier with a shortcut or fake proof (such as substituting fake parameters in the
equation in order to avoid knowing [a]).
Now, let’s see how a zk-SNARK works.
Understanding how a zk-SNARK works
Section titled “Understanding how a zk-SNARK works”In this section, first of all, I will try to synthesize how zk-SNARKs generally work, and then we will return with a zk-SNARK protocol related to a proof of knowledge based on a discrete logarithm.
As we already have seen for the other ZKPs, a zk-SNARK is composed of three parts or items – (G), (P), and (V):
- G: This is a generator of keys, made by a private parameter (the statement or another random key) that generates public parameters given by private keys.
- P: This is a proof algorithm that states what the prover wants to demonstrate.
- V: This is a verification algorithm that returns a TRUE or FALSE Boolean variable from the verifier. I will demonstrate now that using ZKPs (and, in particular, zk-SNARK protocols) is not enough to keep [w] secret, but it is possible to arrive at proving the statement as TRUE if it is also FALSE.
Let’s look at how a similar protocol example explained in the Interactive ZKP section (Schnorr) would work in a non-interactive way (zk-SNARK mode).
In this protocol, we have Anna as the prover and Carl as the verifier.
Here, Anna has to prove that [a] is known to her.
Anna calculates her public key, (y), given by the following:

(g) is a generator (as in D-H and other private-public algorithms we have already seen in this book).
Then, Anna picks up a random value, [v], inside p-1, which she keeps secret, and consequently, she can calculate the following:

Anna calculates (c) as a hash function of the three parameters, (g, y, t), and she can compute (r) as follows:

The verifier, Carl, can check the following:

Finally, if the verification validates the two terms of the function, then Carl accepts that the statement [a] proposed by Anna is TRUE.
Now that we have analyzed how this ZKP works in a zk-SNARK environment, let’s see an attack on this protocol before we cover a numerical example.
Demonstrating an attack on a zk-SNARK protocol
Section titled “Demonstrating an attack on a zk-SNARK protocol”This attack was performed by me in June 2019 and just goes to show that nothing is completely secure. In practice, this attack may face issues encountering protected information, so the purpose is instead to show that it would be mathematically possible.
Let’s say that Eve is a server. We suppose that Eve intercepts the H(g, y, t) public hash function and performs a man-in-the-middle attack.
While Peggy sends (V) to Victor, Eve substitutes (c) = H(g, y, t) with (c1) = H1(g, y, t1), remembering that (H) is the hash function and that (t1) is given by the following:

As you have probably noticed, substituting (v) with (v1) is the same trick that substituted (k) with (k1) in the previous attack.
Simply, Eve can insert (r 1) as follows:

Now, Eve orders the third server connected to the internet to send to Carl (r 1, v1, c1), who can verify the following:

It’s simple to demonstrate that t1 = g v1 because of the following:

Finally, as we have assigned
and
, the final effect will be as follows:

Numerical example:
- p = 3571
- g = 7
- x = 23
Anna’s public key is as follows:



Now, I will show you how Anna can demonstrate to Carl how to get the secret number, [x].
She chooses v = 67, as follows:



Let’s suppose that hash (g, y, t) is as follows:

She computes r as follows:



Anna sends
to Carl.
Carl can verify the following:


However, Eve intercepts the public parameters (y), (t), and (r). Eve leaves the (y) invariant, but she changes (t) to (t1) and (r) to (r 1), performing a man-in-the-middle attack:

Eve calculates the following:




Eve sends
to Carl.
Carl verifies the following:

I highlighted the parameter that Eve substitutes, (t1, r 1, c1); she left the (y, g, p) invariant.
Substituting the new parameters into the equation of verification, we have the following:

Indeed, Carl is able to verify that t1 = 712 corresponds with the parameters received from Eve.
Essentially, if Carl is not able to recognize that r 1 = v1 and/or he doesn’t accept c = p-1, then the trick is done, and Eve can replace Anna.
So what are the protections to adopt against this attack?
If this attack is implemented in a more sophisticated mode, it will probably be very difficult to prevent it.
Note that the parameter (y), the public key from Anna that “envelopes” the private key [a] object of the statement, hasn’t been modified during the attack.
Anyway, zk-SNARKs can be implemented using other methods and protocols to prove statements; we will see what these algorithms and protocols are in the next section. Blockchains and cryptocurrency are evolving quickly to find new methods to authenticate users anonymously. However, with this topic being relatively new, it is better to make the effort to find all the possible attacks and the repair methods for them.
ZK13 – a ZKP for authentication and key exchange
Section titled “ZK13 – a ZKP for authentication and key exchange”Now, we will look at the ZK13 protocol, which I patented in 2013. It’s a non-interactive protocol that solves an issue that is very important to the Crypto Search Engine (CSE) project explained in Chapter 8, Homomorphic Encryption and Crypto Search Engine. Essentially, the issue is this: we need authentication without requiring a public key.
In this section, we will analyze the ZKP that’s used for authentication; we could call this protocol a ZK-proof of authentication. To better understand the problem, imagine Alice and Bob want to share a common secret, something that only they know. Let’s say that the secret is the answer to the following question: how many birds were counted at the lake shoreline today? The answer is known only to Alice and Bob, unless they have revealed it to someone, but this is a problem we will take into consideration later. For now, nobody else can know the answer except Alice and Bob. We can consider the number of birds counted as a shared secret, a key that doesn’t need to be exchanged. It is shared by Alice and Bob, a key that is implicitly formed by a common experience. So besides the authentication problem, there is also the problem of verifying a private Pre-Shared Key (PSK). Indeed, under ZK13, Alice tells Bob to use the secret shared key (the number of birds counted) as a secret password, [private key]. What’s even more interesting here (and this is where it really differs from the D-H key exchange algorithm we saw before) is that the secret key is not really exchanged at all but, instead, is simply something that is known to both parties and is only verified.
So there are several problems that this ZKP can solve. Here, we will just consider the authentication problem. Later in the book, we will analyze how to use a ZKP to exchange a shared private key.
In 2013, I was drawing up the architecture of the CSE. We will talk in detail about the CSE in Chapter 8, Homomorphic Encryption and Crypto Search Engine. At the time, one of the toughest problems to solve with the CSE’s architecture was finding a cryptographic method to identify a Virtual Machine (VM) network. Since the algorithm chosen was symmetric, the problem was to find a method of authentication that would work with the symmetric algorithm.
As you already know from Chapter 2, Symmetric Encryption Algorithms, it’s common to think that symmetric algorithms don’t have a digital signature method of authentication because they do not have public keys. At first glance, it doesn’t seem easy to find such a method of authentication, but it can be possible if the process starts with a shared secret. The goal was to prevent a man-in-the-middle attack by an external hostile VM against a network.
To overcome all these issues, I considered implementing a new ZKP. Taking a look at the most popular ZKPs, I did consider Schnorr (presented earlier in this chapter) as a candidate. However, an interactive protocol didn’t fit well. This scheme needs more steps between the prover and verifier, generating latency in the communication. So I decided to implement a new personal zero-knowledge non-interactive protocol.
After many studies and a pinch of inventiveness, I designed ZK13. Before analyzing it, I will explain what constraints I worked under:
- The secret shared key (the challenge) has to be embedded inside the VM database. Therefore, engineers could inject the secret parameter [s] into both of the VMs without exchanging any keys through an asymmetric algorithm.
- The goal of ZK13 is to enable parties to identify each other by sharing a small amount of sensitive information. This means exchanging only the minimum amount of sensitive information (that is, the hash of [m]: H(m) instead of [m] itself) that needs to be shared. Indeed, the greater the amount of information exchanged, the greater the vulnerability to attack becomes.
- ZK13 had to be a simple and, as I have already said, non-interactive protocol. Therefore, only one piece of information should be required by the prover. The reasons for this are twofold: first, to prevent an excess of information being exchanged (see the previous point) because that could compromise security. The second reason is related to the goal of the application: the CSE is a platform on which encrypted data is searched and retrieved using the cloud or external servers. Because a search engine has to be fast, queries should be fielded and answers given in the least amount of time possible. So it is crucial to prevent latency during the authentication phase.
- Another constraint of ZK13 was for it to use the best and most secure authentication methods. At the time that it was conceived (2011–2013), the quantum computing era was not yet seen as dangerous for cryptography. So the underlying problem on which the system relied was the discrete logarithm, which is still considered a hard problem.
ZK13 explained
Section titled “ZK13 explained”The ZK13 protocol, with only one transmission and a shared secret, is presented as follows:
![Figure 5.6 – The scheme of the shared hash[s] secret](https://static.packt-cdn.com/products/9781835080030/graphics/Images/B21272_05_02.png)
Figure 5.6 – The scheme of the shared hash[s] secret
Figure 5.2: The scheme of the shared hash[s] secret
Let’s dive deeper into ZK13 and see how it works.
Bob (VM-1) has to prove that he knows the secret, [s], to Alice (VM-2) in order to send Alice a set of encrypted files using the CSE system. Remember that [s] is stored inside the brains of both Alice and Bob, the two VMs, as an innate native injected secret.
Bob picks a random number [k] (the (G) element of a zk-SNARK or the random key generator). This random number, [k], is generated and destroyed in each session:
Public parameters:
- p: This is a prime number
- g: This is the generator
Key initialization:
- [k]: This is Bob’s random key
Secret parameters:
- [s]: This is the common shared secret
- H[s]: This is the hash of the secret, [s]
Step 1a: Bob calculates (r) as follows:

Let’s say that the secret shared is [s], but effectively, the VM operates with H[s], the hash functions of [s].
Step 1b: Bob calculates [F], a secret parameter, which is changed in each session (just because [k] changes):

Now, with (g) raised to [F], Bob proves (P), which is the second element of the zk-SNARK:

Bob sends the pair (P, r) to Alice.
Step 2: The verification step (V) validates the proof, (P), based on the function:


If:

Alice proceeds to make a hash of [s]: H[s], and then she accepts the authentication if V = P; if ((V) is not equal to (P)), she doesn’t accept the validation.
In this case, as we have supposed in the initial conditions, [s] is supposed to be known by Alice.
As you can see, ZK13 works in only two steps, but the verifier (in this case, Alice) must know the secret, [s]; otherwise, it is impossible to verify the proof.
Numerical example:
Now, let’s see a numerical example of the ZK13 protocol:
Public parameters:
- p = 2741
- g = 7
Secret parameters:
- H[s] = 88
- k = 23
- g k ≡ r (mod p)
- 7 23 ≡ 2379 (mod 2741)
- r = 2379
Now, Bob calculates [F] and then (P):






Alice verifies the following:


Alice double-checks whether
; if it’s TRUE, then it means that Bob does know the secret, [s]. Now that we have proven that ZK13 works with a numerical example, I want to demonstrate how it works mathematically.
Demonstrating the ZK13 protocol
Section titled “Demonstrating the ZK13 protocol”Since
, what we want to demonstrate is the following:

(Here, I use [s] for the demonstration instead of H[s].)
As
, substituting (r) in the preceding equation, we have the following:

We also know that F is the following:

Finally, for the properties of the modular powers substituting both [F] and (r), we get the following:

Basically, I have substituted the parameter (P), the proof created by Bob, with the elements of the parameter itself, demonstrating that the secret, [s], is contained inside (P). So (P) has to match with the ephemeral parameter (r) [s] generated by Bob and sent to Alice together with the proof, (P). If Alice knows [s], then she can be sure that Bob also knows [s] because (P) contains [s]. That’s what we wanted to demonstrate.
Notes and possible attacks on the ZK13 protocol
Section titled “Notes and possible attacks on the ZK13 protocol”You will agree with me that by using this protocol, it is possible to determine proof of knowledge of the secret, [s], in only one transmission.
During the explanation of the algorithm, I divided it into three steps, but actually, there are only two steps (with only one transmission) because the operations of (G) key generation are offline. So steps 1a and 1b can be combined into effectively only one step.
Possible attacks on ZK13
Section titled “Possible attacks on ZK13”Let’s say Eve (an attacker) wants to substitute herself for Alice or Bob, creating a man-in-the-middle attack.
This could be done as follows.
Eve replaces (r) with (r 1), generating a fake (k1), by calculating the following:

But when Eve computes [F], she doesn’t know H[s] (because it’s assumed that [s] will remain secret), so this attack fails.
Instead, she can collect (r, P) and replay these parameters in the next session, activating a so-called replay attack.
This attack could be avoided here because (r) is generated by a random [k], so it is possible to avoid accepting an (r) already presented in a previous selection.
So that was one attack that could be faced, and we saw how to prevent it.
Summary
Section titled “Summary”Now you have a clear understanding of what ZKPs are and what they are used for.
In this chapter, we have analyzed in detail the different kinds of ZKPs, both interactive and non-interactive. Among these protocols, we saw a ZKP that used RSA as an underlying problem, and I proposed an original way to trick it.
Then, we saw the Schnorr protocol implemented in an interactive way for authentication, on which I proposed an attack attempt.
Moving on, we explored the zk-SNARKs protocols and spooky moon math, just to look at the complexity of some other problems. Among them, we saw an interesting way to attack a discrete logarithm-based zk-SNARK.
Later in the chapter, we encountered and analyzed a non-interactive protocol based on the D-H algorithm. We explored ZK13, a non-interactive protocol, and its use of shared secrets to enable the authentication of VMs.
Finally, we explored zk-SNARKs in the world of cryptocurrency, especially those used to anonymize transactions.
You became familiar with some attack schemes, such as man-in-the-middle, and used some mathematical tricks to experiment with ZKPs.
The topics covered in this chapter should have helped you understand ZKPs in greater depth, and you should now be more familiar with their functions. We will see in later chapters many topics that refer to what we explored here. Now that you have learned the fundamentals of ZKPs, in the next chapter, we will analyze some private/public key algorithms that I have invented.