# in Kimchi

In 2020, showed how to create lookup proofs. Proofs that some witness values are part of a lookup table. Two years later, an independent team published plonkup showing how to integrate into .

This document specifies how we integrate in kimchi. It assumes that the reader understands the basics behind .

## Overview

We integrate in kimchi with the following differences:

• we snake-ify the sorted table instead of wrapping it around (see later)
• we allow fixed-ahead-of-time linear combinations of columns of the queries we make
• we implemented different tables, like RangeCheck and XOR.
• we allow several lookups (or queries) to be performed within the same row
• zero-knowledgeness is added in a specific way (see later)

The following document explains the protocol in more detail

### Recap on the grand product argument of

As per the paper, the prover will have to compute three vectors:

• , the (secret) query vector, containing the witness values that the prover wants to prove are part of the lookup table.
• , the (public) lookup table.
• , the (secret) concatenation of and , sorted by (where elements are listed in the order they are listed in ).

Essentially, proves that all the elements in are indeed in the lookup table if and only if the following multisets are equal:

where is a new set derived by applying a “randomized difference” between every successive pairs of a vector, and is the set union of et .

More precisely, for a set , is defined as the set .

For example, with:

we have:

Note: This assumes that the lookup table is a single column. You will see in the next section how to address lookup tables with more than one column.

The equality between the multisets can be proved with the permutation argument of , which would look like enforcing constraints on the following accumulator:

• init:
• final:
• for every :

Note that the paper uses a slightly different equation to make the proof work. It is possible that the proof would work with the above equation, but for simplicity let’s just use the equation published in :

Note: in is longer than (), and thus it needs to be split into multiple vectors to enforce the constraint at every . This leads to the two terms in the denominator as shown above, so that the degree of is equal in the nominator and denominator.

### Lookup tables

Kimchi uses different lookup tables, including RangeCheck and XOR. The XOR table for values of 1 bit is the following:

lro
101
011
110
000

Whereas kimchi uses the XOR table for values of bits, which has entries.

Note: the entry is at the very end on purpose (as it will be used as dummy entry for rows of the witness that don’t care about lookups).

### Querying the table

The paper handles a vector of lookups which we do not have. So the first step is to create such a table from the witness columns (or registers). To do this, we define the following objects:

• a query tells us what registers, in what order, and scaled by how much, are part of a query
• a query selector tells us which rows are using the query. It is pretty much the same as a gate selector.

Let’s go over the first item in this section.

For example, the following query tells us that we want to check if

lro
1, 1, 2,

, and will be the result of the evaluation at of respectively the wire polynomials , and . To perform vector lookups (i.e. lookups over a list of values, not a single element), we use a standard technique which consists of coining a combiner value and sum the individual elements of the list using powers of this coin.

The grand product argument for the lookup constraint will look like this at this point:

Not all rows need to perform queries into a lookup table. We will use a query selector in the next section to make the constraints work with this in mind.

### Query selector

The associated query selector tells us on which rows the query into the XOR lookup table occurs.

rowquery selector
01
10

Both the (XOR) lookup table and the query are built-ins in kimchi. The query selector is derived from the circuit at setup time.

With the selectors, the grand product argument for the lookup constraint has the following form:

where is constructed so that a dummy query () is used on rows that don’t have a query.

### Supporting multiple queries

Since we would like to allow multiple table lookups per row, we define multiple queries, where each query is associated with a lookup selector.

Previously, ChaCha20 was implemented in Kimchi but has been removed as it has become unneeded. You can still find the implementation here. The ChaCha gates all perform queries in a row. Thus, is trivially the largest number of queries that happen in a row.

Important: to make constraints work, this means that each row must make queries. Potentially some or all of them are dummy queries.

For example, the ChaCha0, ChaCha1, and ChaCha2 gates will jointly apply the following 4 XOR queries on the current and following rows:

lro-lro-lro-lro
1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1,

which you can understand as checking for the current and following row that

The ChaChaFinal also performs (somewhat similar) queries in the XOR lookup table. In total this is different queries that could be associated to selector polynomials.

### Grouping queries by queries pattern

Associating each query with a selector polynomial is not necessarily efficient. To summarize:

• the ChaCha0, ChaCha1, and ChaCha2 gates that in total make queries into the XOR table
• the ChaChaFinal gate makes another different queries into the XOR table

Using the previous section’s method, we’d have to use different lookup selector polynomials for each of the different queries. Since there’s only use-cases, we can simply group them by queries patterns to reduce the number of lookup selector polynomials to .

The grand product argument for the lookup constraint looks like this now:

where is constructed as:

where, for example the first pattern for the ChaCha0, ChaCha1, and ChaCha2 gates looks like this:

Note that there’s now dummy queries, and they only appear when none of the lookup selectors are active. If a pattern uses less than queries, it has to be padded with dummy queries as well.

Finally, note that the denominator of the grand product argument is incomplete in the formula above. Since the nominator has degree in , the denominator must match too. This is achieved by having a longer , and referring to it times. The denominator thus becomes .

## Back to the grand product argument

There are two things that we haven’t touched on:

• The vector representing the combined lookup table (after its columns have been combined with a joint combiner ). The non-combined loookup table is fixed at setup time and derived based on the lookup tables used in the circuit.
• The vector representing the sorted multiset of both the queries and the lookup table. This is created by the prover and sent as commitment to the verifier.

The first vector is quite straightforward to think about:

• if it is smaller than the domain (of size ), then we can repeat the last entry enough times to make the table of size .
• if it is larger than the domain, then we can either increase the domain or split the vector in two (or more) vectors. This is most likely what we will have to do to support multiple lookup tables later.

What about the second vector ?

## The sorted vector

We said earlier that in original the size of is equal to , where encodes queries, and encodes the lookup table. With our multi-query approach, the second vector is of the size

That is, it contains the elements of each query vectors (the actual values being looked up, after being combined with the joint combinator, that’s per row), as well as the elements of our lookup table (after being combined as well).

Because the vector is larger than the domain size , it is split into several vectors of size . Specifically, in the plonkup paper, the two halves of , which are then interpolated as and . The denominator in in the vector form is which, when interpolated into and , becomes

Since one has to compute the difference of every contiguous pairs, the last element of the first half is the replicated as the first element of the second half (). Hence, a separate constraint must be added to enforce that continuity on the interpolated polynomials and :

which is equivalent to checking that .

## The sorted vector in kimchi

Since this vector is known only by the prover, and is evaluated as part of the protocol, zero-knowledge must be added to the corresponding polynomial (in case of approach, to ). To do this in kimchi, we use the same technique as with the other prover polynomials: we randomize the last evaluations (or rows, on the domain) of the polynomial.

This means two things for the lookup grand product argument:

1. We cannot use the wrap around trick to make sure that the list is split in two correctly (enforced by which is equivalent to in the paper)
2. We have even less space to store an entire query vector. Which is actually super correct, as the witness also has some zero-knowledge rows at the end that should not be part of the queries anyway.

The first problem can be solved in two ways:

• Zig-zag technique. By reorganizing to alternate its values between the columns. For example, and so that you can simply write the denominator of the grand product argument as Whis approach is taken by the plonkup paper.
• Snake technique. By reorganizing as a snake. This is what is currently implemented in kimchi.

The snake technique rearranges into the following shape:

__    _
s_0 |  s_{2n-1} |  |  | |
... |       ... |  |  | |
s_{n-1} |       s_n |  |  | |
‾‾‾‾‾‾‾‾‾‾‾    ‾‾   ‾
h1         h2  h3 ...

Assuming that for now we have only one bend and two polynomials , the denominator has the following form:

and the snake doing a U-turn is constrained via , enforced by the following equation:

In practice, will have more sections than just two. Assume that we have sections in total, then the denominator generalizes to

where is Kronecker delta, equal to when is even (for the first term) or odd (for the second one), and equal to otherwise.

Similarly, the U-turn constraints now become

In our concrete case with simultaneous lookups the vector has to be split into sections — each denominator term in the accumulator accounts for queries () and table consistency check ().

## Unsorted in

Note that at setup time, cannot be sorted lexicographically as it is not combined yet. Since must be sorted by (in other words sorting of must follow the elements of ), there are two solutions:

1. Both the prover and the verifier can sort the combined lexicographically, so that can be sorted lexicographically too using typical sorting algorithms
2. The prover can directly sort by , so that the verifier doesn’t have to do any pre-sorting and can just rely on the commitment of the columns of (which the prover can evaluate in the protocol).

We take the second approach. However, this must be done carefully since the combined entries can repeat. For some such that , we might have

For example, if and , then would be a way of correctly sorting the combined vector . At the same time is incorrect since it does not have a second block of s, and thus such an is not sorted by .

## Recap

So to recap, to create the sorted polynomials , the prover:

1. creates a large query vector which contains the concatenation of the per-row (combined with the joint combinator) queries (that might contain dummy queries) for all rows
2. creates the (combined with the joint combinator) table vector
3. sorts all of that into a big vector
4. divides that vector into as many vectors as a necessary following the snake method
5. interpolate these vectors into polynomials
6. commit to them, and evaluate them as part of the protocol.