Cairo is a practically-efficient Turing-complete STARK-friendly CPU architecture. In this article, we introduce the CPU architecture of Cairo in terms of instruction structure and state transition, and provide some examples of instruction.
The word natively supported by Cairo CPU is a field element, where the field is some fixed finite field of characteristic . Each instruction will occupy 1 or 2 words. If an immediate value([ap] = "12345678")follows the instruction, the instruction will occupy 2 words, and the value will be stored in the second word. The first word of each instruction consists of the following elements:
The state transition function represents a general state transition unit (because it contains the processing logic of all instruction types), and a calculation is usually decomposed into multiple continuously executed instructions. Therefore, we need to:
If the state before and after the instruction execution is consistent, the update of the state must be executed according to the following logic:
As shown in Figure 1, one instruction consists of the following elements:
Where:
[ bit0..15], [ bit16..31], [ bit32..47] are in range
But in AIR, we convert them into the following form:
(Where * represents one of dst, op0 and op1)
So the value range of should be
Therefore, for the coding of an instruction, we have:
Note: Instead of allocating 15 virtual columns with a length of NN to the 15-bit flags of the instruction, instead, we use one virtual columnwith a length of 16N, which meets the following requirements:
Therefore, equation (1) can be written as:
Because the finite field's characteristic , one instruction can only correspond to one valid field element.
Therefore, for the instruction itself, the following constraints should be met:
Instruction:
Bit:
Last value is zero:
Offset are in range: virtual column $, so then
The assert equal instruction can be expressed in the following syntax:
<left_handle_op> = <right_handle_op>
It ensures that both sides of the formula are equal; otherwise the execution of the program will be rejected.
The left side of the equation often comes from or , and the right side has some possible forms( and can be or , can be addition or multiplication, and can be any fixed field elements):
Note2: Division and subtraction can be expressed as multiplication and addition with different operand orders, respectively.
An instruction can be considered as an assignment instruction, in which the value of one side is known, and the other side is unknown. For example, can be considered as an assertion that the value of [ap] is 4, or as an assignment setting [ap] to 4, according to the context.
Figure 4 shows some examples of "assert equal" instructions and the flag values for each instruction:
Figure 4. Examples of assert equal instructions
Interpret instruction [fp + 1] = 5:
The instruction allows changing the value of the program counter .
Cairo supports relative jump (its operand represents the offset from the current )and absolute jump - represented by keywords and respectively. The instruction may be conditional. For example, when the value of a memory unit is not 0, the instruction will be triggered.
The syntax of the instruction is as follows:
Figure 5 shows some examples of the instructions and the corresponding flag values of each instruction:
Figure 5 Examples of jump instructions
Interpret instruction jmp rel [ap +1] + [fp - 7]:
The and instructions allow the implementation of the function stack. The instruction updates the program counter( )and frame pointer( )registers. The update of the program counter is similar to the instruction. The previous value of is written to , to allow the instruction to reset the value of to the value before the call; similarly, the returned (the address of the instruction after the call instruction) is written to , to allow the instruction to jump back and continue the execution of the code after the call instruction. As two memory units were written, advanced by 2 and is set to the new .
The syntax of the instructions are as follows:
Figure 6 shows some examples of and instructions and flag values corresponding to each instruction:
Figure 6 Examples of call instructions and ret instructions
Interpret instruction call abs [fp + 4]:
The instruction ap += <op> increments the value of by the given operand.
Figure 7 shows some examples of the advancing instructions and the corresponding flag values of each instruction:
Figure 7 Examples of advancing ap instructions
Interpret instruction ap += 123: