Skip to content

Commit db22b6d

Browse files
committed
feat: store hints more sanely, cleanup, some renaming, add some todos/docs
1 parent 0bff304 commit db22b6d

File tree

9 files changed

+104
-90
lines changed

9 files changed

+104
-90
lines changed

barretenberg/cpp/src/barretenberg/vm2/api_avm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void avm_simulate(const std::filesystem::path& inputs_path)
8484
AVM_TRACK_TIME("command/avm_simulate", {
8585
avm2::AvmAPI avm;
8686
auto inputs = avm2::AvmAPI::ProvingInputs::from(read_file(inputs_path));
87+
// TODO(MW): add simulate_without_hinted_dbs version, either as input option or new API method
8788
avm.simulate_with_hinted_dbs(inputs);
8889
});
8990

barretenberg/cpp/src/barretenberg/vm2/avm_sim_api.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ TxSimulationResult AvmSimAPI::simulate_with_hinted_dbs(const ProvingInputs& inpu
1212
{
1313
info("Simulating...");
1414
AvmSimulationHelper simulation_helper;
15+
// TODO(MW): add simulate_without_hinted_dbs version, either as input option or new API method
1516
auto result = AVM_TRACK_TIME_V("simulation/all", simulation_helper.simulate_fast_with_hinted_dbs(inputs.hints));
1617

1718
if (debug_logging) {
1819
// TODO(fcarreiro): Enable once PI generation is complete.
1920
// BB_ASSERT_EQ(inputs.publicInputs, result.public_inputs);
21+
// TODO(MW): Add hint checks here (if hinting is controlled by input option) or in new
22+
// simulate_without_hinted_dbs method
2023
}
2124

2225
return result;

barretenberg/cpp/src/barretenberg/vm2/avm_sim_api.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AvmSimAPI {
1111
AvmSimAPI() = default;
1212

1313
TxSimulationResult simulate_with_hinted_dbs(const AvmProvingInputs& inputs);
14+
// TODO(MW): add simulate_without_hinted_dbs version, either as input option or new API method
1415
};
1516

1617
} // namespace bb::avm2

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/db_types.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,30 @@ using SequentialInsertHintPublicDataTreeKey = std::tuple<AppendOnlyTreeSnapshot,
2020
using SequentialInsertHintNullifierTreeKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, NullifierLeafValue>;
2121
using AppendLeavesHintKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, std::vector<FF>>;
2222

23-
// TODO(MW): Temp struct for query hints to allow using a ref in the HintingContractsDB class constructor
23+
// TODO(MW): Temp struct for hints to allow using a ref in the HintingContractsDB class constructor
2424
struct MappedContractHints {
2525
unordered_flat_map<AztecAddress, ContractInstanceHint> contract_instances;
2626
unordered_flat_map<ContractClassId, ContractClassHint> contract_classes;
2727
unordered_flat_map<ContractClassId, BytecodeCommitmentHint> bytecode_commitments;
2828
};
2929

30-
// TODO(MW): Temp struct for query hints to allow using a ref in the HintingRawDB class constructor
31-
struct MappedQueryHints {
32-
unordered_flat_map<GetSiblingPathKey, SiblingPath> get_sibling_path_hints;
33-
unordered_flat_map<GetPreviousValueIndexKey, GetLowIndexedLeafResponse> get_previous_value_index_hints;
34-
unordered_flat_map<GetLeafPreimageKey, IndexedLeaf<PublicDataLeafValue>> get_leaf_preimage_hints_public_data_tree;
35-
unordered_flat_map<GetLeafPreimageKey, IndexedLeaf<NullifierLeafValue>> get_leaf_preimage_hints_nullifier_tree;
36-
unordered_flat_map<GetLeafValueKey, FF> get_leaf_value_hints;
30+
// TODO(MW): Temp struct for hints to allow using a ref in the HintingRawDB class constructor
31+
struct MappedMerkleHints {
32+
// Query hints:
33+
unordered_flat_map<GetSiblingPathKey, GetSiblingPathHint> get_sibling_path_hints;
34+
unordered_flat_map<GetPreviousValueIndexKey, GetPreviousValueIndexHint> get_previous_value_index_hints;
35+
unordered_flat_map<GetLeafPreimageKey, GetLeafPreimageHint<PublicDataTreeLeafPreimage>>
36+
get_leaf_preimage_hints_public_data_tree;
37+
unordered_flat_map<GetLeafPreimageKey, GetLeafPreimageHint<NullifierTreeLeafPreimage>>
38+
get_leaf_preimage_hints_nullifier_tree;
39+
unordered_flat_map<GetLeafValueKey, GetLeafValueHint> get_leaf_value_hints;
40+
// State modification hints:
41+
unordered_flat_map<SequentialInsertHintPublicDataTreeKey, SequentialInsertHint<PublicDataLeafValue>>
42+
sequential_insert_hints_public_data_tree;
43+
unordered_flat_map<SequentialInsertHintNullifierTreeKey, SequentialInsertHint<NullifierLeafValue>>
44+
sequential_insert_hints_nullifier_tree;
45+
unordered_flat_map<AppendLeavesHintKey, AppendLeavesHint> append_leaves_hints;
46+
// TODO(MW): Add checkpoint hints here?
3747
};
3848

3949
struct TreeCounters {

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ std::optional<ContractInstance> HintingContractsDB::get_contract_instance(const
4444
// here we simply don't store any hint:
4545
if (instance.has_value()) {
4646
// TODO(MW): Use/write instance to hint methods for ContractInstance, PublicKeys, ContractClass, etc.
47-
mapped_hints.contract_instances[address] = ContractInstanceHint{
47+
contract_hints.contract_instances[address] = ContractInstanceHint{
4848
.address = address,
4949
.salt = instance->salt,
5050
.deployer = instance->deployer_addr,
@@ -69,15 +69,15 @@ std::optional<ContractClass> HintingContractsDB::get_contract_class(const Contra
6969
// here we simply don't store any hint:
7070
if (klass.has_value()) {
7171
// TODO(MW): Use/write instance to hint methods for ContractInstance, PublicKeys, ContractClass, etc.
72-
mapped_hints.contract_classes[class_id] = ContractClassHint{
72+
contract_hints.contract_classes[class_id] = ContractClassHint{
7373
.classId = class_id,
7474
.artifactHash = klass->artifact_hash,
7575
.privateFunctionsRoot = klass->private_function_root,
7676
.packedBytecode = klass->packed_bytecode,
7777
};
7878
// Note: HintedRawContractDB accesses the bytecode commitment 'hint' during get_contract_class, so following
7979
// same logic here:
80-
mapped_hints.bytecode_commitments[class_id] =
80+
contract_hints.bytecode_commitments[class_id] =
8181
BytecodeCommitmentHint{ .classId = class_id, .commitment = klass->public_bytecode_commitment };
8282
}
8383

@@ -87,13 +87,13 @@ std::optional<ContractClass> HintingContractsDB::get_contract_class(const Contra
8787
void HintingContractsDB::dump_hints(ExecutionHints& hints)
8888
{
8989
// TODO(MW): better way than to iterate? do we want push_back?
90-
for (const auto& contract_instance : mapped_hints.contract_instances) {
90+
for (const auto& contract_instance : contract_hints.contract_instances) {
9191
hints.contractInstances.push_back(contract_instance.second);
9292
}
93-
for (const auto& contract_class : mapped_hints.contract_classes) {
93+
for (const auto& contract_class : contract_hints.contract_classes) {
9494
hints.contractClasses.push_back(contract_class.second);
9595
}
96-
for (const auto& bytecode_commitment : mapped_hints.bytecode_commitments) {
96+
for (const auto& bytecode_commitment : contract_hints.bytecode_commitments) {
9797
hints.bytecodeCommitments.push_back(bytecode_commitment.second);
9898
}
9999
}
@@ -109,7 +109,8 @@ SiblingPath HintingRawDB::get_sibling_path(world_state::MerkleTreeId tree_id, in
109109
auto tree_info = get_tree_info(tree_id);
110110
auto path = db.get_sibling_path(tree_id, leaf_index);
111111
GetSiblingPathKey key = { tree_info, tree_id, leaf_index };
112-
query_hints.get_sibling_path_hints[key] = path;
112+
merkle_hints.get_sibling_path_hints[key] =
113+
GetSiblingPathHint{ .hintKey = tree_info, .treeId = tree_id, .index = leaf_index, .path = path };
113114

114115
return path;
115116
}
@@ -119,7 +120,14 @@ GetLowIndexedLeafResponse HintingRawDB::get_low_indexed_leaf(world_state::Merkle
119120
auto tree_info = get_tree_info(tree_id);
120121
auto resp = db.get_low_indexed_leaf(tree_id, value);
121122
GetPreviousValueIndexKey key = { tree_info, tree_id, value };
122-
query_hints.get_previous_value_index_hints[key] = { resp.is_already_present, resp.index };
123+
merkle_hints.get_previous_value_index_hints[key] = GetPreviousValueIndexHint{
124+
.hintKey = tree_info,
125+
.treeId = tree_id,
126+
.value = value,
127+
.index = resp.index,
128+
.alreadyPresent = resp.is_already_present,
129+
};
130+
123131
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
124132
get_sibling_path(tree_id, resp.index);
125133

@@ -146,7 +154,8 @@ FF HintingRawDB::get_leaf_value(world_state::MerkleTreeId tree_id, index_t leaf_
146154
auto tree_info = get_tree_info(tree_id);
147155
auto value = db.get_leaf_value(tree_id, leaf_index);
148156
GetLeafValueKey key = { tree_info, tree_id, leaf_index };
149-
query_hints.get_leaf_value_hints[key] = value;
157+
merkle_hints.get_leaf_value_hints[key] =
158+
GetLeafValueHint{ .hintKey = tree_info, .treeId = tree_id, .index = leaf_index, .value = value };
150159
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
151160
get_sibling_path(tree_id, leaf_index);
152161
return value;
@@ -158,7 +167,9 @@ IndexedLeaf<PublicDataLeafValue> HintingRawDB::get_leaf_preimage_public_data_tre
158167
auto preimage = db.get_leaf_preimage_public_data_tree(leaf_index);
159168

160169
GetLeafPreimageKey key = { tree_info, leaf_index };
161-
query_hints.get_leaf_preimage_hints_public_data_tree[key] = preimage;
170+
merkle_hints.get_leaf_preimage_hints_public_data_tree[key] = GetLeafPreimageHint<PublicDataTreeLeafPreimage>{
171+
.hintKey = tree_info, .index = leaf_index, .leafPreimage = preimage
172+
};
162173
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
163174
get_sibling_path(world_state::MerkleTreeId::PUBLIC_DATA_TREE, leaf_index);
164175
return preimage;
@@ -169,7 +180,9 @@ IndexedLeaf<NullifierLeafValue> HintingRawDB::get_leaf_preimage_nullifier_tree(i
169180
auto tree_info = get_tree_info(world_state::MerkleTreeId::NULLIFIER_TREE);
170181
auto preimage = db.get_leaf_preimage_nullifier_tree(leaf_index);
171182
GetLeafPreimageKey key = { tree_info, leaf_index };
172-
query_hints.get_leaf_preimage_hints_nullifier_tree[key] = preimage;
183+
merkle_hints.get_leaf_preimage_hints_nullifier_tree[key] = GetLeafPreimageHint<NullifierTreeLeafPreimage>{
184+
.hintKey = tree_info, .index = leaf_index, .leafPreimage = preimage
185+
};
173186
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
174187
get_sibling_path(world_state::MerkleTreeId::NULLIFIER_TREE, leaf_index);
175188
return preimage;
@@ -192,7 +205,7 @@ SequentialInsertionResult<PublicDataLeafValue> HintingRawDB::insert_indexed_leav
192205
.insertionWitnessData = result.insertion_witness_data.back(),
193206
.stateAfter = stateAfter
194207
};
195-
sequential_insert_hints_public_data_tree[key] = sequential_insert_hint;
208+
merkle_hints.sequential_insert_hints_public_data_tree[key] = sequential_insert_hint;
196209

197210
return result;
198211
}
@@ -214,7 +227,7 @@ SequentialInsertionResult<NullifierLeafValue> HintingRawDB::insert_indexed_leave
214227
.insertionWitnessData = result.insertion_witness_data.back(),
215228
.stateAfter = stateAfter
216229
};
217-
sequential_insert_hints_nullifier_tree[key] = sequential_insert_hint;
230+
merkle_hints.sequential_insert_hints_nullifier_tree[key] = sequential_insert_hint;
218231

219232
return result;
220233
}
@@ -308,58 +321,44 @@ AppendOnlyTreeSnapshot HintingRawDB::appendLeafInternal(AppendOnlyTreeSnapshot s
308321
AppendLeavesHintKey append_key = { state_before, tree_id, { leaf } };
309322
AppendOnlyTreeSnapshot state_after = { .root = root_after,
310323
.nextAvailableLeafIndex = state_before.nextAvailableLeafIndex + 1 };
311-
append_leaves_hints[append_key] = state_after;
324+
merkle_hints.append_leaves_hints[append_key] =
325+
AppendLeavesHint{ .hintKey = state_before, .stateAfter = state_after, .treeId = tree_id, .leaves = { leaf } };
312326
// TODO(MW): Storing sibling path hint manually using the result since a get_sibling_path() call here will use the
313327
// /current/ db.get_tree_info() (post full append_leaves), which may not match that at result.root. We may not care
314328
// about this (see comment in PureRawMerkleDB::append_leaves())
315329
GetSiblingPathKey path_key = { state_after, tree_id, state_before.nextAvailableLeafIndex };
316-
query_hints.get_sibling_path_hints[path_key] = path;
330+
merkle_hints.get_sibling_path_hints[path_key] = GetSiblingPathHint{
331+
.hintKey = state_after, .treeId = tree_id, .index = state_before.nextAvailableLeafIndex, .path = path
332+
};
317333
return state_after;
318334
}
319335

320336
void HintingRawDB::dump_hints(ExecutionHints& hints)
321337
{
322338
// TODO(MW): better way than to iterate? do we want push_back?
323-
for (const auto& get_sibling_path_hint : query_hints.get_sibling_path_hints) {
324-
auto [hint_key, tree_id, index] = get_sibling_path_hint.first;
325-
hints.getSiblingPathHints.push_back(GetSiblingPathHint{
326-
.hintKey = hint_key, .treeId = tree_id, .index = index, .path = get_sibling_path_hint.second });
339+
for (const auto& get_sibling_path_hint : merkle_hints.get_sibling_path_hints) {
340+
hints.getSiblingPathHints.push_back(get_sibling_path_hint.second);
327341
}
328-
for (const auto& get_previous_value_index_hint : query_hints.get_previous_value_index_hints) {
329-
auto [hint_key, tree_id, value] = get_previous_value_index_hint.first;
330-
hints.getPreviousValueIndexHints.push_back(GetPreviousValueIndexHint{
331-
.hintKey = hint_key,
332-
.treeId = tree_id,
333-
.value = value,
334-
.index = get_previous_value_index_hint.second.index,
335-
.alreadyPresent = get_previous_value_index_hint.second.is_already_present,
336-
});
342+
for (const auto& get_previous_value_index_hint : merkle_hints.get_previous_value_index_hints) {
343+
hints.getPreviousValueIndexHints.push_back(get_previous_value_index_hint.second);
337344
}
338-
for (const auto& get_leaf_preimage_hint : query_hints.get_leaf_preimage_hints_public_data_tree) {
339-
auto [hint_key, index] = get_leaf_preimage_hint.first;
340-
hints.getLeafPreimageHintsPublicDataTree.push_back(
341-
{ .hintKey = hint_key, .index = index, .leafPreimage = get_leaf_preimage_hint.second });
345+
for (const auto& get_leaf_preimage_hint : merkle_hints.get_leaf_preimage_hints_public_data_tree) {
346+
hints.getLeafPreimageHintsPublicDataTree.push_back(get_leaf_preimage_hint.second);
342347
}
343-
for (const auto& get_leaf_preimage_hint : query_hints.get_leaf_preimage_hints_nullifier_tree) {
344-
auto [hint_key, index] = get_leaf_preimage_hint.first;
345-
hints.getLeafPreimageHintsNullifierTree.push_back(
346-
{ .hintKey = hint_key, .index = index, .leafPreimage = get_leaf_preimage_hint.second });
348+
for (const auto& get_leaf_preimage_hint : merkle_hints.get_leaf_preimage_hints_nullifier_tree) {
349+
hints.getLeafPreimageHintsNullifierTree.push_back(get_leaf_preimage_hint.second);
347350
}
348-
for (const auto& get_leaf_value_hint : query_hints.get_leaf_value_hints) {
349-
auto [hint_key, tree_id, index] = get_leaf_value_hint.first;
350-
hints.getLeafValueHints.push_back(GetLeafValueHint{
351-
.hintKey = hint_key, .treeId = tree_id, .index = index, .value = get_leaf_value_hint.second });
351+
for (const auto& get_leaf_value_hint : merkle_hints.get_leaf_value_hints) {
352+
hints.getLeafValueHints.push_back(get_leaf_value_hint.second);
352353
}
353-
for (const auto& sequential_insert_hint : sequential_insert_hints_public_data_tree) {
354+
for (const auto& sequential_insert_hint : merkle_hints.sequential_insert_hints_public_data_tree) {
354355
hints.sequentialInsertHintsPublicDataTree.push_back(sequential_insert_hint.second);
355356
}
356-
for (const auto& sequential_insert_hint : sequential_insert_hints_nullifier_tree) {
357+
for (const auto& sequential_insert_hint : merkle_hints.sequential_insert_hints_nullifier_tree) {
357358
hints.sequentialInsertHintsNullifierTree.push_back(sequential_insert_hint.second);
358359
}
359-
for (const auto& append_leaves_hint : append_leaves_hints) {
360-
auto [hint_key, tree_id, leaves] = append_leaves_hint.first;
361-
hints.appendLeavesHints.push_back(AppendLeavesHint{
362-
.hintKey = hint_key, .stateAfter = append_leaves_hint.second, .treeId = tree_id, .leaves = leaves });
360+
for (const auto& append_leaves_hint : merkle_hints.append_leaves_hints) {
361+
hints.appendLeavesHints.push_back(append_leaves_hint.second);
363362
}
364363
for (const auto& create_checkpoint_hint : create_checkpoint_hints) {
365364
hints.createCheckpointHints.push_back(create_checkpoint_hint.second);

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.hpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace bb::avm2::simulation {
2121

2222
class HintingContractsDB final : public ContractDBInterface {
2323
public:
24-
HintingContractsDB(ContractDBInterface& db, MappedContractHints& mapped_hints)
24+
HintingContractsDB(ContractDBInterface& db, MappedContractHints& contract_hints)
2525
: db(db)
26-
, mapped_hints(mapped_hints)
26+
, contract_hints(contract_hints)
2727
{}
2828

2929
std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) const override;
@@ -35,20 +35,15 @@ class HintingContractsDB final : public ContractDBInterface {
3535
private:
3636
ContractDBInterface& db;
3737
// TODO(MW): Just here so we can use a ref to query hints and not remove const from getters
38-
MappedContractHints& mapped_hints;
39-
40-
// unordered_flat_map<AztecAddress, ContractInstanceHint>& contract_instances;
41-
// unordered_flat_map<ContractClassId, ContractClassHint>& contract_classes;
42-
// // TODO(MW): below required? Exists in HintedRawContractDB and used for dumping hints.
43-
// unordered_flat_map<ContractClassId, BytecodeCommitmentHint>& bytecode_commitments;
38+
MappedContractHints& contract_hints;
4439
};
4540

4641
class HintingRawDB final : public LowLevelMerkleDBInterface {
4742
public:
48-
HintingRawDB(LowLevelMerkleDBInterface& db, MappedQueryHints& query_hints)
43+
HintingRawDB(LowLevelMerkleDBInterface& db, MappedMerkleHints& merkle_hints)
4944
: db(db)
50-
, query_hints(
51-
query_hints) // TODO(MW): Just here so we can use a ref to query hints and not remove const from getters
45+
, merkle_hints(
46+
merkle_hints) // TODO(MW): Just here so we can use a ref to query hints and not remove const from getters
5247
{}
5348

5449
TreeSnapshots get_tree_roots() const override { return db.get_tree_roots(); }
@@ -82,18 +77,7 @@ class HintingRawDB final : public LowLevelMerkleDBInterface {
8277

8378
// Query hints.
8479
// TODO(MW): Just here so we can use a ref to query hints and not remove const from getters:
85-
MappedQueryHints& query_hints;
86-
// unordered_flat_map<GetSiblingPathKey, SiblingPath>& get_sibling_path_hints;
87-
// unordered_flat_map<GetPreviousValueIndexKey, GetLowIndexedLeafResponse>& get_previous_value_index_hints;
88-
// unordered_flat_map<GetLeafPreimageKey, IndexedLeaf<PublicDataLeafValue>>&
89-
// get_leaf_preimage_hints_public_data_tree; unordered_flat_map<GetLeafPreimageKey,
90-
// IndexedLeaf<NullifierLeafValue>>& get_leaf_preimage_hints_nullifier_tree; unordered_flat_map<GetLeafValueKey,
91-
// FF>& get_leaf_value_hints; State modification hints.
92-
unordered_flat_map<SequentialInsertHintPublicDataTreeKey, SequentialInsertHint<PublicDataLeafValue>>
93-
sequential_insert_hints_public_data_tree;
94-
unordered_flat_map<SequentialInsertHintNullifierTreeKey, SequentialInsertHint<NullifierLeafValue>>
95-
sequential_insert_hints_nullifier_tree;
96-
unordered_flat_map<AppendLeavesHintKey, AppendOnlyTreeSnapshot> append_leaves_hints;
80+
MappedMerkleHints& merkle_hints;
9781
unordered_flat_map</*action_counter*/ uint32_t, CreateCheckpointHint> create_checkpoint_hints;
9882
unordered_flat_map</*action_counter*/ uint32_t, CommitCheckpointHint> commit_checkpoint_hints;
9983
unordered_flat_map</*action_counter*/ uint32_t, RevertCheckpointHint> revert_checkpoint_hints;

0 commit comments

Comments
 (0)