Skip to main content
Version: 0.11 (stable)

Stack Manipulation

In this section we describe the AIR constraints for Miden VM stack manipulation operations.

PAD

The PAD operation pushes a 00 onto the stack. The diagram below illustrates this graphically.

pad

Stack transition for this operation must satisfy the following constraints:

s0=0 | degree=1s_{0}' = 0 \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • Right shift starting from position 00.

DROP

The DROP operation removes an element from the top of the stack. The diagram below illustrates this graphically.

drop

The DROP operation shifts the stack by 11 element to the left, but does not impose any additional constraints. The degree of left shift constraints is 11.

The effect of this operation on the rest of the stack is:

  • Left shift starting from position 11.

DUP(n)

The DUP(n) operations push a copy of the nn-th stack element onto the stack. Eg. DUP (same as DUP0) pushes a copy of the top stack element onto the stack. Similarly, DUP5 pushes a copy of the 66-th stack element onto the stack. This operation is valid for n{0,...,7,9,11,13,15}n \in \{0, ..., 7, 9, 11, 13, 15\}. The diagram below illustrates this graphically.

dupn

Stack transition for this operation must satisfy the following constraints:

s0sn=0 for n{0,...,7,9,11,13,15} | degree=1s_{0}' - s_{n} = 0 \text{ for } n \in \{0, ..., 7, 9, 11, 13, 15\} \text{ | degree} = 1

where nn is the depth of the stack from where the element has been copied.

The effect of this operation on the rest of the stack is:

  • Right shift starting from position 00.

SWAP

The SWAP operations swaps the top two elements of the stack. The diagram below illustrates this graphically.

swap

Stack transition for this operation must satisfy the following constraints:

s0s1=0 | degree=1s_{0}' - s_{1} = 0 \text{ | degree} = 1 s1s0=0 | degree=1s_{1}' - s_{0} = 0 \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • No change starting from position 22.

SWAPW

The SWAPW operation swaps stack elements 0,1,2,30, 1, 2, 3 with elements 4,5,6,74, 5, 6, 7. The diagram below illustrates this graphically.

swapw

Stack transition for this operation must satisfy the following constraints:

sisi+4=0 for i{0,1,2,3} | degree=1s_{i}' - s_{i+4} = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1 si+4si=0 for i{0,1,2,3} | degree=1s_{i + 4}' - s_i = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • No change starting from position 88.

SWAPW2

The SWAPW2 operation swaps stack elements 0,1,2,30, 1, 2, 3 with elements 8,9,10,118, 9, 10, 11. The diagram below illustrates this graphically.

swapw2

Stack transition for this operation must satisfy the following constraints:

sisi+8=0 for i{0,1,2,3} | degree=1s_i' - s_{i+8} = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1 si+8si=0 for i{0,1,2,3} | degree=1s_{i + 8}' - s_i = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • No change for elements 4,5,6,74, 5, 6, 7.
  • No change starting from position 1212.

SWAPW3

The SWAPW3 operation swaps stack elements 0,1,2,30, 1, 2, 3 with elements 12,13,14,1512, 13, 14, 15. The diagram below illustrates this graphically.

swapw3

Stack transition for this operation must satisfy the following constraints:

sisi+12=0 for i{0,1,2,3} | degree=1s_i' - s_{i+12} = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1 si+12si=0 for i{0,1,2,3} | degree=1s_{i+12}' - s_i = 0 \text{ for } i \in \{0, 1, 2, 3\} \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • No change for elements 4,5,6,7,8,9,10,114, 5, 6, 7, 8, 9, 10, 11.
  • No change starting from position 1616.

SWAPDW

The SWAPDW operation swaps stack elements [0,8)[0, 8) with elements [8,16)[8, 16). The diagram below illustrates this graphically.

swapdw

Stack transition for this operation must satisfy the following constraints:

sisi+8=0 for i[0,8) | degree=1s_i' - s_{i+8} = 0 \text{ for } i \in [0, 8) \text{ | degree} = 1 si+8si=0 for i[0,8) | degree=1s_{i+8}' - s_i = 0 \text{ for } i \in [0, 8) \text{ | degree} = 1

The effect of this operation on the rest of the stack is:

  • No change starting from position 1616.

MOVUP(n)

The MOVUP(n) operation moves the nn-th element of the stack to the top of the stack. For example, MOVUP2 moves element at depth 22 to the top of the stack. All elements with depth less than nn are shifted to the right by one, while elements with depth greater than nn remain in place, and the depth of the stack does not change. This operation is valid for n[2,9)n \in [2, 9). The diagram below illustrates this graphically.

movup

Stack transition for this operation must satisfy the following constraints:

s0sn=0 for n[2,9) | degree=1s_0' - s_n = 0 \text{ for } n \in [2, 9) \text{ | degree} = 1

where nn is the depth of the element which is moved moved to the top of the stack.

The effect of this operation on the rest of the stack is:

  • Right shift for elements between 00 and n1n-1.
  • No change starting from position n+1n+1.

MOVDN(n)

The MOVDN(n) operation moves the top element of the stack to the nn-th position. For example, MOVDN2 moves the top element of the stack to depth 22. All the elements with depth less than nn are shifted to the left by one, while elements with depth greater than nn remain in place, and the depth of the stack does not change. This operation is valid for n[2,9)n \in [2, 9). The diagram below illustrates this graphically.

movdn

Stack transition for this operation must satisfy the following constraints:

sns0=0 for n[2,9) | degree=1s_n' - s_0 = 0 \text{ for } n \in [2, 9) \text{ | degree} = 1

where nn is the depth to which the top stack element is moved.

The effect of this operation on the rest of the stack is:

  • Left shift for elements between 11 and nn.
  • No change starting from position n+1n+1.

CSWAP

The CSWAP operation pops an element off the stack and if the element is 11, swaps the top two remaining elements. If the popped element is 00, the rest of the stack remains unchanged. The diagram below illustrates this graphically.

cswap

In the above:

d={a,if c=0b,if c=1 e={b,if c=0a,if c=1 d = \begin{cases} a, & \text{if}\ c = 0 b, & \text{if}\ c = 1\ \end{cases} e = \begin{cases} b, & \text{if}\ c = 0 a, & \text{if}\ c = 1\ \end{cases}

Stack transition for this operation must satisfy the following constraints:

s0s0s2(1s0)s1=0 | degree=2s_0' - s_{0} \cdot s_{2} - (1-s_0) \cdot s_1 = 0 \text{ | degree} = 2 s1s0s1(1s0)s2=0 | degree=2s_1' - s_0 \cdot s_{1} - (1-s_0) \cdot s_2 = 0 \text{ | degree} = 2

We also need to enforce that the value in s0s_0 is binary. This can be done with the following constraint:

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

The effect of this operation on the rest of the stack is:

  • Left shift starting from position 33.

CSWAPW

The CSWAPW operation pops an element off the stack and if the element is 11, swaps elements 1,2,3,41, 2, 3, 4 with elements 5,6,7,85, 6, 7, 8. If the popped element is 00, the rest of the stack remains unchanged. The diagram below illustrates this graphically.

cswapw

In the above:

D={A,if c=0B,if c=1 E={B,if c=0A,if c=1 D = \begin{cases} A, & \text{if}\ c = 0 B, & \text{if}\ c = 1\ \end{cases} E = \begin{cases} B, & \text{if}\ c = 0 A, & \text{if}\ c = 1\ \end{cases}

Stack transition for this operation must satisfy the following constraints:

sis0si+5(1s0)si+1=0 for i[0,4) | degree=2s_i' - s_0 \cdot s_{i+5} - (1-s_0) \cdot s_{i+1} = 0 \text{ for } i \in [0, 4) \text{ | degree} = 2 si+4s0si+1(1s0)si+5=0 for i[0,4) | degree=2s_{i+4}' - s_0 \cdot s_{i+1} - (1-s_0) \cdot s_{i+5} = 0 \text{ for } i \in [0, 4) \text{ | degree} = 2

We also need to enforce that the value in s0s_0 is binary. This can be done with the following constraint:

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

The effect of this operation on the rest of the stack is:

  • Left shift starting from position 99.