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 code which can be any 32-bit value specified either directly or via a named constant. For example:
assert.err=123
assert.err=MY_CONSTANT
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 . Also, the result of divisions may appear counter-intuitive because divisions are defined via inversions.
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 |
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 - (44 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 - (14 cycles) lt.b - (15 cycles) | [b, a, ...] | [c, ...] | |
lte - (15 cycles) lte.b - (16 cycles) | [b, a, ...] | [c, ...] | |
gt - (15 cycles) gt.b - (16 cycles) | [b, a, ...] | [c, ...] | |
gte - (16 cycles) gte.b - (17 cycles) | [b, a, ...] | [c, ...] | |
is_odd - (5 cycles) | [a, ...] | [b, ...] | |
eqw - (15 cycles) | [A, B, ...] | [c, A, B, ...] |
Extension Field Operations
Instruction | Stack_input | Stack_output | Notes |
---|---|---|---|
ext2add - (5 cycles) | [b1, b0, a1, a0, ...] | [c1, c0, ...] | and |
ext2sub - (7 cycles) | [b1, b0, a1, a0, ...] | [c1, c0, ...] | and |
ext2mul - (3 cycles) | [b1, b0, a1, a0, ...] | [c1, c0, ...] | and |
ext2neg - (4 cycles) | [a1, a0, ...] | [a1', a0', ...] | and |
ext2inv - (8 cycles) | [a1, a0, ...] | [a1', a0', ...] | Fails if |
ext2div - (11 cycles) | [b1, b0, a1, a0, ...] | [c1, c0,] | fails if , where multiplication and inversion are as defined by the operations above |