Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
execution.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <stack>
6#include <vector>
7
36
37namespace bb::avm2::simulation {
38
39// Forward declaration.
40class CallStackMetadataCollectorInterface;
41class AluInterface;
42class BitwiseInterface;
43class DataCopyInterface;
44class Poseidon2Interface;
45class EccInterface;
46class ToRadixInterface;
47class Sha256Interface;
48class ExecutionComponentsProviderInterface;
49class ContextProviderInterface;
50class InstructionInfoDBInterface;
51class ExecutionIdManagerInterface;
52class KeccakF1600Interface;
53class GreaterThanInterface;
54class GetContractInstanceInterface;
55class EmitUnencryptedLogInterface;
56class DebugLoggerInterface;
57class HighLevelMerkleDBInterface;
58class GasTrackerInterface;
59
60// In charge of executing a single enqueued call.
62 public:
85 , alu(alu)
90 , sha256(sha256)
101 , ctx_stack_events(ctx_stack_emitter)
103 {}
104
105 EnqueuedCallResult execute(std::unique_ptr<ContextInterface> enqueued_call_context) override;
106
107 // Opcode handlers. The order of the operands matters and should be the same as the wire format.
119 void set(ContextInterface& context, MemoryAddress dst_addr, uint8_t tag, const FF& value);
121 void jump(ContextInterface& context, uint32_t loc);
122 void jumpi(ContextInterface& context, MemoryAddress cond_addr, uint32_t loc);
124 MemoryAddress l2_gas_offset,
125 MemoryAddress da_gas_offset,
126 MemoryAddress addr,
127 MemoryAddress cd_size_offset,
130 MemoryAddress l2_gas_offset,
131 MemoryAddress da_gas_offset,
132 MemoryAddress addr,
133 MemoryAddress cd_size_offset,
135 void ret(ContextInterface& context, MemoryAddress ret_size_offset, MemoryAddress ret_offset);
136 void revert(ContextInterface& context, MemoryAddress rev_size_offset, MemoryAddress rev_offset);
138 MemoryAddress cd_size_offset,
142 MemoryAddress rd_size_offset,
143 MemoryAddress rd_offset,
146 void internal_call(ContextInterface& context, uint32_t loc);
151 MemoryAddress level_offset,
152 MemoryAddress message_offset,
153 MemoryAddress fields_offset,
154 MemoryAddress fields_size_offset,
155 uint16_t message_size);
160 void sstore(ContextInterface& context, MemoryAddress src_addr, MemoryAddress slot_addr);
162 MemoryAddress unique_note_hash_addr,
163 MemoryAddress leaf_index_addr,
166 MemoryAddress nullifier_offset,
167 MemoryAddress address_offset,
168 MemoryAddress exists_offset);
171 MemoryAddress address_offset,
172 MemoryAddress dst_offset,
173 uint8_t member_enum);
176 MemoryAddress msg_hash_addr,
177 MemoryAddress leaf_index_addr,
181 MemoryAddress p_x_addr,
182 MemoryAddress p_y_addr,
183 MemoryAddress p_inf_addr,
184 MemoryAddress q_x_addr,
185 MemoryAddress q_y_addr,
186 MemoryAddress q_inf_addr,
189 MemoryAddress value_addr,
190 MemoryAddress radix_addr,
191 MemoryAddress num_limbs_addr,
192 MemoryAddress is_output_bits_addr,
194 void emit_unencrypted_log(ContextInterface& context, MemoryAddress log_size_offset, MemoryAddress log_offset);
195 void send_l2_to_l1_msg(ContextInterface& context, MemoryAddress recipient_addr, MemoryAddress content_addr);
197 MemoryAddress output_addr,
198 MemoryAddress state_addr,
199 MemoryAddress input_addr);
202
203 protected:
204 // The result of a nested call execution.
210 uint32_t halting_pc = 0; // PC at which the context halted.
212 };
213
214 // Only here for testing. TODO(fcarreiro): try to improve.
216
217 void set_execution_result(const ExecutionResult& exec_result) { this->exec_result = exec_result; }
221 const std::vector<Operand>& resolved_operands);
222 template <typename... Ts>
223 void call_with_operands(void (Execution::*f)(ContextInterface&, Ts...),
225 const std::vector<Operand>& resolved_operands);
227
228 void handle_enter_call(ContextInterface& parent_context, std::unique_ptr<ContextInterface> child_context);
229 void handle_exit_call();
230 void handle_exceptional_halt(ContextInterface& context, const std::string& halting_message);
231
232 // TODO(#13683): This is leaking circuit implementation details. We should have a better way to do this.
233 // Setters for inputs and output for gadgets/subtraces. These are used for register allocation.
235 void set_output(ExecutionOpCode opcode, const MemoryValue& output);
236 const std::vector<MemoryValue>& get_inputs() const { return inputs; }
237 const MemoryValue& get_output() const { return output; }
238
241
257
261
263
268};
269
270} // namespace bb::avm2::simulation
MemoryTag dst_tag
void lt(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
LT execution opcode handler: Check if the first value is less than the second.
void emit_note_hash(ContextInterface &context, MemoryAddress note_hash_addr)
EMITNOTEHASH execution opcode handler: Emit a note hash to the note hash tree.
std::vector< MemoryValue > inputs
void mov(ContextInterface &context, MemoryAddress src_addr, MemoryAddress dst_addr)
MOV execution opcode handler: Move a memory value from one memory location to another.
void static_call(ContextInterface &context, MemoryAddress l2_gas_offset, MemoryAddress da_gas_offset, MemoryAddress addr, MemoryAddress cd_size_offset, MemoryAddress cd_offset)
STATICCALL execution opcode handler: Call a contract in a static context. Creates a new (nested) exec...
void debug_log(ContextInterface &context, MemoryAddress level_offset, MemoryAddress message_offset, MemoryAddress fields_offset, MemoryAddress fields_size_offset, uint16_t message_size)
DEBUGLOG execution opcode handler: Log a debug message. Logs a debug message to the debug logger if t...
EventEmitterInterface< ExecutionEvent > & events
void cd_copy(ContextInterface &context, MemoryAddress cd_size_offset, MemoryAddress cd_offset, MemoryAddress dst_addr)
CALLDATACOPY execution opcode handler: Copy calldata from the parent context to the current context....
Execution(AluInterface &alu, BitwiseInterface &bitwise, DataCopyInterface &data_copy, Poseidon2Interface &poseidon2, EccInterface &ecc, ToRadixInterface &to_radix, Sha256Interface &sha256, ExecutionComponentsProviderInterface &execution_components, ContextProviderInterface &context_provider, const InstructionInfoDBInterface &instruction_info_db, ExecutionIdManagerInterface &execution_id_manager, EventEmitterInterface< ExecutionEvent > &event_emitter, EventEmitterInterface< ContextStackEvent > &ctx_stack_emitter, KeccakF1600Interface &keccakf1600, GreaterThanInterface &greater_than, GetContractInstanceInterface &get_contract_instance_component, EmitUnencryptedLogInterface &emit_unencrypted_log_component, DebugLoggerInterface &debug_log_component, HighLevelMerkleDBInterface &merkle_db, CallStackMetadataCollectorInterface &call_stack_metadata_collector)
Definition execution.hpp:63
std::unique_ptr< GasTrackerInterface > gas_tracker
void dispatch_opcode(ExecutionOpCode opcode, ContextInterface &context, const std::vector< Operand > &resolved_operands)
Dispatch an opcode. This is the main function that dispatches the opcode to the appropriate handler.
void set_execution_result(const ExecutionResult &exec_result)
ExecutionComponentsProviderInterface & execution_components
const std::vector< MemoryValue > & get_inputs() const
void set(ContextInterface &context, MemoryAddress dst_addr, uint8_t tag, const FF &value)
SET execution opcode handler: Set the value at a memory location. If the value does not fit in the ta...
void internal_return(ContextInterface &context)
INTERNALRETURN execution opcode handler: Return from a function in the current context....
void set_output(ExecutionOpCode opcode, const MemoryValue &output)
Set the output register.
KeccakF1600Interface & keccakf1600
virtual GasTrackerInterface & get_gas_tracker()
void poseidon2_permutation(ContextInterface &context, MemoryAddress src_addr, MemoryAddress dst_addr)
POSEIDON2PERMUTATION execution opcode handler: Perform a Poseidon2 permutation on the input value....
void success_copy(ContextInterface &context, MemoryAddress dst_addr)
SUCCESSCOPY execution opcode handler: Copy the success flag to the destination memory location.
void fdiv(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
FDIV execution opcode handler: Divide two field values.
void jumpi(ContextInterface &context, MemoryAddress cond_addr, uint32_t loc)
JUMPI execution opcode handler: Jump to a new program counter conditionally. Next instruction will be...
void sub(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
SUB execution opcode handler: Subtract two values.
CallStackMetadataCollectorInterface & call_stack_metadata_collector
void rd_copy(ContextInterface &context, MemoryAddress rd_size_offset, MemoryAddress rd_offset, MemoryAddress dst_addr)
RETURNDATACOPY execution opcode handler: Copy return data from the current context to the parent cont...
void div(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
DIV execution opcode handler: Divide two values.
EventEmitterInterface< ContextStackEvent > & ctx_stack_events
const MemoryValue & get_output() const
void ecc_add(ContextInterface &context, MemoryAddress p_x_addr, MemoryAddress p_y_addr, MemoryAddress p_inf_addr, MemoryAddress q_x_addr, MemoryAddress q_y_addr, MemoryAddress q_inf_addr, MemoryAddress dst_addr)
ECADD execution opcode handler: Perform an elliptic curve addition and write the result to the destin...
void keccak_permutation(ContextInterface &context, MemoryAddress dst_addr, MemoryAddress src_addr)
KECCAKF1600 execution opcode handler: Perform a Keccak permutation on the data.
void jump(ContextInterface &context, uint32_t loc)
JUMP execution opcode handler: Jump to a new program counter. Next instruction will be executed at th...
const ExecutionResult & get_execution_result() const
void sha256_compression(ContextInterface &context, MemoryAddress output_addr, MemoryAddress state_addr, MemoryAddress input_addr)
SHA256COMPRESSION execution opcode handler: Perform a SHA256 compression on the input and state value...
void ret(ContextInterface &context, MemoryAddress ret_size_offset, MemoryAddress ret_offset)
RETURN execution opcode handler: Return from a contract. Sets the execution result to the return data...
void op_not(ContextInterface &context, MemoryAddress src_addr, MemoryAddress dst_addr)
NOT execution opcode handler: Perform bitwise NOT operation on a value.
void shl(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
SHL execution opcode handler: Perform left shift operation on a value.
void handle_enter_call(ContextInterface &parent_context, std::unique_ptr< ContextInterface > child_context)
Handle the entering of a call. This is called when a call is made from a context. This is a helper fu...
void handle_exceptional_halt(ContextInterface &context, const std::string &halting_message)
Handle the exceptional halt of a context. This is called when an exception is thrown during the execu...
void note_hash_exists(ContextInterface &context, MemoryAddress unique_note_hash_addr, MemoryAddress leaf_index_addr, MemoryAddress dst_addr)
NOTEHASHEXISTS execution opcode handler: Check if a note hash exists in the note hash tree at the spe...
ContextProviderInterface & context_provider
void cast(ContextInterface &context, MemoryAddress src_addr, MemoryAddress dst_addr, uint8_t dst_tag)
CAST execution opcode handler: Cast a value to a different tag.
void to_radix_be(ContextInterface &context, MemoryAddress value_addr, MemoryAddress radix_addr, MemoryAddress num_limbs_addr, MemoryAddress is_output_bits_addr, MemoryAddress dst_addr)
TORADIXBE execution opcode handler: Convert a value to a radix-based representation....
EmitUnencryptedLogInterface & emit_unencrypted_log_component
void eq(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
EQ execution opcode handler: Check if two values are equal.
std::vector< Operand > resolve_operands(const Instruction &instruction, const ExecInstructionSpec &spec)
void call(ContextInterface &context, MemoryAddress l2_gas_offset, MemoryAddress da_gas_offset, MemoryAddress addr, MemoryAddress cd_size_offset, MemoryAddress cd_offset)
CALL execution opcode handler: Call a contract. Creates a new (nested) execution context and triggers...
DataCopyInterface & data_copy
std::stack< std::unique_ptr< ContextInterface > > external_call_stack
void handle_exit_call()
Handle the exiting of a call. This is called when a call returns or reverts.
GreaterThanInterface & greater_than
void revert(ContextInterface &context, MemoryAddress rev_size_offset, MemoryAddress rev_offset)
REVERT execution opcode handler: Revert from a contract. Sets the execution result to the revert data...
void rd_size(ContextInterface &context, MemoryAddress dst_addr)
RETURNDATASIZE execution opcode handler: Get the size of the return data.
void mul(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
MUL execution opcode handler: Multiply two values.
void or_op(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
OR execution opcode handler: Perform a bitwise OR operation on the two input values.
void shr(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
SHR execution opcode handler: Perform right shift operation on a value.
DebugLoggerInterface & debug_log_component
void xor_op(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
XOR execution opcode handler: Perform a bitwise XOR operation on the two input values.
Poseidon2Interface & poseidon2
GetContractInstanceInterface & get_contract_instance_component
void set_and_validate_inputs(ExecutionOpCode opcode, const std::vector< MemoryValue > &inputs)
Set the register inputs and validate the tags. The tag information is taken from the instruction info...
void lte(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
LTE execution opcode handler: Check if the first value is less than or equal to the second.
const InstructionInfoDBInterface & instruction_info_db
HighLevelMerkleDBInterface & merkle_db
void and_op(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
AND execution opcode handler: Perform a bitwise AND operation on the two input values.
EnqueuedCallResult execute(std::unique_ptr< ContextInterface > enqueued_call_context) override
Execute a top-level enqueued call.
ExecutionIdManagerInterface & execution_id_manager
void add(ContextInterface &context, MemoryAddress a_addr, MemoryAddress b_addr, MemoryAddress dst_addr)
ADD execution opcode handler: Add two values.
Definition execution.cpp:69
void call_with_operands(void(Execution::*f)(ContextInterface &, Ts...), ContextInterface &context, const std::vector< Operand > &resolved_operands)
Call with operands. This is a template magic function to dispatch the opcode by deducing the number o...
EventEmitter< DataCopyEvent > event_emitter
uint32_t dst_addr
Instruction instruction
uint32_t MemoryAddress
AvmFlavorSettings::FF FF
Definition field.hpp:10
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint32_t cd_offset
std::optional< std::string > halting_message