Skip to content

Commit 8664b51

Browse files
committed
address comments from peer review
1 parent 119efb2 commit 8664b51

27 files changed

+88
-120
lines changed

barretenberg/cpp/pil/vm2/bytecode/bc_retrieval.pil

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,22 @@ pol commit remaining_bytecodes_inv;
9797
#[NO_REMAINING_BYTECODES]
9898
sel * (REMAINING_BYTECODES * (no_remaining_bytecodes * (1 - remaining_bytecodes_inv) + remaining_bytecodes_inv) - 1 + no_remaining_bytecodes) = 0;
9999

100-
pol commit new_bytecode;
100+
pol commit is_new_class;
101101

102-
#[NEW_BYTECODE_CHECK]
102+
#[IS_NEW_CLASS_CHECK]
103103
instance_exists {
104104
current_class_id,
105-
new_bytecode,
105+
is_new_class,
106106
prev_retrieved_bytecodes_tree_root
107107
} in retrieved_bytecodes_tree_check.sel {
108108
retrieved_bytecodes_tree_check.class_id,
109109
retrieved_bytecodes_tree_check.leaf_not_exists,
110110
retrieved_bytecodes_tree_check.root
111111
};
112112

113-
sel * (1 - instance_exists) * new_bytecode = 0;
113+
sel * (1 - instance_exists) * is_new_class = 0;
114114

115-
pol TOO_MANY_BYTECODES = no_remaining_bytecodes * new_bytecode;
115+
pol TOO_MANY_BYTECODES = no_remaining_bytecodes * is_new_class;
116116

117117
// We error if instance doesn't exist or if we have too many bytecodes.
118118
sel * (instance_exists * (1 - TOO_MANY_BYTECODES) - (1 - error)) = 0;

barretenberg/cpp/src/barretenberg/vm2/constraining/relations/bc_retrieval.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ TEST(BytecodeRetrievalConstrainingTest, SuccessfulRetrieval)
7373
.public_data_tree_root = public_data_tree_root,
7474
.retrieved_bytecodes_snapshot_before = snapshot_before,
7575
.retrieved_bytecodes_snapshot_after = snapshot_after,
76-
.new_bytecode = true,
76+
.is_new_class = true,
7777
} },
7878
trace);
7979

@@ -114,7 +114,7 @@ TEST(BytecodeRetrievalConstrainingTest, TooManyBytecodes)
114114
.public_data_tree_root = public_data_tree_root,
115115
.retrieved_bytecodes_snapshot_before = snapshot_before,
116116
.retrieved_bytecodes_snapshot_after = snapshot_after,
117-
.new_bytecode = true,
117+
.is_new_class = true,
118118
.limit_error = true,
119119
} },
120120
trace);

barretenberg/cpp/src/barretenberg/vm2/constraining/relations/storage_write.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ TEST(SStoreConstrainingTest, Interactions)
212212
.root = 42,
213213
.nextAvailableLeafIndex = 128,
214214
};
215-
AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.snapshot();
215+
AppendOnlyTreeSnapshot written_slots_tree_before = written_public_data_slots_tree_check.get_snapshot();
216216

217217
EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& inputs) {
218218
return RawPoseidon2::hash(inputs);
@@ -242,7 +242,7 @@ TEST(SStoreConstrainingTest, Interactions)
242242
{},
243243
false);
244244
written_public_data_slots_tree_check.insert(contract_address, slot);
245-
auto written_slots_tree_after = written_public_data_slots_tree_check.snapshot();
245+
auto written_slots_tree_after = written_public_data_slots_tree_check.get_snapshot();
246246

247247
TestTraceContainer trace({
248248
{

barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

barretenberg/cpp/src/barretenberg/vm2/generated/flavor_variables.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct AvmFlavorVariables {
250250
lookup_bc_hashing_iv_is_len_relation<FF_>,
251251
lookup_bc_retrieval_class_id_derivation_relation<FF_>,
252252
lookup_bc_retrieval_contract_instance_retrieval_relation<FF_>,
253-
lookup_bc_retrieval_new_bytecode_check_relation<FF_>,
253+
lookup_bc_retrieval_is_new_class_check_relation<FF_>,
254254
lookup_bc_retrieval_retrieved_bytecodes_insertion_relation<FF_>,
255255
lookup_bitwise_byte_operations_relation<FF_>,
256256
lookup_bitwise_dispatch_exec_bitwise_relation<FF_>,

barretenberg/cpp/src/barretenberg/vm2/generated/relations/bc_retrieval_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void bc_retrievalImpl<FF_>::accumulate(ContainerOverSubrelations& evals,
2222
constants_AVM_RETRIEVED_BYTECODES_TREE_INITIAL_SIZE) -
2323
in.get(C::bc_retrieval_prev_retrieved_bytecodes_tree_size));
2424
const auto bc_retrieval_TOO_MANY_BYTECODES =
25-
in.get(C::bc_retrieval_no_remaining_bytecodes) * in.get(C::bc_retrieval_new_bytecode);
25+
in.get(C::bc_retrieval_no_remaining_bytecodes) * in.get(C::bc_retrieval_is_new_class);
2626

2727
{
2828
using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
@@ -64,7 +64,7 @@ void bc_retrievalImpl<FF_>::accumulate(ContainerOverSubrelations& evals,
6464
{
6565
using Accumulator = typename std::tuple_element_t<5, ContainerOverSubrelations>;
6666
auto tmp = in.get(C::bc_retrieval_sel) * (FF(1) - in.get(C::bc_retrieval_instance_exists)) *
67-
in.get(C::bc_retrieval_new_bytecode);
67+
in.get(C::bc_retrieval_is_new_class);
6868
tmp *= scaling_factor;
6969
std::get<5>(evals) += typename Accumulator::View(tmp);
7070
}

barretenberg/cpp/src/barretenberg/vm2/generated/relations/lookups_bc_retrieval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace bb::avm2 {
2727

2828
INSTANTIATE_LOOKUP(lookup_bc_retrieval_contract_instance_retrieval_relation);
29-
INSTANTIATE_LOOKUP(lookup_bc_retrieval_new_bytecode_check_relation);
29+
INSTANTIATE_LOOKUP(lookup_bc_retrieval_is_new_class_check_relation);
3030
INSTANTIATE_LOOKUP(lookup_bc_retrieval_class_id_derivation_relation);
3131
INSTANTIATE_LOOKUP(lookup_bc_retrieval_retrieved_bytecodes_insertion_relation);
3232

barretenberg/cpp/src/barretenberg/vm2/generated/relations/lookups_bc_retrieval.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ template <typename FF_>
4343
using lookup_bc_retrieval_contract_instance_retrieval_relation =
4444
lookup_relation_base<FF_, lookup_bc_retrieval_contract_instance_retrieval_settings>;
4545

46-
/////////////////// lookup_bc_retrieval_new_bytecode_check ///////////////////
46+
/////////////////// lookup_bc_retrieval_is_new_class_check ///////////////////
4747

48-
struct lookup_bc_retrieval_new_bytecode_check_settings_ {
49-
static constexpr std::string_view NAME = "LOOKUP_BC_RETRIEVAL_NEW_BYTECODE_CHECK";
48+
struct lookup_bc_retrieval_is_new_class_check_settings_ {
49+
static constexpr std::string_view NAME = "LOOKUP_BC_RETRIEVAL_IS_NEW_CLASS_CHECK";
5050
static constexpr std::string_view RELATION_NAME = "bc_retrieval";
5151
static constexpr size_t LOOKUP_TUPLE_SIZE = 3;
5252
static constexpr Column SRC_SELECTOR = Column::bc_retrieval_instance_exists;
5353
static constexpr Column DST_SELECTOR = Column::retrieved_bytecodes_tree_check_sel;
54-
static constexpr Column COUNTS = Column::lookup_bc_retrieval_new_bytecode_check_counts;
55-
static constexpr Column INVERSES = Column::lookup_bc_retrieval_new_bytecode_check_inv;
54+
static constexpr Column COUNTS = Column::lookup_bc_retrieval_is_new_class_check_counts;
55+
static constexpr Column INVERSES = Column::lookup_bc_retrieval_is_new_class_check_inv;
5656
static constexpr std::array<ColumnAndShifts, LOOKUP_TUPLE_SIZE> SRC_COLUMNS = {
5757
ColumnAndShifts::bc_retrieval_current_class_id,
58-
ColumnAndShifts::bc_retrieval_new_bytecode,
58+
ColumnAndShifts::bc_retrieval_is_new_class,
5959
ColumnAndShifts::bc_retrieval_prev_retrieved_bytecodes_tree_root
6060
};
6161
static constexpr std::array<ColumnAndShifts, LOOKUP_TUPLE_SIZE> DST_COLUMNS = {
@@ -65,11 +65,11 @@ struct lookup_bc_retrieval_new_bytecode_check_settings_ {
6565
};
6666
};
6767

68-
using lookup_bc_retrieval_new_bytecode_check_settings =
69-
lookup_settings<lookup_bc_retrieval_new_bytecode_check_settings_>;
68+
using lookup_bc_retrieval_is_new_class_check_settings =
69+
lookup_settings<lookup_bc_retrieval_is_new_class_check_settings_>;
7070
template <typename FF_>
71-
using lookup_bc_retrieval_new_bytecode_check_relation =
72-
lookup_relation_base<FF_, lookup_bc_retrieval_new_bytecode_check_settings>;
71+
using lookup_bc_retrieval_is_new_class_check_relation =
72+
lookup_relation_base<FF_, lookup_bc_retrieval_is_new_class_check_settings>;
7373

7474
/////////////////// lookup_bc_retrieval_class_id_derivation ///////////////////
7575

barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.cpp

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,69 @@ namespace bb::avm2::simulation {
1414

1515
BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address)
1616
{
17+
1718
// Use shared ContractInstanceManager for contract instance retrieval and validation
1819
// This handles nullifier checks, address derivation, and update validation
19-
AppendOnlyTreeSnapshot snapshot_before = retrieved_bytecodes_tree_check.snapshot();
2020
auto tree_states = merkle_db.get_tree_state();
21+
AppendOnlyTreeSnapshot before_snapshot = retrieved_bytecodes_tree_check.get_snapshot();
22+
23+
BytecodeRetrievalEvent retrieval_event = {
24+
.bytecode_id = FF(0), // Use default ID for error cases
25+
.address = address,
26+
.nullifier_root = tree_states.nullifierTree.tree.root,
27+
.public_data_tree_root = tree_states.publicDataTree.tree.root,
28+
.retrieved_bytecodes_snapshot_before = before_snapshot,
29+
.retrieved_bytecodes_snapshot_after = before_snapshot,
30+
};
2131

2232
auto maybe_instance = contract_instance_manager.get_contract_instance(address);
2333

2434
if (!maybe_instance.has_value()) {
35+
retrieval_event.instance_not_found_error = true;
2536
// Contract instance not found - emit error event and throw
26-
retrieval_events.emit({
27-
.bytecode_id = FF(0), // Use default ID for error case
28-
.address = address,
29-
.nullifier_root = tree_states.nullifierTree.tree.root,
30-
.public_data_tree_root = tree_states.publicDataTree.tree.root,
31-
.retrieved_bytecodes_snapshot_before = snapshot_before,
32-
.retrieved_bytecodes_snapshot_after = snapshot_before,
33-
.instance_not_found_error = true,
34-
});
37+
retrieval_events.emit(std::move(retrieval_event));
3538
vinfo("Contract ", field_to_string(address), " is not deployed!");
36-
throw BytecodeNotFoundError("Contract " + field_to_string(address) + " is not deployed");
39+
throw BytecodeRetrievalError("Contract " + field_to_string(address) + " is not deployed");
3740
}
3841

3942
ContractInstance instance = maybe_instance.value();
4043
ContractClassId current_class_id = instance.current_class_id;
44+
retrieval_event.current_class_id = current_class_id;
45+
46+
bool is_new_class = !retrieved_bytecodes_tree_check.contains(current_class_id);
47+
retrieval_event.is_new_class = is_new_class;
4148

42-
bool new_bytecode = !retrieved_bytecodes_tree_check.contains(current_class_id);
4349
uint32_t retrieved_bytecodes_count = retrieved_bytecodes_tree_check.size();
4450

45-
if (new_bytecode && retrieved_bytecodes_count == MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
46-
retrieval_events.emit({
47-
.bytecode_id = FF(0), // Use default ID for error case
48-
.address = address,
49-
.current_class_id = current_class_id,
50-
.nullifier_root = tree_states.nullifierTree.tree.root,
51-
.public_data_tree_root = tree_states.publicDataTree.tree.root,
52-
.retrieved_bytecodes_snapshot_before = snapshot_before,
53-
.retrieved_bytecodes_snapshot_after = snapshot_before,
54-
.new_bytecode = new_bytecode,
55-
.limit_error = true,
56-
});
57-
throw BytecodeRetrievalLimitReachedError("Can't retrieve more than " +
58-
std::to_string(MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) +
59-
" bytecodes per tx");
51+
if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
52+
retrieval_event.limit_error = true;
53+
retrieval_events.emit(std::move(retrieval_event));
54+
throw BytecodeRetrievalError("Can't retrieve more than " +
55+
std::to_string(MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) +
56+
" bytecodes per tx");
6057
}
6158

6259
retrieved_bytecodes_tree_check.insert(current_class_id);
63-
AppendOnlyTreeSnapshot snapshot_after = retrieved_bytecodes_tree_check.snapshot();
60+
AppendOnlyTreeSnapshot snapshot_after = retrieved_bytecodes_tree_check.get_snapshot();
61+
retrieval_event.retrieved_bytecodes_snapshot_after = snapshot_after;
6462

6563
// Contract class retrieval and class ID validation
6664
std::optional<ContractClass> maybe_klass = contract_db.get_contract_class(current_class_id);
6765
// Note: we don't need to silo and check the class id because the deployer contract guarrantees
6866
// that if a contract instance exists, the class has been registered.
6967
assert(maybe_klass.has_value());
7068
auto& klass = maybe_klass.value();
69+
retrieval_event.contract_class = klass; // WARNING: this class has the whole bytecode.
7170
info("Bytecode for ", address, " successfully retrieved!");
7271

7372
// Bytecode hashing and decomposition, deduplicated by bytecode_id (commitment)
7473
BytecodeId bytecode_id = klass.public_bytecode_commitment;
74+
retrieval_event.bytecode_id = bytecode_id;
7575

7676
// Check if we've already processed this bytecode. If so, don't do hashing and decomposition again!
7777
if (bytecodes.contains(bytecode_id)) {
7878
// Already processed this bytecode - just emit retrieval event and return
79-
retrieval_events.emit({
80-
.bytecode_id = bytecode_id,
81-
.address = address,
82-
.current_class_id = current_class_id,
83-
.contract_class = klass,
84-
.nullifier_root = tree_states.nullifierTree.tree.root,
85-
.public_data_tree_root = tree_states.publicDataTree.tree.root,
86-
.retrieved_bytecodes_snapshot_before = snapshot_before,
87-
.retrieved_bytecodes_snapshot_after = snapshot_after,
88-
.new_bytecode = new_bytecode,
89-
});
79+
retrieval_events.emit(std::move(retrieval_event));
9080
return bytecode_id;
9181
}
9282

@@ -102,17 +92,7 @@ BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address)
10292
// We now save the bytecode so that we don't repeat this process.
10393
bytecodes.emplace(bytecode_id, std::move(shared_bytecode));
10494

105-
retrieval_events.emit({
106-
.bytecode_id = bytecode_id,
107-
.address = address,
108-
.current_class_id = current_class_id,
109-
.contract_class = klass, // WARNING: this class has the whole bytecode.
110-
.nullifier_root = tree_states.nullifierTree.tree.root,
111-
.public_data_tree_root = tree_states.publicDataTree.tree.root,
112-
.retrieved_bytecodes_snapshot_before = snapshot_before,
113-
.retrieved_bytecodes_snapshot_after = snapshot_after,
114-
.new_bytecode = new_bytecode,
115-
});
95+
retrieval_events.emit(std::move(retrieval_event));
11696

11797
return bytecode_id;
11898
}

barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@
2424

2525
namespace bb::avm2::simulation {
2626

27-
struct BytecodeNotFoundError : public std::runtime_error {
28-
BytecodeNotFoundError(const std::string& message)
29-
: std::runtime_error(message)
30-
{}
31-
};
32-
33-
struct BytecodeRetrievalLimitReachedError : public std::runtime_error {
34-
BytecodeRetrievalLimitReachedError(const std::string& message)
27+
struct BytecodeRetrievalError : public std::runtime_error {
28+
BytecodeRetrievalError(const std::string& message)
3529
: std::runtime_error(message)
3630
{}
3731
};

0 commit comments

Comments
 (0)