Hash Functions in Plonkup
Plonkup is a new zero-knowledge proving system that combines Plonk with Plookup, allowing Plonk's arithmetic and custom gates to coexist with lookup gates enabled by Plookup. Some expensive components of zk-SNARK circuits, like hash functions, can be expressed more efficiently using lookups and arithmetic together. In this article I will give a quick overview of the proving system and show how to write a circuit that computes a small part of a common hash function that benefits greatly from the inclusion of lookups alongside arithmetic gates.
Plonk is a proving system from Gabizon, Williamson, and Ciobotaru first formulated in 2019 which allows a universal, updatable trusted-setup. The trusted-setup is public data, created in a multiparty computation "ceremony", which can be used to create and verify proofs. It is called trusted because a setup can be compromised if all of its creators collude. (If even one participant is honest then the setup is secure).
Universality means that a single trusted-setup cermony can be used by any circuit below a certain size. Without this property a new ceremony would need to be performed for each circuit.
Updatability means an existing trusted-setup can be updated in a new ceremony, potentially with new participants, and is secure as long as even one participant, in any update, was honest.
These two properties address the most common security concerns with zk-SNARK trusted-setups and makes collusion much more difficult.
Plonk, like most other proving systems, uses arithmetic circuits to represent computations. Arithmetic circuits use two gates, and , to express all operations done in the circuit. Complex computations require many, many gates to represent them and can take quite awhile to prove. Certain operations, like the bit-wise arithmetic often used in cryptographic hash functions, cannot be expressed very efficiently as an arithmetic circuit. Circuits that use multiple hash functions can grow to an enormous size and may take minutes to prove. (This bottle-neck applies to any proving system using arithmetic gates, not just Plonk.)
Plonk has two major parts to its circuit: arithmetic gates and copy constraints. The arithmetic gates are easily checked using a single polynomial constraint. Copy constraints, which hook arithmetic gates together, are used to build a permutation polynomial which check the validity of all the copy constraints at once.
Plookup is its own proving system from Gabizon and Williamson that is very similar to Plonk, but dispenses with the arithmetic circuit and instead can be used to prove that each element in a list of queries exists in a public lookup table.
Plookup doesn't use arithmetic gates, but it has its own permutation polynomial that checks that lookup queries are legitimate.
With lookups, operations that are inefficient as arithmetic circuits, like range proofs and bit-wise operations, become much more efficient.
Why combine two protocols?
The number of gates in a circuit is a primary factor in the amount of time it takes to create a proof. Some operations we might want to use in a circuit can be written using fewer gates depending on the method.
Take XOR for instance. Many cryptographic hash functions we'd like to model in a circuit, like SHA or Blake, make liberal use of XOR. To show that three 8-bit values and satisfy using Plonk arithmetic gates only you would first need to constrain each bit of the inputs using 16 gates, then perform 8 single-bit XOR operations with 8 more gates, and packing the resulting bits back together with an additional 7 gates, for a grand total of 31 gates per XOR.
Using a Rank-1 Constraint System (R1CS) is a bit better in this case, because of unlimited addition we can replace the final 7 gates with a single rank-1 constraint, giving 25 total constraints.
With Plookup and an 8-bit XOR lookup table, however, this can be done in a single gate.
Some other operations, like point addition on an embedded elliptic curve, are already pretty efficient using arithmetic gates alone. To maximize efficiency we'd like to use both lookup gates and arithmetic gates in the same circuit.
A Plonkup circuit consists of several parts.
Wires are vectors containing the Prover's private inputs. The wires can be thought of as columns of a large matrix. We'll use three wires in these examples which we will call and .
In this matrix a row is a tuple of private inputs that must satisfy any constraints that are turned on for this row.
Selectors are vectors which turn on or off constraints for each row. A one at index 47 of the multiplication selector means that row 47 must satisfy a multiplication constraint, like .
Copy constraints are simple equations that link the values of entries in one row with those in another, say for instance.
Finally, the lookup table is another matrix with the same number of columns as the Prover has wires. A lookup table with three columns can simulate a function with two inputs and one output. A row in a lookup table simulating the function means that .
An Example Circuit
Let's show how to construct a small Plonkup circuit that does something useful. In the Blake2 hash, 32-bit words are XORed with one another and the resulting bits are rotated some number of places. This is a perfect operation to use as a small example of a Plonkup circuit.
Since our table is simulating XOR, a valid row of the lookup table will look like . We will need to cycle through all possiblities of and . If and were 32 bits each we would have a lookup table with rows. Each row has three entries, and each entry is an element of a scalar field for some ellipitic curve, typically 256 bits or larger. For 256-bit entries our table will be a slim, reasonable, totally-not-universe-breaking 1.5 zebibytes.
So we'll need to make our table a little smaller. Let's use just 8 bits for and , giving us a table of rows and and a total size of just 6 mebibytes. An example row of the table would be if we use decimal integers to stand in place of the 256-bit scalar field elements for convenience.
32-bit XOR To XOR two 32-bit numbers we split each number into four 8-bit chunks and use four lookup gates to compute the XOR. Then we need to pack the four chunks back together to get the 32-bit result. Let's call our two 32-bit inputs and and divide them into four parts each so that and . We compute the four XORs as , , , and . Then our four XOR gates will be:
Now we need to pack the results of the XOR back together into one 32-bit value. We'll pair the two upper and two lower chunks together, left-shift the higher chunks of each pair by 8 bits and add the lower chunks to get two 16-bit values. Then we repeat with a 16-bit shift and addition to get our final 32-bit result.
A bit shift is just multiplication by a power of two, so we can do the packing with a few addition gates. Plonkup supports scalar multiplication in an addition gate by putting the scalars into the selectors for the left, right, and output variables. An addition gate like will enforce the constraint .
Our bit-packing addition gates will look like this:
A bit rotation can be tought of as a "swap" of two chunks of bits. If we're careful, we can use a field element to represent each of the two chunks rather than breaking them down bit-by-bit. This swap is a left rotation by however many bits are in (or a right rotation by the number of bits in ).
The operation denoted by is concatenation of the bit representations of and , but we can use field elements to represent and and never need to mess with the bits at all. Using field elements we can express the concatenations using these simple formulas:
As long as the bit representations of and are actually and bits respectively these formulas correctly compute the leftwards rotation by bits.
The last thing to do is check the bit lengths of and . Unfortunately they will be different lengths most of the time and will require two additional range lookup tables or some extra constraints to adapt them to use the same additional range lookup table. Neither of these is ideal. Thankfully, it turns out that constraining the input and output variables and to bits also constrains and to their correct bit lengths. Since these variables are the same size we can use the same range lookup table for both. Furthermore, if is the output of a previous step of the circuit it may already be range constrained. This is the exact case we find ourselves in if we follow an XOR with a rotation.
The XOR lookup necessarily bounds the range of its output, which is another stroke of luck. We can re-use the XOR lookup table to function as a range lookup table for ranges in multiple of 8 bits. If and are both less than then will be a row in the lookup table. We can check two field elements with each XOR lookup gate.
One of the rounds in Blake2s uses a rotation of 7 bits. Assuming is constrained to 32 bits by earlier constraints, we can start with two addition gates.
The Prover computes and so that and so that and are the correct number of bits. Then our addition gates will be:
Then the Prover computes 8-bit pieces so that and computes two XORs and . Then we have two lookup gates showing all four chunks are less than 8 bits:
Then we show the 8-bit chunks were correctly computed with three more addition gates:
Putting it All Together
Using an 8-bit XOR lookup table (which does double-duty as an 8-bit range table) we can express for 32-bit using these 14 constraints:
Comparison to R1CS
Let's compare this to the same circuit expressed using a Rank-One Constraint System as in Groth16. R1CS has a few advantages and several disadvantages. An advantage is that R1CS allows unlimited additions in a single constraint, so a single R1CS constraint can replace several of the addition constraints we used in our Plonkup-based circuit. A drawback is that R1CS doesn't support lookups or efficient range gates, so the XOR operations and range proofs need to be done bit-by-bit. By my count, an R1CS version of the same circuit would require bit constraints for range proofs, bit-wise XOR constraints, and 1 rotation-and-bit-packing constraint. Overall an R1CS version of this circuit would need almost seven times as many constraints at . While R1CS constraints aren't exactly comparable one-to-one with Plonkup constraints with respect to Prover time, using 1/7th the number of gates will certainly be a major improvement.
Plonkup's lookup table adds some overhead to the cost of constructing a proof and to the proof size. This 8-bit XOR lookup table has rows which is fairly large. In a large, complex circuit where XOR is used many times a table of that size may be worth it, but such a large table is decidedly wasteful for this tiny example ciruit. A 4-bit table would be quite small at only rows but would roughly double the number of constraints needed in the circuit to , still pretty small.
Plonk is a highly adaptable and flexible proving system which admits endless custom configurations, such as Plonkup, which can dramatically improve performance for certain operations. There will likely be many more iterations of Plonk that address all sorts of little inefficiencies or are really effective for certain specialized circuits. Multiple modes of proof accumulation, recursion, and circuit optimization will come next. These features will enable Anoma to be a maximally private and scalable protocol.