Instructions

Most instructions are contained within a single, parameterless machine word.

Some instructions take a machine word as argument and are so considered double-word instructions. They are recognized by the form “instr + arg”.

Regarding Opcodes

An instruction's operation code, or opcode, is the machine word uniquely identifying the instruction. For reasons of efficient arithmetization, certain properties of the instruction are encoded in the opcode. Concretely, interpreting the field element in standard representation:

  • for all double-word instructions, the least significant bit is 1.
  • for all instructions shrinking the operational stack, the second-to-least significant bit is 1.
  • for all u32 instructions , the third-to-least significant bit is 1.

The first property is used by instruction skiz. The second property helps guarantee that operational stack underflow cannot happen. It is used by several instructions through instruction group stack_shrinks_and_top_3_unconstrained. The third property allows efficient arithmetization of the running product for the Permutation Argument between Processor Table and U32 Table.

OpStack Manipulation

InstructionOpcodeold OpStacknew OpStackDescription
pop2_ a_Pops top element from stack.
push + a1__ aPushes a onto the stack.
divine8__ aPushes a non-deterministic element a to the stack. Interface for secret input.
dup + i9e.g., _ e d c b ae.g., _ e d c b a dDuplicates the element i positions away from the top, assuming 0 <= i < 16.
swap + i17e.g., _ e d c b ae.g., _ e a c b dSwaps the ith stack element with the top of the stack, assuming 0 < i < 16.

Instruction divine (together with divine_sibling) make TritonVM a virtual machine that can execute non-deterministic programs. As programs go, this concept is somewhat unusual and benefits from additional explanation. The name of the instruction is the verb (not the adjective) meaning “to discover by intuition or insight.”

From the perspective of the program, the instruction divine makes some element a magically appear on the stack. It is not at all specified what a is, but generally speaking, a has to be exactly correct, else execution fails. Hence, from the perspective of the program, it just non-deterministically guesses the correct value of a in a moment of divine clarity.

Looking at the entire system, consisting of the VM, the program, and all inputs – both public and secret – execution is deterministic: the value a was supplied as a secret input.

Control Flow

InstructionOpcodeold OpStacknew OpStackold ipnew ipold JumpStacknew JumpStackDescription
nop16____ + 1__Do nothing
skiz10_ a___ + s__Skip next instruction if a is zero. s ∈ {1, 2, 3} depends on a and whether the next instruction takes an argument.
call + d25__od__ (o+2, d)Push (o+2,d) to the jump stack, and jump to absolute address d
return24___o_ (o, d)_Pop one pair off the jump stack and jump to that pair's return address (which is the first element).
recurse32___d_ (o, d)_ (o, d)Peek at the top pair of the jump stack and jump to that pair's destination address (which is the second element).
assert18_ a___ + 1__Pops a if a == 1, else crashes the virtual machine.
halt0____ + 1__Solves the halting problem (if the instruction is reached). Indicates graceful shutdown of the VM.

Memory Access

InstructionOpcodeold OpStacknew OpStackold ramvnew ramvDescription
read_mem40_ p_ p vvvReads value v from RAM at address p and pushes v onto the OpStack.
write_mem26_ p v_ p_vWrites OpStack's top-most value v to RAM at the address p, popping v.

Hashing

InstructionOpcodeold OpStacknew OpStackDescription
hash48_jihgfedcba_yxwvu00000Overwrites the stack's 10 top-most elements with their hash digest (length 5) and 5 zeros.
divine_sibling56_ iedcba*****e.g., _ (i div 2)edcbazyxwvHelps traversing a Merkle tree during authentication path verification. See extended description below.
assert_vector64__Assert equality of st(i) to st(i+5) for 0 <= i < 4. Crashes the VM if any pair is unequal.
absorb_init72_jihgfedcba_jihgfedcbaResets the Sponge's state and absorbs the stack's ten top-most elements.
absorb80_jihgfedcba_jihgfedcbaAbsorbs the stack's ten top-most elements into the Sponge state.
squeeze88_jihgfedcba_zyxwvutsrqSqueezes the Sponge, overwriting the stack's ten top-most elements

The instruction hash works as follows. The stack's 10 top-most elements (jihgfedcba) are reversed and concatenated with six zeros, resulting in abcdefghij000000. The permutation xlix is applied to abcdefghij000000, resulting in αβγδεζηθικuvwxyz. The first five elements of this result, i.e., αβγδε, are reversed and written to the stack, overwriting st5 through st9. The top elements of the stack st0 through st4 are set to zero. For example, the old stack was _ jihgfedcba and the new stack is _ εδγβα 00000.

The instruction divine_sibling works as follows. The 11th element of the stack i is taken as the node index for a Merkle tree that is claimed to include data whose digest is the content of stack registers st5 through st9, i.e., edcba. The sibling digest of edcba is zyxwv and is read from the input interface of secret data. The least-significant bit of i indicates whether edcba is the digest of a left leaf or a right leaf of the Merkle tree's base level. Depending on this least significant bit of i, divine_sibling either

  1. (i = 0 mod 2, i.e., current node is left sibling) moves edcba into registers st0 through st4 and moves zyxwv into registers st5 through st9, or
  2. (i = 1 mod 2, i.e., current node is right sibling) does not change registers st5 through st9 and moves zyxwv into registers st0 through st4.

The 11th element of the operational stack i is shifted by 1 bit to the right, i.e., the least-significant bit is dropped. In conjunction with instruction hash and assert_vector, the instruction divine_sibling allows to efficiently verify a Merkle authentication path.

The instructions absorb_init, absorb, and squeeze are the interface for using the permutation xlix in a Sponge construction. The capacity is never accessible to the program that's being executed by Triton VM. At any given time, at most one Sponge state exists. Only instruction absorb_init resets the state of the Sponge, and only the three Sponge instructions influence the Sponge's state. Notably, executing instruction hash does not modify the Sponge's state. When using the Sponge instructions, it is the programmer's responsibility to take care of proper input padding: Triton VM cannot know the number of elements that will be absorbed.

Base Field Arithmetic on Stack

InstructionOpcodeold OpStacknew OpStackDescription
add34_ b a_ cComputes the sum (c) of the top two elements of the stack (b and a) over the field.
mul42_ b a_ cComputes the product (c) of the top two elements of the stack (b and a) over the field.
invert96_ a_ bComputes the multiplicative inverse (over the field) of the top of the stack. Crashes the VM if the top of the stack is 0.
eq50_ b a_ (a == b)Tests the top two stack elements for equality.

Bitwise Arithmetic on Stack

InstructionOpcodeold OpStacknew OpStackDescription
split4_ a_ hi loDecomposes the top of the stack into the lower 32 bits and the upper 32 bits.
lt6_ b a_ a<b“Less than” of the stack's two top-most elements. Crashes the VM if a or b is not u32.
and14_ b a_ a&bBitwise and of the stack's two top-most elements. Crashes the VM if a or b is not u32.
xor22_ b a_ a^bBitwise exclusive or of the stack's two top-most elements. Crashes the VM if a or b is not u32.
log_2_floor12_ a_ ⌊log₂(a)⌋The number of bits in a minus 1, i.e., . Crashes the VM if a is 0 or not u32.
pow30_ e b_ b**eThe top of the stack to the power of the stack's runner up. Crashes the VM if a or b is not u32. The result might be no u32 – care advised.
div20_ d n_ q rDivision with remainder of numerator n by denominator d. Guarantees the properties n == q·d + r and r < d. Crashes the VM if n or d is not u32 or if d is 0.
pop_count28_ a_ wComputes the hamming weight or “population count” of a. Crashes the VM if a is not u32.

Extension Field Arithmetic on Stack

InstructionOpcodeold OpStacknew OpStackDescription
xxadd104_ z y x b c a_ z y x w v uAdds the two extension field elements encoded by field elements z y x and b c a, overwriting the top-most extension field element with the result.
xxmul112_ z y x b c a_ z y x w v uMultiplies the two extension field elements encoded by field elements z y x and b c a, overwriting the top-most extension field element with the result.
xinvert120_ z y x_ w v uInverts the extension field element encoded by field elements z y x in-place. Crashes the VM if the extension field element is 0.
xbmul58_ z y x a_ w v uScalar multiplication of the extension field element encoded by field elements z y x with field element a. Overwrites z y x with the result.

Input/Output

InstructionOpcodeold OpStacknew OpStackDescription
read_io128__ aReads a B-Field element from standard input and pushes it to the stack.
write_io66_ a_Pops a from the stack and writes it to standard output.