Skip to main content
Version: 0.11 (stable)

Field Operations

In this section we describe the AIR constraints for Miden VM field operations (i.e., arithmetic operations over field elements).

ADD

Assume aa and bb are the elements at the top of the stack. The ADD operation computes c(a+b)c \leftarrow (a + b). The diagram below illustrates this graphically.

add

Stack transition for this operation must satisfy the following constraints:

s0(s0+s1)=0 | degree=1s_0' - (s_0 + s_1) = 0 \text{ | degree} = 1

The effect on the rest of the stack is:

  • Left shift starting from position 22.

NEG

Assume aa is the element at the top of the stack. The NEG operation computes b(a)b \leftarrow (-a). The diagram below illustrates this graphically.

neg

Stack transition for this operation must satisfy the following constraints:

s0+s0=0 | degree=1s_0' + s_0 = 0 \text{ | degree} = 1

The effect on the rest of the stack is:

  • No change starting from position 11.

MUL

Assume aa and bb are the elements at the top of the stack. The MUL operation computes c(ab)c \leftarrow (a \cdot b). The diagram below illustrates this graphically.

mul

Stack transition for this operation must satisfy the following constraints:

s0s0s1=0 | degree=2s_0' - s_0 \cdot s_1 = 0 \text{ | degree} = 2

The effect on the rest of the stack is:

  • Left shift starting from position 22.

INV

Assume aa is the element at the top of the stack. The INV operation computes b(a1)b \leftarrow (a^{-1}). The diagram below illustrates this graphically.

inv

Stack transition for this operation must satisfy the following constraints:

1s0s0=0 | degree=21 - s_0' \cdot s_0 = 0 \text{ | degree} = 2

Note that the above constraint can be satisfied only if the value in s00s_0 \neq 0.

The effect on the rest of the stack is:

  • No change starting from position 11.

INCR

Assume aa is the element at the top of the stack. The INCR operation computes b(a+1)b \leftarrow (a+1). The diagram below illustrates this graphically.

incr

Stack transition for this operation must satisfy the following constraints:

s0(s0+1)=0 | degree=1s_0' - (s_0 + 1) = 0 \text{ | degree} = 1

The effect on the rest of the stack is:

  • No change starting from position 11.

NOT

Assume aa is a binary value at the top of the stack. The NOT operation computes b(¬a)b \leftarrow (\lnot a). The diagram below illustrates this graphically.

not

Stack transition for this operation must satisfy the following constraints:

s02s0=0 | degree=2s_0^2 - s_0 = 0 \text{ | degree} = 2 s0(1s0)=0 | degree=1s_0' - (1 - s_0) = 0 \text{ | degree} = 1

The first constraint ensures that the value in s0s_0 is binary, and the second constraint ensures the correctness of the boolean NOT operation.

The effect on the rest of the stack is:

  • No change starting from position 11.

AND

Assume aa and bb are binary values at the top of the stack. The AND operation computes c(ab)c \leftarrow (a \land b). The diagram below illustrates this graphically.

and

Stack transition for this operation must satisfy the following constraints:

si2si=0 for i{0,1} | degree=2s_i^2 - s_i = 0 \text{ for } i \in \{0, 1\} \text{ | degree} = 2 s0s0s1=0 | degree=2s_0' - s_0 \cdot s_1 = 0 \text{ | degree} = 2

The first two constraints ensure that the value in s0s_0 and s1s_1 are binary, and the third constraint ensures the correctness of the boolean AND operation.

The effect on the rest of the stack is:

  • Left shift starting from position 22.

OR

Assume aa and bb are binary values at the top of the stack. The OR operation computes c(ab)c \leftarrow (a \lor b) The diagram below illustrates this graphically.

or

Stack transition for this operation must satisfy the following constraints:

si2si=0 for i{0,1} | degree=2s_i^2 - s_i = 0 \text{ for } i \in \{0, 1\} \text{ | degree} = 2 s0(s1+s0s1s0)=0 | degree=2s_{0}' - (s_{1} + s_{0} - s_{1} \cdot s_{0}) = 0 \text{ | degree} = 2

The first two constraints ensure that the value in s0s_0 and s1s_1 are binary, and the third constraint ensures the correctness of the boolean OR operation.

The effect on the rest of the stack is:

  • Left shift starting from position 22.

EQ

Assume aa and bb are the elements at the top of the stack. The EQ operation computes cc such that c=1c = 1 if a=ba = b, and 00 otherwise. The diagram below illustrates this graphically.

eq

Stack transition for this operation must satisfy the following constraints:

s0(s0s1)=0 | degree=2s_0' \cdot (s_0 - s_1) = 0 \text{ | degree} = 2 s0(1(s0s1)h0)=0 | degree=2s_0' - (1 - (s_0 - s_1) \cdot h_0) = 0 \text{ | degree} = 2

To satisfy the above constraints, the prover must populate the value of helper register h0h_0 as follows:

  • If s0s1s_0 \neq s_1, set h0=1s0s1h_0 = \frac{1}{s_0 - s_1}.
  • Otherwise, set h0h_0 to any value (e.g., 00).

The effect on the rest of the stack is:

  • Left shift starting from position 22.

EQZ

Assume aa is the element at the top of the stack. The EQZ operation computes bb such that b=1b = 1 if a=0a = 0, and 00 otherwise. The diagram below illustrates this graphically.

eqz

Stack transition for this operation must satisfy the following constraints:

s0s0=0 | degree=2s_0' \cdot s_0 = 0 \text{ | degree} = 2 s0(1s0h0)=0 | degree=2s_0' - (1 - s_0 \cdot h_0) = 0 \text{ | degree} = 2

To satisfy the above constraints, the prover must populate the value of helper register h0h_0 as follows:

  • If s00s_0 \neq 0, set h0=1s0h_0 = \frac{1}{s_0}.
  • Otherwise, set h0h_0 to any value (e.g., 00).

The effect on the rest of the stack is:

  • No change starting from position 11.

EXPACC

The EXPACC operation computes one round of the expression baseexpbase^{exp}. It is expected that Expacc is called at least num_exp_bits times, where num_exp_bits is the number of bits required to represent exp.

It pops 44 elements from the top of the stack, performs a single round of exponent aggregation, and pushes the resulting 44 values onto the stack. The diagram below illustrates this graphically.

expacc

Expacc is based on the observation that the exponentiation of a number can be computed by repeatedly squaring the base and multiplying those powers of the base by the accumulator, for the powers of the base which correspond to the exponent's bits which are set to 1.

For example, take b5=(b2)2bb^5 = (b^2)^2 \cdot b. Over the course of 3 iterations (55 is 101101 in binary), the algorithm will compute bb, b2b^2 and b4b^4 (placed in base_acc). Hence, we want to multiply base_acc in acc when baseacc=bbase_acc = b and when baseacc=b4base_acc = b^4, which occurs on the first and third iterations (corresponding to the 11 bits in the binary representation of 5).

Stack transition for this operation must satisfy the following constraints:

bit' should be a binary.

s02s0=0 | degree=2s_0'^{2} - s_0' = 0 \text{ | degree} = 2

The base in the next frame should be the square of the base in the current frame.

s1s12=0 | degree=2s_1' - s_1^{2} = 0 \text{ | degree} = 2

The value val in the helper register is computed correctly using the bit and exp in next and current frame respectively.

h0((s11)s0+1)=0 | degree=2h_0 - ((s_1 - 1) * s_0' + 1) = 0 \text{ | degree} = 2

The acc in the next frame is the product of val and acc in the current frame.

s2s2h0=0 | degree=2s_2' - s_2 * h_0 = 0 \text{ | degree} = 2

exp in the next frame is half of exp in the current frame (accounting for even/odd).

s3(s32+s0)=0 | degree=1s_3' - (s_3 * 2 + s_0') = 0 \text{ | degree} = 1

The effect on the rest of the stack is:

  • No change starting from position 44.

EXT2MUL

The EXT2MUL operation pops top 44 values from the top of the stack, performs multiplication between the two extension field elements, and pushes the resulting 44 values onto the stack. The diagram below illustrates this graphically.

ext2mul

Stack transition for this operation must satisfy the following constraints:

The first stack element should be unchanged in the next frame.

s0s0=0 | degree=1s_0' - s_0 = 0 \text{ | degree} = 1

The second stack element should be unchanged in the next frame.

s1s1=0 | degree=1s_1' - s_1 = 0 \text{ | degree} = 1

The third stack element should satisfy the following constraint.

s2(s0+s1)(s2+s3)+s0s2=0 | degree=2s_2' - (s_0 + s_1) \cdot (s_2 + s_3) + s_0 \cdot s_2 = 0 \text{ | degree} = 2

The fourth stack element should satisfy the following constraint.

s3s1s3+2s0s2=0 | degree=2s_3' - s_1 \cdot s_3 + 2 \cdot s_0 \cdot s_2 = 0 \text{ | degree} = 2

The effect on the rest of the stack is:

  • No change starting from position 44.