Skip to content

Commit 208fcb4

Browse files
committed
feat: rework some storage read collection, change update_check, bring in changes from pi PR
1 parent 7415bd3 commit 208fcb4

File tree

9 files changed

+271
-16
lines changed

9 files changed

+271
-16
lines changed

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/response.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ template <typename LeafType> struct LeafUpdateWitnessData {
7171
LeafUpdateWitnessData(LeafUpdateWitnessData&& other) noexcept = default;
7272
LeafUpdateWitnessData& operator=(const LeafUpdateWitnessData& other) = default;
7373
LeafUpdateWitnessData& operator=(LeafUpdateWitnessData&& other) noexcept = default;
74+
bool operator==(const LeafUpdateWitnessData& other) const = default;
7475

7576
MSGPACK_FIELDS(leaf, index, path);
7677
};

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ TxSimulationResult AvmSimAPI::simulate_with_hinted_dbs(const ProvingInputs& inpu
1414
AvmSimulationHelper simulation_helper;
1515
auto result = AVM_TRACK_TIME_V("simulation/all", simulation_helper.simulate_fast_with_hinted_dbs(inputs.hints));
1616

17-
if (debug_logging) {
18-
// TODO(fcarreiro): Enable once PI generation is complete.
19-
// BB_ASSERT_EQ(inputs.publicInputs, result.public_inputs);
20-
}
21-
2217
return result;
2318
}
2419

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/tx_execution.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class TxExecution final {
5151

5252
const TxContext& get_tx_context() const { return tx_context; }
5353

54+
const TxContext& get_tx_context() const { return tx_context; }
55+
5456
private:
5557
ExecutionInterface& call_execution;
5658
ContextProviderInterface& context_provider;

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/update_check.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ namespace {
1111

1212
using UnconstrainedPoseidon2 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>;
1313

14-
FF unconstrained_read(LowLevelMerkleDBInterface& merkle_db, const FF& leaf_slot)
14+
FF unconstrained_read(const LowLevelMerkleDBInterface& merkle_db, const FF& leaf_slot)
1515
{
1616
auto [present, index] = merkle_db.get_low_indexed_leaf(world_state::MerkleTreeId::PUBLIC_DATA_TREE, leaf_slot);
17+
info("performing unconstrained read for slot: ", leaf_slot, ", w res index: ", index);
1718
auto preimage = merkle_db.get_leaf_preimage_public_data_tree(index);
19+
info("performing get leaf preimage for index: ", index, ", got value: ", preimage.leaf.value);
1820
return present ? preimage.leaf.value : 0;
1921
}
2022

@@ -23,6 +25,7 @@ FF unconstrained_read(LowLevelMerkleDBInterface& merkle_db, const FF& leaf_slot)
2325
void UpdateCheck::check_current_class_id(const AztecAddress& address, const ContractInstance& instance)
2426
{
2527
// Compute the public data tree slots
28+
info("should arrive here after instance");
2629
FF delayed_public_mutable_slot = poseidon2.hash({ UPDATED_CLASS_IDS_SLOT, address });
2730
FF delayed_public_mutable_hash_slot = delayed_public_mutable_slot + UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN;
2831
// Read the hash from the tree. We do a trick with delayed public mutables (updates are delayed public mutables)
@@ -38,6 +41,13 @@ void UpdateCheck::check_current_class_id(const AztecAddress& address, const Cont
3841
uint64_t current_timestamp = globals.timestamp;
3942

4043
if (hash == 0) {
44+
info("hash 0, not performing unconstrained reads, would be for slots:");
45+
info(unconstrained_compute_leaf_slot(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS, delayed_public_mutable_slot));
46+
info(unconstrained_compute_leaf_slot(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
47+
delayed_public_mutable_slot + 1));
48+
info(unconstrained_compute_leaf_slot(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
49+
delayed_public_mutable_slot + 2));
50+
4151
// If the delayed public mutable has never been written, then the contract was never updated. We short circuit
4252
// early.
4353
if (instance.original_class_id != instance.current_class_id) {
@@ -52,6 +62,7 @@ void UpdateCheck::check_current_class_id(const AztecAddress& address, const Cont
5262
for (size_t i = 0; i < update_preimage.size(); ++i) {
5363
FF leaf_slot = unconstrained_compute_leaf_slot(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS,
5464
delayed_public_mutable_slot + i);
65+
info("performing unconstrained reads i = ", i);
5566
update_preimage[i] = unconstrained_read(unconstrained_merkle_db, leaf_slot);
5667
}
5768

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <string>
88
#include <vector>
99

10-
#include "barretenberg/common/bb_bench.hpp"
1110
#include "barretenberg/common/log.hpp"
1211
#include "barretenberg/vm2/common/avm_inputs.hpp"
1312
#include "barretenberg/vm2/common/aztec_types.hpp"
@@ -127,6 +126,22 @@ GetLowIndexedLeafResponse HintingRawDB::get_low_indexed_leaf(world_state::Merkle
127126
query_hints.get_previous_value_index_hints[key] = { resp.is_already_present, resp.index };
128127
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
129128
get_sibling_path(tree_id, resp.index);
129+
130+
if (tree_id == world_state::MerkleTreeId::NULLIFIER_TREE) {
131+
// TODO(MW): We may need a GetLeafPreimageHint for the nullifier tree when calling nullifier_exists, so collect
132+
// it
133+
// in case.
134+
// NB: The PureMerkleDB does not perform this, but the nullifier check gadget requires a leaf preimage. Ts
135+
// gathers the hint: (state_manager -> checkNullifierExists() -> doMerkleOperations -> public_db_sources ->
136+
// checkNullifierExists())
137+
get_leaf_preimage_nullifier_tree(resp.index);
138+
} else if ((tree_id == world_state::MerkleTreeId::PUBLIC_DATA_TREE) && (!resp.is_already_present)) {
139+
// TODO(MW): We may need a GetLeafPreimageHint for the public data tree when calling storage_read, so collect it
140+
// in case.
141+
// NB: The PureMerkleDB does not perform this if !is_already_present, but MerkleDB and ts perform it
142+
// unconditionally. Ts gathers the hint: (public_db_sources -> storageRead())
143+
get_leaf_preimage_public_data_tree(resp.index);
144+
}
130145
return resp;
131146
}
132147

@@ -286,14 +301,14 @@ std::vector<AppendLeafResult> HintingRawDB::append_leaves(world_state::MerkleTre
286301
for (uint32_t i = 0; i < leaves.size(); i++) {
287302
FF root_after = i == leaves.size() - 1 ? get_tree_info(tree_id).root : results[i + 1].root;
288303
// Iterate tree_info to the be state after adding this leaf:
289-
tree_info = appendLeafInternal(tree_info, root_after, tree_id, leaves[i]);
304+
tree_info = appendLeafInternal(tree_info, results[i].path, root_after, tree_id, leaves[i]);
290305
}
291306

292307
return results;
293308
}
294309

295-
// TODO(MW): rework
296310
AppendOnlyTreeSnapshot HintingRawDB::appendLeafInternal(AppendOnlyTreeSnapshot state_before,
311+
SiblingPath& path,
297312
const FF& root_after,
298313
world_state::MerkleTreeId tree_id,
299314
const FF& leaf)
@@ -303,14 +318,15 @@ AppendOnlyTreeSnapshot HintingRawDB::appendLeafInternal(AppendOnlyTreeSnapshot s
303318
// constraint the insertion.
304319
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/13380): This can be changed if the world state
305320
// appendLeaves returns the sibling paths.
306-
AppendLeavesHintKey key = { state_before, tree_id, { leaf } };
321+
AppendLeavesHintKey append_key = { state_before, tree_id, { leaf } };
307322
AppendOnlyTreeSnapshot state_after = { .root = root_after,
308323
.nextAvailableLeafIndex = state_before.nextAvailableLeafIndex + 1 };
309-
append_leaves_hints[key] = state_after;
310-
// TODO(MW): Just store hint using result in append_leaves to avoid unnecessary extra calls to underlying db?
311-
// NOTE: this call will use the /current/ db.get_tree_info() (post full append_leaves), which may not match that at
312-
// root_after:
313-
get_sibling_path(tree_id, state_before.nextAvailableLeafIndex);
324+
append_leaves_hints[append_key] = state_after;
325+
// TODO(MW): Storing sibling path hint manually using the result since a get_sibling_path() call here will use the
326+
// /current/ db.get_tree_info() (post full append_leaves), which may not match that at result.root. We may not care
327+
// about this (see comment in PureRawMerkleDB::append_leaves())
328+
GetSiblingPathKey path_key = { state_after, tree_id, state_before.nextAvailableLeafIndex };
329+
query_hints.get_sibling_path_hints[path_key] = path;
314330
return state_after;
315331
}
316332

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class HintingRawDB final : public LowLevelMerkleDBInterface {
101101
// Private helper methods. TODO(MW): extract out? Copied from raw_data_dbs
102102
const AppendOnlyTreeSnapshot& get_tree_info(MerkleTreeId tree_id) const;
103103
AppendOnlyTreeSnapshot appendLeafInternal(AppendOnlyTreeSnapshot state_before,
104+
SiblingPath& path,
104105
const FF& root_after,
105106
MerkleTreeId tree_id,
106107
const FF& leaf);

0 commit comments

Comments
 (0)