miden_core/operations/mod.rs
1use core::fmt;
2
3use super::Felt;
4mod decorators;
5pub use decorators::{
6 AssemblyOp, DebugOptions, Decorator, DecoratorIterator, DecoratorList, SignatureKind,
7};
8// OPERATIONS OP CODES
9// ================================================================================================
10use opcode_constants::*;
11use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
12
13/// Opcode patterns have the following meanings:
14/// - 00xxxxx operations do not shift the stack; constraint degree can be up to 2.
15/// - 010xxxx operations shift the stack the left; constraint degree can be up to 2.
16/// - 011xxxx operations shift the stack to the right; constraint degree can be up to 2.
17/// - 100xxx-: operations consume 4 range checks; constraint degree can be up to 3. These are used
18/// to encode most u32 operations.
19/// - 101xxx-: operations where constraint degree can be up to 3. These include control flow
20/// operations and some other operations requiring high degree constraints.
21/// - 11xxx--: operations where constraint degree can be up to 5. These include control flow
22/// operations and some other operations requiring very high degree constraints.
23#[rustfmt::skip]
24pub(super) mod opcode_constants {
25 pub const OPCODE_NOOP: u8 = 0b0000_0000;
26 pub const OPCODE_EQZ: u8 = 0b0000_0001;
27 pub const OPCODE_NEG: u8 = 0b0000_0010;
28 pub const OPCODE_INV: u8 = 0b0000_0011;
29 pub const OPCODE_INCR: u8 = 0b0000_0100;
30 pub const OPCODE_NOT: u8 = 0b0000_0101;
31 pub const OPCODE_FMPADD: u8 = 0b0000_0110;
32 pub const OPCODE_MLOAD: u8 = 0b0000_0111;
33 pub const OPCODE_SWAP: u8 = 0b0000_1000;
34 pub const OPCODE_CALLER: u8 = 0b0000_1001;
35 pub const OPCODE_MOVUP2: u8 = 0b0000_1010;
36 pub const OPCODE_MOVDN2: u8 = 0b0000_1011;
37 pub const OPCODE_MOVUP3: u8 = 0b0000_1100;
38 pub const OPCODE_MOVDN3: u8 = 0b0000_1101;
39 pub const OPCODE_ADVPOPW: u8 = 0b0000_1110;
40 pub const OPCODE_EXPACC: u8 = 0b0000_1111;
41
42 pub const OPCODE_MOVUP4: u8 = 0b0001_0000;
43 pub const OPCODE_MOVDN4: u8 = 0b0001_0001;
44 pub const OPCODE_MOVUP5: u8 = 0b0001_0010;
45 pub const OPCODE_MOVDN5: u8 = 0b0001_0011;
46 pub const OPCODE_MOVUP6: u8 = 0b0001_0100;
47 pub const OPCODE_MOVDN6: u8 = 0b0001_0101;
48 pub const OPCODE_MOVUP7: u8 = 0b0001_0110;
49 pub const OPCODE_MOVDN7: u8 = 0b0001_0111;
50 pub const OPCODE_SWAPW: u8 = 0b0001_1000;
51 pub const OPCODE_EXT2MUL: u8 = 0b0001_1001;
52 pub const OPCODE_MOVUP8: u8 = 0b0001_1010;
53 pub const OPCODE_MOVDN8: u8 = 0b0001_1011;
54 pub const OPCODE_SWAPW2: u8 = 0b0001_1100;
55 pub const OPCODE_SWAPW3: u8 = 0b0001_1101;
56 pub const OPCODE_SWAPDW: u8 = 0b0001_1110;
57
58 pub const OPCODE_ASSERT: u8 = 0b0010_0000;
59 pub const OPCODE_EQ: u8 = 0b0010_0001;
60 pub const OPCODE_ADD: u8 = 0b0010_0010;
61 pub const OPCODE_MUL: u8 = 0b0010_0011;
62 pub const OPCODE_AND: u8 = 0b0010_0100;
63 pub const OPCODE_OR: u8 = 0b0010_0101;
64 pub const OPCODE_U32AND: u8 = 0b0010_0110;
65 pub const OPCODE_U32XOR: u8 = 0b0010_0111;
66 pub const OPCODE_FRIE2F4: u8 = 0b0010_1000;
67 pub const OPCODE_DROP: u8 = 0b0010_1001;
68 pub const OPCODE_CSWAP: u8 = 0b0010_1010;
69 pub const OPCODE_CSWAPW: u8 = 0b0010_1011;
70 pub const OPCODE_MLOADW: u8 = 0b0010_1100;
71 pub const OPCODE_MSTORE: u8 = 0b0010_1101;
72 pub const OPCODE_MSTOREW: u8 = 0b0010_1110;
73 pub const OPCODE_FMPUPDATE: u8 = 0b0010_1111;
74
75 pub const OPCODE_PAD: u8 = 0b0011_0000;
76 pub const OPCODE_DUP0: u8 = 0b0011_0001;
77 pub const OPCODE_DUP1: u8 = 0b0011_0010;
78 pub const OPCODE_DUP2: u8 = 0b0011_0011;
79 pub const OPCODE_DUP3: u8 = 0b0011_0100;
80 pub const OPCODE_DUP4: u8 = 0b0011_0101;
81 pub const OPCODE_DUP5: u8 = 0b0011_0110;
82 pub const OPCODE_DUP6: u8 = 0b0011_0111;
83 pub const OPCODE_DUP7: u8 = 0b0011_1000;
84 pub const OPCODE_DUP9: u8 = 0b0011_1001;
85 pub const OPCODE_DUP11: u8 = 0b0011_1010;
86 pub const OPCODE_DUP13: u8 = 0b0011_1011;
87 pub const OPCODE_DUP15: u8 = 0b0011_1100;
88 pub const OPCODE_ADVPOP: u8 = 0b0011_1101;
89 pub const OPCODE_SDEPTH: u8 = 0b0011_1110;
90 pub const OPCODE_CLK: u8 = 0b0011_1111;
91
92 pub const OPCODE_U32ADD: u8 = 0b0100_0000;
93 pub const OPCODE_U32SUB: u8 = 0b0100_0010;
94 pub const OPCODE_U32MUL: u8 = 0b0100_0100;
95 pub const OPCODE_U32DIV: u8 = 0b0100_0110;
96 pub const OPCODE_U32SPLIT: u8 = 0b0100_1000;
97 pub const OPCODE_U32ASSERT2: u8 = 0b0100_1010;
98 pub const OPCODE_U32ADD3: u8 = 0b0100_1100;
99 pub const OPCODE_U32MADD: u8 = 0b0100_1110;
100
101 pub const OPCODE_HPERM: u8 = 0b0101_0000;
102 pub const OPCODE_MPVERIFY: u8 = 0b0101_0001;
103 pub const OPCODE_PIPE: u8 = 0b0101_0010;
104 pub const OPCODE_MSTREAM: u8 = 0b0101_0011;
105 pub const OPCODE_SPLIT: u8 = 0b0101_0100;
106 pub const OPCODE_LOOP: u8 = 0b0101_0101;
107 pub const OPCODE_SPAN: u8 = 0b0101_0110;
108 pub const OPCODE_JOIN: u8 = 0b0101_0111;
109 pub const OPCODE_DYN: u8 = 0b0101_1000;
110 pub const OPCODE_HORNEREXT: u8 = 0b0101_1001;
111 pub const OPCODE_EMIT: u8 = 0b0101_1010;
112 pub const OPCODE_PUSH: u8 = 0b0101_1011;
113 pub const OPCODE_DYNCALL: u8 = 0b0101_1100;
114
115 pub const OPCODE_MRUPDATE: u8 = 0b0110_0000;
116 pub const OPCODE_HORNERBASE: u8 = 0b0110_0100;
117 pub const OPCODE_SYSCALL: u8 = 0b0110_1000;
118 pub const OPCODE_CALL: u8 = 0b0110_1100;
119 pub const OPCODE_END: u8 = 0b0111_0000;
120 pub const OPCODE_REPEAT: u8 = 0b0111_0100;
121 pub const OPCODE_RESPAN: u8 = 0b0111_1000;
122 pub const OPCODE_HALT: u8 = 0b0111_1100;
123}
124
125// OPERATIONS
126// ================================================================================================
127
128/// A set of native VM operations which take exactly one cycle to execute.
129#[derive(Copy, Clone, Debug, Eq, PartialEq)]
130#[repr(u8)]
131pub enum Operation {
132 // ----- system operations -------------------------------------------------------------------
133 /// Advances cycle counter, but does not change the state of user stack.
134 Noop = OPCODE_NOOP,
135
136 /// Pops the stack; if the popped value is not 1, execution fails.
137 ///
138 /// The internal value specifies an error code associated with the error in case when the
139 /// execution fails.
140 Assert(u32) = OPCODE_ASSERT,
141
142 /// Pops an element off the stack, adds the current value of the `fmp` register to it, and
143 /// pushes the result back onto the stack.
144 FmpAdd = OPCODE_FMPADD,
145
146 /// Pops an element off the stack and adds it to the current value of `fmp` register.
147 FmpUpdate = OPCODE_FMPUPDATE,
148
149 /// Pushes the current depth of the stack onto the stack.
150 SDepth = OPCODE_SDEPTH,
151
152 /// Overwrites the top four stack items with the hash of a function which initiated the current
153 /// SYSCALL. Thus, this operation can be executed only inside a SYSCALL code block.
154 Caller = OPCODE_CALLER,
155
156 /// Pushes the current value of the clock cycle onto the stack. This operation can be used to
157 /// measure the number of cycles it has taken to execute the program up to the current
158 /// instruction.
159 Clk = OPCODE_CLK,
160
161 /// Emits an event id (`u32` value) to the host.
162 ///
163 /// We interpret the event id as follows:
164 /// - 16 most significant bits identify the event source,
165 /// - 16 least significant bits identify the actual event.
166 ///
167 /// Similar to Noop, this operation does not change the state of user stack. The immediate
168 /// value affects the program MAST root computation.
169 Emit(u32) = OPCODE_EMIT,
170
171 // ----- flow control operations -------------------------------------------------------------
172 /// Marks the beginning of a join block.
173 Join = OPCODE_JOIN,
174
175 /// Marks the beginning of a split block.
176 Split = OPCODE_SPLIT,
177
178 /// Marks the beginning of a loop block.
179 Loop = OPCODE_LOOP,
180
181 /// Marks the beginning of a function call.
182 Call = OPCODE_CALL,
183
184 /// Marks the beginning of a dynamic code block, where the target is specified by the stack.
185 Dyn = OPCODE_DYN,
186
187 /// Marks the beginning of a dynamic function call, where the target is specified by the stack.
188 Dyncall = OPCODE_DYNCALL,
189
190 /// Marks the beginning of a kernel call.
191 SysCall = OPCODE_SYSCALL,
192
193 /// Marks the beginning of a span code block.
194 Span = OPCODE_SPAN,
195
196 /// Marks the end of a program block.
197 End = OPCODE_END,
198
199 /// Indicates that body of an executing loop should be executed again.
200 Repeat = OPCODE_REPEAT,
201
202 /// Starts processing a new operation batch.
203 Respan = OPCODE_RESPAN,
204
205 /// Indicates the end of the program. This is used primarily to pad the execution trace to
206 /// the required length. Once HALT operation is executed, no other operations can be executed
207 /// by the VM (HALT operation itself excepted).
208 Halt = OPCODE_HALT,
209
210 // ----- field operations --------------------------------------------------------------------
211 /// Pops two elements off the stack, adds them, and pushes the result back onto the stack.
212 Add = OPCODE_ADD,
213
214 /// Pops an element off the stack, negates it, and pushes the result back onto the stack.
215 Neg = OPCODE_NEG,
216
217 /// Pops two elements off the stack, multiplies them, and pushes the result back onto the
218 /// stack.
219 Mul = OPCODE_MUL,
220
221 /// Pops an element off the stack, computes its multiplicative inverse, and pushes the result
222 /// back onto the stack.
223 Inv = OPCODE_INV,
224
225 /// Pops an element off the stack, adds 1 to it, and pushes the result back onto the stack.
226 Incr = OPCODE_INCR,
227
228 /// Pops two elements off the stack, multiplies them, and pushes the result back onto the
229 /// stack.
230 ///
231 /// If either of the elements is greater than 1, execution fails. This operation is equivalent
232 /// to boolean AND.
233 And = OPCODE_AND,
234
235 /// Pops two elements off the stack and subtracts their product from their sum.
236 ///
237 /// If either of the elements is greater than 1, execution fails. This operation is equivalent
238 /// to boolean OR.
239 Or = OPCODE_OR,
240
241 /// Pops an element off the stack and subtracts it from 1.
242 ///
243 /// If the element is greater than one, the execution fails. This operation is equivalent to
244 /// boolean NOT.
245 Not = OPCODE_NOT,
246
247 /// Pops two elements off the stack and compares them. If the elements are equal, pushes 1
248 /// onto the stack, otherwise pushes 0 onto the stack.
249 Eq = OPCODE_EQ,
250
251 /// Pops an element off the stack and compares it to 0. If the element is 0, pushes 1 onto
252 /// the stack, otherwise pushes 0 onto the stack.
253 Eqz = OPCODE_EQZ,
254
255 /// Computes a single turn of exponent accumulation for the given inputs. This operation can be
256 /// be used to compute a single turn of power of a field element.
257 ///
258 /// The top 4 elements of the stack are expected to be arranged as follows (form the top):
259 /// - least significant bit of the exponent in the previous trace if there's an expacc call,
260 /// otherwise ZERO
261 /// - exponent of base number `a` for this turn
262 /// - accumulated power of base number `a` so far
263 /// - number which needs to be shifted to the right
264 ///
265 /// At the end of the operation, exponent is replaced with its square, current value of power
266 /// of base number `a` on exponent is incorporated into the accumulator and the number is
267 /// shifted to the right by one bit.
268 Expacc = OPCODE_EXPACC,
269
270 // ----- ext2 operations ---------------------------------------------------------------------
271 /// Computes the product of two elements in the extension field of degree 2 and pushes the
272 /// result back onto the stack as the third and fourth elements. Pushes 0 onto the stack as
273 /// the first and second elements.
274 Ext2Mul = OPCODE_EXT2MUL,
275
276 // ----- u32 operations ----------------------------------------------------------------------
277 /// Pops an element off the stack, splits it into upper and lower 32-bit values, and pushes
278 /// these values back onto the stack.
279 U32split = OPCODE_U32SPLIT,
280
281 /// Pops two elements off the stack, adds them, and splits the result into upper and lower
282 /// 32-bit values. Then pushes these values back onto the stack.
283 ///
284 /// If either of these elements is greater than or equal to 2^32, the result of this
285 /// operation is undefined.
286 U32add = OPCODE_U32ADD,
287
288 /// Pops two elements off the stack and checks if each of them represents a 32-bit value.
289 /// If both of them are, they are pushed back onto the stack, otherwise an error is returned.
290 ///
291 /// The internal value specifies an error code associated with the error in case when the
292 /// assertion fails.
293 U32assert2(u32) = OPCODE_U32ASSERT2,
294
295 /// Pops three elements off the stack, adds them together, and splits the result into upper
296 /// and lower 32-bit values. Then pushes the result back onto the stack.
297 U32add3 = OPCODE_U32ADD3,
298
299 /// Pops two elements off the stack and subtracts the first element from the second. Then,
300 /// the result, together with a flag indicating whether subtraction underflowed is pushed
301 /// onto the stack.
302 ///
303 /// If their of the values is greater than or equal to 2^32, the result of this operation is
304 /// undefined.
305 U32sub = OPCODE_U32SUB,
306
307 /// Pops two elements off the stack, multiplies them, and splits the result into upper and
308 /// lower 32-bit values. Then pushes these values back onto the stack.
309 ///
310 /// If their of the values is greater than or equal to 2^32, the result of this operation is
311 /// undefined.
312 U32mul = OPCODE_U32MUL,
313
314 /// Pops two elements off the stack and multiplies them. Then pops the third element off the
315 /// stack, and adds it to the result. Finally, splits the result into upper and lower 32-bit
316 /// values, and pushes them onto the stack.
317 ///
318 /// If any of the three values is greater than or equal to 2^32, the result of this operation
319 /// is undefined.
320 U32madd = OPCODE_U32MADD,
321
322 /// Pops two elements off the stack and divides the second element by the first. Then pushes
323 /// the integer result of the division, together with the remainder, onto the stack.
324 ///
325 /// If their of the values is greater than or equal to 2^32, the result of this operation is
326 /// undefined.
327 U32div = OPCODE_U32DIV,
328
329 /// Pops two elements off the stack, computes their binary AND, and pushes the result back
330 /// onto the stack.
331 ///
332 /// If either of the elements is greater than or equal to 2^32, execution fails.
333 U32and = OPCODE_U32AND,
334
335 /// Pops two elements off the stack, computes their binary XOR, and pushes the result back
336 /// onto the stack.
337 ///
338 /// If either of the elements is greater than or equal to 2^32, execution fails.
339 U32xor = OPCODE_U32XOR,
340
341 // ----- stack manipulation ------------------------------------------------------------------
342 /// Pushes 0 onto the stack.
343 Pad = OPCODE_PAD,
344
345 /// Removes to element from the stack.
346 Drop = OPCODE_DROP,
347
348 /// Pushes a copy of stack element 0 onto the stack.
349 Dup0 = OPCODE_DUP0,
350
351 /// Pushes a copy of stack element 1 onto the stack.
352 Dup1 = OPCODE_DUP1,
353
354 /// Pushes a copy of stack element 2 onto the stack.
355 Dup2 = OPCODE_DUP2,
356
357 /// Pushes a copy of stack element 3 onto the stack.
358 Dup3 = OPCODE_DUP3,
359
360 /// Pushes a copy of stack element 4 onto the stack.
361 Dup4 = OPCODE_DUP4,
362
363 /// Pushes a copy of stack element 5 onto the stack.
364 Dup5 = OPCODE_DUP5,
365
366 /// Pushes a copy of stack element 6 onto the stack.
367 Dup6 = OPCODE_DUP6,
368
369 /// Pushes a copy of stack element 7 onto the stack.
370 Dup7 = OPCODE_DUP7,
371
372 /// Pushes a copy of stack element 9 onto the stack.
373 Dup9 = OPCODE_DUP9,
374
375 /// Pushes a copy of stack element 11 onto the stack.
376 Dup11 = OPCODE_DUP11,
377
378 /// Pushes a copy of stack element 13 onto the stack.
379 Dup13 = OPCODE_DUP13,
380
381 /// Pushes a copy of stack element 15 onto the stack.
382 Dup15 = OPCODE_DUP15,
383
384 /// Swaps stack elements 0 and 1.
385 Swap = OPCODE_SWAP,
386
387 /// Swaps stack elements 0, 1, 2, and 3 with elements 4, 5, 6, and 7.
388 SwapW = OPCODE_SWAPW,
389
390 /// Swaps stack elements 0, 1, 2, and 3 with elements 8, 9, 10, and 11.
391 SwapW2 = OPCODE_SWAPW2,
392
393 /// Swaps stack elements 0, 1, 2, and 3, with elements 12, 13, 14, and 15.
394 SwapW3 = OPCODE_SWAPW3,
395
396 /// Swaps the top two words pair wise.
397 ///
398 /// Input: [D, C, B, A, ...]
399 /// Output: [B, A, D, C, ...]
400 SwapDW = OPCODE_SWAPDW,
401
402 /// Moves stack element 2 to the top of the stack.
403 MovUp2 = OPCODE_MOVUP2,
404
405 /// Moves stack element 3 to the top of the stack.
406 MovUp3 = OPCODE_MOVUP3,
407
408 /// Moves stack element 4 to the top of the stack.
409 MovUp4 = OPCODE_MOVUP4,
410
411 /// Moves stack element 5 to the top of the stack.
412 MovUp5 = OPCODE_MOVUP5,
413
414 /// Moves stack element 6 to the top of the stack.
415 MovUp6 = OPCODE_MOVUP6,
416
417 /// Moves stack element 7 to the top of the stack.
418 MovUp7 = OPCODE_MOVUP7,
419
420 /// Moves stack element 8 to the top of the stack.
421 MovUp8 = OPCODE_MOVUP8,
422
423 /// Moves the top stack element to position 2 on the stack.
424 MovDn2 = OPCODE_MOVDN2,
425
426 /// Moves the top stack element to position 3 on the stack.
427 MovDn3 = OPCODE_MOVDN3,
428
429 /// Moves the top stack element to position 4 on the stack.
430 MovDn4 = OPCODE_MOVDN4,
431
432 /// Moves the top stack element to position 5 on the stack.
433 MovDn5 = OPCODE_MOVDN5,
434
435 /// Moves the top stack element to position 6 on the stack.
436 MovDn6 = OPCODE_MOVDN6,
437
438 /// Moves the top stack element to position 7 on the stack.
439 MovDn7 = OPCODE_MOVDN7,
440
441 /// Moves the top stack element to position 8 on the stack.
442 MovDn8 = OPCODE_MOVDN8,
443
444 /// Pops an element off the stack, and if the element is 1, swaps the top two remaining
445 /// elements on the stack. If the popped element is 0, the stack remains unchanged.
446 ///
447 /// If the popped element is neither 0 nor 1, execution fails.
448 CSwap = OPCODE_CSWAP,
449
450 /// Pops an element off the stack, and if the element is 1, swaps the remaining elements
451 /// 0, 1, 2, and 3 with elements 4, 5, 6, and 7. If the popped element is 0, the stack
452 /// remains unchanged.
453 ///
454 /// If the popped element is neither 0 nor 1, execution fails.
455 CSwapW = OPCODE_CSWAPW,
456
457 // ----- input / output ----------------------------------------------------------------------
458 /// Pushes the immediate value onto the stack.
459 Push(Felt) = OPCODE_PUSH,
460
461 /// Removes the next element from the advice stack and pushes it onto the operand stack.
462 AdvPop = OPCODE_ADVPOP,
463
464 /// Removes a word (4 elements) from the advice stack and overwrites the top four operand
465 /// stack elements with it.
466 AdvPopW = OPCODE_ADVPOPW,
467
468 /// Pops an element off the stack, interprets it as a memory address, and replaces the
469 /// remaining 4 elements at the top of the stack with values located at the specified address.
470 MLoadW = OPCODE_MLOADW,
471
472 /// Pops an element off the stack, interprets it as a memory address, and writes the remaining
473 /// 4 elements at the top of the stack into memory at the specified address.
474 MStoreW = OPCODE_MSTOREW,
475
476 /// Pops an element off the stack, interprets it as a memory address, and pushes the first
477 /// element of the word located at the specified address to the stack.
478 MLoad = OPCODE_MLOAD,
479
480 /// Pops an element off the stack, interprets it as a memory address, and writes the remaining
481 /// element at the top of the stack into the first element of the word located at the specified
482 /// memory address. The remaining 3 elements of the word are not affected.
483 MStore = OPCODE_MSTORE,
484
485 /// Loads two words from memory, and replaces the top 8 elements of the stack with them,
486 /// element-wise, in stack order.
487 ///
488 /// The operation works as follows:
489 /// - The memory address of the first word is retrieved from 13th stack element (position 12).
490 /// - Two consecutive words, starting at this address, are loaded from memory.
491 /// - The top 8 elements of the stack are overwritten with these words (element-wise, in stack
492 /// order).
493 /// - Memory address (in position 12) is incremented by 2.
494 /// - All other stack elements remain the same.
495 MStream = OPCODE_MSTREAM,
496
497 /// Pops two words from the advice stack, writes them to memory, and replaces the top 8
498 /// elements of the stack with them, element-wise, in stack order.
499 ///
500 /// The operation works as follows:
501 /// - Two words are popped from the advice stack.
502 /// - The destination memory address for the first word is retrieved from the 13th stack element
503 /// (position 12).
504 /// - The two words are written to memory consecutively, starting at this address.
505 /// - The top 8 elements of the stack are overwritten with these words (element-wise, in stack
506 /// order).
507 /// - Memory address (in position 12) is incremented by 2.
508 /// - All other stack elements remain the same.
509 Pipe = OPCODE_PIPE,
510
511 // ----- cryptographic operations ------------------------------------------------------------
512 /// Performs a Rescue Prime Optimized permutation on the top 3 words of the operand stack,
513 /// where the top 2 words are the rate (words C and B), the deepest word is the capacity (word
514 /// A), and the digest output is the middle word E.
515 ///
516 /// Stack transition:
517 /// [C, B, A, ...] -> [F, E, D, ...]
518 HPerm = OPCODE_HPERM,
519
520 /// Verifies that a Merkle path from the specified node resolves to the specified root. This
521 /// operation can be used to prove that the prover knows a path in the specified Merkle tree
522 /// which starts with the specified node.
523 ///
524 /// The stack is expected to be arranged as follows (from the top):
525 /// - value of the node, 4 elements.
526 /// - depth of the path, 1 element.
527 /// - index of the node, 1 element.
528 /// - root of the tree, 4 elements.
529 ///
530 /// The Merkle path itself is expected to be provided by the prover non-deterministically (via
531 /// merkle sets). If the prover is not able to provide the required path, the operation fails.
532 /// The state of the stack does not change.
533 ///
534 /// The internal value specifies an error code associated with the error in case when the
535 /// assertion fails.
536 MpVerify(u32) = OPCODE_MPVERIFY,
537
538 /// Computes a new root of a Merkle tree where a node at the specified position is updated to
539 /// the specified value.
540 ///
541 /// The stack is expected to be arranged as follows (from the top):
542 /// - old value of the node, 4 element
543 /// - depth of the node, 1 element
544 /// - index of the node, 1 element
545 /// - current root of the tree, 4 elements
546 /// - new value of the node, 4 element
547 ///
548 /// The Merkle path for the node is expected to be provided by the prover non-deterministically
549 /// via the advice provider. At the end of the operation, the old node value is replaced with
550 /// the new root value, that is computed based on the provided path. Everything else on the
551 /// stack remains the same.
552 ///
553 /// The tree will always be copied into a new instance, meaning the advice provider will keep
554 /// track of both the old and new Merkle trees.
555 MrUpdate = OPCODE_MRUPDATE,
556
557 /// Performs FRI (Fast Reed-Solomon Interactive Oracle Proofs) layer folding by a factor of 4
558 /// for FRI protocol executed in a degree 2 extension of the base field.
559 ///
560 /// This operation:
561 /// - Folds 4 query values (v0, v1), (v2, v3), (v4, v5), (v6, v7) into a single value (ne0, ne1)
562 /// - Computes new value of the domain generator power: poe' = poe^4
563 /// - Increments layer pointer (cptr) by 2
564 /// - Checks that the previous folding was done correctly
565 /// - Shifts the stack to move an item from the overflow table to stack position 15
566 ///
567 /// Stack transition:
568 /// Input: [v7, v6, v5, v4, v3, v2, v1, v0, f_pos, d_seg, poe, pe1, pe0, a1, a0, cptr, ...]
569 /// Output: [t1, t0, s1, s0, df3, df2, df1, df0, poe^2, f_tau, cptr+2, poe^4, f_pos, ne1, ne0,
570 /// eptr, ...] where eptr is moved from the stack overflow table and is the address of the
571 /// final FRI layer.
572 FriE2F4 = OPCODE_FRIE2F4,
573
574 /// Performs 8 steps of the Horner evaluation method on a polynomial with coefficients over
575 /// the base field, i.e., it computes
576 ///
577 /// acc' = (((acc_tmp * alpha + c3) * alpha + c2) * alpha + c1) * alpha + c0
578 ///
579 /// where
580 ///
581 /// acc_tmp := (((acc * alpha + c7) * alpha + c6) * alpha + c5) * alpha + c4
582 ///
583 ///
584 /// In other words, the intsruction computes the evaluation at alpha of the polynomial
585 ///
586 /// P(X) := c7 * X^7 + c6 * X^6 + ... + c1 * X + c0
587 HornerBase = OPCODE_HORNERBASE,
588
589 /// Performs 4 steps of the Horner evaluation method on a polynomial with coefficients over
590 /// the extension field, i.e., it computes
591 ///
592 /// acc' = (((acc * alpha + c3) * alpha + c2) * alpha + c1) * alpha + c0
593 ///
594 /// In other words, the intsruction computes the evaluation at alpha of the polynomial
595 ///
596 /// P(X) := c3 * X^3 + c2 * X^2 + c1 * X + c0
597 HornerExt = OPCODE_HORNEREXT,
598}
599
600impl Operation {
601 pub const OP_BITS: usize = 7;
602
603 /// Returns the opcode of this operation.
604 #[rustfmt::skip]
605 pub fn op_code(&self) -> u8 {
606 // SAFETY: This is safe because we have given this enum a primitive representation with
607 // #[repr(u8)], with the first field of the underlying union-of-structs the discriminant.
608 //
609 // See the section on "accessing the numeric value of the discriminant"
610 // here: https://doc.rust-lang.org/std/mem/fn.discriminant.html
611 unsafe { *<*const _>::from(self).cast::<u8>() }
612 }
613
614 /// Returns an immediate value carried by this operation.
615 pub fn imm_value(&self) -> Option<Felt> {
616 match *self {
617 Self::Push(imm) => Some(imm),
618 Self::Emit(imm) => Some(imm.into()),
619 _ => None,
620 }
621 }
622
623 /// Returns true if this operation writes any data to the decoder hasher registers.
624 ///
625 /// In other words, if so, then the user op helper registers are not available.
626 pub fn populates_decoder_hasher_registers(&self) -> bool {
627 matches!(
628 self,
629 Self::End
630 | Self::Join
631 | Self::Split
632 | Self::Loop
633 | Self::Repeat
634 | Self::Respan
635 | Self::Span
636 | Self::Halt
637 | Self::Call
638 | Self::SysCall
639 )
640 }
641}
642
643impl crate::prettier::PrettyPrint for Operation {
644 fn render(&self) -> crate::prettier::Document {
645 crate::prettier::display(self)
646 }
647}
648
649impl fmt::Display for Operation {
650 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
651 match self {
652 // ----- system operations ------------------------------------------------------------
653 Self::Noop => write!(f, "noop"),
654 Self::Assert(err_code) => write!(f, "assert({err_code})"),
655
656 Self::FmpAdd => write!(f, "fmpadd"),
657 Self::FmpUpdate => write!(f, "fmpupdate"),
658
659 Self::SDepth => write!(f, "sdepth"),
660 Self::Caller => write!(f, "caller"),
661
662 Self::Clk => write!(f, "clk"),
663
664 // ----- flow control operations ------------------------------------------------------
665 Self::Join => write!(f, "join"),
666 Self::Split => write!(f, "split"),
667 Self::Loop => write!(f, "loop"),
668 Self::Call => writeln!(f, "call"),
669 Self::Dyncall => writeln!(f, "dyncall"),
670 Self::SysCall => writeln!(f, "syscall"),
671 Self::Dyn => writeln!(f, "dyn"),
672 Self::Span => write!(f, "span"),
673 Self::End => write!(f, "end"),
674 Self::Repeat => write!(f, "repeat"),
675 Self::Respan => write!(f, "respan"),
676 Self::Halt => write!(f, "halt"),
677
678 // ----- field operations -------------------------------------------------------------
679 Self::Add => write!(f, "add"),
680 Self::Neg => write!(f, "neg"),
681 Self::Mul => write!(f, "mul"),
682 Self::Inv => write!(f, "inv"),
683 Self::Incr => write!(f, "incr"),
684
685 Self::And => write!(f, "and"),
686 Self::Or => write!(f, "or"),
687 Self::Not => write!(f, "not"),
688
689 Self::Eq => write!(f, "eq"),
690 Self::Eqz => write!(f, "eqz"),
691
692 Self::Expacc => write!(f, "expacc"),
693
694 // ----- ext2 operations --------------------------------------------------------------
695 Self::Ext2Mul => write!(f, "ext2mul"),
696
697 // ----- u32 operations ---------------------------------------------------------------
698 Self::U32assert2(err_code) => write!(f, "u32assert2({err_code})"),
699 Self::U32split => write!(f, "u32split"),
700 Self::U32add => write!(f, "u32add"),
701 Self::U32add3 => write!(f, "u32add3"),
702 Self::U32sub => write!(f, "u32sub"),
703 Self::U32mul => write!(f, "u32mul"),
704 Self::U32madd => write!(f, "u32madd"),
705 Self::U32div => write!(f, "u32div"),
706
707 Self::U32and => write!(f, "u32and"),
708 Self::U32xor => write!(f, "u32xor"),
709
710 // ----- stack manipulation -----------------------------------------------------------
711 Self::Drop => write!(f, "drop"),
712 Self::Pad => write!(f, "pad"),
713
714 Self::Dup0 => write!(f, "dup0"),
715 Self::Dup1 => write!(f, "dup1"),
716 Self::Dup2 => write!(f, "dup2"),
717 Self::Dup3 => write!(f, "dup3"),
718 Self::Dup4 => write!(f, "dup4"),
719 Self::Dup5 => write!(f, "dup5"),
720 Self::Dup6 => write!(f, "dup6"),
721 Self::Dup7 => write!(f, "dup7"),
722 Self::Dup9 => write!(f, "dup9"),
723 Self::Dup11 => write!(f, "dup11"),
724 Self::Dup13 => write!(f, "dup13"),
725 Self::Dup15 => write!(f, "dup15"),
726
727 Self::Swap => write!(f, "swap"),
728 Self::SwapW => write!(f, "swapw"),
729 Self::SwapW2 => write!(f, "swapw2"),
730 Self::SwapW3 => write!(f, "swapw3"),
731 Self::SwapDW => write!(f, "swapdw"),
732
733 Self::MovUp2 => write!(f, "movup2"),
734 Self::MovUp3 => write!(f, "movup3"),
735 Self::MovUp4 => write!(f, "movup4"),
736 Self::MovUp5 => write!(f, "movup5"),
737 Self::MovUp6 => write!(f, "movup6"),
738 Self::MovUp7 => write!(f, "movup7"),
739 Self::MovUp8 => write!(f, "movup8"),
740
741 Self::MovDn2 => write!(f, "movdn2"),
742 Self::MovDn3 => write!(f, "movdn3"),
743 Self::MovDn4 => write!(f, "movdn4"),
744 Self::MovDn5 => write!(f, "movdn5"),
745 Self::MovDn6 => write!(f, "movdn6"),
746 Self::MovDn7 => write!(f, "movdn7"),
747 Self::MovDn8 => write!(f, "movdn8"),
748
749 Self::CSwap => write!(f, "cswap"),
750 Self::CSwapW => write!(f, "cswapw"),
751
752 // ----- input / output ---------------------------------------------------------------
753 Self::Push(value) => write!(f, "push({value})"),
754
755 Self::AdvPop => write!(f, "advpop"),
756 Self::AdvPopW => write!(f, "advpopw"),
757
758 Self::MLoadW => write!(f, "mloadw"),
759 Self::MStoreW => write!(f, "mstorew"),
760
761 Self::MLoad => write!(f, "mload"),
762 Self::MStore => write!(f, "mstore"),
763
764 Self::MStream => write!(f, "mstream"),
765 Self::Pipe => write!(f, "pipe"),
766
767 Self::Emit(value) => write!(f, "emit({value})"),
768
769 // ----- cryptographic operations -----------------------------------------------------
770 Self::HPerm => write!(f, "hperm"),
771 Self::MpVerify(err_code) => write!(f, "mpverify({err_code})"),
772 Self::MrUpdate => write!(f, "mrupdate"),
773 Self::FriE2F4 => write!(f, "frie2f4"),
774 Self::HornerBase => write!(f, "horner_eval_base"),
775 Self::HornerExt => write!(f, "horner_eval_ext"),
776 }
777 }
778}
779
780impl Serializable for Operation {
781 fn write_into<W: ByteWriter>(&self, target: &mut W) {
782 target.write_u8(self.op_code());
783
784 // For operations that have extra data, encode it in `data`.
785 match self {
786 Operation::Assert(err_code)
787 | Operation::MpVerify(err_code)
788 | Operation::U32assert2(err_code) => {
789 err_code.write_into(target);
790 },
791 Operation::Push(value) => value.as_int().write_into(target),
792 Operation::Emit(value) => value.write_into(target),
793
794 // Note: we explicitly write out all the operations so that whenever we make a
795 // modification to the `Operation` enum, we get a compile error here. This
796 // should help us remember to properly encode/decode each operation variant.
797 Operation::Noop
798 | Operation::FmpAdd
799 | Operation::FmpUpdate
800 | Operation::SDepth
801 | Operation::Caller
802 | Operation::Clk
803 | Operation::Join
804 | Operation::Split
805 | Operation::Loop
806 | Operation::Call
807 | Operation::Dyn
808 | Operation::Dyncall
809 | Operation::SysCall
810 | Operation::Span
811 | Operation::End
812 | Operation::Repeat
813 | Operation::Respan
814 | Operation::Halt
815 | Operation::Add
816 | Operation::Neg
817 | Operation::Mul
818 | Operation::Inv
819 | Operation::Incr
820 | Operation::And
821 | Operation::Or
822 | Operation::Not
823 | Operation::Eq
824 | Operation::Eqz
825 | Operation::Expacc
826 | Operation::Ext2Mul
827 | Operation::U32split
828 | Operation::U32add
829 | Operation::U32add3
830 | Operation::U32sub
831 | Operation::U32mul
832 | Operation::U32madd
833 | Operation::U32div
834 | Operation::U32and
835 | Operation::U32xor
836 | Operation::Pad
837 | Operation::Drop
838 | Operation::Dup0
839 | Operation::Dup1
840 | Operation::Dup2
841 | Operation::Dup3
842 | Operation::Dup4
843 | Operation::Dup5
844 | Operation::Dup6
845 | Operation::Dup7
846 | Operation::Dup9
847 | Operation::Dup11
848 | Operation::Dup13
849 | Operation::Dup15
850 | Operation::Swap
851 | Operation::SwapW
852 | Operation::SwapW2
853 | Operation::SwapW3
854 | Operation::SwapDW
855 | Operation::MovUp2
856 | Operation::MovUp3
857 | Operation::MovUp4
858 | Operation::MovUp5
859 | Operation::MovUp6
860 | Operation::MovUp7
861 | Operation::MovUp8
862 | Operation::MovDn2
863 | Operation::MovDn3
864 | Operation::MovDn4
865 | Operation::MovDn5
866 | Operation::MovDn6
867 | Operation::MovDn7
868 | Operation::MovDn8
869 | Operation::CSwap
870 | Operation::CSwapW
871 | Operation::AdvPop
872 | Operation::AdvPopW
873 | Operation::MLoadW
874 | Operation::MStoreW
875 | Operation::MLoad
876 | Operation::MStore
877 | Operation::MStream
878 | Operation::Pipe
879 | Operation::HPerm
880 | Operation::MrUpdate
881 | Operation::FriE2F4
882 | Operation::HornerBase
883 | Operation::HornerExt => (),
884 }
885 }
886}
887
888impl Deserializable for Operation {
889 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
890 let op_code = source.read_u8()?;
891
892 let operation = match op_code {
893 OPCODE_NOOP => Self::Noop,
894 OPCODE_EQZ => Self::Eqz,
895 OPCODE_NEG => Self::Neg,
896 OPCODE_INV => Self::Inv,
897 OPCODE_INCR => Self::Incr,
898 OPCODE_NOT => Self::Not,
899 OPCODE_FMPADD => Self::FmpAdd,
900 OPCODE_MLOAD => Self::MLoad,
901 OPCODE_SWAP => Self::Swap,
902 OPCODE_CALLER => Self::Caller,
903 OPCODE_MOVUP2 => Self::MovUp2,
904 OPCODE_MOVDN2 => Self::MovDn2,
905 OPCODE_MOVUP3 => Self::MovUp3,
906 OPCODE_MOVDN3 => Self::MovDn3,
907 OPCODE_ADVPOPW => Self::AdvPopW,
908 OPCODE_EXPACC => Self::Expacc,
909
910 OPCODE_MOVUP4 => Self::MovUp4,
911 OPCODE_MOVDN4 => Self::MovDn4,
912 OPCODE_MOVUP5 => Self::MovUp5,
913 OPCODE_MOVDN5 => Self::MovDn5,
914 OPCODE_MOVUP6 => Self::MovUp6,
915 OPCODE_MOVDN6 => Self::MovDn6,
916 OPCODE_MOVUP7 => Self::MovUp7,
917 OPCODE_MOVDN7 => Self::MovDn7,
918 OPCODE_SWAPW => Self::SwapW,
919 OPCODE_EXT2MUL => Self::Ext2Mul,
920 OPCODE_MOVUP8 => Self::MovUp8,
921 OPCODE_MOVDN8 => Self::MovDn8,
922 OPCODE_SWAPW2 => Self::SwapW2,
923 OPCODE_SWAPW3 => Self::SwapW3,
924 OPCODE_SWAPDW => Self::SwapDW,
925
926 OPCODE_ASSERT => {
927 let err_code = source.read_u32()?;
928 Self::Assert(err_code)
929 },
930 OPCODE_EQ => Self::Eq,
931 OPCODE_ADD => Self::Add,
932 OPCODE_MUL => Self::Mul,
933 OPCODE_AND => Self::And,
934 OPCODE_OR => Self::Or,
935 OPCODE_U32AND => Self::U32and,
936 OPCODE_U32XOR => Self::U32xor,
937 OPCODE_FRIE2F4 => Self::FriE2F4,
938 OPCODE_DROP => Self::Drop,
939 OPCODE_CSWAP => Self::CSwap,
940 OPCODE_CSWAPW => Self::CSwapW,
941 OPCODE_MLOADW => Self::MLoadW,
942 OPCODE_MSTORE => Self::MStore,
943 OPCODE_MSTOREW => Self::MStoreW,
944 OPCODE_FMPUPDATE => Self::FmpUpdate,
945
946 OPCODE_PAD => Self::Pad,
947 OPCODE_DUP0 => Self::Dup0,
948 OPCODE_DUP1 => Self::Dup1,
949 OPCODE_DUP2 => Self::Dup2,
950 OPCODE_DUP3 => Self::Dup3,
951 OPCODE_DUP4 => Self::Dup4,
952 OPCODE_DUP5 => Self::Dup5,
953 OPCODE_DUP6 => Self::Dup6,
954 OPCODE_DUP7 => Self::Dup7,
955 OPCODE_DUP9 => Self::Dup9,
956 OPCODE_DUP11 => Self::Dup11,
957 OPCODE_DUP13 => Self::Dup13,
958 OPCODE_DUP15 => Self::Dup15,
959 OPCODE_ADVPOP => Self::AdvPop,
960 OPCODE_SDEPTH => Self::SDepth,
961 OPCODE_CLK => Self::Clk,
962
963 OPCODE_U32ADD => Self::U32add,
964 OPCODE_U32SUB => Self::U32sub,
965 OPCODE_U32MUL => Self::U32mul,
966 OPCODE_U32DIV => Self::U32div,
967 OPCODE_U32SPLIT => Self::U32split,
968 OPCODE_U32ASSERT2 => {
969 let err_code = source.read_u32()?;
970
971 Self::U32assert2(err_code)
972 },
973 OPCODE_U32ADD3 => Self::U32add3,
974 OPCODE_U32MADD => Self::U32madd,
975
976 OPCODE_HPERM => Self::HPerm,
977 OPCODE_MPVERIFY => {
978 let err_code = source.read_u32()?;
979
980 Self::MpVerify(err_code)
981 },
982 OPCODE_PIPE => Self::Pipe,
983 OPCODE_MSTREAM => Self::MStream,
984 OPCODE_SPLIT => Self::Split,
985 OPCODE_LOOP => Self::Loop,
986 OPCODE_SPAN => Self::Span,
987 OPCODE_JOIN => Self::Join,
988 OPCODE_DYN => Self::Dyn,
989 OPCODE_DYNCALL => Self::Dyncall,
990 OPCODE_HORNERBASE => Self::HornerBase,
991 OPCODE_HORNEREXT => Self::HornerExt,
992
993 OPCODE_MRUPDATE => Self::MrUpdate,
994 OPCODE_PUSH => {
995 let value_u64 = source.read_u64()?;
996 let value_felt = Felt::try_from(value_u64).map_err(|_| {
997 DeserializationError::InvalidValue(format!(
998 "Operation associated data doesn't fit in a field element: {value_u64}"
999 ))
1000 })?;
1001
1002 Self::Push(value_felt)
1003 },
1004 OPCODE_EMIT => {
1005 let value = source.read_u32()?;
1006
1007 Self::Emit(value)
1008 },
1009 OPCODE_SYSCALL => Self::SysCall,
1010 OPCODE_CALL => Self::Call,
1011 OPCODE_END => Self::End,
1012 OPCODE_REPEAT => Self::Repeat,
1013 OPCODE_RESPAN => Self::Respan,
1014 OPCODE_HALT => Self::Halt,
1015 _ => {
1016 return Err(DeserializationError::InvalidValue(format!(
1017 "Invalid opcode '{op_code}'"
1018 )));
1019 },
1020 };
1021
1022 Ok(operation)
1023 }
1024}