Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bytecode_manager.cpp
Go to the documentation of this file.
2
3#include <cassert>
4
11
12namespace bb::avm2::simulation {
13
40{
41 BB_BENCH_NAME("TxBytecodeManager::get_bytecode");
42 // Use shared ContractInstanceManager for contract instance retrieval and validation
43 // This handles nullifier checks, address derivation, and update validation
44 auto tree_states = merkle_db.get_tree_state();
45 AppendOnlyTreeSnapshot before_snapshot = retrieved_bytecodes_tree_check.get_snapshot();
46
47 BytecodeRetrievalEvent retrieval_event = {
48 .bytecode_id = FF(0), // Use default ID for error cases
49 .address = address,
50 .nullifier_root = tree_states.nullifier_tree.tree.root,
51 .public_data_tree_root = tree_states.public_data_tree.tree.root,
52 .retrieved_bytecodes_snapshot_before = before_snapshot,
53 .retrieved_bytecodes_snapshot_after = before_snapshot,
54 };
55
57
58 if (!maybe_instance.has_value()) {
59 retrieval_event.instance_not_found_error = true;
60 // Contract instance not found - emit error event and throw
61 retrieval_events.emit(std::move(retrieval_event));
62 vinfo("Contract ", field_to_string(address), " is not deployed!");
63 throw BytecodeRetrievalError("Contract " + field_to_string(address) + " is not deployed");
64 }
65
66 ContractInstance instance = maybe_instance.value();
67 ContractClassId current_class_id = instance.current_contract_class_id;
68 retrieval_event.current_class_id = current_class_id;
69
70 bool is_new_class = !retrieved_bytecodes_tree_check.contains(current_class_id);
71 retrieval_event.is_new_class = is_new_class;
72
73 uint32_t retrieved_bytecodes_count = retrieved_bytecodes_tree_check.size();
74
75 if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
76 retrieval_event.limit_error = true;
77 retrieval_events.emit(std::move(retrieval_event));
78 throw BytecodeRetrievalError("Can't retrieve more than " +
80 " bytecodes per tx");
81 }
82
84 AppendOnlyTreeSnapshot snapshot_after = retrieved_bytecodes_tree_check.get_snapshot();
85 retrieval_event.retrieved_bytecodes_snapshot_after = snapshot_after;
86
87 // Contract class retrieval and class ID validation
89 // Note: we don't need to silo and check the class id because the deployer contract guarantees
90 // that if a contract instance exists, the class has been registered.
91 BB_ASSERT(maybe_klass.has_value(), "Contract class not found");
92 auto& klass = maybe_klass.value();
93 retrieval_event.contract_class = klass; // WARNING: this class has the whole bytecode.
94
95 // Bytecode hashing (bc_hashing.pil) and decomposition (bc_decomposition.pil)
97 // If we reach this point, class ID and instance both exist which means bytecode commitment must exist.
98 BB_ASSERT(maybe_bytecode_commitment.has_value(), "Bytecode commitment not found");
99 BytecodeId bytecode_id = maybe_bytecode_commitment.value();
100 retrieval_event.bytecode_id = bytecode_id;
101 debug("Bytecode for ", address, " successfully retrieved!");
102
103 // Check if we've already processed this bytecode by deduplicating by bytecode_id (=commitment). If so, don't do
104 // hashing and decomposition again!
105 if (bytecodes.contains(bytecode_id)) {
106 // Already processed this bytecode - just emit retrieval event and return
107 retrieval_events.emit(std::move(retrieval_event));
108 return bytecode_id;
109 }
110
111 // First time seeing this bytecode - perform hashing and decomposition.
112 // Emits BytecodeHashingEvent and corresponding Poseidon2HashEvent and Poseidon2PermutationEvent(s).
114 bytecode_id, klass.packed_bytecode, /*public_bytecode_commitment=*/bytecode_id);
115
116 // We convert the bytecode to a shared_ptr because it will be shared by some events.
117 auto shared_bytecode = std::make_shared<std::vector<uint8_t>>(std::move(klass.packed_bytecode));
118 // Emits BytecodeDecompositionEvent.
119 decomposition_events.emit({ .bytecode_id = bytecode_id, .bytecode = shared_bytecode });
120
121 // We now save the bytecode against its id so that we don't repeat this process.
122 bytecodes.emplace(bytecode_id, std::move(shared_bytecode));
123
124 retrieval_events.emit(std::move(retrieval_event));
125
126 return bytecode_id;
127}
128
130{
131 return read_instruction(bytecode_id, get_bytecode_data(bytecode_id), pc);
132}
133
135 std::shared_ptr<std::vector<uint8_t>> bytecode_ptr,
136 PC pc)
137{
138 BB_BENCH_NAME("TxBytecodeManager::read_instruction");
139
140 // We'll be filling in the event as we progress.
141 InstructionFetchingEvent instr_fetching_event;
142
143 instr_fetching_event.bytecode_id = bytecode_id;
144 instr_fetching_event.pc = pc;
145
146 const auto& bytecode = *bytecode_ptr;
147 instr_fetching_event.bytecode = std::move(bytecode_ptr);
148
149 // Keep full error for exception message, but only store enum in event
150 std::optional<InstrDeserializationError> deserialization_error;
151
152 try {
153 instr_fetching_event.instruction = deserialize_instruction(bytecode, pc);
154
155 // If the following code is executed, no error was thrown in deserialize_instruction().
156 if (!check_tag(instr_fetching_event.instruction)) {
158 };
159 } catch (const InstrDeserializationError& error) {
160 instr_fetching_event.error = error.type;
161 }
162
163 // We are showing whether bytecode_size > pc or not. If there is no fetching error,
164 // we always have bytecode_size > pc.
165 const auto bytecode_size = bytecode.size();
166 const uint128_t pc_diff = bytecode_size > pc ? bytecode_size - pc - 1 : pc - bytecode_size;
167 range_check.assert_range(pc_diff, AVM_PC_SIZE_IN_BITS);
168
169 // The event will be deduplicated internally.
170 fetching_events.emit(InstructionFetchingEvent(instr_fetching_event));
171
172 // Communicate error to the caller.
173 if (instr_fetching_event.error.has_value()) {
174 throw InstructionFetchingError("Instruction fetching error: " +
175 std::to_string(static_cast<int>(instr_fetching_event.error.value())));
176 }
177
178 return instr_fetching_event.instruction;
179}
180
182{
183 auto it = bytecodes.find(bytecode_id);
184 BB_ASSERT(it != bytecodes.end(), "Bytecode not found for the given bytecode_id");
185 return it->second;
186}
187
188} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
std::shared_ptr< Napi::ThreadSafeFunction > instance
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
#define MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS
#define AVM_PC_SIZE_IN_BITS
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
virtual void assert_public_bytecode_commitment(const BytecodeId &bytecode_id, const std::vector< uint8_t > &bytecode, const FF &public_bytecode_commitment)=0
virtual std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const =0
virtual std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const =0
virtual std::optional< ContractInstance > get_contract_instance(const FF &contract_address)=0
Retrieve and validate a contract instance.
virtual TreeStates get_tree_state() const =0
HighLevelMerkleDBInterface & merkle_db
Instruction read_instruction(const BytecodeId &bytecode_id, PC pc) override
EventEmitterInterface< BytecodeDecompositionEvent > & decomposition_events
EventEmitterInterface< BytecodeRetrievalEvent > & retrieval_events
EventEmitterInterface< InstructionFetchingEvent > & fetching_events
unordered_flat_map< BytecodeId, std::shared_ptr< std::vector< uint8_t > > > bytecodes
std::shared_ptr< std::vector< uint8_t > > get_bytecode_data(const BytecodeId &bytecode_id) override
BytecodeHashingInterface & bytecode_hasher
ContractInstanceManagerInterface & contract_instance_manager
BytecodeId get_bytecode(const AztecAddress &address) override
Retrieves and validates bytecode from the TxBytecodeManager's ContractDBInterface....
#define vinfo(...)
Definition log.hpp:94
#define debug(...)
Definition log.hpp:99
AVM range check gadget for witness generation.
bool check_tag(const Instruction &instruction)
Check whether the instruction must have a tag operand and whether the operand value is in the value t...
Instruction deserialize_instruction(std::span< const uint8_t > bytecode, size_t pos)
Parsing of an instruction in the supplied bytecode at byte position pos. This checks that the WireOpC...
uint32_t PC
AvmFlavorSettings::FF FF
Definition field.hpp:10
FF ContractClassId
std::string field_to_string(const FF &ff)
Definition stringify.cpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
unsigned __int128 uint128_t
Definition serialize.hpp:45
FF current_class_id
AppendOnlyTreeSnapshot retrieved_bytecodes_snapshot_after
std::shared_ptr< std::vector< uint8_t > > bytecode
std::optional< InstrDeserializationEventError > error