Memory
Registers and memory share one address space and one memory argument. The 36 registers (the 32 general-purpose registers, LO, HI and two internal registers for the program break and heap) occupy addresses 0 to 35, and memory instructions may not address them. Every access is a pair of tuples on the Memory bus: the accessing row sends the previous access (prev_shard, prev_clk, addr, prev_value) and receives the current one (shard, clk, addr, value), and asserts that the current timestamp is strictly larger. The argument is described in Memory Consistency Checking. This page describes the chips.
The memory chips are the five memory-instruction chips, MemoryBump, MemoryLocal, MemoryGlobalInit, MemoryGlobalFinal, MemoryProgram and MemoryImage. The sources are in crates/core/machine/src/memory/.
Memory instructions
The load and store opcodes are split by width and direction so that each chip carries only the columns its opcodes need:
| Chip | Opcodes |
|---|---|
LoadWord | LW, LL |
LoadNarrow | LB, LBU, LH, LHU |
StoreWord | SW, SC |
StoreNarrow | SB, SH |
MemoryUnaligned | LWL, LWR, SWL, SWR |
Each chip embeds the I-type instruction frame and a common block that constrains:
- The effective address \( addr = op_b + op_c \), computed inline with an addition gadget (its value is the only column; the carries are expressions).
- That the address word is a canonical KoalaBear value with byte-checked limbs, and that \( addr \ge 36 \), so a memory instruction cannot touch a register.
- The two low address bits, and the memory access at the aligned address \( addr - (addr \bmod 4) \), at timestamp \( clk + 0 \).
- \( next\_next\_pc = next\_pc + 4 \): memory instructions are sequential.
Per chip:
LoadWordandStoreWordpin the low address bits to zero. A load's value isop_a; a store's memory value isop_a.SCstores the previous value ofop_aand setsop_ato 1. The machine is single-threaded, so a store-conditional always succeeds andLLis an ordinary load.LoadNarrowselects the byte or half-word with three offset flags and extends it. For a signed load whose top bit is set, the bytes above the loaded value are0xFF. This is a byte constraint, not an ALU lookup.StoreNarrowuses the offset flags to replace one byte or half-word of the previous memory word.MemoryUnalignedcombines the memory word with the previous value ofop_aaccording to the offset, asLWL/LWR/SWL/SWRspecify.
The memory-instruction chips do not range-check the bytes of the loaded or stored word. A stored word comes from a register, and a loaded word goes to one. Values written into memory from any other source (global initialization, precompiles) are byte-checked where they enter.
MemoryBump
A register access compares only clocks, not shards, and checks the clock difference with a single 16-bit lookup. To make that sound, MemoryBump inserts shadow reads of a register in two cases:
- one row per (register, shard) for every register the shard touches, at timestamp
(shard, 0). Its previous access may be in any earlier shard, so this row uses the full access columns with a shard comparison. Becauseclkrestarts at 0 in every shard and real register accesses sit at \( clk + 1 \) to \( clk + 4 \), the shadow read is always the first access of the shard to that register; - one row just before an access whose previous access to the register is \( 2^{16} \) or more clock ticks back, so the difference seen by the access itself is small again.
Every other register access then has prev_shard = shard and \( clk - prev\_clk - 1 < 2^{16} \). This reduces each register access to five columns (value and previous clock) and one range lookup, instead of nine columns and two lookups. A shard has at most 36 first-touch rows plus one gap row per register per \( 2^{16} \) ticks.
MemoryLocal
MemoryLocal has one row per address the shard accesses. The row opens and closes the shard's chain for that address:
- It receives the initial tuple
(initial_shard, initial_clk, addr, initial_value)on theMemorybus, where the shard's first access sends it as its "previous" access. It also receives the same tuple as a global lookup. - It sends the final tuple
(final_shard, final_clk, addr, final_value), matching the shard's last access, and sends it again as a global lookup.
The initial side is otherwise a free witness, so the row range-checks both shards to 16 bits, both clocks to 26 bits (a 16-bit and a 10-bit limb) and every value limb to a byte.
Among the global lookups, a shard's final value for an address cancels against the next accessing shard's initial value (see Memory Consistency Checking).
MemoryGlobalInit and MemoryGlobalFinal
These chips cover the lifetime of an address across the whole execution:
MemoryGlobalInithas a row for each address the program touches that is not in the program image. It sends(0, 0, addr, value)as a global lookup with its initial value.MemoryGlobalFinalhas a row for each touched address. It receives the last(shard, clk, addr, value)as a global lookup.
Initial values of the program image are rows of the preprocessed MemoryProgram table, part of the verifying key. Every shard includes the table, but only in the first shard are its rows used: they go over the MemoryImage bus to the MemoryImage chip, which sends them as global lookups, since a global lookup reads committed main columns only.
Each address may be initialized and finalized once. Both chips enforce it the same way:
- The value is four bytes, each range-checked through the byte table.
- The address is two 16-bit limbs, both range-checked. The high limb is at most
0x7F00, and when it equals0x7F00the low limb is zero, so the limbs spell an integer below the field's order and every address has exactly one limb pair. The largest address,0x7F000000, is provable. - Consecutive rows are chained on a control bus (
MemoryGlobalInitControlorMemoryGlobalFinalizeControl) carrying the index, the two address limbs and the valid flag, so a row compares against the limbs the row before it range-checked. - Each row asserts
prev_addr < addrby a range check of the difference of the first differing limbs, less one; no assignment accepts a pair that is not strictly increasing.
A row is 18 columns (it was 152 when the address, the previous address and the value were held as bits).
The chain continues across shards: each shard's first row receives the previous shard's last address from the public values (previous_init_addr_bits, last_init_addr_bits and the finalize equivalents), and the leaf checks that the ranges of consecutive shards join. The public values hold an address as 32 bit elements; at the two ends of the control bus they send the two limbs those bits spell, the weighted sums of the low 16 and of the high 16 bits. The only row exempt from the comparison is the genesis row, index 0 with previous address 0, which initializes and finalizes address 0 to zero.