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 .immutables_hash = instance->immutables_hash,
35 .public_keys = PublicKeysHint{ .npk_m_hash = instance->public_keys.nullifier_key_hash,
36 .ivpk_m = instance->public_keys.incoming_viewing_key,
37 .ovpk_m_hash = instance->public_keys.outgoing_viewing_key_hash,
38 .tpk_m_hash = instance->public_keys.tagging_key_hash,
39 .mspk_m_hash = instance->public_keys.message_signing_key_hash,
40 .fbpk_m_hash = instance->public_keys.fallback_key_hash }
41
42 };
43 }
44
45 return instance;
46}
47
49{
51 // If we don't find the instance hint, this is not a catastrophic failure. The inner db should handle it, and
52 // here we simply don't store any hint:
53 if (klass.has_value()) {
57 .class_id = class_id,
58 .artifact_hash = klass->artifact_hash,
59 .private_functions_root = klass->private_functions_root,
60 // TODO(fcarreiro): find out why moving things here breaks things
61 .packed_bytecode = klass->packed_bytecode,
62 };
63 }
64
65 return klass;
66}
67
69{
70 std::optional<FF> commitment = db.get_bytecode_commitment(class_id);
71 if (commitment.has_value()) {
74 .class_id = class_id,
75 .commitment = commitment.value() };
76 }
77
78 return commitment;
79}
80
82 const FunctionSelector& selector) const
83{
85 if (name.has_value()) {
86 GetDebugFunctionNameKey key = { address, selector };
88 DebugFunctionNameHint{ .address = address, .selector = selector, .name = name.value() };
89 }
90
91 return name;
92}
93
94void HintingContractsDB::add_contracts(const ContractDeploymentData& contract_deployment_data)
95{
96 // Adding contracts does not require any hints
97 db.add_contracts(contract_deployment_data);
98}
99
101{
102 uint32_t old_checkpoint_id = get_checkpoint_id();
103 // Update underlying db:
105 // Update this db:
107 // Store hint:
109 .action_counter = checkpoint_action_counter,
110 .old_checkpoint_id = old_checkpoint_id,
111 .new_checkpoint_id = get_checkpoint_id(),
112 };
113
114 // Update this db:
116}
117
119{
120 uint32_t old_checkpoint_id = get_checkpoint_id();
121 // Update underlying db:
123 // Update this db:
124 checkpoint_stack.pop();
125 // Store hint:
127 .action_counter = checkpoint_action_counter,
128 .old_checkpoint_id = old_checkpoint_id,
129 .new_checkpoint_id = get_checkpoint_id(),
130 };
131
132 // Update this db:
134}
135
137{
138 uint32_t old_checkpoint_id = get_checkpoint_id();
139 // Update underlying db:
141 // Update this db:
142 checkpoint_stack.pop();
143 // Store hint:
145 .action_counter = checkpoint_action_counter,
146 .old_checkpoint_id = old_checkpoint_id,
147 .new_checkpoint_id = get_checkpoint_id(),
148 };
149
150 // Update this db:
152}
153
155{
156 return checkpoint_stack.top();
157}
158
160{
161 std::ranges::transform(contract_hints.contract_instances,
163 [](const auto& mapped_contract_instance) { return mapped_contract_instance.second; });
164
165 std::ranges::transform(contract_hints.contract_classes,
167 [](const auto& mapped_contract_class) { return mapped_contract_class.second; });
168
169 std::ranges::transform(contract_hints.bytecode_commitments,
171 [](const auto& mapped_bytecode_commitment) { return mapped_bytecode_commitment.second; });
172
173 std::ranges::transform(contract_hints.debug_function_names,
175 [](const auto& mapped_debug_function_name) { return mapped_debug_function_name.second; });
176
177 std::ranges::transform(
180 [](const auto& mapped_create_checkpoint_hint) { return mapped_create_checkpoint_hint.second; });
181
182 std::ranges::transform(
185 [](const auto& mapped_commit_checkpoint_hint) { return mapped_commit_checkpoint_hint.second; });
186
187 std::ranges::transform(
190 [](const auto& mapped_revert_checkpoint_hint) { return mapped_revert_checkpoint_hint.second; });
191}
192
193// Hinting MerkleDB starts.
195{
196 auto roots = db.get_tree_roots();
197 return get_tree_info_helper(tree_id, roots);
198}
199
201{
202 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
203 SiblingPath path = db.get_sibling_path(tree_id, leaf_index);
204 GetSiblingPathKey key = { tree_info, tree_id, leaf_index };
206 GetSiblingPathHint{ .hint_key = tree_info, .tree_id = tree_id, .index = leaf_index, .path = path };
207
208 return path;
209}
210
212{
213 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
215 GetPreviousValueIndexKey key = { tree_info, tree_id, value };
217 .hint_key = tree_info,
218 .tree_id = tree_id,
219 .value = value,
220 .index = resp.index,
221 .already_present = resp.is_already_present,
222 };
223
224 return resp;
225}
226
228{
229 AppendOnlyTreeSnapshot tree_info = get_tree_info(tree_id);
230 FF value = db.get_leaf_value(tree_id, leaf_index);
231 GetLeafValueKey key = { tree_info, tree_id, leaf_index };
233 GetLeafValueHint{ .hint_key = tree_info, .tree_id = tree_id, .index = leaf_index, .value = value };
234 return value;
235}
236
248
259
261 const PublicDataLeafValue& leaf_value)
262{
265 // The underlying db should update its state post insertion:
267
269 BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion");
270 BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion");
271 SequentialInsertHint<PublicDataLeafValue> sequential_insert_hint = {
272 .hint_key = tree_info,
274 .leaf = leaf_value,
275 .low_leaves_witness_data = result.low_leaf_witness_data.back(),
276 .insertion_witness_data = result.insertion_witness_data.back(),
277 .state_after = state_after
278 };
280
281 return result;
282}
283
285 const NullifierLeafValue& leaf_value)
286{
289 // The underlying db should update its state post insertion:
290 auto state_after = db.get_tree_roots().nullifier_tree;
291
293 BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion");
294 BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion");
295 SequentialInsertHint<NullifierLeafValue> sequential_insert_hint = {
296 .hint_key = tree_info,
298 .leaf = leaf_value,
299 .low_leaves_witness_data = result.low_leaf_witness_data.back(),
300 .insertion_witness_data = result.insertion_witness_data.back(),
301 .state_after = state_after
302 };
304
305 return result;
306}
307
309{
310 uint32_t old_checkpoint_id = db.get_checkpoint_id();
311 // Update underlying db:
313
314 // Store hint:
316 .action_counter = checkpoint_action_counter,
317 .old_checkpoint_id = old_checkpoint_id,
318 .new_checkpoint_id = db.get_checkpoint_id(),
319 };
320
321 // Update this db:
323}
324
326{
327 uint32_t old_checkpoint_id = db.get_checkpoint_id();
328 // Update underlying db:
330 // Store hint:
332 .action_counter = checkpoint_action_counter,
333 .old_checkpoint_id = old_checkpoint_id,
334 .new_checkpoint_id = db.get_checkpoint_id(),
335 };
336
337 // Update this db:
339}
340
342{
343 TreeSnapshots state_before = db.get_tree_roots();
344 uint32_t old_checkpoint_id = db.get_checkpoint_id();
345 // Update underlying db:
347 TreeSnapshots state_after = db.get_tree_roots();
348 // Store hint:
350 .action_counter = checkpoint_action_counter,
351 .old_checkpoint_id = old_checkpoint_id,
352 .new_checkpoint_id = db.get_checkpoint_id(),
353 .state_before = state_before,
354 .state_after = state_after,
355 };
356
357 // Update this db:
359}
360
361void HintingRawDB::pad_tree(world_state::MerkleTreeId tree_id, size_t num_leaves)
362{
363 // Padding the tree does not require any hints:
364 db.pad_tree(tree_id, num_leaves);
365}
366
368{
369 AppendOnlyTreeSnapshot state_before = get_tree_info(tree_id);
370 AppendLeavesHintKey append_key = { state_before, tree_id, std::vector<FF>(leaves.begin(), leaves.end()) };
371
372 // Update underlying db:
373 db.append_leaves(tree_id, leaves);
374
376 AppendLeavesHint{ .hint_key = state_before,
377 .state_after = get_tree_info(tree_id),
378 .tree_id = tree_id,
379 .leaves = std::vector<FF>(leaves.begin(), leaves.end()) };
380}
381
383{
384 std::ranges::transform(
387 [](const auto& mapped_get_sibling_path_hint) { return mapped_get_sibling_path_hint.second; });
388
389 std::ranges::transform(
392 [](const auto& mapped_get_previous_value_index_hint) { return mapped_get_previous_value_index_hint.second; });
393
394 std::ranges::transform(
397 [](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
398
399 std::ranges::transform(
402 [](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
403
404 std::ranges::transform(merkle_hints.get_leaf_value_hints,
406 [](const auto& mapped_get_leaf_value_hint) { return mapped_get_leaf_value_hint.second; });
407
408 std::ranges::transform(
411 [](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
412
413 std::ranges::transform(
416 [](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
417
418 std::ranges::transform(merkle_hints.append_leaves_hints,
420 [](const auto& mapped_append_leaves_hint) { return mapped_append_leaves_hint.second; });
421
422 std::ranges::transform(
425 [](const auto& mapped_create_checkpoint_hint) { return mapped_create_checkpoint_hint.second; });
426
427 std::ranges::transform(
430 [](const auto& mapped_commit_checkpoint_hint) { return mapped_commit_checkpoint_hint.second; });
431
432 std::ranges::transform(
435 [](const auto& mapped_revert_checkpoint_hint) { return mapped_revert_checkpoint_hint.second; });
436}
437
438} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
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
AVM range check gadget for witness generation.
::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
AvmFlavorSettings::FF FF
Definition field.hpp:10
FF ContractClassId
FF FunctionSelector
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:260
std::vector< SequentialInsertHint< crypto::merkle_tree::NullifierLeafValue > > sequential_insert_hints_nullifier_tree
Definition avm_io.hpp:384
std::vector< GetSiblingPathHint > get_sibling_path_hints
Definition avm_io.hpp:375
std::vector< DebugFunctionNameHint > debug_function_names
Definition avm_io.hpp:369
std::vector< ContractDBCreateCheckpointHint > contract_db_create_checkpoint_hints
Definition avm_io.hpp:370
std::vector< ContractDBCommitCheckpointHint > contract_db_commit_checkpoint_hints
Definition avm_io.hpp:371
std::vector< CommitCheckpointHint > commit_checkpoint_hints
Definition avm_io.hpp:387
std::vector< SequentialInsertHint< crypto::merkle_tree::PublicDataLeafValue > > sequential_insert_hints_public_data_tree
Definition avm_io.hpp:383
std::vector< RevertCheckpointHint > revert_checkpoint_hints
Definition avm_io.hpp:388
std::vector< ContractDBRevertCheckpointHint > contract_db_revert_checkpoint_hints
Definition avm_io.hpp:372
std::vector< GetPreviousValueIndexHint > get_previous_value_index_hints
Definition avm_io.hpp:376
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::PublicDataLeafValue > > > get_leaf_preimage_hints_public_data_tree
Definition avm_io.hpp:378
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > > > get_leaf_preimage_hints_nullifier_tree
Definition avm_io.hpp:380
std::vector< CreateCheckpointHint > create_checkpoint_hints
Definition avm_io.hpp:386
std::vector< GetLeafValueHint > get_leaf_value_hints
Definition avm_io.hpp:381
std::vector< AppendLeavesHint > append_leaves_hints
Definition avm_io.hpp:385
std::vector< ContractInstanceHint > contract_instances
Definition avm_io.hpp:366
std::vector< ContractClassHint > contract_classes
Definition avm_io.hpp:367
std::vector< BytecodeCommitmentHint > bytecode_commitments
Definition avm_io.hpp:368
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:217
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:229
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:203
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:190
AppendOnlyTreeSnapshot hint_key
Definition avm_io.hpp:242
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