pub mod vm
# ===== SYSTEM PROCEDURES ==========================================================================
#! Removes elements deep in the stack until the depth of the stack is exactly 16. The elements
#! are removed in such a way that the top 16 elements of the stack remain unchanged. If the stack
#! would otherwise contain more than 16 elements at the end of execution, then adding a call to this
#! function at the end will reduce the size of the public inputs that are shared with the verifier.
#!
#! Input: Stack with 16 or more elements.
#! Output: Stack with only the original top 16 elements.
#!
#! Cycles: 17 + 11 * overflow_words, where `overflow_words` is the number of words needed to drop.
@locals(4)
pub proc truncate_stack()
# save the first word to memory and bring elements to be dropped to the top of the stack
loc_storew_le.0 dropw movupw.3
# => [X, B, C, D, ...]
# until stack depth greater than 16, keep dropping extra elements
sdepth neq.16
while.true
dropw movupw.3
# => [X, B, C, D, ...]
sdepth neq.16
end
# => [X, B, C, D, ...]
# bring the previously saved word back onto the stack
loc_loadw_le.0
# => [A, B, C, D, ...]
end
#! Drop 16 values from the stack.
pub proc drop_stack_top()
dropw dropw dropw dropw
end
#! Folds a precompile commitment into the rolling transcript via `log_precompile` and removes
#! the helper words produced by the instruction.
#!
#! Computes the per-call statement word `STMNT = Poseidon2::merge(COMM, TAG)` from the supplied
#! commitment halves, seats it at stack[4..8] (the HPERM rate1 lanes), and lets the underlying
#! `log_precompile` opcode fold it into the transcript state. Pads stack[0..4] and stack[8..12]
#! with zero words so the opcode's writes there don't clobber data deeper in the stack.
#!
#! Input: `[COMM, TAG, ...]`
#! Output: Stack with `COMM` and `TAG` consumed and the three helper words produced by
#! `log_precompile` dropped.
pub proc log_precompile_request
hmerge
# => [STMNT, ...]
padw padw movdnw.2
# => [_, STMNT, _, ...]
log_precompile
# => [STATE_NEW, OUT_RATE1, OUT_CAP, ...]
dropw dropw dropw
end