miden_core/operations/mod.rs
1use core::fmt;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6mod debug_metadata;
7pub use debug_metadata::{AssemblyOp, DebugVarInfo, DebugVarLocation};
8
9use crate::{
10 Felt,
11 serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
12};
13
14// OPERATIONS AND CONTROL FLOW OPCODES
15// ================================================================================================
16
17/// Opcode patterns have the following meanings:
18/// - 00xxxxx operations do not shift the stack; constraint degree can be up to 2.
19/// - 010xxxx operations shift the stack the left; constraint degree can be up to 2.
20/// - 011xxxx operations shift the stack to the right; constraint degree can be up to 2.
21/// - 100xxx-: operations consume 4 range checks; constraint degree can be up to 3. These are used
22/// to encode most u32 operations.
23/// - 101xxx-: operations where constraint degree can be up to 3. These include control flow
24/// operations and some other operations requiring high degree constraints.
25/// - 11xxx--: operations where constraint degree can be up to 5. These include control flow
26/// operations and some other operations requiring very high degree constraints.
27#[rustfmt::skip]
28pub mod opcodes {
29 pub const NOOP: u8 = 0b0000_0000;
30 pub const EQZ: u8 = 0b0000_0001;
31 pub const NEG: u8 = 0b0000_0010;
32 pub const INV: u8 = 0b0000_0011;
33 pub const INCR: u8 = 0b0000_0100;
34 pub const NOT: u8 = 0b0000_0101;
35 /* unused 0b0000_0110 */
36 pub const MLOAD: u8 = 0b0000_0111;
37 pub const SWAP: u8 = 0b0000_1000;
38 pub const CALLER: u8 = 0b0000_1001;
39 pub const MOVUP2: u8 = 0b0000_1010;
40 pub const MOVDN2: u8 = 0b0000_1011;
41 pub const MOVUP3: u8 = 0b0000_1100;
42 pub const MOVDN3: u8 = 0b0000_1101;
43 pub const ADVPOPW: u8 = 0b0000_1110;
44 pub const EXPACC: u8 = 0b0000_1111;
45
46 pub const MOVUP4: u8 = 0b0001_0000;
47 pub const MOVDN4: u8 = 0b0001_0001;
48 pub const MOVUP5: u8 = 0b0001_0010;
49 pub const MOVDN5: u8 = 0b0001_0011;
50 pub const MOVUP6: u8 = 0b0001_0100;
51 pub const MOVDN6: u8 = 0b0001_0101;
52 pub const MOVUP7: u8 = 0b0001_0110;
53 pub const MOVDN7: u8 = 0b0001_0111;
54 pub const SWAPW: u8 = 0b0001_1000;
55 pub const EXT2MUL: u8 = 0b0001_1001;
56 pub const MOVUP8: u8 = 0b0001_1010;
57 pub const MOVDN8: u8 = 0b0001_1011;
58 pub const SWAPW2: u8 = 0b0001_1100;
59 pub const SWAPW3: u8 = 0b0001_1101;
60 pub const SWAPDW: u8 = 0b0001_1110;
61 pub const EMIT: u8 = 0b0001_1111;
62
63 pub const ASSERT: u8 = 0b0010_0000;
64 pub const EQ: u8 = 0b0010_0001;
65 pub const ADD: u8 = 0b0010_0010;
66 pub const MUL: u8 = 0b0010_0011;
67 pub const AND: u8 = 0b0010_0100;
68 pub const OR: u8 = 0b0010_0101;
69 pub const U32AND: u8 = 0b0010_0110;
70 pub const U32XOR: u8 = 0b0010_0111;
71 pub const FRIE2F4: u8 = 0b0010_1000;
72 pub const DROP: u8 = 0b0010_1001;
73 pub const CSWAP: u8 = 0b0010_1010;
74 pub const CSWAPW: u8 = 0b0010_1011;
75 pub const MLOADW: u8 = 0b0010_1100;
76 pub const MSTORE: u8 = 0b0010_1101;
77 pub const MSTOREW: u8 = 0b0010_1110;
78 /* unused 0b0010_1111 */
79
80 pub const PAD: u8 = 0b0011_0000;
81 pub const DUP0: u8 = 0b0011_0001;
82 pub const DUP1: u8 = 0b0011_0010;
83 pub const DUP2: u8 = 0b0011_0011;
84 pub const DUP3: u8 = 0b0011_0100;
85 pub const DUP4: u8 = 0b0011_0101;
86 pub const DUP5: u8 = 0b0011_0110;
87 pub const DUP6: u8 = 0b0011_0111;
88 pub const DUP7: u8 = 0b0011_1000;
89 pub const DUP9: u8 = 0b0011_1001;
90 pub const DUP11: u8 = 0b0011_1010;
91 pub const DUP13: u8 = 0b0011_1011;
92 pub const DUP15: u8 = 0b0011_1100;
93 pub const ADVPOP: u8 = 0b0011_1101;
94 pub const SDEPTH: u8 = 0b0011_1110;
95 pub const CLK: u8 = 0b0011_1111;
96
97 pub const U32ADD: u8 = 0b0100_0000;
98 pub const U32SUB: u8 = 0b0100_0010;
99 pub const U32MUL: u8 = 0b0100_0100;
100 pub const U32DIV: u8 = 0b0100_0110;
101 pub const U32SPLIT: u8 = 0b0100_1000;
102 pub const U32ASSERT2: u8 = 0b0100_1010;
103 pub const U32ADD3: u8 = 0b0100_1100;
104 pub const U32MADD: u8 = 0b0100_1110;
105
106 pub const HPERM: u8 = 0b0101_0000;
107 pub const MPVERIFY: u8 = 0b0101_0001;
108 pub const PIPE: u8 = 0b0101_0010;
109 pub const MSTREAM: u8 = 0b0101_0011;
110 pub const SPLIT: u8 = 0b0101_0100;
111 pub const LOOP: u8 = 0b0101_0101;
112 pub const SPAN: u8 = 0b0101_0110;
113 pub const JOIN: u8 = 0b0101_0111;
114 pub const DYN: u8 = 0b0101_1000;
115 pub const HORNERBASE: u8 = 0b0101_1001;
116 pub const HORNEREXT: u8 = 0b0101_1010;
117 pub const PUSH: u8 = 0b0101_1011;
118 pub const DYNCALL: u8 = 0b0101_1100;
119 pub const EVALCIRCUIT: u8 = 0b0101_1101;
120 pub const LOGPRECOMPILE: u8 = 0b0101_1110;
121
122 pub const MRUPDATE: u8 = 0b0110_0000;
123 pub const CRYPTOSTREAM: u8 = 0b0110_0100;
124 pub const SYSCALL: u8 = 0b0110_1000;
125 pub const CALL: u8 = 0b0110_1100;
126 pub const END: u8 = 0b0111_0000;
127 pub const REPEAT: u8 = 0b0111_0100;
128 pub const RESPAN: u8 = 0b0111_1000;
129 pub const HALT: u8 = 0b0111_1100;
130}
131
132// OPERATIONS
133// ================================================================================================
134
135/// The set of native VM basic block operations executable which take exactly one cycle to execute.
136///
137/// Specifically, the operations encoded here are only those which can be executed within basic
138/// blocks, i.e., they exclude all control flow operations (e.g., `Loop`, `Span`, `Join`, etc.).
139/// Note though that those operations have their own unique opcode which lives in the same 7-bit
140/// opcode space as the basic block operations.
141#[derive(Copy, Clone, Debug, Eq, PartialEq)]
142#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
143#[repr(u8)]
144pub enum Operation {
145 // ----- system operations -------------------------------------------------------------------
146 /// Advances cycle counter, but does not change the state of user stack.
147 Noop = opcodes::NOOP,
148
149 /// Pops the stack; if the popped value is not 1, execution fails.
150 ///
151 /// The internal value specifies an error code associated with the error in case when the
152 /// execution fails.
153 Assert(Felt) = opcodes::ASSERT,
154
155 /// Pushes the current depth of the stack onto the stack.
156 SDepth = opcodes::SDEPTH,
157
158 /// Overwrites the top four stack items with the hash of a function which initiated the current
159 /// SYSCALL. Thus, this operation can be executed only inside a SYSCALL code block.
160 Caller = opcodes::CALLER,
161
162 /// Pushes the current value of the clock cycle onto the stack. This operation can be used to
163 /// measure the number of cycles it has taken to execute the program up to the current
164 /// instruction.
165 Clk = opcodes::CLK,
166
167 /// Emits an event to the host.
168 ///
169 /// Semantics:
170 /// - Reads the event id from the top of the stack (as a `Felt`) without consuming it; the
171 /// caller is responsible for pushing and later dropping the id.
172 /// - User-defined events are conventionally derived from strings via
173 /// `hash_string_to_word(name)[0]` (Blake3-based) and may be emitted via immediate forms in
174 /// assembly (`emit.event("...")` or `emit.CONST` where `CONST=event("...")`).
175 /// - System events are still identified by specific 32-bit codes; the VM attempts to interpret
176 /// the stack `Felt` as `u32` to dispatch known system events, and otherwise forwards the
177 /// event to the host.
178 ///
179 /// This operation does not change the state of the user stack aside from reading the value.
180 Emit = opcodes::EMIT,
181
182 // ----- field operations --------------------------------------------------------------------
183 /// Pops two elements off the stack, adds them, and pushes the result back onto the stack.
184 Add = opcodes::ADD,
185
186 /// Pops an element off the stack, negates it, and pushes the result back onto the stack.
187 Neg = opcodes::NEG,
188
189 /// Pops two elements off the stack, multiplies them, and pushes the result back onto the
190 /// stack.
191 Mul = opcodes::MUL,
192
193 /// Pops an element off the stack, computes its multiplicative inverse, and pushes the result
194 /// back onto the stack.
195 Inv = opcodes::INV,
196
197 /// Pops an element off the stack, adds 1 to it, and pushes the result back onto the stack.
198 Incr = opcodes::INCR,
199
200 /// Pops two elements off the stack, multiplies them, and pushes the result back onto the
201 /// stack.
202 ///
203 /// If either of the elements is greater than 1, execution fails. This operation is equivalent
204 /// to boolean AND.
205 And = opcodes::AND,
206
207 /// Pops two elements off the stack and subtracts their product from their sum.
208 ///
209 /// If either of the elements is greater than 1, execution fails. This operation is equivalent
210 /// to boolean OR.
211 Or = opcodes::OR,
212
213 /// Pops an element off the stack and subtracts it from 1.
214 ///
215 /// If the element is greater than one, the execution fails. This operation is equivalent to
216 /// boolean NOT.
217 Not = opcodes::NOT,
218
219 /// Pops two elements off the stack and compares them. If the elements are equal, pushes 1
220 /// onto the stack, otherwise pushes 0 onto the stack.
221 Eq = opcodes::EQ,
222
223 /// Pops an element off the stack and compares it to 0. If the element is 0, pushes 1 onto
224 /// the stack, otherwise pushes 0 onto the stack.
225 Eqz = opcodes::EQZ,
226
227 /// Computes a single turn of exponent accumulation for the given inputs. This operation can be
228 /// be used to compute a single turn of power of a field element.
229 ///
230 /// The top 4 elements of the stack are expected to be arranged as follows (form the top):
231 /// - least significant bit of the exponent in the previous trace if there's an expacc call,
232 /// otherwise ZERO
233 /// - exponent of base number `a` for this turn
234 /// - accumulated power of base number `a` so far
235 /// - number which needs to be shifted to the right
236 ///
237 /// At the end of the operation, exponent is replaced with its square, current value of power
238 /// of base number `a` on exponent is incorporated into the accumulator and the number is
239 /// shifted to the right by one bit.
240 Expacc = opcodes::EXPACC,
241
242 // ----- ext2 operations ---------------------------------------------------------------------
243 /// Computes the product of two elements in the extension field of degree 2 and pushes the
244 /// result back onto the stack as the third and fourth elements. Pushes 0 onto the stack as
245 /// the first and second elements.
246 ///
247 /// The extension field is defined as 𝔽ₚ\[x\]/(x² - 7), i.e. using the
248 /// irreducible quadratic polynomial x² - 7 over the base field.
249 Ext2Mul = opcodes::EXT2MUL,
250
251 // ----- u32 operations ----------------------------------------------------------------------
252 /// Pops an element off the stack, splits it into upper and lower 32-bit values, and pushes
253 /// these values back onto the stack.
254 U32split = opcodes::U32SPLIT,
255
256 /// Pops two elements off the stack, adds them, and splits the result into upper and lower
257 /// 32-bit values. Then pushes these values back onto the stack.
258 ///
259 /// If either of these elements is greater than or equal to 2^32, the result of this
260 /// operation is undefined.
261 U32add = opcodes::U32ADD,
262
263 /// Pops two elements off the stack and checks if each of them represents a 32-bit value.
264 /// If both of them are, they are pushed back onto the stack, otherwise an error is returned.
265 ///
266 /// The internal value specifies an error code associated with the error in case when the
267 /// assertion fails.
268 U32assert2(Felt) = opcodes::U32ASSERT2,
269
270 /// Pops three elements off the stack, adds them together, and splits the result into upper
271 /// and lower 32-bit values. Then pushes the result back onto the stack.
272 U32add3 = opcodes::U32ADD3,
273
274 /// Pops two elements off the stack and subtracts the first element from the second. Then,
275 /// the result, together with a flag indicating whether subtraction underflowed is pushed
276 /// onto the stack.
277 ///
278 /// If their of the values is greater than or equal to 2^32, the result of this operation is
279 /// undefined.
280 U32sub = opcodes::U32SUB,
281
282 /// Pops two elements off the stack, multiplies them, and splits the result into upper and
283 /// lower 32-bit values. Then pushes these values back onto the stack.
284 ///
285 /// If their of the values is greater than or equal to 2^32, the result of this operation is
286 /// undefined.
287 U32mul = opcodes::U32MUL,
288
289 /// Pops two elements off the stack and multiplies them. Then pops the third element off the
290 /// stack, and adds it to the result. Finally, splits the result into upper and lower 32-bit
291 /// values, and pushes them onto the stack.
292 ///
293 /// If any of the three values is greater than or equal to 2^32, the result of this operation
294 /// is undefined.
295 U32madd = opcodes::U32MADD,
296
297 /// Pops two elements off the stack and divides the second element by the first. Then pushes
298 /// the integer result of the division, together with the remainder, onto the stack.
299 ///
300 /// If their of the values is greater than or equal to 2^32, the result of this operation is
301 /// undefined.
302 U32div = opcodes::U32DIV,
303
304 /// Pops two elements off the stack, computes their binary AND, and pushes the result back
305 /// onto the stack.
306 ///
307 /// If either of the elements is greater than or equal to 2^32, execution fails.
308 U32and = opcodes::U32AND,
309
310 /// Pops two elements off the stack, computes their binary XOR, and pushes the result back
311 /// onto the stack.
312 ///
313 /// If either of the elements is greater than or equal to 2^32, execution fails.
314 U32xor = opcodes::U32XOR,
315
316 // ----- stack manipulation ------------------------------------------------------------------
317 /// Pushes 0 onto the stack.
318 Pad = opcodes::PAD,
319
320 /// Removes to element from the stack.
321 Drop = opcodes::DROP,
322
323 /// Pushes a copy of stack element 0 onto the stack.
324 Dup0 = opcodes::DUP0,
325
326 /// Pushes a copy of stack element 1 onto the stack.
327 Dup1 = opcodes::DUP1,
328
329 /// Pushes a copy of stack element 2 onto the stack.
330 Dup2 = opcodes::DUP2,
331
332 /// Pushes a copy of stack element 3 onto the stack.
333 Dup3 = opcodes::DUP3,
334
335 /// Pushes a copy of stack element 4 onto the stack.
336 Dup4 = opcodes::DUP4,
337
338 /// Pushes a copy of stack element 5 onto the stack.
339 Dup5 = opcodes::DUP5,
340
341 /// Pushes a copy of stack element 6 onto the stack.
342 Dup6 = opcodes::DUP6,
343
344 /// Pushes a copy of stack element 7 onto the stack.
345 Dup7 = opcodes::DUP7,
346
347 /// Pushes a copy of stack element 9 onto the stack.
348 Dup9 = opcodes::DUP9,
349
350 /// Pushes a copy of stack element 11 onto the stack.
351 Dup11 = opcodes::DUP11,
352
353 /// Pushes a copy of stack element 13 onto the stack.
354 Dup13 = opcodes::DUP13,
355
356 /// Pushes a copy of stack element 15 onto the stack.
357 Dup15 = opcodes::DUP15,
358
359 /// Swaps stack elements 0 and 1.
360 Swap = opcodes::SWAP,
361
362 /// Swaps stack elements 0, 1, 2, and 3 with elements 4, 5, 6, and 7.
363 SwapW = opcodes::SWAPW,
364
365 /// Swaps stack elements 0, 1, 2, and 3 with elements 8, 9, 10, and 11.
366 SwapW2 = opcodes::SWAPW2,
367
368 /// Swaps stack elements 0, 1, 2, and 3, with elements 12, 13, 14, and 15.
369 SwapW3 = opcodes::SWAPW3,
370
371 /// Swaps the top two words pair wise.
372 ///
373 /// Input: [D, C, B, A, ...]
374 /// Output: [B, A, D, C, ...]
375 SwapDW = opcodes::SWAPDW,
376
377 /// Moves stack element 2 to the top of the stack.
378 MovUp2 = opcodes::MOVUP2,
379
380 /// Moves stack element 3 to the top of the stack.
381 MovUp3 = opcodes::MOVUP3,
382
383 /// Moves stack element 4 to the top of the stack.
384 MovUp4 = opcodes::MOVUP4,
385
386 /// Moves stack element 5 to the top of the stack.
387 MovUp5 = opcodes::MOVUP5,
388
389 /// Moves stack element 6 to the top of the stack.
390 MovUp6 = opcodes::MOVUP6,
391
392 /// Moves stack element 7 to the top of the stack.
393 MovUp7 = opcodes::MOVUP7,
394
395 /// Moves stack element 8 to the top of the stack.
396 MovUp8 = opcodes::MOVUP8,
397
398 /// Moves the top stack element to position 2 on the stack.
399 MovDn2 = opcodes::MOVDN2,
400
401 /// Moves the top stack element to position 3 on the stack.
402 MovDn3 = opcodes::MOVDN3,
403
404 /// Moves the top stack element to position 4 on the stack.
405 MovDn4 = opcodes::MOVDN4,
406
407 /// Moves the top stack element to position 5 on the stack.
408 MovDn5 = opcodes::MOVDN5,
409
410 /// Moves the top stack element to position 6 on the stack.
411 MovDn6 = opcodes::MOVDN6,
412
413 /// Moves the top stack element to position 7 on the stack.
414 MovDn7 = opcodes::MOVDN7,
415
416 /// Moves the top stack element to position 8 on the stack.
417 MovDn8 = opcodes::MOVDN8,
418
419 /// Pops an element off the stack, and if the element is 1, swaps the top two remaining
420 /// elements on the stack. If the popped element is 0, the stack remains unchanged.
421 ///
422 /// If the popped element is neither 0 nor 1, execution fails.
423 CSwap = opcodes::CSWAP,
424
425 /// Pops an element off the stack, and if the element is 1, swaps the remaining elements
426 /// 0, 1, 2, and 3 with elements 4, 5, 6, and 7. If the popped element is 0, the stack
427 /// remains unchanged.
428 ///
429 /// If the popped element is neither 0 nor 1, execution fails.
430 CSwapW = opcodes::CSWAPW,
431
432 // ----- input / output ----------------------------------------------------------------------
433 /// Pushes the immediate value onto the stack.
434 Push(Felt) = opcodes::PUSH,
435
436 /// Removes the next element from the advice stack and pushes it onto the operand stack.
437 AdvPop = opcodes::ADVPOP,
438
439 /// Removes a word (4 elements) from the advice stack and overwrites the top four operand
440 /// stack elements with it.
441 AdvPopW = opcodes::ADVPOPW,
442
443 /// Pops an element off the stack, interprets it as a memory address, and replaces the
444 /// remaining 4 elements at the top of the stack with values located at the specified address.
445 MLoadW = opcodes::MLOADW,
446
447 /// Pops an element off the stack, interprets it as a memory address, and writes the remaining
448 /// 4 elements at the top of the stack into memory at the specified address.
449 MStoreW = opcodes::MSTOREW,
450
451 /// Pops an element off the stack, interprets it as a memory address, and pushes the first
452 /// element of the word located at the specified address to the stack.
453 MLoad = opcodes::MLOAD,
454
455 /// Pops an element off the stack, interprets it as a memory address, and writes the remaining
456 /// element at the top of the stack into the first element of the word located at the specified
457 /// memory address. The remaining 3 elements of the word are not affected.
458 MStore = opcodes::MSTORE,
459
460 /// Loads two words from memory, and replaces the top 8 elements of the stack with them,
461 /// element-wise, in stack order.
462 ///
463 /// The operation works as follows:
464 /// - The memory address of the first word is retrieved from 13th stack element (position 12).
465 /// - Two consecutive words, starting at this address, are loaded from memory.
466 /// - The top 8 elements of the stack are overwritten with these words (element-wise, in stack
467 /// order).
468 /// - Memory address (in position 12) is incremented by 2.
469 /// - All other stack elements remain the same.
470 MStream = opcodes::MSTREAM,
471
472 /// Pops two words from the advice stack, writes them to memory, and replaces the top 8
473 /// elements of the stack with them, element-wise, in stack order.
474 ///
475 /// The operation works as follows:
476 /// - Two words are popped from the advice stack.
477 /// - The destination memory address for the first word is retrieved from the 13th stack element
478 /// (position 12).
479 /// - The two words are written to memory consecutively, starting at this address.
480 /// - The top 8 elements of the stack are overwritten with these words (element-wise, in stack
481 /// order).
482 /// - Memory address (in position 12) is incremented by 2.
483 /// - All other stack elements remain the same.
484 Pipe = opcodes::PIPE,
485
486 /// Encrypts data from source memory to destination memory using the Poseidon2 sponge keystream.
487 ///
488 /// Two consecutive words (8 elements) are loaded from source memory, each element is added
489 /// to the corresponding element in the rate (top 8 stack elements), and the resulting
490 /// ciphertext is written to destination memory and replaces the rate. Source and destination
491 /// addresses are incremented by 8.
492 ///
493 /// Stack transition:
494 /// ```text
495 /// [rate(8), cap(4), src, dst, ...]
496 /// ↓
497 /// [ct(8), cap(4), src+8, dst+8, ...]
498 /// ```
499 /// where `ct = mem[src..src+8] + rate`, where addition is element-wise.
500 ///
501 /// After this operation, `hperm` should be applied to refresh the keystream for the next block.
502 CryptoStream = opcodes::CRYPTOSTREAM,
503
504 // ----- cryptographic operations ------------------------------------------------------------
505 /// Performs a Poseidon2 permutation on the top 3 words of the operand stack,
506 /// where the top 2 words are the rate (words C and B), the deepest word is the capacity (word
507 /// A), and the digest output is the middle word E.
508 ///
509 /// Stack transition:
510 /// [C, B, A, ...] -> [F, E, D, ...]
511 HPerm = opcodes::HPERM,
512
513 /// Verifies that a Merkle path from the specified node resolves to the specified root. This
514 /// operation can be used to prove that the prover knows a path in the specified Merkle tree
515 /// which starts with the specified node.
516 ///
517 /// The stack is expected to be arranged as follows (from the top):
518 /// - value of the node, 4 elements.
519 /// - depth of the path, 1 element.
520 /// - index of the node, 1 element.
521 /// - root of the tree, 4 elements.
522 ///
523 /// The Merkle path itself is expected to be provided by the prover non-deterministically (via
524 /// merkle sets). If the prover is not able to provide the required path, the operation fails.
525 /// The state of the stack does not change.
526 ///
527 /// The internal value specifies an error code associated with the error in case when the
528 /// assertion fails.
529 MpVerify(Felt) = opcodes::MPVERIFY,
530
531 /// Computes a new root of a Merkle tree where a node at the specified position is updated to
532 /// the specified value.
533 ///
534 /// The stack is expected to be arranged as follows (from the top):
535 /// - old value of the node, 4 element
536 /// - depth of the node, 1 element
537 /// - index of the node, 1 element
538 /// - current root of the tree, 4 elements
539 /// - new value of the node, 4 element
540 ///
541 /// The Merkle path for the node is expected to be provided by the prover non-deterministically
542 /// via the advice provider. At the end of the operation, the old node value is replaced with
543 /// the new root value, that is computed based on the provided path. Everything else on the
544 /// stack remains the same.
545 ///
546 /// The tree will always be copied into a new instance, meaning the advice provider will keep
547 /// track of both the old and new Merkle trees.
548 MrUpdate = opcodes::MRUPDATE,
549
550 /// Performs FRI (Fast Reed-Solomon Interactive Oracle Proofs) layer folding by a factor of 4
551 /// for FRI protocol executed in a degree 2 extension of the base field.
552 ///
553 /// This operation:
554 /// - Folds 4 query values (v0, v1), (v2, v3), (v4, v5), (v6, v7) into a single value (ne0, ne1)
555 /// - Computes new value of the domain generator power: poe' = poe^4
556 /// - Increments layer pointer (cptr) by 2
557 /// - Checks that the previous folding was done correctly
558 /// - Shifts the stack to move an item from the overflow table to stack position 15
559 ///
560 /// Stack transition:
561 /// Input: [v7, v6, v5, v4, v3, v2, v1, v0, f_pos, d_seg, poe, pe1, pe0, a1, a0, cptr, ...]
562 /// Output: [t1, t0, s1, s0, df3, df2, df1, df0, poe^2, f_tau, cptr+2, poe^4, f_pos, ne1, ne0,
563 /// eptr, ...] where eptr is moved from the stack overflow table and is the address of the
564 /// final FRI layer.
565 FriE2F4 = opcodes::FRIE2F4,
566
567 /// Performs 8 steps of the Horner evaluation method on a polynomial with coefficients over
568 /// the base field, i.e., it computes
569 ///
570 /// acc' = (((acc_tmp * alpha + c3) * alpha + c2) * alpha + c1) * alpha + c0
571 ///
572 /// where
573 ///
574 /// acc_tmp := (((acc * alpha + c7) * alpha + c6) * alpha + c5) * alpha + c4
575 ///
576 ///
577 /// In other words, the intsruction computes the evaluation at alpha of the polynomial
578 ///
579 /// P(X) := c7 * X^7 + c6 * X^6 + ... + c1 * X + c0
580 HornerBase = opcodes::HORNERBASE,
581
582 /// Performs 4 steps of the Horner evaluation method on a polynomial with coefficients over
583 /// the extension field, i.e., it computes
584 ///
585 /// acc' = (((acc * alpha + c3) * alpha + c2) * alpha + c1) * alpha + c0
586 ///
587 /// In other words, the intsruction computes the evaluation at alpha of the polynomial
588 ///
589 /// P(X) := c3 * X^3 + c2 * X^2 + c1 * X + c0
590 HornerExt = opcodes::HORNEREXT,
591
592 /// Evaluates an arithmetic circuit given a pointer to its description in memory, the number
593 /// of arithmetic gates, and the sum of the input and constant gates.
594 EvalCircuit = opcodes::EVALCIRCUIT,
595
596 /// Logs a precompile event. This instruction is used to signal that a precompile computation
597 /// was requested.
598 LogPrecompile = opcodes::LOGPRECOMPILE,
599}
600
601impl Operation {
602 pub const OP_BITS: usize = 7;
603
604 /// Returns the opcode of this operation.
605 #[rustfmt::skip]
606 pub fn op_code(&self) -> u8 {
607 // SAFETY: This is safe because we have given this enum a primitive representation with
608 // #[repr(u8)], with the first field of the underlying union-of-structs the discriminant.
609 //
610 // See the section on "accessing the numeric value of the discriminant"
611 // here: https://doc.rust-lang.org/std/mem/fn.discriminant.html
612 unsafe { *<*const _>::from(self).cast::<u8>() }
613 }
614
615 /// Returns an immediate value carried by this operation.
616 // Proptest generators for operations in crate::mast::node::basic_block_node::tests discriminate
617 // on this flag, please update them when you modify the semantics of this method.
618 pub fn imm_value(&self) -> Option<Felt> {
619 match *self {
620 Self::Push(imm) => Some(imm),
621 _ => None,
622 }
623 }
624
625 /// Returns true if this basic block operation increases the stack depth by one.
626 ///
627 /// Note: this only applies to operations within basic blocks (i.e. those executed via
628 /// `ResumeBasicBlock` continuations). Control flow operations that affect stack size
629 /// (e.g. Split, Loop, Dyn) are handled separately.
630 pub fn increments_stack_size(&self) -> bool {
631 matches!(
632 self,
633 Self::Push(_)
634 | Self::Pad
635 | Self::Dup0
636 | Self::Dup1
637 | Self::Dup2
638 | Self::Dup3
639 | Self::Dup4
640 | Self::Dup5
641 | Self::Dup6
642 | Self::Dup7
643 | Self::Dup9
644 | Self::Dup11
645 | Self::Dup13
646 | Self::Dup15
647 | Self::U32split
648 | Self::SDepth
649 | Self::Clk
650 | Self::AdvPop
651 )
652 }
653
654 /// Returns true if this basic block operation decreases the stack depth by one.
655 ///
656 /// Note: this only applies to operations within basic blocks (i.e. those executed via
657 /// `ResumeBasicBlock` continuations). Control flow operations that affect stack size
658 /// (e.g. Split, Loop, Dyn) are handled separately.
659 pub fn decrements_stack_size(&self) -> bool {
660 matches!(
661 self,
662 Self::Drop
663 | Self::Assert(_)
664 | Self::Add
665 | Self::Mul
666 | Self::And
667 | Self::Or
668 | Self::Eq
669 | Self::U32add3
670 | Self::U32madd
671 | Self::U32and
672 | Self::U32xor
673 | Self::CSwap
674 | Self::CSwapW
675 | Self::MLoadW
676 | Self::MStoreW
677 | Self::MStore
678 | Self::FriE2F4
679 )
680 }
681}
682
683impl crate::prettier::PrettyPrint for Operation {
684 fn render(&self) -> crate::prettier::Document {
685 crate::prettier::display(self)
686 }
687}
688
689impl fmt::Display for Operation {
690 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
691 match self {
692 // ----- system operations ------------------------------------------------------------
693 Self::Noop => write!(f, "noop"),
694 Self::Assert(err_code) => write!(f, "assert({err_code})"),
695
696 Self::SDepth => write!(f, "sdepth"),
697 Self::Caller => write!(f, "caller"),
698
699 Self::Clk => write!(f, "clk"),
700
701 // ----- field operations -------------------------------------------------------------
702 Self::Add => write!(f, "add"),
703 Self::Neg => write!(f, "neg"),
704 Self::Mul => write!(f, "mul"),
705 Self::Inv => write!(f, "inv"),
706 Self::Incr => write!(f, "incr"),
707
708 Self::And => write!(f, "and"),
709 Self::Or => write!(f, "or"),
710 Self::Not => write!(f, "not"),
711
712 Self::Eq => write!(f, "eq"),
713 Self::Eqz => write!(f, "eqz"),
714
715 Self::Expacc => write!(f, "expacc"),
716
717 // ----- ext2 operations --------------------------------------------------------------
718 Self::Ext2Mul => write!(f, "ext2mul"),
719
720 // ----- u32 operations ---------------------------------------------------------------
721 Self::U32assert2(err_code) => write!(f, "u32assert2({err_code})"),
722 Self::U32split => write!(f, "u32split"),
723 Self::U32add => write!(f, "u32add"),
724 Self::U32add3 => write!(f, "u32add3"),
725 Self::U32sub => write!(f, "u32sub"),
726 Self::U32mul => write!(f, "u32mul"),
727 Self::U32madd => write!(f, "u32madd"),
728 Self::U32div => write!(f, "u32div"),
729
730 Self::U32and => write!(f, "u32and"),
731 Self::U32xor => write!(f, "u32xor"),
732
733 // ----- stack manipulation -----------------------------------------------------------
734 Self::Drop => write!(f, "drop"),
735 Self::Pad => write!(f, "pad"),
736
737 Self::Dup0 => write!(f, "dup0"),
738 Self::Dup1 => write!(f, "dup1"),
739 Self::Dup2 => write!(f, "dup2"),
740 Self::Dup3 => write!(f, "dup3"),
741 Self::Dup4 => write!(f, "dup4"),
742 Self::Dup5 => write!(f, "dup5"),
743 Self::Dup6 => write!(f, "dup6"),
744 Self::Dup7 => write!(f, "dup7"),
745 Self::Dup9 => write!(f, "dup9"),
746 Self::Dup11 => write!(f, "dup11"),
747 Self::Dup13 => write!(f, "dup13"),
748 Self::Dup15 => write!(f, "dup15"),
749
750 Self::Swap => write!(f, "swap"),
751 Self::SwapW => write!(f, "swapw"),
752 Self::SwapW2 => write!(f, "swapw2"),
753 Self::SwapW3 => write!(f, "swapw3"),
754 Self::SwapDW => write!(f, "swapdw"),
755
756 Self::MovUp2 => write!(f, "movup2"),
757 Self::MovUp3 => write!(f, "movup3"),
758 Self::MovUp4 => write!(f, "movup4"),
759 Self::MovUp5 => write!(f, "movup5"),
760 Self::MovUp6 => write!(f, "movup6"),
761 Self::MovUp7 => write!(f, "movup7"),
762 Self::MovUp8 => write!(f, "movup8"),
763
764 Self::MovDn2 => write!(f, "movdn2"),
765 Self::MovDn3 => write!(f, "movdn3"),
766 Self::MovDn4 => write!(f, "movdn4"),
767 Self::MovDn5 => write!(f, "movdn5"),
768 Self::MovDn6 => write!(f, "movdn6"),
769 Self::MovDn7 => write!(f, "movdn7"),
770 Self::MovDn8 => write!(f, "movdn8"),
771
772 Self::CSwap => write!(f, "cswap"),
773 Self::CSwapW => write!(f, "cswapw"),
774
775 // ----- input / output ---------------------------------------------------------------
776 Self::Push(value) => write!(f, "push({value})"),
777
778 Self::AdvPop => write!(f, "advpop"),
779 Self::AdvPopW => write!(f, "advpopw"),
780
781 Self::MLoadW => write!(f, "mloadw"),
782 Self::MStoreW => write!(f, "mstorew"),
783
784 Self::MLoad => write!(f, "mload"),
785 Self::MStore => write!(f, "mstore"),
786
787 Self::MStream => write!(f, "mstream"),
788 Self::Pipe => write!(f, "pipe"),
789 Self::CryptoStream => write!(f, "crypto_stream"),
790
791 Self::Emit => write!(f, "emit"),
792
793 // ----- cryptographic operations -----------------------------------------------------
794 Self::HPerm => write!(f, "hperm"),
795 Self::MpVerify(err_code) => write!(f, "mpverify({err_code})"),
796 Self::MrUpdate => write!(f, "mrupdate"),
797
798 // ----- STARK proof verification -----------------------------------------------------
799 Self::FriE2F4 => write!(f, "frie2f4"),
800 Self::HornerBase => write!(f, "horner_eval_base"),
801 Self::HornerExt => write!(f, "horner_eval_ext"),
802 Self::EvalCircuit => write!(f, "eval_circuit"),
803 Self::LogPrecompile => write!(f, "log_precompile"),
804 }
805 }
806}
807
808impl Serializable for Operation {
809 fn write_into<W: ByteWriter>(&self, target: &mut W) {
810 target.write_u8(self.op_code());
811
812 // For operations that have extra data, encode it in `data`.
813 match self {
814 Operation::Assert(err_code)
815 | Operation::MpVerify(err_code)
816 | Operation::U32assert2(err_code) => {
817 err_code.write_into(target);
818 },
819 Operation::Push(value) => value.as_canonical_u64().write_into(target),
820
821 // Note: we explicitly write out all the operations so that whenever we make a
822 // modification to the `Operation` enum, we get a compile error here. This
823 // should help us remember to properly encode/decode each operation variant.
824 Operation::Noop
825 | Operation::SDepth
826 | Operation::Caller
827 | Operation::Clk
828 | Operation::Add
829 | Operation::Neg
830 | Operation::Mul
831 | Operation::Inv
832 | Operation::Incr
833 | Operation::And
834 | Operation::Or
835 | Operation::Not
836 | Operation::Eq
837 | Operation::Eqz
838 | Operation::Expacc
839 | Operation::Ext2Mul
840 | Operation::U32split
841 | Operation::U32add
842 | Operation::U32add3
843 | Operation::U32sub
844 | Operation::U32mul
845 | Operation::U32madd
846 | Operation::U32div
847 | Operation::U32and
848 | Operation::U32xor
849 | Operation::Pad
850 | Operation::Drop
851 | Operation::Dup0
852 | Operation::Dup1
853 | Operation::Dup2
854 | Operation::Dup3
855 | Operation::Dup4
856 | Operation::Dup5
857 | Operation::Dup6
858 | Operation::Dup7
859 | Operation::Dup9
860 | Operation::Dup11
861 | Operation::Dup13
862 | Operation::Dup15
863 | Operation::Swap
864 | Operation::SwapW
865 | Operation::SwapW2
866 | Operation::SwapW3
867 | Operation::SwapDW
868 | Operation::Emit
869 | Operation::MovUp2
870 | Operation::MovUp3
871 | Operation::MovUp4
872 | Operation::MovUp5
873 | Operation::MovUp6
874 | Operation::MovUp7
875 | Operation::MovUp8
876 | Operation::MovDn2
877 | Operation::MovDn3
878 | Operation::MovDn4
879 | Operation::MovDn5
880 | Operation::MovDn6
881 | Operation::MovDn7
882 | Operation::MovDn8
883 | Operation::CSwap
884 | Operation::CSwapW
885 | Operation::AdvPop
886 | Operation::AdvPopW
887 | Operation::MLoadW
888 | Operation::MStoreW
889 | Operation::MLoad
890 | Operation::MStore
891 | Operation::MStream
892 | Operation::Pipe
893 | Operation::CryptoStream
894 | Operation::HPerm
895 | Operation::MrUpdate
896 | Operation::FriE2F4
897 | Operation::HornerBase
898 | Operation::HornerExt
899 | Operation::EvalCircuit
900 | Operation::LogPrecompile => (),
901 }
902 }
903}
904
905impl Operation {
906 /// Returns the serialized size of this operation in bytes.
907 pub(crate) fn encoded_size(&self) -> usize {
908 let mut size = size_of::<u8>();
909 match self {
910 Operation::Assert(err_code)
911 | Operation::MpVerify(err_code)
912 | Operation::U32assert2(err_code) => {
913 size += err_code.get_size_hint();
914 },
915 Operation::Push(value) => {
916 size += value.as_canonical_u64().get_size_hint();
917 },
918 Operation::Noop
919 | Operation::SDepth
920 | Operation::Caller
921 | Operation::Clk
922 | Operation::Add
923 | Operation::Neg
924 | Operation::Mul
925 | Operation::Inv
926 | Operation::Incr
927 | Operation::And
928 | Operation::Or
929 | Operation::Not
930 | Operation::Eq
931 | Operation::Eqz
932 | Operation::Expacc
933 | Operation::Ext2Mul
934 | Operation::U32split
935 | Operation::U32add
936 | Operation::U32add3
937 | Operation::U32sub
938 | Operation::U32mul
939 | Operation::U32madd
940 | Operation::U32div
941 | Operation::U32and
942 | Operation::U32xor
943 | Operation::Pad
944 | Operation::Drop
945 | Operation::Dup0
946 | Operation::Dup1
947 | Operation::Dup2
948 | Operation::Dup3
949 | Operation::Dup4
950 | Operation::Dup5
951 | Operation::Dup6
952 | Operation::Dup7
953 | Operation::Dup9
954 | Operation::Dup11
955 | Operation::Dup13
956 | Operation::Dup15
957 | Operation::Swap
958 | Operation::SwapW
959 | Operation::SwapW2
960 | Operation::SwapW3
961 | Operation::SwapDW
962 | Operation::Emit
963 | Operation::MovUp2
964 | Operation::MovUp3
965 | Operation::MovUp4
966 | Operation::MovUp5
967 | Operation::MovUp6
968 | Operation::MovUp7
969 | Operation::MovUp8
970 | Operation::MovDn2
971 | Operation::MovDn3
972 | Operation::MovDn4
973 | Operation::MovDn5
974 | Operation::MovDn6
975 | Operation::MovDn7
976 | Operation::MovDn8
977 | Operation::CSwap
978 | Operation::CSwapW
979 | Operation::AdvPop
980 | Operation::AdvPopW
981 | Operation::MLoadW
982 | Operation::MStoreW
983 | Operation::MLoad
984 | Operation::MStore
985 | Operation::MStream
986 | Operation::Pipe
987 | Operation::CryptoStream
988 | Operation::HPerm
989 | Operation::MrUpdate
990 | Operation::FriE2F4
991 | Operation::HornerBase
992 | Operation::HornerExt
993 | Operation::EvalCircuit
994 | Operation::LogPrecompile => (),
995 }
996 size
997 }
998}
999
1000impl Deserializable for Operation {
1001 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
1002 let op_code = source.read_u8()?;
1003
1004 let operation = match op_code {
1005 opcodes::NOOP => Self::Noop,
1006 opcodes::EQZ => Self::Eqz,
1007 opcodes::NEG => Self::Neg,
1008 opcodes::INV => Self::Inv,
1009 opcodes::INCR => Self::Incr,
1010 opcodes::NOT => Self::Not,
1011 opcodes::MLOAD => Self::MLoad,
1012 opcodes::SWAP => Self::Swap,
1013 opcodes::CALLER => Self::Caller,
1014 opcodes::MOVUP2 => Self::MovUp2,
1015 opcodes::MOVDN2 => Self::MovDn2,
1016 opcodes::MOVUP3 => Self::MovUp3,
1017 opcodes::MOVDN3 => Self::MovDn3,
1018 opcodes::ADVPOPW => Self::AdvPopW,
1019 opcodes::EXPACC => Self::Expacc,
1020
1021 opcodes::MOVUP4 => Self::MovUp4,
1022 opcodes::MOVDN4 => Self::MovDn4,
1023 opcodes::MOVUP5 => Self::MovUp5,
1024 opcodes::MOVDN5 => Self::MovDn5,
1025 opcodes::MOVUP6 => Self::MovUp6,
1026 opcodes::MOVDN6 => Self::MovDn6,
1027 opcodes::MOVUP7 => Self::MovUp7,
1028 opcodes::MOVDN7 => Self::MovDn7,
1029 opcodes::SWAPW => Self::SwapW,
1030 opcodes::EXT2MUL => Self::Ext2Mul,
1031 opcodes::MOVUP8 => Self::MovUp8,
1032 opcodes::MOVDN8 => Self::MovDn8,
1033 opcodes::SWAPW2 => Self::SwapW2,
1034 opcodes::SWAPW3 => Self::SwapW3,
1035 opcodes::SWAPDW => Self::SwapDW,
1036 opcodes::EMIT => Self::Emit,
1037
1038 opcodes::ASSERT => Self::Assert(Felt::read_from(source)?),
1039 opcodes::EQ => Self::Eq,
1040 opcodes::ADD => Self::Add,
1041 opcodes::MUL => Self::Mul,
1042 opcodes::AND => Self::And,
1043 opcodes::OR => Self::Or,
1044 opcodes::U32AND => Self::U32and,
1045 opcodes::U32XOR => Self::U32xor,
1046 opcodes::FRIE2F4 => Self::FriE2F4,
1047 opcodes::DROP => Self::Drop,
1048 opcodes::CSWAP => Self::CSwap,
1049 opcodes::CSWAPW => Self::CSwapW,
1050 opcodes::MLOADW => Self::MLoadW,
1051 opcodes::MSTORE => Self::MStore,
1052 opcodes::MSTOREW => Self::MStoreW,
1053
1054 opcodes::PAD => Self::Pad,
1055 opcodes::DUP0 => Self::Dup0,
1056 opcodes::DUP1 => Self::Dup1,
1057 opcodes::DUP2 => Self::Dup2,
1058 opcodes::DUP3 => Self::Dup3,
1059 opcodes::DUP4 => Self::Dup4,
1060 opcodes::DUP5 => Self::Dup5,
1061 opcodes::DUP6 => Self::Dup6,
1062 opcodes::DUP7 => Self::Dup7,
1063 opcodes::DUP9 => Self::Dup9,
1064 opcodes::DUP11 => Self::Dup11,
1065 opcodes::DUP13 => Self::Dup13,
1066 opcodes::DUP15 => Self::Dup15,
1067 opcodes::ADVPOP => Self::AdvPop,
1068 opcodes::SDEPTH => Self::SDepth,
1069 opcodes::CLK => Self::Clk,
1070
1071 opcodes::U32ADD => Self::U32add,
1072 opcodes::U32SUB => Self::U32sub,
1073 opcodes::U32MUL => Self::U32mul,
1074 opcodes::U32DIV => Self::U32div,
1075 opcodes::U32SPLIT => Self::U32split,
1076 opcodes::U32ASSERT2 => Self::U32assert2(Felt::read_from(source)?),
1077 opcodes::U32ADD3 => Self::U32add3,
1078 opcodes::U32MADD => Self::U32madd,
1079
1080 opcodes::HPERM => Self::HPerm,
1081 opcodes::MPVERIFY => Self::MpVerify(Felt::read_from(source)?),
1082 opcodes::PIPE => Self::Pipe,
1083 opcodes::MSTREAM => Self::MStream,
1084 opcodes::CRYPTOSTREAM => Self::CryptoStream,
1085 opcodes::HORNERBASE => Self::HornerBase,
1086 opcodes::HORNEREXT => Self::HornerExt,
1087 opcodes::LOGPRECOMPILE => Self::LogPrecompile,
1088 opcodes::EVALCIRCUIT => Self::EvalCircuit,
1089
1090 opcodes::MRUPDATE => Self::MrUpdate,
1091 opcodes::PUSH => Self::Push(Felt::read_from(source)?),
1092 _ => {
1093 return Err(DeserializationError::InvalidValue(format!(
1094 "Invalid opcode '{op_code}'"
1095 )));
1096 },
1097 };
1098
1099 Ok(operation)
1100 }
1101
1102 /// Returns the minimum serialized size: 1 byte opcode.
1103 ///
1104 /// Some operations have additional payload (e.g., Push has 8 bytes for Felt),
1105 /// but the minimum is just the opcode byte.
1106 fn min_serialized_size() -> usize {
1107 1
1108 }
1109}
1110
1111#[cfg(test)]
1112mod tests;