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().try_get_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.snapshot(),
93 // Side Effects
94 .side_effect_states = get_side_effect_states(),
95 // Phase
96 .phase = get_phase(),
97 };
98};
99
101// Nested Context
103std::vector<FF> NestedContext::get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const
104{
105 // This is the amount of the parent calldata we will read
106 // We need to do it over a wider integer type to avoid overflow issues
107 // Explicit for clarity
108 uint64_t parent_cd_size_u64 = static_cast<uint64_t>(parent_cd_size);
109
110 uint64_t max_read_index = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, parent_cd_size_u64);
111
112 std::vector<FF> padded_calldata;
113 padded_calldata.reserve(max_read_index - cd_offset);
114
115 for (uint32_t i = cd_offset; i < max_read_index; i++) {
116 padded_calldata.push_back(parent_context.get_memory().get(parent_cd_addr + i));
117 }
118 // Resize because relying on default initialization of FF (in reserve) is a potential footgun
119 padded_calldata.resize(cd_copy_size, 0);
120
121 return padded_calldata;
122};
123
125{
126 return {
127 .id = get_context_id(),
128 .parent_id = get_parent_id(),
129 .last_child_id = get_last_child_id(),
130 .pc = get_pc(),
131 .msg_sender = get_msg_sender(),
132 .contract_addr = get_address(),
133 .bytecode_id = get_bytecode_manager().try_get_bytecode_id().value_or(FF(0)),
134 .transaction_fee = get_transaction_fee(),
135 .is_static = get_is_static(),
136 .parent_cd_addr = parent_cd_addr,
137 .parent_cd_size = parent_cd_size,
138 .last_child_rd_addr = get_last_rd_addr(),
139 .last_child_rd_size = get_last_rd_size(),
140 .last_child_success = get_last_success(),
141 .gas_used = get_gas_used(),
142 .gas_limit = get_gas_limit(),
143 .parent_gas_used = get_parent_gas_used(),
144 .parent_gas_limit = get_parent_gas_limit(),
145 // internal call stack
146 .internal_call_id = get_internal_call_stack_manager().get_call_id(),
147 .internal_call_return_id = get_internal_call_stack_manager().get_return_call_id(),
148 .next_internal_call_id = get_internal_call_stack_manager().get_next_call_id(),
149 // Tree states
150 .tree_states = merkle_db.get_tree_state(),
151 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.snapshot(),
152 // Side Effect
153 .side_effect_states = get_side_effect_states(),
154 // Phase
155 .phase = get_phase(),
156 };
157};
158
159} // namespace bb::avm2::simulation
const AztecAddress & get_address() const override
Definition context.hpp:146
TransactionPhase get_phase() const override
Definition context.hpp:156
InternalCallStackManagerInterface & get_internal_call_stack_manager() override
Definition context.hpp:131
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:170
uint32_t get_last_rd_size() const override
Definition context.hpp:173
Gas get_gas_used() const override
Definition context.hpp:179
const AztecAddress & get_msg_sender() const override
Definition context.hpp:147
ContextInterface & get_child_context() override
Definition context.hpp:164
SideEffectStates & get_side_effect_states() override
Definition context.hpp:150
WrittenPublicDataSlotsTreeCheckInterface & written_public_data_slots_tree
Definition context.hpp:194
std::unique_ptr< ContextInterface > child_context
Definition context.hpp:218
uint32_t get_context_id() const override
Definition context.hpp:143
uint32_t get_last_child_id() const override
Definition context.cpp:36
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:130
bool get_last_success() const override
Definition context.hpp:176
Gas get_gas_limit() const override
Definition context.hpp:180
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:192
const FF & get_transaction_fee() const override
Definition context.hpp:148
uint32_t get_pc() const override
Definition context.hpp:136
bool get_is_static() const override
Definition context.hpp:149
virtual std::optional< BytecodeId > try_get_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:272
uint32_t get_parent_cd_size() const override
Definition context.hpp:275
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:321
ContextEvent serialize_context_event() override
Definition context.cpp:124
std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:103
Gas get_parent_gas_used() const override
Definition context.hpp:324
Gas get_parent_gas_limit() const override
Definition context.hpp:325
ContextInterface & parent_context
Definition context.hpp:341
virtual AppendOnlyTreeSnapshot snapshot() const =0
uint32_t cd_offset