Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
instruction_spec.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <optional>
6#include <unordered_map>
7#include <vector>
8
11
12namespace bb::avm2 {
13
14constexpr size_t NUM_OP_DC_SELECTORS = 18;
15constexpr uint32_t DECOMPOSE_WINDOW_SIZE = 37; // Max size of instruction in bytes.
16// We do not use a static constant to compute this value from WIRE_INSTRUCTION_SPEC
17// because we do not want it to be changed transparently. Namely, if this constant
18// is changed, we need to make significant changes in pil/vm2/bytecode/bc_decomposition.pil
19// and pil/vm2/bytecode/instr_fetching.pil.
20// We rely on the unit test InstructionSpecTest.CheckDecomposeWindowSize to detect if this constant is changed.
21
23 struct GasInfo {
24 uint16_t opcode_gas = 0; // Base l2 gas is computed as opcode_gas + addressing_gas
25 uint16_t base_da = 0;
26 uint16_t dyn_l2 = 0;
27 uint16_t dyn_da = 0;
28
29 bool operator==(const GasInfo& other) const = default;
30 };
31
32 // This builder is used to generate the register information based on the number of inputs and outputs.
33 // Output will always come last.
35 public:
37 RegisterInfo& add_inputs(const std::vector<std::optional<ValueTag>>& tags);
39
40 size_t num_inputs() const { return inputs.size(); }
41 size_t num_outputs() const { return has_output ? 1 : 0; }
42 size_t total_registers() const { return num_inputs() + num_outputs(); }
43
44 // Given a register index, returns if the register is active for this instruction
45 bool is_active(size_t index) const;
46 // Given a register index, returns if the register is used for writing to memory
47 bool is_write(size_t index) const;
48 // Given a register index, returns if the tag check is needed.
49 bool need_tag_check(size_t index) const;
50 // Given a register index, returns the expected tag for this instruction.
51 std::optional<ValueTag> expected_tag(size_t index) const;
52
53 static constexpr auto ANY_TAG = std::nullopt;
54
55 bool operator==(const RegisterInfo& other) const = default;
56
57 private:
59 bool has_output = false;
60 };
61
62 uint8_t num_addresses = 0;
63 GasInfo gas_cost; // Default values are 0.
64 uint32_t dyn_gas_id = 0; // Composition of dyn gas selectors.
66
67 bool operator==(const ExecInstructionSpec& other) const = default;
68};
69
72 uint32_t size_in_bytes = 0;
73 std::array<uint8_t, NUM_OP_DC_SELECTORS> op_dc_selectors = {};
75 tag_operand_idx; // Index of relevant operand in vector of operands as defined in WireOpCode_WIRE_FORMAT
76
77 bool operator==(const WireInstructionSpec& other) const = default;
78};
79
80// These are "extern" because the definition is in a different file.
81// Note: in the circuit, we can choose to merge both tables.
84
85} // namespace bb::avm2
RegisterInfo & add_inputs(const std::vector< std::optional< ValueTag > > &tags)
bool operator==(const RegisterInfo &other) const =default
std::optional< ValueTag > expected_tag(size_t index) const
RegisterInfo & add_input(std::optional< ValueTag > tag=std::nullopt)
std::vector< std::optional< ValueTag > > inputs
constexpr uint32_t DECOMPOSE_WINDOW_SIZE
constexpr size_t NUM_OP_DC_SELECTORS
const std::unordered_map< WireOpCode, WireInstructionSpec > WIRE_INSTRUCTION_SPEC
const std::unordered_map< ExecutionOpCode, ExecInstructionSpec > EXEC_INSTRUCTION_SPEC
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bool operator==(const GasInfo &other) const =default
bool operator==(const ExecInstructionSpec &other) const =default
std::optional< uint8_t > tag_operand_idx
std::array< uint8_t, NUM_OP_DC_SELECTORS > op_dc_selectors
bool operator==(const WireInstructionSpec &other) const =default