Field Operations
Field operations
Miden assembly provides a set of instructions which can perform operations with raw field elements. These instructions are described in the tables below.
While most operations place no restrictions on inputs, some operations expect inputs to be binary values, and fail if executed with non-binary inputs.
For instructions where one or more operands can be provided as immediate parameters (e.g., add and add.b), we provide stack transition diagrams only for the non-immediate version. For the immediate version, it can be assumed that the operand with the specified name is not present on the stack.
Assertions and tests
| Instruction | Stack_input | Stack_output | Notes |
|---|---|---|---|
| assert - (1 cycle) | [a, ...] | [...] | If , removes it from the stack. Fails if |
| assertz - (2 cycles) | [a, ...] | [...] | If , removes it from the stack, Fails if |
| assert_eq - (2 cycles) | [b, a, ...] | [...] | If , removes them from the stack. Fails if |
| assert_eqw - (11 cycles) | [B, A, ...] | [...] | If , removes them from the stack. Fails if |
The above instructions can also be parametrized with an error message which can be specified either directly or via a named constant. For example:
u32assert.err="Division by zero"
u32assert.err=MY_CONSTANT
The message is hashed and turned into a field element. If the error code is omitted, the default value of is assumed.
Arithmetic and Boolean operations
The arithmetic operations below are performed in a 64-bit prime field defined by modulus . This means that overflow happens after a value exceeds .
Warning:
divperforms field division, not integer floor division. Field division computes , which bears no relation to and will produce unexpected results for most integer use cases. For integer floor division, useu32divinstead.
| Instruction | Stack_input | Stack_output | Notes |
|---|---|---|---|
| add - (1 cycle) add.b - (1-2 cycle) | [b, a, ...] | [c, ...] | |
| sub - (2 cycles) sub.b - (2 cycles) | [b, a, ...] | [c, ...] | |
| mul - (1 cycle) mul.b - (2 cycles) | [b, a, ...] | [c, ...] | |
| div - (2 cycles) div.b - (2 cycles) | [b, a, ...] | [c, ...] | Fails if Note: this is field division, not integer floor division. Use u32div for floor division. |
| neg - (1 cycle) | [a, ...] | [b, ...] | |
| inv - (1 cycle) | [a, ...] | [b, ...] | Fails if |
| pow2 - (16 cycles) | [a, ...] | [b, ...] | Fails if |
| exp.uxx - (9 + xx cycles) exp.b - (9 + log2(b) cycles) | [b, a, ...] | [c, ...] | Fails if xx is outside [0, 63) exp is equivalent to exp.u64 and needs 73 cycles |
| ilog2 - (66 cycles) | [a, ...] | [b, ...] | Fails if |
| not - (1 cycle) | [a, ...] | [b, ...] | Fails if |
| and - (1 cycle) | [b, a, ...] | [c, ...] | Fails if |
| or - (1 cycle) | [b, a, ...] | [c, ...] | Fails if |
| xor - (7 cycles) | [b, a, ...] | [c, ...] | Fails if |
Comparison operations
| Instruction | Stack_input | Stack_output | Notes |
|---|---|---|---|
| eq - (1 cycle) eq.b - (1-2 cycles) | [b, a, ...] | [c, ...] | |
| neq - (2 cycle) neq.b - (2-3 cycles) | [b, a, ...] | [c, ...] | |
| lt - (17 cycles) lt.b - (18 cycles) | [b, a, ...] | [c, ...] | |
| lte - (18 cycles) lte.b - (19 cycles) | [b, a, ...] | [c, ...] | |
| gt - (16 cycles) gt.b - (17 cycles) | [b, a, ...] | [c, ...] | |
| gte - (17 cycles) gte.b - (18 cycles) | [b, a, ...] | [c, ...] | |
| is_odd - (6 cycles) | [a, ...] | [b, ...] | |
| eqw - (15 cycles) | [A, B, ...] | [c, A, B, ...] |
Extension Field Operations
All operations in this section are defined over the quadratic extension field , with modulus .
| Instruction | Stack_input | Stack_output | Notes |
|---|---|---|---|
| ext2add - (5 cycles) | [b0, b1, a0, a1, ...] | [c0, c1, ...] | and |
| ext2sub - (7 cycles) | [b0, b1, a0, a1, ...] | [c0, c1, ...] | and |
| ext2mul - (3 cycles) | [b0, b1, a0, a1, ...] | [c0, c1, ...] | and |
| ext2neg - (4 cycles) | [a0, a1, ...] | [a0', a1', ...] | and |
| ext2inv - (8 cycles) | [a0, a1, ...] | [a0', a1', ...] | in Fails if |
| ext2div - (11 cycles) | [b0, b1, a0, a1, ...] | [c0, c1,] | in Fails if |