Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
public_inputs_builder.cpp
Go to the documentation of this file.
2
3#include <ranges>
4
5namespace bb::avm2::simulation {
6
8 const GlobalVariables& global_variables,
9 const ProtocolContracts& protocol_contracts,
10 const FF& prover_id,
12{
13 public_inputs_.global_variables = global_variables;
14 public_inputs_.protocol_contracts = protocol_contracts;
15 public_inputs_.prover_id = prover_id;
16 public_inputs_.start_gas_used = tx.gas_used_by_private;
17 public_inputs_.gas_settings = tx.gas_settings;
18 public_inputs_.effective_gas_fees = tx.effective_gas_fees;
19 public_inputs_.fee_payer = tx.fee_payer;
21
23 // Public Call Requests.
26 .setup_calls = static_cast<uint32_t>(tx.setup_enqueued_calls.size()),
27 .app_logic_calls = static_cast<uint32_t>(tx.app_logic_enqueued_calls.size()),
28 .teardown_call = tx.teardown_enqueued_call.has_value(),
29 };
30 // The protocol does not allow this, but from the TX object PoV the size could be larger.
31 // Therefore we keep this check. We also keep it as an exception because it's not
32 // expensive to check and it allows for a more informative error message.
33 if (tx.setup_enqueued_calls.size() > MAX_ENQUEUED_CALLS_PER_TX ||
34 tx.app_logic_enqueued_calls.size() > MAX_ENQUEUED_CALLS_PER_TX) {
35 throw std::runtime_error(
36 "Too many enqueued calls. Setup calls: " + std::to_string(tx.setup_enqueued_calls.size()) +
37 ", App logic calls: " + std::to_string(tx.app_logic_enqueued_calls.size()));
38 }
39
40 std::ranges::transform(tx.setup_enqueued_calls.begin(),
41 tx.setup_enqueued_calls.end(),
43 [](const auto& call) { return call.request; });
44 std::ranges::transform(tx.app_logic_enqueued_calls,
46 [](const auto& call) { return call.request; });
47
48 // If there is a teardown call, set it. Otherwise it will be full of zeroes.
49 if (tx.teardown_enqueued_call.has_value()) {
50 public_inputs_.public_teardown_call_request = tx.teardown_enqueued_call.value().request;
51 }
52
54 // Side effects from private.
56
57 // Note that the side effects that come from private, both revertible and non-revertible,
58 // need to be in the `**previous**(Non)RevertibleAccumulatedData` part of the public inputs.
59 // This is irrespective of whether they end up inserted, or reverted in the actual execution.
60
61 // Non-revertible.
63 .note_hashes = static_cast<uint32_t>(tx.non_revertible_accumulated_data.note_hashes.size()),
64 .nullifiers = static_cast<uint32_t>(tx.non_revertible_accumulated_data.nullifiers.size()),
65 .l2_to_l1_msgs = static_cast<uint32_t>(tx.non_revertible_accumulated_data.l2_to_l1_messages.size()),
66 };
67 // The protocol does not allow this, but from the TX object PoV the size could be larger.
68 // Therefore we keep this check. We also keep it as an exception because it's not
69 // expensive to check and it allows for a more informative error message.
70 if (tx.non_revertible_accumulated_data.note_hashes.size() > MAX_NOTE_HASHES_PER_TX ||
71 tx.non_revertible_accumulated_data.nullifiers.size() > MAX_NULLIFIERS_PER_TX ||
72 tx.non_revertible_accumulated_data.l2_to_l1_messages.size() > MAX_L2_TO_L1_MSGS_PER_TX) {
73 throw std::runtime_error(
74 "Too many non-revertible side effects from private. Note hashes: " +
75 std::to_string(tx.non_revertible_accumulated_data.note_hashes.size()) +
76 ", Nullifiers: " + std::to_string(tx.non_revertible_accumulated_data.nullifiers.size()) +
77 ", L2 to L1 messages: " + std::to_string(tx.non_revertible_accumulated_data.l2_to_l1_messages.size()));
78 }
79
80 std::ranges::copy(tx.non_revertible_accumulated_data.note_hashes,
82 std::ranges::copy(tx.non_revertible_accumulated_data.nullifiers,
84 std::ranges::copy(tx.non_revertible_accumulated_data.l2_to_l1_messages,
86
87 // Revertible.
89 .note_hashes = static_cast<uint32_t>(tx.revertible_accumulated_data.note_hashes.size()),
90 .nullifiers = static_cast<uint32_t>(tx.revertible_accumulated_data.nullifiers.size()),
91 .l2_to_l1_msgs = static_cast<uint32_t>(tx.revertible_accumulated_data.l2_to_l1_messages.size()),
92 };
93 // The protocol does not allow this, but from the TX object PoV the size could be larger.
94 // Therefore we keep this check. We also keep it as an exception because it's not
95 // expensive to check and it allows for a more informative error message.
96 if (tx.revertible_accumulated_data.note_hashes.size() > MAX_NOTE_HASHES_PER_TX ||
97 tx.revertible_accumulated_data.nullifiers.size() > MAX_NULLIFIERS_PER_TX ||
98 tx.revertible_accumulated_data.l2_to_l1_messages.size() > MAX_L2_TO_L1_MSGS_PER_TX) {
99 throw std::runtime_error(
100 "Too many revertible side effects from private. Note hashes: " +
101 std::to_string(tx.revertible_accumulated_data.note_hashes.size()) +
102 ", Nullifiers: " + std::to_string(tx.revertible_accumulated_data.nullifiers.size()) +
103 ", L2 to L1 messages: " + std::to_string(tx.revertible_accumulated_data.l2_to_l1_messages.size()));
104 }
105
106 std::ranges::copy(tx.revertible_accumulated_data.note_hashes,
108 std::ranges::copy(tx.revertible_accumulated_data.nullifiers,
110 std::ranges::copy(tx.revertible_accumulated_data.l2_to_l1_messages,
112
113 return *this;
114}
115
117 const Gas& end_gas_used,
118 const FF& transaction_fee,
119 const bool reverted,
120 const TrackedSideEffects& side_effects)
121{
123 public_inputs_.end_gas_used = end_gas_used;
124 public_inputs_.transaction_fee = transaction_fee;
125 public_inputs_.reverted = reverted;
126
128 // accumulated_data_array_lengths.
131 .note_hashes = static_cast<uint32_t>(side_effects.note_hashes.size()),
132 .nullifiers = static_cast<uint32_t>(side_effects.nullifiers.size()),
133 .l2_to_l1_msgs = static_cast<uint32_t>(side_effects.l2_to_l1_messages.size()),
134 .public_data_writes = static_cast<uint32_t>(side_effects.storage_writes_slot_to_value.size()),
135 };
136 // If this happens, it's a bug in our code. We keep it as an exception because it's not
137 // expensive to check and it allows for a more informative error message.
138 if (side_effects.note_hashes.size() > MAX_NOTE_HASHES_PER_TX ||
139 side_effects.nullifiers.size() > MAX_NULLIFIERS_PER_TX ||
140 side_effects.l2_to_l1_messages.size() > MAX_L2_TO_L1_MSGS_PER_TX ||
142 throw std::runtime_error(
143 "Too many side effects. Note hashes: " + std::to_string(side_effects.note_hashes.size()) +
144 ", Nullifiers: " + std::to_string(side_effects.nullifiers.size()) +
145 ", L2 to L1 messages: " + std::to_string(side_effects.l2_to_l1_messages.size()) +
146 ", Storage writes: " + std::to_string(side_effects.storage_writes_slot_to_value.size()));
147 }
148
150 // accumulated_data.
152 std::ranges::copy(side_effects.note_hashes, public_inputs_.accumulated_data.note_hashes.begin());
153 std::ranges::copy(side_effects.nullifiers, public_inputs_.accumulated_data.nullifiers.begin());
154 std::ranges::copy(side_effects.l2_to_l1_messages, public_inputs_.accumulated_data.l2_to_l1_msgs.begin());
156 // We need to copy the storage writes slot to value in the order of the slots by insertion.
157 for (uint32_t i = 0; i < side_effects.storage_writes_slots_by_insertion.size(); i++) {
158 const auto& slot = side_effects.storage_writes_slots_by_insertion.at(i);
159 const auto& value = side_effects.storage_writes_slot_to_value.at(slot);
161 }
162
163 return *this;
164}
165
166} // namespace bb::avm2::simulation
#define MAX_ENQUEUED_CALLS_PER_TX
#define MAX_L2_TO_L1_MSGS_PER_TX
#define MAX_NOTE_HASHES_PER_TX
#define MAX_NULLIFIERS_PER_TX
#define MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
StrictMock< MockHighLevelMerkleDB > merkle_db
PublicInputsBuilder & extract_inputs(const Tx &tx, const GlobalVariables &global_variables, const ProtocolContracts &protocol_contracts, const FF &prover_id, const LowLevelMerkleDBInterface &merkle_db)
PublicInputsBuilder & extract_outputs(const LowLevelMerkleDBInterface &merkle_db, const Gas &end_gas_used, const FF &transaction_fee, const bool reverted, const TrackedSideEffects &side_effects)
AvmFlavorSettings::FF FF
Definition field.hpp:10
std::string to_string(bb::avm2::ValueTag tag)
std::array< FF, MAX_NULLIFIERS_PER_TX > nullifiers
std::array< PublicDataWrite, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX > public_data_writes
std::array< ScopedL2ToL1Message, MAX_L2_TO_L1_MSGS_PER_TX > l2_to_l1_msgs
std::array< FF, MAX_NOTE_HASHES_PER_TX > note_hashes
std::array< FF, MAX_NOTE_HASHES_PER_TX > note_hashes
std::array< ScopedL2ToL1Message, MAX_L2_TO_L1_MSGS_PER_TX > l2_to_l1_msgs
std::array< FF, MAX_NULLIFIERS_PER_TX > nullifiers
PublicCallRequest public_teardown_call_request
Definition avm_io.hpp:40
PrivateToAvmAccumulatedDataArrayLengths previous_non_revertible_accumulated_data_array_lengths
Definition avm_io.hpp:41
TreeSnapshots end_tree_snapshots
Definition avm_io.hpp:47
PrivateToAvmAccumulatedData previous_non_revertible_accumulated_data
Definition avm_io.hpp:43
GasSettings gas_settings
Definition avm_io.hpp:33
GlobalVariables global_variables
Definition avm_io.hpp:29
GasFees effective_gas_fees
Definition avm_io.hpp:34
PublicCallRequestArrayLengths public_call_request_array_lengths
Definition avm_io.hpp:37
AztecAddress fee_payer
Definition avm_io.hpp:35
TreeSnapshots start_tree_snapshots
Definition avm_io.hpp:31
std::array< PublicCallRequest, MAX_ENQUEUED_CALLS_PER_TX > public_app_logic_call_requests
Definition avm_io.hpp:39
ProtocolContracts protocol_contracts
Definition avm_io.hpp:30
AvmAccumulatedDataArrayLengths accumulated_data_array_lengths
Definition avm_io.hpp:49
PrivateToAvmAccumulatedDataArrayLengths previous_revertible_accumulated_data_array_lengths
Definition avm_io.hpp:42
std::array< PublicCallRequest, MAX_ENQUEUED_CALLS_PER_TX > public_setup_call_requests
Definition avm_io.hpp:38
AvmAccumulatedData accumulated_data
Definition avm_io.hpp:50
PrivateToAvmAccumulatedData previous_revertible_accumulated_data
Definition avm_io.hpp:44
std::unordered_map< FF, FF > storage_writes_slot_to_value
std::vector< ScopedL2ToL1Message > l2_to_l1_messages