Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 76aedac

Browse files
authored
fix: update zkvm primer (#1082)
1 parent 48965c5 commit 76aedac

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

cspell-zksync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ REDC
212212
ECADD
213213
ECMUL
214214
raas
215+
gtlt
215216

216217

217218
// Used programming language words

docs/zk-stack/components/zkEVM/vm-specification/vm-primer.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ set flags by appending a “set flags” modifier to them, like that:
131131
sub! r1, r2, r3 ; r3 <- (r1 - r2); EQ = 1
132132
```
133133

134+
Most instructions with “set flags” modifier set the flags as follows:
135+
136+
- `EQ` - if result is zero
137+
- `LT` - if overflow occurs (result is "less" than zero)
138+
- `GT` - if not `EQ` and not `LT` (result is "greater" than zero)
139+
140+
Note that the details of the behavior may vary depending on which instruction is used.
141+
134142
You can learn more in the
135143
[formal specification](./formal-spec.md).
136144

@@ -144,27 +152,28 @@ Recall the three flags: LT, EQ and GT.
144152
For example, this `sub` instruction is only executed if EQ is set:
145153

146154
```nasm
147-
sub.if_eq r1, r2, r5
155+
sub.eq r1, r2, r5
148156
```
149157

150158
Here is how we can execute `jump` to a label `.label_if_equals` only if `r1 == r2` :
151159

152160
```nasm
153161
sub! r1, r2, r3 ; r3 <- (r1 - r2); EQ = 1 if r1 == r2
154-
jump.if_eq .label_if_equals
162+
jump.eq .label_if_equals
155163
```
156164

157165
If the condition is not satisfied, we skip the instruction, but still pay its basic cost in gas.
158166

159167
Here is a full list of available predicates:
160168

161-
- `if_gt`
162-
- `if_eq`
163-
- `if_lt`
164-
- `if_ge` (short for “GT or EQ”)
165-
- `if_le` (short for “LT or EQ”)
166-
- `if_not_eq`
167-
- `if_gt_or_eq`
169+
- `gt`
170+
- `eq`
171+
- `lt`
172+
- `ge` (short for “GT or EQ”)
173+
- `le` (short for “LT or EQ”)
174+
- `ne` (short for "not EQ")
175+
- `gtlt` (short for "GT" or "LT")
176+
- `of` (synonym for "LT")
168177

169178
You can learn more in the
170179
[formal specification](./formal-spec.md).
@@ -188,12 +197,12 @@ sub.s r1, r2, r3 ; r3 <- r2 - r1
188197
Finally, here is an example of an instruction adorned with all possible modifiers:
189198

190199
```nasm
191-
sub.s.if_lt! r8, r4, r12
200+
sub.s.lt! r8, r4, r12
192201
```
193202

194203
Here is a breakdown of modifiers:
195204

196-
- `.if_lt` : is only executed if the LT flag is set
205+
- `.lt` : is only executed if the LT flag is set
197206
- `.s` : computes `r4 - r8` instead of `r8 - r4`
198207
- `!` : sets flags
199208

@@ -221,8 +230,8 @@ Each call gets its own stack, heap, code memories, and allocated gas.
221230

222231
It is impossible to allocate more than 63/64 of the currently available gas to a far call.
223232

224-
Calls can revert or panic (on executing an illegal instruction for example), which undoes all the changes to storage and
225-
events emitted during the call, and burns all remaining gas allocated to this call.
233+
Calls can revert or panic (on executing an illegal instruction for example), which undoes all the changes to storage, transient storage and
234+
events emitted during the call, and returns unspent gas to the caller.
226235

227236
Suppose we far called a contract $C$. After the execution of $C$, the register `r1` holds a pointer to the return value,
228237
allowing a read-only access to a fragment of $C$’s heap. Alternatively, `r1` can hold a pointer to the heap of some
@@ -249,7 +258,7 @@ There are three types of situations where control returns to the caller:
249258
- Return: a normal way of returning to the caller when no errors occurred. The instruction is `ret`.
250259
- Revert: a recoverable error happened. Unspent gas is returned to the caller, which will execute the exception handler.
251260
The instruction is `revert`.
252-
- Panic: an irrecoverable error happened. Same as revert, but unspent gas is burned. The instruction is `ret.panic`.
261+
- Panic: an irrecoverable error happened. Same as revert, but `LT` flag is set. The instruction is `ret.panic`.
253262

254263
### Near calls
255264

@@ -284,11 +293,8 @@ Additional two arguments:
284293
As we see, zkEVM supports allocating ergs not only for far calls, but also for near calls. Passing zero will allocate
285294
all available gas. Unlike in far calls, near calls do not limit the amount of gas passed to 63/64 of available gas.
286295

287-
- On revert, unspent gas of the function is **returned**
288-
- On panic, unspent gas of the function is **lost**
289-
290296
All near calls inside the contract are sharing the same memory space (heap, stack), and do not roll back the changes to
291-
this memory if they fail. They do, however, roll back the changes to storage and events.
297+
this memory if they fail. They do, however, roll back the changes to storage, transient storage and events.
292298

293299
Near calls cannot be used from Solidity to their full extent. Compiler generates them, but makes sure that if functions
294300
revert or panic, the whole contract reverts of panics. Explicit exception handlers and allocating just a portion of
@@ -486,7 +492,7 @@ Only special instructions can manipulate fat pointers without automatically clea
486492
pass to another contract B up the call chain, again without copying data.
487493
- `ptr.pack` allows putting data in the top 128 bit of the pointer value without clearing the pointer tag.
488494

489-
Doing e.g. `add r1, 0, r2` on a pointer in `r1` clears its tag, and it is now considered as a raw integer.
495+
Doing e.g. `add r1, 0, r1` on a pointer in `r1` clears its tag, and it is now considered as a raw integer.
490496

491497
Instructions `ld` and `[ld.inc](http://ld.inc)` (without indices 1 or 2) allow loading data by fat pointers, possibly
492498
incrementing the pointer. It is impossible to write by a fat pointer.

0 commit comments

Comments
 (0)