Skip to main content
Version: 0.16

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​

InstructionStack_inputStack_outputNotes
assert
- (1 cycle)
[a, ...][...]If a=1a = 1, removes it from the stack.
Fails if a≠1a \ne 1
assertz
- (2 cycles)
[a, ...][...]If a=0a = 0, removes it from the stack,
Fails if a≠0a \ne 0
assert_eq
- (2 cycles)
[b, a, ...][...]If a=ba = b, removes them from the stack.
Fails if a≠ba \ne b
assert_eqw
- (11 cycles)
[B, A, ...][...]If A=BA = B, removes them from the stack.
Fails if A≠BA \ne B

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 00 is assumed.

Arithmetic and Boolean operations​

The arithmetic operations below are performed in a 64-bit prime field defined by modulus p=264−232+1p = 2^{64} - 2^{32} + 1. This means that overflow happens after a value exceeds pp.

Warning: div performs field division, not integer floor division. Field division computes a⋅b−1 mod pa \cdot b^{-1} \bmod p, which bears no relation to ⌊a/b⌋\lfloor a / b \rfloor and will produce unexpected results for most integer use cases. For integer floor division, use u32div instead.

InstructionStack_inputStack_outputNotes
add
- (1 cycle)
add.b
- (1-2 cycle)
[b, a, ...][c, ...]c←(a+b)mod  pc \leftarrow (a + b) \mod p
sub
- (2 cycles)
sub.b
- (2 cycles)
[b, a, ...][c, ...]c←(a−b)mod  pc \leftarrow (a - b) \mod p
mul
- (1 cycle)
mul.b
- (2 cycles)
[b, a, ...][c, ...]c←(a⋅b)mod  pc \leftarrow (a \cdot b) \mod p
div
- (2 cycles)
div.b
- (2 cycles)
[b, a, ...][c, ...]c←(a⋅b−1)mod  pc \leftarrow (a \cdot b^{-1}) \mod p
Fails if b=0b = 0
Note: this is field division, not integer floor division. Use u32div for floor division.
neg
- (1 cycle)
[a, ...][b, ...]b←−amod  pb \leftarrow -a \mod p
inv
- (1 cycle)
[a, ...][b, ...]b←a−1mod  pb \leftarrow a^{-1} \mod p
Fails if a=0a = 0
pow2
- (16 cycles)
[a, ...][b, ...]b←2ab \leftarrow 2^a
Fails if a>63a > 63
exp.uxx
- (9 + xx cycles)
exp.b
- (see notes)
[b, a, ...][c, ...]c←abc \leftarrow a^b
Fails if xx is outside [0, 63]
exp is equivalent to exp.u64 and needs 73 cycles
exp.b needs 3, 1, 2, 4, 6, 8, 10, 12 cycles for b=0,1,…,7b = 0, 1, \ldots, 7, and 11+⌊log2(b)⌋11 + \lfloor log_2(b) \rfloor cycles for b>7b > 7
ilog2
- (70 cycles)
[a, ...][b, ...]b←⌊log2a⌋b \leftarrow \lfloor{log_2{a}}\rfloor
Fails if a=0a = 0
not
- (1 cycle)
[a, ...][b, ...]b←1−ab \leftarrow 1 - a
Fails if a>1a > 1
and
- (1 cycle)
[b, a, ...][c, ...]c←a⋅bc \leftarrow a \cdot b
Fails if max(a,b)>1max(a, b) > 1
or
- (1 cycle)
[b, a, ...][c, ...]c←a+b−a⋅bc \leftarrow a + b - a \cdot b
Fails if max(a,b)>1max(a, b) > 1
xor
- (7 cycles)
[b, a, ...][c, ...]c←a+b−2⋅a⋅bc \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 a≠b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \ne b 0, & \text{otherwise}\ \end{cases}
lt
- (17 cycles)
lt.b
- (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}
lte
- (18 cycles)
lte.b
- (19 cycles)
[b, a, ...][c, ...]c←{1,if a≤b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \le b 0, & \text{otherwise}\ \end{cases}
gt
- (16 cycles)
gt.b
- (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}
gte
- (17 cycles)
gte.b
- (18 cycles)
[b, a, ...][c, ...]c←{1,if a≥b0,otherwise c \leftarrow \begin{cases} 1, & \text{if}\ a \ge b 0, & \text{otherwise}\ \end{cases}
is_odd
- (6 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​

All operations in this section are defined over the quadratic extension field Fp[x]/(x2−7)\mathbb{F}_p[x] / (x^2 - 7), with modulus p=264−232+1p = 2^{64} - 2^{32} + 1.

InstructionStack_inputStack_outputNotes
ext2add
- (5 cycles)
[b0, b1, a0, a1, ...][c0, c1, ...]c0←(a0+b0)mod  pc0 \leftarrow (a0 + b0) \mod p and
c1←(a1+b1)mod  pc1 \leftarrow (a1 + b1) \mod p
ext2sub
- (7 cycles)
[b0, b1, a0, a1, ...][c0, c1, ...]c0←(a0−b0)mod  pc0 \leftarrow (a0 - b0) \mod p and
c1←(a1−b1)mod  pc1 \leftarrow (a1 - b1) \mod p
ext2mul
- (3 cycles)
[b0, b1, a0, a1, ...][c0, c1, ...]c0←a0b0−7a1b1mod  pc0 \leftarrow a0b0 - 7a1b1 \mod p and
c1←a0b0+a1b0mod  pc1 \leftarrow a0b0 + a1b0 \mod p
ext2neg
- (4 cycles)
[a0, a1, ...][a0', a1', ...]a0′←−a0a0' \leftarrow -a0 and a1′←−a1a1' \leftarrow -a1
ext2inv
- (11 cycles)
[a0, a1, ...][a0', a1', ...]a′←a−1a' \leftarrow a^{-1} in Fp[x]/(x2−7)\mathbb{F}_p[x]/(x^2 - 7)
Fails if a=0a = 0
ext2div
- (14 cycles)
[b0, b1, a0, a1, ...][c0, c1,]c←a⋅b−1c \leftarrow a \cdot b^{-1} in Fp[x]/(x2−7)\mathbb{F}_p[x]/(x^2 - 7)
Fails if b=0b=0