Memory State Machine
The Memory State Machine is a secondary state machine, therefore it has the executor part (the Memory SM Executor) and an internal Memory PIL program which is a set of verification rules, written in the PIL language.
The memory of the Ethereum Virtual Machine (EVM) is a volatile read-write memory, and it is therefore used to store temporary data during the execution of transactions when smart contracts are deployed.
That is, data in memory is populated during transaction execution but it does not persist between transactions. The memory is an array of -bit ( bytes) words that can be accessed through addresses at byte level, that is to say, each byte in the memory has a different address.
Memory has addresses of bits, and initially, all memory locations are composed by bytes set to zero.
Now, let's see the layout in memory of the following two words and .
The below table displays this layout. Let's call this Table 1.
Observe that each word has 32 bytes and the words are stored in Big-Endian form. So, the most significant bytes are set in the lower addresses. The EVM provides three opcodes to interact with the memory area. There is an opcode to read, and an opcode to write 32-byte words providing an offset:
- : It receives an offset and returns the 32 bytes in memory starting at that offset
- : It receives an offset and saves 32 bytes from the offset address of the memory
Considering our previous memory contents, if we perform an with an offset of , we would obtain the following word: .
On the other hand, if we do an with an offset of with the word , we would modify the content of the memory as shown in the below table (or Table 2).
When the offset is not a multiple of 32 (or 0x20), as in the previous example, we have to use bytes from two different words when doing or .
Finally, the EVM provides a write memory operation that just writes a byte:
- : It receives an offset and saves one byte on that address of the memory. always uses only one word.
The Memory SM is in charge of proving the memory operations in the execution trace. As mentioned, read and write operations use addresses at byte level in the EVM. However, doing the proofs byte-by-byte would consume many values in the trace of this state machine. Instead, in this machine, we operate addressing words (32 bytes).
For example, if we have the memory layout from Table 1, then we would have the memory layout of the below provided table with addresses that point to 32-byte words.
The Memory SM uses this latter layout, the 32-byte word access, to check reads and writes. However, as previously mentioned, the EVM can read and write with offsets at a byte level. As a result, we will need to check the relationship between byte access and 32-byte word access. For these checks, we have another state machine called Memory Align SM.
As with any state machine, the Memory SM has an executor to compute the trace that proves the correctness of memory reads and writes and a PIL description that enforces that the trace is correct.
Execution Trace Design
The Memory SM defines the design of the trace and the PIL description that checks that memory reads and writes aligned to 32-byte words are correct. The addresses, denoted as , are represented through bits ( bytes) and point to 32-byte words. The value of words stored in memory, denoted as , are represented through registers of bytes each, making a total of bytes ( bits).
The below table shows an example with all the memory operations present at an execution trace of the Main SM.
The is the execution step number at the Main SM and in this case, we are showing only the steps that are performing a memory operation. The instruction to execute a memory operation is indicated by the selector. The is also a selector that shows whether the memory operation is a read or a write. In the previous trace, we can observe that the first memory operation is performed at step 11 and it is the write of the sixth 32-byte word. The eight registers provide the bytes to be written in that word.
It is worth to mention that for a specific word address, the first operation is always a write because it makes no sense to read a position that has not been previously written. Then, in this word address there can be a sequence of reads and writes. In the previous trace, we can observe that for the sixth word, there is a write at step 11, then a read at step and finally another write at step .
The trace of the Memory SM must check that the writes are done according to their step and that reads provide the correct words according also to their step.
In order to implement these checks, the execution trace of the Memory SM sorts all the memory operations; firstly by , and secondly by , as shown in the above table. This ordering is referred to as the topology of the Memory SM.
Finally, we will need to add a few more columns to ensure that the memory execution trace goes exactly across all the ordered writes and reads of the Main SM, with writes storing the provided values and with reads not changing the previous value of the word.
In particular, we add three more columns. One of these columns is called and it is used to provide an order for the values of the columns. Another column called is used to enable an address change when all the memory operations for this address have appeared at the trace. The last column is called and it is used to make the checks pass when there are no more memory accesses.
List of Columns
The following columns (polynomials) are used by the Memory SM. We divide them between preprocessed and committed polynomials.
: Counter that goes from up to , where is the number of rows in the computational trace,
It is used to do a range check to prove the incremental order of other columns.
: Selector that is in every row except in the -th row, in which its value is ,
- : Position in which the memory operation was called in the Main SM.
- : Selector indicating whether it is being performed a memory operation or not.
- : Selector that is if the memory operation is a write and if it is a read operation.
- : A -byte (or bit) integer indicating the address of a 32-byte word.
- : Selector indicating whether it has been reached the last memory access for a particular address or not.
- : Vector containing -byte integer indicating the -bit value associated to a given address.
The table below shows the complete Memory SM trace for our example in which the computational trace size is .
There are various important details to remark from the point in which all memory accesses have been completed but the -th row has not been reached yet:
- and are set to until the last row.
- is incremented by to keep the incremental order of the addresses. This value is maintained until the last row.
- is also set to except in the very last row, where it is set back to to create the ciclycity behavior.
- is incremented by in each row so that this column fulfills the constraints describing this state machine.
- . Notice that can take values beyond and that the value of after the row of the last address can coincide with a previous value. As we will show in the next section, where we describe the constraints, these facts do not cause any issue.
- are all set to until the last row.
What constraints does the execution trace have to satisfy? And how is the Memory SM connected with the Main State Machine.
Let's start with the set of constraints regarding the topology of the state machine.
Equations (1) and (2) are straightforward. Equation (1) asserts that is a selector (i.e., a column whose values lie in the set ), while Equation (2) confirms that does not change until it is accessed for the last time. Note that Equation (2) implies that addresses are processed one-by-one in the Memory SM, but it does not guarantee that they are ordered incrementally.
Equation (3) is a little bit more tricky. Let's do a case analysis on it.
The curly braces notation in Equation (3) means that the inclusion is only checked at values such that the corresponding selector is equal to .
Then, depending on the value of the selector we have two cases:
In words, whenever a transition do not change the address in question, verify that ; otherwise verify that . Therefore, Equation (3) ensures that both and are ordered incrementally (first by and then by ). A combination of Eqs. (2) and (3) gives the desired topology.
Let's continue with the operation selectors: and .
Eqs. (4) and (5) ensure that and are, effectively, selectors. Eq. (6) is imposing a restriction to (and binding it with ) in the following sense: can be set to only if is also set to . Similarly, if is set to , then should be set to as well. This restriction comes naturally from the definition of these selectors.
Updating the Value
Finally, we explain the constraints that deal with the value columns .
We analyze both Eqs. (7) and (8) at the same time. Notice that we simply discuss the feasible cases:
- If and , then it should be the case that , since this means that we will perform a read in the next step. Eq. (7) ensures this case.
- If , and , then it should be the case that , i.e., the register is set to in the forthcoming steps.
More cases are not possible because for an address we always start with a write operation which limits the behavior of these constraints to these cases. For example, it cannot happen that and some of or is 0; because the first operation that is always performed over a memory address is a write.
However, notice that to be able reset to its state in the first row (where it is the case that ) it should be the case that in the last row of the computational case. If would have not been set to , then Eq. (2) would not be satisfied.
We obtain this condition by adding the following constraint:
Connection with the Main SM
The last constraint that we need to add is to relate the execution trace of the Main SM with the execution trace of the Memory SM.
The constraint has to check that all the rows in the trace of the Main SM that make memory operations (i.e. rows where ) are a permutation (any permutation) of the rows of the Memory SM where .
The key point is that if both vectors would not be a permutation of each other, then that would mean that the Main SM is performing an incorrect memory action.
The Polygon zkEVM repository is available on GitHub.
Memory SM Executor: sm_mem.js
Memory SM PIL: mem.pil