Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
context.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6
9
10namespace bb::avm2::simulation {
11
13// Base Context
15std::vector<FF> BaseContext::get_returndata(uint32_t rd_offset, uint32_t rd_copy_size)
16{
18 // The amount to rd copy is the minimum of the requested size (with the offset into rd) and the size of the
19 // returndata We need to do it over a wider integer type to avoid overflow issues, but the result is guaranteed to
20 // be a u32 since last_child_rd_size would have previously been constrained to be u32.
21 uint32_t max_read_index = static_cast<uint32_t>(
22 std::min(static_cast<uint64_t>(rd_offset) + rd_copy_size, static_cast<uint64_t>(last_child_rd_size)));
23
24 std::vector<FF> padded_returndata;
25 padded_returndata.reserve(max_read_index - rd_offset);
26
27 for (uint32_t i = rd_offset; i < max_read_index; i++) {
28 padded_returndata.push_back(child_memory.get(get_last_rd_addr() + i));
29 }
30 // Resize because relying on default initialization of FF (in reserve) is a potential footgun
31 padded_returndata.resize(rd_copy_size, 0);
32
33 return padded_returndata;
34};
35
37{
38 if (child_context == nullptr) {
39 return 0; // No child context, so no last child id.
40 }
41 return child_context->get_context_id();
42}
43
45// Enqueued Context
47std::vector<FF> EnqueuedCallContext::get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const
48{
49 uint64_t calldata_size = static_cast<uint64_t>(calldata.size());
50 // We first take a slice of the data, the most we can slice is the actual size of the data
51 uint64_t max_read_index = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, calldata_size);
52
53 std::vector<FF> padded_calldata;
54 padded_calldata.reserve(max_read_index - cd_offset);
55
56 for (size_t i = cd_offset; i < max_read_index; i++) {
57 padded_calldata.push_back(calldata[i]);
58 }
59 // Resize because relying on default initialization of FF (in reserve) is a potential footgun
60 padded_calldata.resize(cd_copy_size, 0);
61
62 return padded_calldata;
63};
64
66{
67 return {
68 .id = get_context_id(),
69 .parent_id = 0,
70 .last_child_id = get_last_child_id(),
71 .pc = get_pc(),
72 .msg_sender = get_msg_sender(),
73 .contract_addr = get_address(),
74 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
75 .transaction_fee = get_transaction_fee(),
76 .is_static = get_is_static(),
77 .parent_cd_addr = 0,
78 .parent_cd_size = get_parent_cd_size(),
79 .last_child_rd_addr = get_last_rd_addr(),
80 .last_child_rd_size = get_last_rd_size(),
81 .last_child_success = get_last_success(),
82 .gas_used = get_gas_used(),
83 .gas_limit = get_gas_limit(),
84 .parent_gas_used = get_parent_gas_used(),
85 .parent_gas_limit = get_parent_gas_limit(),
86 // internal call stack
87 .internal_call_id = get_internal_call_stack_manager().get_call_id(),
88 .internal_call_return_id = get_internal_call_stack_manager().get_return_call_id(),
89 .next_internal_call_id = get_internal_call_stack_manager().get_next_call_id(),
90 // Tree States
91 .tree_states = merkle_db.get_tree_state(),
92 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
93 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
94 // Side Effects
95 .side_effect_states = get_side_effect_states(),
96 // Phase
97 .phase = get_phase(),
98 };
99};
100
102// Nested Context
104std::vector<FF> NestedContext::get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const
105{
106 // This is the amount of the parent calldata we will read
107 // We need to do it over a wider integer type to avoid overflow issues
108 // Explicit for clarity
109 uint64_t parent_cd_size_u64 = static_cast<uint64_t>(parent_cd_size);
110
111 uint64_t max_read_index = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, parent_cd_size_u64);
112
113 std::vector<FF> padded_calldata;
114 padded_calldata.reserve(max_read_index - cd_offset);
115
116 for (uint32_t i = cd_offset; i < max_read_index; i++) {
117 padded_calldata.push_back(parent_context.get_memory().get(parent_cd_addr + i));
118 }
119 // Resize because relying on default initialization of FF (in reserve) is a potential footgun
120 padded_calldata.resize(cd_copy_size, 0);
121
122 return padded_calldata;
123};
124
126{
127 return {
128 .id = get_context_id(),
129 .parent_id = get_parent_id(),
130 .last_child_id = get_last_child_id(),
131 .pc = get_pc(),
132 .msg_sender = get_msg_sender(),
133 .contract_addr = get_address(),
134 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
135 .transaction_fee = get_transaction_fee(),
136 .is_static = get_is_static(),
137 .parent_cd_addr = parent_cd_addr,
138 .parent_cd_size = parent_cd_size,
139 .last_child_rd_addr = get_last_rd_addr(),
140 .last_child_rd_size = get_last_rd_size(),
141 .last_child_success = get_last_success(),
142 .gas_used = get_gas_used(),
143 .gas_limit = get_gas_limit(),
144 .parent_gas_used = get_parent_gas_used(),
145 .parent_gas_limit = get_parent_gas_limit(),
146 // internal call stack
147 .internal_call_id = get_internal_call_stack_manager().get_call_id(),
148 .internal_call_return_id = get_internal_call_stack_manager().get_return_call_id(),
149 .next_internal_call_id = get_internal_call_stack_manager().get_next_call_id(),
150 // Tree states
151 .tree_states = merkle_db.get_tree_state(),
152 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
153 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
154 // Side Effect
155 .side_effect_states = get_side_effect_states(),
156 // Phase
157 .phase = get_phase(),
158 };
159};
160
161} // namespace bb::avm2::simulation
const AztecAddress & get_address() const override
Definition context.hpp:83
TransactionPhase get_phase() const override
Definition context.hpp:93
InternalCallStackManagerInterface & get_internal_call_stack_manager() override
Definition context.hpp:68
std::vector< FF > get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) override
Definition context.cpp:15
MemoryAddress get_last_rd_addr() const override
Definition context.hpp:107
RetrievedBytecodesTreeCheckInterface & retrieved_bytecodes_tree
Definition context.hpp:132
uint32_t get_last_rd_size() const override
Definition context.hpp:110
Gas get_gas_used() const override
Definition context.hpp:116
const AztecAddress & get_msg_sender() const override
Definition context.hpp:84
ContextInterface & get_child_context() override
Definition context.hpp:101
SideEffectStates & get_side_effect_states() override
Definition context.hpp:87
WrittenPublicDataSlotsTreeCheckInterface & written_public_data_slots_tree
Definition context.hpp:131
std::unique_ptr< ContextInterface > child_context
Definition context.hpp:156
uint32_t get_context_id() const override
Definition context.hpp:80
uint32_t get_last_child_id() const override
Definition context.cpp:36
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:67
bool get_last_success() const override
Definition context.hpp:113
Gas get_gas_limit() const override
Definition context.hpp:117
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:129
const FF & get_transaction_fee() const override
Definition context.hpp:85
uint32_t get_pc() const override
Definition context.hpp:73
bool get_is_static() const override
Definition context.hpp:86
virtual std::optional< BytecodeId > get_retrieved_bytecode_id()=0
virtual MemoryInterface & get_memory()=0
std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:47
ContextEvent serialize_context_event() override
Definition context.cpp:65
Gas get_parent_gas_limit() const override
Definition context.hpp:212
uint32_t get_parent_cd_size() const override
Definition context.hpp:215
virtual TreeStates get_tree_state() const =0
virtual InternalCallId get_return_call_id() const =0
virtual InternalCallId get_next_call_id() const =0
virtual const MemoryValue & get(MemoryAddress index) const =0
uint32_t get_parent_id() const override
Definition context.hpp:263
ContextEvent serialize_context_event() override
Definition context.cpp:125
std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:104
Gas get_parent_gas_used() const override
Definition context.hpp:266
Gas get_parent_gas_limit() const override
Definition context.hpp:267
ContextInterface & parent_context
Definition context.hpp:283
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
AvmFlavorSettings::FF FF
Definition field.hpp:10
uint32_t cd_offset