# Public Memory

Here are some notes on how the Cairo zkVM encodes its (public) memory in the AIR (arithmetization) of the STARK.

If you’d rather watch a 25min video of the article, here it is:

The AIR arithmetization is limited on how it can handle public inputs and outputs, as it only offer boundary constraints. These boundary constraints can only be used on a few rows, otherwise they’re expensive to compute for the verifier. (A verifier would have to compute $∏_{i∈S}(x−g_{i})$ for some given $x$, so we want to keep $∣S∣$ small.)

For this reason Cairo introduce another way to get the program and its public inputs/outputs in: **public memory**. This public memory is strongly related to the **memory** vector of cairo which a program can read and write to.

In this article we’ll talk about both. This is to accompany this video and section 9.7 of the Cairo paper.

## Cairo’s memory

Cairo’s memory layout is a single vector that is indexed (each rows/entries is assigned to an address starting from 1) and is segmented. For example, the first $l$ rows are reserved for the program itself, some other rows are reserved for the program to write and read cells, etc.

Cairo uses a very natural “constraint-led” approach to memory, by making it **write-once** instead of read-write. That is, all accesses to the same address should yield the same value. Thus, we will check at some point that for an address $a$ and a value $v$, there’ll be some constraint that for any two $(a_{1},v_{1})$ and $(a_{2},v_{2})$ such that $a_{1}=a_{2}$, then $v_{1}=v_{2}$.

## Accesses are part of the execution trace

At the beginning of our STARK, we saw in How STARKs work if you don’t care about FRI that the prover encodes, commits, and sends the columns of the execution trace to the verifier.

The memory, or memory accesses rather (as we will see), are columns of the execution trace as well.

The first two columns introduced in the paper are called $L_{1}.a$ and $L_{1}.v$. For each rows in these columns, they represent the access made to the address $a$ in memory, with value $v$. As said previously, we don’t care if that access is a write or a read as the difference between them are blurred (any read for a specific address could be *the* write).

These columns can be used as part of the Cairo CPU, but they don’t really prevent the prover from lying about the memory accesses:

- First, we haven’t proven that all accesses to the same addresses $a_{i}$ always return the same value $v_{i}$.
- Second, we haven’t proven that the memory contains fixed values in specific addresses. For example, it should contain the program itself in the first $l$ cells.

Let’s tackle the first question first, and we will address the second one later.

## Another list to help

In order to prove that the two columns in the $L_{1}$ part of the execution trace, Cairo adds two columns to the execution trace: $L_{2}.a_{′}$ and $L_{2}.v_{′}$. These two columns contain essentially the same things as the $L_{1}$ columns, except that these times the accesses are sorted by address.

One might wonder at this point, why can’t L1 memory accesses be sorted? Because these accesses represents the actual memory accesses of the program during runtime, and this row by row (or step by step). The program might read the next instruction in some address, then jump and read the next instruction at some other address, etc. We can’t force the accesses to be sorted at this point.

We will have to prove (later) that $L_{1}$ and $L_{2}$ represent the same accesses (up to some permutation we don’t care about).

So let’s assume for now that $L_{2}$ correctly contains the same accesses as $L_{1}$ but sorted, what can we check on $L_{2}$?

The first thing we want to check is that it is indeed sorted. Or in other words:

- each access is on the same address as previous: $a_{i+1}=a_{i}$
- or on the next address: $a_{i+1}=a_{i}+1$

For this, Cairo adds a **continuity constraint** to its AIR:

The second thing we want to check is that accesses to the same addresses yield the same values. Now that things are sorted its easy to check this! We just need to check that:

- either the values are the same: $v_{i+1}=v_{i}$
- or the address being accessed was bumped so it’s fine to have different values: $a_{i+1}=a_{i}+1$

For this, Cairo adds a **single-valued constraint** to its AIR:

And that’s it! We now have proven that the $L_{2}$ columns represent correct memory accesses through the whole memory (although we didn’t check that the first access was at address $1$, not sure if Cairo checks that somewhere), and that the accesses are correct.

That is, as long as $L_{2}$ contains the same list of accesses as $L_{1}$.

## A multiset check between $L_{1}$ and $L_{2}$

To ensure that two list of elements match, up to some permutation (meaning we don’t care how they were reordered), we can use the same permutation that Plonk uses (except that plonk fixes the permutation).

The check we want to perform is the following:

${(a_{i},v_{i})}_{i}={(a_{i},v_{i})}_{i}$

But we can’t check tuples like that, so let’s get a random value $α$ from the verifier and encode tuples as linear combinations:

${a_{i}+α⋅v_{i}}_{i}={a_{i}+α⋅v_{i}}_{i}$

Now, let’s observe that instead of checking that these two sets match, we can just check that two polynomials have the same roots (where the roots have been encoded to be the elements in our lists):

$i∏ [X−(a_{i}+α⋅v_{i})]=i∏ [X−(a_{i}+α⋅v_{i})]$

Which is the same as checking that

$∏_{i}[X−(a_{i}+α⋅v_{i})]∏_{i}[X−(a_{i}+α⋅v_{i})] =1$

Finally, we observe that we can use Schwartz-Zippel to reduce this claim to evaluating the LHS at a random verifier point $z$. If the following is true at the random point $z$ then with high probability it is true in general:

$∏_{i}[z−(a_{i}+α⋅v_{i})]∏_{i}[z−(a_{i}+α⋅v_{i})] =1$

The next question to answer is, how do we check this thing in our STARK?

## Creating a circuit for the multiset check

Recall that our AIR allows us to write a circuit using successive pairs of rows in the columns of our execution trace.

That is, while we can’t access all the $a_{i}$ and $a_{i}$ and $v_{i}$ and $v_{i}$ in one shot, we can access them row by row.

So the idea is to write a circuit that produces the previous section’s ratio row by row. To do that, we introduce a new column $p$ in our execution trace which will help us keep track of the ratio as we produce it.

$p_{i}=p_{i−1}⋅z−(a_{i}+α⋅v_{i})z−(a_{i}+α⋅v_{i}) $

This is how you compute that $p$ column of the execution trace as the prover.

Note that on the verifier side, as we can’t divide, we will have to create the circuit constraint by moving the denominator to the right-hand side:

$p(g⋅x)⋅[z−(a_{′}(x)+α⋅v_{′}(x))]=p(x)⋅[z−(a(x)+α⋅v(x))]$

There are two additional (boundary) constraints that the verifier needs to impose to ensure that the multiset check is coherent:

- the initial value $p_{0}$ should be computed correctly ($p_{0}=z−(a_{0}+α⋅v_{0})z−(a_{0}+α⋅v_{0}) $)
- the final value $p_{−1}$ should contain $1$

Importantly, let me note that this new column $p$ of the execution trace cannot be created, encoded to a polynomial, committed, and sent to the verifier in the same round as other columns of the execution trace. This is because it makes uses of two verifier challenges $z$ and $α$ which have to be revealed *after* the other columns of the execution trace have been sent to the verifier.

Note: a way to understand this article is that the prover is now building the execution trace interactively with the help of the verifier, and parts of the circuits (here a permutation circuit) will need to use these columns of the execution trace that are built at different stages of the proof.

## Inserting the public memory in the memory

Now is time to address the second half of the problem we stated earlier:

Second, we haven’t proven that the memory contains fixed values in specific addresses. For example, it should contain the program itself in the first $l$ cells.

To do this, the first $l$ accesses are replaced with accesses to $(0,0)$ in $L_{1}$. $L_{2}$ on the other hand uses acceses to the first parts of the memory and retrieves values from the public memory $m_{∗}$ (e.g. $(1,m_{∗}[0]),(2,m_{∗}[1]),⋯$).

This means two things:

- the nominator of $p$ will contain $z−(0+α⋅0)=z$ in the first $l$ iterations (so $z_{l}$). Furthermore, these will not be cancelled by any values in the denominator (as $L_{2}$ is supposedly using actual accesses to the public memory)
- the denominator of $p$ will contain $∏_{i∈[0,l]}[z−(a_{i}+α⋅m_{∗}[i])]$, and these values won’t be canceled by values in the nominator either

As such, the final value of the accumulator should look like this if the prover followed our directions:

$∏_{i∈[0,l]}[z−(a_{i}+α⋅m_{∗}[i])]z_{l} $

which we can enforce (as the verifier) with a boundary constraint.

Section 9.8 of the Cairo paper writes exactly that: