Skip to main content

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 a=1a = 1, removes it from the stack.
Fails if a1a \ne 1
assertz
- (2 cycles)
[a, ...][...]If a=0a = 0, removes it from the stack,
Fails if a0a \ne 0
assert_eq
- (2 cycles)
[b, a, ...][...]If a=ba = b, removes them from the stack.
Fails if aba \ne b
assert_eqw
- (11 cycles)
[B, A, ...][...]If A=BA = B, removes them from the stack.
Fails if ABA \ne B

Arithmetic and Boolean operations

InstructionStack_inputStack_outputNotes
add
- (1 cycle)
add.b
- (1-2 cycle)
[b, a, ...][c, ...]c(a+b)modpc \leftarrow (a + b) \mod p
sub
- (2 cycles)
sub.b
- (2 cycles)
[b, a, ...][c, ...]c(ab)modpc \leftarrow (a - b) \mod p
mul
- (1 cycle)
mul.b
- (2 cycles)
[b, a, ...][c, ...]c(ab)modpc \leftarrow (a \cdot b) \mod p
div
- (2 cycles)
div.b
- (2 cycles)
[b, a, ...][c, ...]c(ab1)modpc \leftarrow (a \cdot b^{-1}) \mod p
Fails if b=0b = 0
neg
- (1 cycle)
[a, ...][b, ...]bamodpb \leftarrow -a \mod p
inv
- (1 cycle)
[a, ...][b, ...]ba1modpb \leftarrow a^{-1} \mod p
Fails if a=0a = 0
pow2
- (16 cycles)
[a, ...][b, ...]b2ab \leftarrow 2^a
Fails if a>63a > 63
exp.uxx
- (9 + xx cycles)
exp.b
- (9 + log2(b) cycles)
[b, a, ...][c, ...]cabc \leftarrow a^b
Fails if xx is outside [0, 63)
exp is equivalent to exp.u64 and needs 73 cycles
not
- (1 cycle)
[a, ...][b, ...]b1ab \leftarrow 1 - a
Fails if a>1a > 1
and
- (1 cycle)
[b, a, ...][c, ...]cabc \leftarrow a \cdot b
Fails if max(a,b)>1max(a, b) > 1
or
- (1 cycle)
[b, a, ...][c, ...]ca+babc \leftarrow a + b - a \cdot b
Fails if max(a,b)>1max(a, b) > 1
xor
- (7 cycles)
[b, a, ...][c, ...]ca+b2abc \leftarrow a + b - 2 \cdot a \cdot b
Fails if max(a,b)>1max(a, b) > 1

Comparison operations

InstructionStack_inputStack_outputNotes
eq
- (1 cycle)
eq.b
- (1-2 cycles)
[b, a, ...][c, ...]c{1,if a=b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a=b \\ 0, & \text{otherwise}\ \end{cases}
neq
- (2 cycle)
neq.b
- (2-3 cycles)
[b, a, ...][c, ...]c{1,if ab0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \ne b \\ 0, & \text{otherwise}\ \end{cases}
lt
- (17 cycles)
[b, a, ...][c, ...]c{1,if a<b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a < b \\ 0, & \text{otherwise}\ \end{cases}
lte
- (18 cycles)
[b, a, ...][c, ...]c{1,if ab0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \le b \\ 0, & \text{otherwise}\ \end{cases}
gt
- (18 cycles)
[b, a, ...][c, ...]c{1,if a>b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a > b \\ 0, & \text{otherwise}\ \end{cases}
gte
- (19 cycles)
[b, a, ...][c, ...]c{1,if ab0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \ge b \\ 0, & \text{otherwise}\ \end{cases}
is_odd
- (5 cycles)
[a, ...][b, ...]b{1,if a is odd0,otherwise b \leftarrow \begin{cases} 1, & \text{if}\ a \text{ is odd} \\ 0, & \text{otherwise}\ \end{cases}
eqw
- (15 cycles)
[A, B, ...][c, A, B, ...]c{1,if ai=bi  i{0,1,2,3}0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a_i = b_i \; \forall i \in \{0, 1, 2, 3\} \\ 0, & \text{otherwise}\ \end{cases}

Extension Field Operations

InstructionStack InputStack OutputNotes
ext2add
- (5 cycles)
[b, a, ...][c, ...]c(a+b)modqc \leftarrow (a + b) \mod q
ext2sub
- (7 cycles)
[b, a, ...][c, ...]c(ab)modqc \leftarrow (a - b) \mod q
ext2mul
- (3 cycles)
[b, a, ...][c, ...]c(ab)modqc \leftarrow (a \cdot b) \mod q
ext2div
- (11 cycles)
[b, a, ...][c, ...]c(ab1)modqc \leftarrow (a \cdot b^{-1}) \mod q
Fails if b=0b = 0
ext2neg
- (4 cycles)
[a, ...][b, ...]bamodqb \leftarrow -a \mod q
ext2inv
- (8 cycles)
[a, ...][b, ...]ba1modqb \leftarrow a^{-1} \mod q
Fails if a=0a = 0

where qq is an irreducible polynomial x2x+2x^2 - x + 2 over FpF_p for p=264232+1p = 2^{64} - 2^{32} + 1