Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

InstructionStack_inputStack_outputNotes
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 . Also, the result of divisions may appear counter-intuitive because divisions are defined via inversions.

InstructionStack_inputStack_outputNotes
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

InstructionStack_inputStack_outputNotes
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

InstructionStack_inputStack_outputNotes
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