Introduction
In our last post, we discussed the design rationale and performance of various ZK-friendly hash functions. These include MiMC, Poseidon, Vision, and Rescue. Although the structures may differ slightly, the underlying concept remains the same: representing the circuit as simple expressions in a large field, it allows efficient proof in terms of both prover execution time and proof size. However, the question remains: Are they truly secure enough to be entrusted with billions of dollars? Let’s dive into the attacks that are particularly effective against ZK-friendly hash functions.
ZK-Friendly Hash Functions
From a high-level perspective, ZK-friendly hash functions are constructed using algebraically simple keyed permutations, called arithmetization-oriented ciphers (AOCs). AOCs are based on either SPN↗ or Feistel↗ networks. Then the permutations are transformed into hash functions using the sponge framework.
(In addition, new permutations have been proposed recently, such as this↗ and this↗. Both were accepted to CRYPTO 2023. We recommend checking out these papers because they offer significant performance benefits.)
As keyed permutations are structured as multiple rounds of linear and nonlinear operations, the higher the round count, the more secure the hash function but the worse the performance. The designers must consider every possible attack and determine the lower bounds of the round count required to mitigate them.
Maturation Takes Time, Including AOCs
Unfortunately, although the designers carefully consider every possible attack and even add security margins, this is not strong evidence that the structure is safe. As we will see in the examples presented in this post, even if the designer has done their best, there may be new attacks that the designer has not considered. Even peer-reviewed papers published in top conferences can be vulnerable to attacks. For instance, a cipher targeting homomorphic encryption named Chaghri↗ was published in ACM CCS 2022, but an attack↗ was discovered only three months after its publication.
New primitives often become adopted by industry after enough time has passed since their publication. This is independent of the claims made in the original paper and is instead based on the fact that enough people have analyzed them to determine that they are mature enough for practical use.
And from a designer’s perspective, it is important to consider a variety of attacks, including those we will discuss as well as those evolving, to create a robust security design.
In this post, we will discuss algebraic attacks that are particularly efficient against ZK-friendly hash functions and should be taken into consideration, including attacks on the actual proposed primitive. Although you do not extensive ZK knowledge to understand this article, you may check out our latest ZK related blog post if you are a beginner to ZK.
Algebraic Attacks
Before starting to introduce algebraic attacks, we should clarify targets of attacks. The basic secure properties of hash functions are preimage resistance, second-preimage resistance, and collision resistance. Therefore, the attacker’s primary goal is to recover preimages or find collision pairs. However, from the point of view of designers, it is hard to convince someone that a their hash function satisfies security properties.
Meanwhile, ZK hash functions are usually based on the block ciphers. Once the key is fixed, the block cipher becomes a permutation, and these permutations are transformed into hash functions using the sponge framework. It is mathmetically proven that the hash function derived from sponge construction is secure if the permutation is secure. Therefore, instead of demonstrating security for the hash function directly, designers show that a block cipher used inside the sponge construction is secure. Conversely, an attacker would like to show that the block cipher is insecure. For example, if attacker is given plaintext-ciphertext pairs and enable to recover key faster than testing all the possible key candidates, it is called key-recovery attack. If the block cipher is insecure, it weakens the designer’s claim that the hash function created from the block cipher is secure.
ZK-friendly hash functions are represented by simple expressions in a large field. In traditional cipher design, the strongest attacks are considered to be statistical attacks called differential↗ and linear↗ cryptanalysis. However, when the field is large, these attacks are not suitable even for a small number of rounds. On the other hand, targeting low arithmetic complexity in attacks is effective.
Interpolation Attack
An interpolation attack is an attack that constructs a polynomial corresponding to the encryption function without knowing the key. Let be an encryption function. For a fixed key , the polynomial can be constructed using Lagrange interpolation — for example, when we consider two rounds of MiMC, . This is a polynomial with degree in terms of .
Once the attacker constructs the polynomial , then can obatin ciphertext of any plaintext, which has the same effect as knowing the key. Moreover, the attacker may extract key from because the coefficients of is .
When a polynomial has a degree of , the complexity of constructing a Lagrange interpolation polynomial is . Therefore, the degree of the polynomial used to represent the encryption function should be high enough. For instance, in the MiMC, the round number is determined as to ensure that the degree attains the maximum degree of .
An interpolation attack targets a univariate equation in terms of plaintext. If the plaintext is instead of , then this attack cannot be applied directly.
GCD Attack
The GCD attack is a key recovery attack that involves computing the GCD. In the GCD attack, the variable is the key . We denote the encryption function as . For the plaintext-ciphertext pair , we have for . Now, let’s consider two polynomials derived from the two pairs and . Both and have a common factor of , because . ( denotes the variable of the key, and is the actual value of the key.) This common factor can be determined by computing the GCD.
The complexity of finding the GCD of two polynomials with a degree of is . Therefore, keeping the maximum degree also thwarts this attack. Fortunately, degree grows exponentially as the number of rounds increases, so designers can build defenses against interpolation attacks and GCD attacks without much difficulty.
The GCD attack targets a univariate equation in terms of a key. If the key is in instead of , then this attack cannot be applied directly.
Invariant Subfields Attack
If the round constants or linear layers are poorly chosen, the ciphertext range may be limited to the subfields. For instance, consider the case where the field is and is a subfield of . This implies that divides . If all the round constants and the key in MiMC are in the subfield , an adversary can ensure that by selecting a plaintext , and this reduces the possible key space to for an attacker — for example, in MiMC, if the field is and all round constants and the key are in the . Then, if the attack setting is chosen as a plaintext attack, an attacker may notice that the ciphertext is always in if the plaintext is in . Therefore, an attacker is trying to test only key candidates instead of .
This attack can be easily prevented by selecting a random round constant, such as a nothing-up-my-sleeve number. In other words, if an implementation utilizes custom round constants and the rationale behind their selection is undisclosed, there may be vulnerabilities to an invariant subfield attack. Please note that the invariant subfield attack is only effective on binary fields, as prime fields do not have subfields.
Linearization
Linearization is a technique that represents multivariate equations as linear equations by transforming every monomial as new variables. Consider below the multivariate equations in :
Then, by replacing every monomial to new variables like so,
the equations become a system of linear equations, and they can be easily solvable by Gaussian elimination.
Let be a number of unique monomials and be a number of equations. To solve given equations using linearization, must be hold, and the time complexity is , where is the minimum exponent for the complexity of matrix multiplication. At least every element in the matrix should be checked before multiplication, lower bounds of . And the current state-of-the-art↗ algorithm is . To show a resistance on the linearization, the designer should claim the number of monomials is enough that attack complexity is higher than expected security parameters.
For example, for with , let key be . The designer wants to show this primitive has 128-bits security. If has a max degree , then the number of monomials that can be constructed from the equation is approximately , since the degrees of vary to . Then the linearization time complexity is , even if we assume that . Therefore, this primitive is secure against linearization.
Meanwhile, the important question is of whether all the monomials can actually appear. If the expression is sparse and most of the monomials don’t actually occur, then the attack may be possible. Therefore, the designer should claim that most of monomials have appeared theoretically, or experimentally shown using toy parameters.
Gröbner Basis Attack
The Gröbner basis attack is a method for solving a multivariate system by computing the Gröbner basis. Algebraic structures of AOCs allow us to build multivariate equations for key and intermediate values. For example, let’s consider three rounds of MiMC. Let and be the outputs of the first and second S-boxes, respectively.
Then, given plaintext-ciphertext pair , we have a multivariate system:
The benefits of this multivariate system compared to the univariate system is that the degree of the equations are much lower. If we can solve this multivariate system for , then the key is recovered. However, while univariate equations are easily solvable using well-studied algorithms like the Kaltofen-Shoup algorithm if the degree is not that high, there is no efficient algorithm for a multivariate system.
Instead of solving a multivariate system directly, the Gröbner basis of multivariate equations always contains a univariate equation. Once we find the Gröbner basis of the given equations, we obtain a univariate equation that we can solve.
So, what is the Gröbner basis? Understanding the Gröbner basis requires a very high level of mathematical proficiency; therefore, we would like to provide only a brief sketch and omit the details. For a deeper understanding of the technical knowledge, we recommend referring to this paper↗.
Let’s consider the following multivariate system: