Introduction
Hash Function
A cryptographic hash function↗ is one of the basic primitives for cryptography. (In this article, the word hash function will mean cryptographic hash function unless otherwise noted.) A cryptographic hash function satisfies the below three secure properties:
- Preimage resistance: Given hash value , it is hard to find any input message that .
- Second-preimage resistance: Given input message , it is hard to find message that .
- Collision resistance: It is hard to find any two different input messages that .
Cryptographic schemes such as digital signatures, message authentication codes, commitment schemes, and authentications are built on top of hash function. For example, the preimage resistance property of a hash function enables the proof-of-work↗ system in cryptocurrency. SHA-2 and SHA-3 are common examples of hash functions, and they are adopted for general purposes.
Usage of Hashes in the ZK protocol
A hash function is also useful in the ZK protocol. One famous example is the membership check in a Merkle tree. For a Merkle root , the prover will claim knowledge of the witness such that
to prove their knowledge of element , which is a member of the Merkle tree. We already discussed this usage in our previous post, “How Does Tornado Cash Work?”.
Traditional uses of hash functions can be utilized in the ZK protocol as well, such as integrity checks using vanilla hash computation, retrieving pseudorandom strings from the fixed length seed, and signing transactions.
Why Are ZK-Friendly Hash Functions Required?
The standardized hash functions such as SHA-2 and SHA-3 are extensively studied, and their security is widely believed. Moreover, traditional software and hardware implementations of them are efficient compared to their competitors.
So when we need to evaluate a hash in ZK protocols, it seems nice to use SHA-2 or SHA-3. However, many ZK protocols are using relatively unfamiliar hash functions such as MiMC↗, Poseidon↗, and Rescue↗ instead of SHA-2 and SHA-3.
The main reason for this is that efficiency in ZK protocols is determined in quite different ways from traditional metrics such as running time, power consumption, and gate count. The efficiency of the circuits in ZK protocols depends on their algebraic structure.
Generally, if the circuit is represented as simple expressions in a large field, it allows efficient proof in terms of prover execution time and proof size. Unfortunately, traditional hash functions are inappropriate for this.
For example, SHA-2 is about 50–100 times more inefficient than ZK-friendly hash functions when the hash is computed in zk-STARK. This huge performance gap demonstrates the need for a ZK-friendly hash function.
In this post, we will introduce several ZK-friendly hash functions that are introduced in famous conferences and journals. It might be fun to focus on the design rationale for each hash function.
ZK Protocols
We first discuss the ZK protocols. Rather than trying to fully understand the protocols, we’ll focus on their high-level characteristics and what the metric is to determine efficiency.
zk-SNARK
zk-SNARK is an acronym that stands for zero-knowledge succinct non-interactive argument of knowledge.
- Zero-knowledge: Verifier learns nothing other than the statement is true.
- Succinct: Proof size and verifier time is sublinear.
- Non-interactive: Needs no interaction between prover and verifier.
- Argument of knowledge: Not only for existence of input but also prover’s knowledge of .
While there are several ways to build a protocol that satisfies the above characteristics, such as linear PCP + pairing-based cryptography↗, constant-round polynomial IOP↗, and polynomial commitment scheme + IOP-based↗, Groth16↗ based on the linear PCP and pairing-based cryptography is the most widely used one.
Groth16 and many other zero-knowledge proof systems such as Aurora↗, Ligero↗, and Bulletproofs↗ are evaluating the circuit in Rank-1 Constraint System (R1CS) form.
R1CS Examples
The R1CS is a system of equations, where each equation is defined by a triplet of vectors such that .
For example, can be interpreted as two equations defined by a triplet of vectors (with intermediate value ):
In the SNARK setting, the cost is determined by the number of constraints in the R1CS. For example, for the constant requires about constraints.
zk-STARK
zk-STARK is an acronym that stands for zero-knowledge scalable transparent argument of knowledge.
- Zero-knowledge: Verifier learns nothing other than that the statement is true.
- Scalable: Prover time is quasilinear; proof size and verifier time is sublinear.
- Transparent: No trusted setup is needed.
- Argument of knowledge: Not only for existence of input but also for prover’s knowledge of .
Any protocol that satisfies the above conditions is called a zk-STARK. However, zk-STARK often refers to the protocol in the paper↗ where the concept was first proposed. We also refer to the protocol in that paper as zk-STARK in this article.
To evaluate the circuits in a STARK setting, the circuit should be transformed into algebraic intermediate representation (AIR).
AIR Examples
We give an example of computing a Fibonacci sequence. The Fibonacci sequence is defined as and .
The prover wants to prove that . Then an algebraic execution trace (AET), an execution trace of a computation of the Fibonacci sequence, is
1 |
1 |
2 |
3 |
5 |
8 |
13 |
21 |
And the AIR constraints described in the polynomial forms are
Let be a number of rows and be a number of columns in AET. Then the size of the AET is . Moreover, let be the maximal degree of an AIR constraint. In our case, . The efficiency of a zk-STARK is determined by . For each circuit, the efficiency is compared by measuring .
ZK-Friendly Hash Functions
Roughly speaking, the simpler the algebraic representation of a given circuit, the more efficient it is for both R1CS and AIR representations. Therefore, cryptographers began to design structures that were both algebraically simple and still secure. We often called these ciphers as arithmetization-oriented ciphers (AOCs).
For AOCs, most traditional symmetric cryptanalysis such as differential and linear cryptanalysis are generally less relevant. The most powerful attack type for AOCs is algebraic attack, such as Gröbner basis, linearization, GCD, and interpolation attack. Once the designer decides their cipher’s design, the round number is chosen to be safe from those attacks.
On the other hand, this field is relatively new and interesting attacks (see examples one↗, two↗, three↗, and four↗) have been claimed, often resulting in ciphers being broken or parameters being modified, but we’ll talk about those in the next post.
Let’s take a look at the design rationale behind the different AOCs.
MiMC
MiMC↗ is the first ZK-friendly hash function, introduced in ASIACRYPT 2016. Although more efficient ZK-friendly hash functions are suggested after MiMC, MiMC is still widely used in many applications since it is considered mature compared to the other ZK-friendly hash functions.
The design of MiMC is extremely simple: a function is iterated with subkey additions. This concept was already proposed by Nyberg and Knudsen in the 1990s, called KN-Cipher↗.
MiMC-
We first take a look at a block cipher. The block cipher is constructed by iterating rounds, where each round function is described as . In the round function, is a round constant and is a key, and the field is where is a large prime or . The encryption process is defined as
and is fixed to because does not affect security. (If you’re interested, I recommend checking it out for yourself!)
For a field , has an inverse if and only if . Therefore, the author suggested choosing odd for and for prime .
The round constants are not specified in the paper, but is
recommended to be chosen randomly at first and hardcoded into the
implementation. To show nothing up my
sleeve↗ on
the round constants, it is often generated from the SHA-2 (or any other
secure hash functions) digest of certain messages like MiMC0
, MiMC1
,
.
The round number is determined by . For example, the round number for a field . We discuss the reason of this later.
rounds of MiMC-. Images from MiMC paper. |
MiMC-
It is also possible to construct a block cipher with the same nonlinear permutation in a Feistel network. The round function of MiMC- is described in the below figure and can be defined as
MiMC- round function. Images from here↗. |
For each round, only half of the data is changed; therefore, the number of rounds for the Feistel version is , which is doubled from the number of rounds of MiMC-.
The Hash Function
When the key is fixed at , then the block cipher becomes a permutation. Given a permutation, there is a well-known construction deriving hash functions from a permutation called the sponge framework↗, which is proven secure and used in many other hash functions, including SHA-3↗.
The sponge construction. is input; is hashed output. The unused capacity should be twice the desired collision or preimage attacks. Images from Wikipedia↗. |
The sponge construction consists of two phases called absorb and squeeze. In the absorbing phase, the input data is absorbed into the sponge. Afterwards, the result is squeezed out.
The hash function from MiMC is also based on the sponge framework. In the absorbing phase, message blocks are added to a subset of the state, then transformed as a whole using a permutation function (in our case, an MiMC permutation). Note that all the other ZK-friendly hash functions introduced in this post are also based on the sponge framework.
Security Analysis
While interpolation attack, GCD attack, invariant subfields attack, differential cryptanalysis, and linear cryptanalysis are considered in the paper, we will only introduce interpolation attack in here, which provides the lower bound of the round numbers. An interpolation attack constructs a polynomial corresponding to the encryption function without knowledge of the secret key. If an adversary can construct such a polynomial, then for any given plaintext, the corresponding ciphertext can be produced without knowing the secret key.
Let be an encryption function with degree in terms of input . Then with the plaintext-ciphertext pairs, can be constructed using Lagrange’s theorem, and the complexity of constructing a Lagrangian interpolation polynomial is . In our case, degree . By choosing , the degree reaches and the attack becomes infeasible.
SNARK Complexity
Since the single constraint can square the value, there are two R1CS constraints in each round of MiMC permutation:
x2 = x*x
x3 = x2*x
To generate a 256-bits output hash for the 256-bits input , the sponge construction is used. In details, calculate MiMC- of , then left 256-bits are hash value. The round number is chosen as . Then the number of constraints is . Note that field should be chosen as , which means that should not be chosen.