Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
hinting_dbs.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4#include <optional>
5#include <span>
6#include <stdexcept>
7#include <string>
8#include <vector>
9
15
16namespace bb::avm2::simulation {
17
18// HintingContractsDB starts.
20{
22 // If we don't find the instance hint, this is not a catastrophic failure. The inner db should handle it, and
23 // here we simply don't store any hint:
24 if (instance.has_value()) {
28 .address = address,
29 .salt = instance->salt,
30 .deployer = instance->deployer,
31 .current_contract_class_id = instance->current_contract_class_id,
32 .original_contract_class_id = instance->original_contract_class_id,
33 .initialization_hash = instance->initialization_hash,
34 .public_keys =
35 PublicKeysHint{ .master_nullifier_public_key = instance->public_keys.nullifier_key,
36 .master_incoming_viewing_public_key = instance->public_keys.incoming_viewing_key,
37 .master_outgoing_viewing_public_key = instance->public_keys.outgoing_viewing_key,
38 .master_tagging_public_key = instance->public_keys.tagging_key }
39
40 };
41 }
42
43 return instance;
44}
45
47{
49 // If we don't find the instance hint, this is not a catastrophic failure. The inner db should handle it, and
50 // here we simply don't store any hint:
51 if (klass.has_value()) {
55 .class_id = class_id,
56 .artifact_hash = klass->artifact_hash,
57 .private_functions_root = klass->private_functions_root,
58 // TODO(fcarreiro): find out why moving things here breaks things
59 .packed_bytecode = klass->packed_bytecode,
60 };
61 }
62
63 return klass;
64}
65
67{
69 if (commitment.has_value()) {
72 .class_id = class_id,
73 .commitment = commitment.value() };
74 }
75
76 return commitment;
77}
78
80 const FunctionSelector& selector) const
81{
83 if (name.has_value()) {
84 GetDebugFunctionNameKey key = { address, selector };
86 DebugFunctionNameHint{ .address = address, .selector = selector, .name = name.value() };
87 }
88
89 return name;
90}
91
92void HintingContractsDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
93{
94 // Adding contracts does not require any hints
95 db.add_contracts(contract_deployment_data);
96}
97
99{
100 uint32_t old_checkpoint_id = get_checkpoint_id();
101 // Update underlying db:
103 // Update this db:
105 // Store hint:
107 .action_counter = checkpoint_action_counter,
108 .old_checkpoint_id = old_checkpoint_id,
109 .new_checkpoint_id = get_checkpoint_id(),
110 };
111
112 // Update this db:
114}
115
117{
118 uint32_t old_checkpoint_id = get_checkpoint_id();
119 // Update underlying db:
121 // Update this db:
122 checkpoint_stack.pop();
123 // Store hint:
125 .action_counter = checkpoint_action_counter,
126 .old_checkpoint_id = old_checkpoint_id,
127 .new_checkpoint_id = get_checkpoint_id(),
128 };
129
130 // Update this db:
132}
133
135{
136 uint32_t old_checkpoint_id = get_checkpoint_id();
137 // Update underlying db:
139 // Update this db:
140 checkpoint_stack.pop();
141 // Store hint:
143 .action_counter = checkpoint_action_counter,
144 .old_checkpoint_id = old_checkpoint_id,
145 .new_checkpoint_id = get_checkpoint_id(),
146 };
147
148 // Update this db:
150}
151
153{
154 return checkpoint_stack.top();
155}
156
158{
159 std::ranges::transform(contract_hints.contract_instances,
161 [](const auto& mapped_contract_instance) { return mapped_contract_instance.second; });
162
163 std::ranges::transform(contract_hints.contract_classes,
165 [](const auto& mapped_contract_class) { return mapped_contract_class.second; });
166
167 std::ranges::transform(contract_hints.bytecode_commitments,
169 [](const auto& mapped_bytecode_commitment) { return mapped_bytecode_commitment.second; });
170
171 std::ranges::transform(contract_hints.debug_function_names,
173 [](const auto& mapped_debug_function_name) { return mapped_debug_function_name.second; });
174
175 std::ranges::transform(
178 [](const auto& mapped_create_checkpoint_hint) { return mapped_create_checkpoint_hint.second; });
179
180 std::ranges::transform(
183 [](const auto& mapped_commit_checkpoint_hint) { return mapped_commit_checkpoint_hint.second; });
184
185 std::ranges::transform(
188 [](const auto& mapped_revert_checkpoint_hint) { return mapped_revert_checkpoint_hint.second; });
189}
190
191// Hinting MerkleDB starts.
193{
194 auto roots = db.get_tree_roots();
195 return get_tree_info_helper(tree_id, roots);
196}
197
199{
200 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
201 SiblingPath path = db.get_sibling_path(tree_id, leaf_index);
202 GetSiblingPathKey key = { tree_info, tree_id, leaf_index };
204 GetSiblingPathHint{ .hint_key = tree_info, .tree_id = tree_id, .index = leaf_index, .path = path };
205
206 return path;
207}
208
210{
211 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
213 GetPreviousValueIndexKey key = { tree_info, tree_id, value };
215 .hint_key = tree_info,
216 .tree_id = tree_id,
217 .value = value,
218 .index = resp.index,
219 .already_present = resp.is_already_present,
220 };
221
222 return resp;
223}
224
226{
227 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
228 FF value = db.get_leaf_value(tree_id, leaf_index);
229 GetLeafValueKey key = { tree_info, tree_id, leaf_index };
231 GetLeafValueHint{ .hint_key = tree_info, .tree_id = tree_id, .index = leaf_index, .value = value };
232 return value;
233}
234
246
257
259 const PublicDataLeafValue& leaf_value)
260{
263 // The underlying db should update its state post insertion:
265
267 BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion");
268 BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion");
269 SequentialInsertHint<PublicDataLeafValue> sequential_insert_hint = {
270 .hint_key = tree_info,
272 .leaf = leaf_value,
273 .low_leaves_witness_data = result.low_leaf_witness_data.back(),
274 .insertion_witness_data = result.insertion_witness_data.back(),
275 .state_after = state_after
276 };
278
279 return result;
280}
281
283 const NullifierLeafValue& leaf_value)
284{
287 // The underlying db should update its state post insertion:
288 auto state_after = db.get_tree_roots().nullifier_tree;
289
291 BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion");
292 BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion");
293 SequentialInsertHint<NullifierLeafValue> sequential_insert_hint = {
294 .hint_key = tree_info,
296 .leaf = leaf_value,
297 .low_leaves_witness_data = result.low_leaf_witness_data.back(),
298 .insertion_witness_data = result.insertion_witness_data.back(),
299 .state_after = state_after
300 };
302
303 return result;
304}
305
307{
308 uint32_t old_checkpoint_id = db.get_checkpoint_id();
309 // Update underlying db:
311
312 // Store hint:
314 .action_counter = checkpoint_action_counter,
315 .old_checkpoint_id = old_checkpoint_id,
316 .new_checkpoint_id = db.get_checkpoint_id(),
317 };
318
319 // Update this db:
321}
322
324{
325 uint32_t old_checkpoint_id = db.get_checkpoint_id();
326 // Update underlying db:
328 // Store hint:
330 .action_counter = checkpoint_action_counter,
331 .old_checkpoint_id = old_checkpoint_id,
332 .new_checkpoint_id = db.get_checkpoint_id(),
333 };
334
335 // Update this db:
337}
338
340{
341 TreeSnapshots state_before = db.get_tree_roots();
342 uint32_t old_checkpoint_id = db.get_checkpoint_id();
343 // Update underlying db:
345 TreeSnapshots state_after = db.get_tree_roots();
346 // Store hint:
348 .action_counter = checkpoint_action_counter,
349 .old_checkpoint_id = old_checkpoint_id,
350 .new_checkpoint_id = db.get_checkpoint_id(),
351 .state_before = state_before,
352 .state_after = state_after,
353 };
354
355 // Update this db:
357}
358
359void HintingRawDB::pad_tree(world_state::MerkleTreeId tree_id, size_t num_leaves)
360{
361 // Padding the tree does not require any hints:
362 db.pad_tree(tree_id, num_leaves);
363}
364
366{
367 AppendOnlyTreeSnapshot state_before = get_tree_info(tree_id);
368 AppendLeavesHintKey append_key = { state_before, tree_id, std::vector<FF>(leaves.begin(), leaves.end()) };
369
370 // Update underlying db:
371 db.append_leaves(tree_id, leaves);
372
374 AppendLeavesHint{ .hint_key = state_before,
375 .state_after = get_tree_info(tree_id),
376 .tree_id = tree_id,
377 .leaves = std::vector<FF>(leaves.begin(), leaves.end()) };
378}
379
381{
382 std::ranges::transform(
385 [](const auto& mapped_get_sibling_path_hint) { return mapped_get_sibling_path_hint.second; });
386
387 std::ranges::transform(
390 [](const auto& mapped_get_previous_value_index_hint) { return mapped_get_previous_value_index_hint.second; });
391
392 std::ranges::transform(
395 [](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
396
397 std::ranges::transform(
400 [](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
401
402 std::ranges::transform(merkle_hints.get_leaf_value_hints,
404 [](const auto& mapped_get_leaf_value_hint) { return mapped_get_leaf_value_hint.second; });
405
406 std::ranges::transform(
409 [](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
410
411 std::ranges::transform(
414 [](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
415
416 std::ranges::transform(merkle_hints.append_leaves_hints,
418 [](const auto& mapped_append_leaves_hint) { return mapped_append_leaves_hint.second; });
419
420 std::ranges::transform(
423 [](const auto& mapped_create_checkpoint_hint) { return mapped_create_checkpoint_hint.second; });
424
425 std::ranges::transform(
428 [](const auto& mapped_commit_checkpoint_hint) { return mapped_commit_checkpoint_hint.second; });
429
430 std::ranges::transform(
433 [](const auto& mapped_revert_checkpoint_hint) { return mapped_revert_checkpoint_hint.second; });
434}
435
436} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
std::shared_ptr< Napi::ThreadSafeFunction > instance
virtual void add_contracts(const ContractDeploymentData &contract_deployment_data)=0
virtual std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const =0
virtual std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const =0
virtual std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const =0
virtual std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const =0
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
void add_contracts(const ContractDeploymentData &contract_deployment_data) override
void dump_hints(ExecutionHints &hints)
std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const override
void dump_hints(ExecutionHints &hints)
SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value) override
IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const override
SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const override
GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF &value) const override
LowLevelMerkleDBInterface & db
void append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves) override
FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const override
IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const override
SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value) override
AppendOnlyTreeSnapshot get_tree_info(MerkleTreeId tree_id) const
void pad_tree(MerkleTreeId tree_id, size_t num_leaves) override
virtual IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const =0
virtual TreeSnapshots get_tree_roots() const =0
virtual void pad_tree(MerkleTreeId tree_id, size_t num_leaves)=0
virtual IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const =0
virtual SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value)=0
virtual GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF &value) const =0
virtual SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const =0
virtual uint32_t get_checkpoint_id() const =0
virtual SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value)=0
virtual void append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves)=0
virtual FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const =0
auto & get_tree_info_helper(world_state::MerkleTreeId tree_id, auto &tree_roots)
Definition db_types.hpp:72
::bb::crypto::merkle_tree::fr_sibling_path SiblingPath
Definition db.hpp:36
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, index_t > GetSiblingPathKey
Definition db_types.hpp:16
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, index_t > GetLeafValueKey
Definition db_types.hpp:19
std::tuple< uint32_t, ContractClassId > GetBytecodeCommitmentKey
Definition db_types.hpp:25
std::tuple< AztecAddress, FunctionSelector > GetDebugFunctionNameKey
Definition db_types.hpp:26
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, NullifierLeafValue > SequentialInsertHintNullifierTreeKey
Definition db_types.hpp:21
std::tuple< AppendOnlyTreeSnapshot, index_t > GetLeafPreimageKey
Definition db_types.hpp:18
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, PublicDataLeafValue > SequentialInsertHintPublicDataTreeKey
Definition db_types.hpp:20
std::tuple< uint32_t, AztecAddress > GetContractInstanceKey
Definition db_types.hpp:23
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, FF > GetPreviousValueIndexKey
Definition db_types.hpp:17
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, std::vector< FF > > AppendLeavesHintKey
Definition db_types.hpp:22
::bb::crypto::merkle_tree::index_t index_t
Definition db.hpp:37
std::tuple< uint32_t, ContractClassId > GetContractClassKey
Definition db_types.hpp:24
FF ContractClassId
FF FunctionSelector
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:258
std::vector< SequentialInsertHint< crypto::merkle_tree::NullifierLeafValue > > sequential_insert_hints_nullifier_tree
Definition avm_io.hpp:382
std::vector< GetSiblingPathHint > get_sibling_path_hints
Definition avm_io.hpp:373
std::vector< DebugFunctionNameHint > debug_function_names
Definition avm_io.hpp:367
std::vector< ContractDBCreateCheckpointHint > contract_db_create_checkpoint_hints
Definition avm_io.hpp:368
std::vector< ContractDBCommitCheckpointHint > contract_db_commit_checkpoint_hints
Definition avm_io.hpp:369
std::vector< CommitCheckpointHint > commit_checkpoint_hints
Definition avm_io.hpp:385
std::vector< SequentialInsertHint< crypto::merkle_tree::PublicDataLeafValue > > sequential_insert_hints_public_data_tree
Definition avm_io.hpp:381
std::vector< RevertCheckpointHint > revert_checkpoint_hints
Definition avm_io.hpp:386
std::vector< ContractDBRevertCheckpointHint > contract_db_revert_checkpoint_hints
Definition avm_io.hpp:370
std::vector< GetPreviousValueIndexHint > get_previous_value_index_hints
Definition avm_io.hpp:374
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::PublicDataLeafValue > > > get_leaf_preimage_hints_public_data_tree
Definition avm_io.hpp:376
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > > > get_leaf_preimage_hints_nullifier_tree
Definition avm_io.hpp:378
std::vector< CreateCheckpointHint > create_checkpoint_hints
Definition avm_io.hpp:384
std::vector< GetLeafValueHint > get_leaf_value_hints
Definition avm_io.hpp:379
std::vector< AppendLeavesHint > append_leaves_hints
Definition avm_io.hpp:383
std::vector< ContractInstanceHint > contract_instances
Definition avm_io.hpp:364
std::vector< ContractClassHint > contract_classes
Definition avm_io.hpp:365
std::vector< BytecodeCommitmentHint > bytecode_commitments
Definition avm_io.hpp:366
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:215
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:227
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:201
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:188
AffinePoint master_nullifier_public_key
Definition avm_io.hpp:117
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:240
AppendOnlyTreeSnapshot public_data_tree
AppendOnlyTreeSnapshot nullifier_tree
unordered_flat_map< GetContractClassKey, ContractClassHint > contract_classes
Definition db_types.hpp:31
unordered_flat_map< uint32_t, ContractDBRevertCheckpointHint > revert_checkpoint_hints
Definition db_types.hpp:36
unordered_flat_map< uint32_t, ContractDBCreateCheckpointHint > create_checkpoint_hints
Definition db_types.hpp:34
unordered_flat_map< uint32_t, ContractDBCommitCheckpointHint > commit_checkpoint_hints
Definition db_types.hpp:35
unordered_flat_map< GetDebugFunctionNameKey, DebugFunctionNameHint > debug_function_names
Definition db_types.hpp:33
unordered_flat_map< GetContractInstanceKey, ContractInstanceHint > contract_instances
Definition db_types.hpp:30
unordered_flat_map< GetBytecodeCommitmentKey, BytecodeCommitmentHint > bytecode_commitments
Definition db_types.hpp:32
unordered_flat_map< uint32_t, CreateCheckpointHint > create_checkpoint_hints
Definition db_types.hpp:55
unordered_flat_map< uint32_t, CommitCheckpointHint > commit_checkpoint_hints
Definition db_types.hpp:56
unordered_flat_map< uint32_t, RevertCheckpointHint > revert_checkpoint_hints
Definition db_types.hpp:57
unordered_flat_map< AppendLeavesHintKey, AppendLeavesHint > append_leaves_hints
Definition db_types.hpp:54
unordered_flat_map< SequentialInsertHintPublicDataTreeKey, SequentialInsertHint< PublicDataLeafValue > > sequential_insert_hints_public_data_tree
Definition db_types.hpp:51
unordered_flat_map< GetLeafPreimageKey, GetLeafPreimageHint< PublicDataTreeLeafPreimage > > get_leaf_preimage_hints_public_data_tree
Definition db_types.hpp:45
unordered_flat_map< SequentialInsertHintNullifierTreeKey, SequentialInsertHint< NullifierLeafValue > > sequential_insert_hints_nullifier_tree
Definition db_types.hpp:53
unordered_flat_map< GetSiblingPathKey, GetSiblingPathHint > get_sibling_path_hints
Definition db_types.hpp:42
unordered_flat_map< GetLeafPreimageKey, GetLeafPreimageHint< NullifierTreeLeafPreimage > > get_leaf_preimage_hints_nullifier_tree
Definition db_types.hpp:47
unordered_flat_map< GetLeafValueKey, GetLeafValueHint > get_leaf_value_hints
Definition db_types.hpp:48
unordered_flat_map< GetPreviousValueIndexKey, GetPreviousValueIndexHint > get_previous_value_index_hints
Definition db_types.hpp:43
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > insertion_witness_data