Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
update_check.test.cpp
Go to the documentation of this file.
2
12
13#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
17
18namespace bb::avm2::simulation {
19
20using ::testing::_;
21using ::testing::ElementsAre;
22using ::testing::NiceMock;
23using ::testing::Return;
24using ::testing::ReturnRef;
25using ::testing::SizeIs;
26using ::testing::StrictMock;
27using ::testing::TestWithParam;
28
30
31namespace {
32
33TEST(AvmSimulationUpdateCheck, NeverWritten)
34{
35 uint64_t current_timestamp = 100;
36 ContractInstance instance = testing::random_contract_instance();
37 instance.current_class_id = instance.original_class_id;
38 AztecAddress derived_address = compute_contract_address(instance);
39 FF delayed_public_mutable_slot = poseidon2::hash({ UPDATED_CLASS_IDS_SLOT, derived_address });
40 FF delayed_public_mutable_hash_slot = delayed_public_mutable_slot + UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN;
41
42 TreeStates tree_states = {};
43 tree_states.publicDataTree.tree.root = 42;
44
45 NiceMock<MockPoseidon2> poseidon2;
46 NiceMock<MockHighLevelMerkleDB> merkle_db;
47 StrictMock<MockLowLevelMerkleDB> low_level_merkle_db;
48 StrictMock<MockRangeCheck> range_check;
49
50 EventEmitter<UpdateCheckEvent> event_emitter;
51 GlobalVariables globals{ .timestamp = current_timestamp };
52 UpdateCheck update_check(poseidon2, range_check, merkle_db, event_emitter, globals);
53
54 EXPECT_CALL(
56 storage_read(AztecAddress(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS), delayed_public_mutable_hash_slot))
57 .WillRepeatedly(Return(FF(0)));
58 EXPECT_CALL(merkle_db, get_tree_state()).WillRepeatedly(Return(tree_states));
59 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& input) { return poseidon2::hash(input); });
60
61 update_check.check_current_class_id(derived_address, instance);
62
63 EXPECT_THAT(event_emitter.dump_events(),
64 ElementsAre(UpdateCheckEvent{
65 .address = derived_address,
66 .current_class_id = instance.current_class_id,
67 .original_class_id = instance.original_class_id,
68 .public_data_tree_root = tree_states.publicDataTree.tree.root,
69 .current_timestamp = current_timestamp,
70 .update_hash = 0,
71 .update_preimage_metadata = 0,
72 .update_preimage_pre_class_id = 0,
73 .update_preimage_post_class_id = 0,
74 .delayed_public_mutable_slot = delayed_public_mutable_slot,
75 }));
76
77 // Negative: class id must be original class id
78 instance.current_class_id = instance.current_class_id + 1;
79 EXPECT_THROW_WITH_MESSAGE(update_check.check_current_class_id(derived_address, instance),
80 "Current class id.*does not match expected class id.*");
81}
82
83struct TestParams {
90};
91
92std::vector<TestParams> hash_nonzero_tests = {
93 TestParams{ // Hash is not zero, but scheduled value change is zeroed out
94 // Only delay has been touched
95 .original_class_id = 27,
96 .current_class_id = 27,
97 .should_throw = false },
98 TestParams{ // Hash is not zero, but scheduled value change is zeroed out
99 // Only delay has been touched
100 .original_class_id = 27,
101 .current_class_id = 28,
102 .should_throw = true },
103 TestParams{ .original_class_id = 27,
104 .current_class_id = 2,
105 .update_pre_class = 2, // From 2
106 .update_post_class = 3, // To 3
107 .update_timestamp_of_change = 101, // At timestamp after current
108 .should_throw = false },
109 TestParams{ .original_class_id = 27,
110 .current_class_id = 2,
111 .update_pre_class = 2, // From 2
112 .update_post_class = 3, // To 3
113 .update_timestamp_of_change = 99, // At timestamp before current
114 .should_throw = true },
115 TestParams{ .original_class_id = 27,
116 .current_class_id = 3,
117 .update_pre_class = 2, // From 2
118 .update_post_class = 3, // To 3
119 .update_timestamp_of_change = 99, // At timestamp before current
120 .should_throw = false },
121 TestParams{ .original_class_id = 27,
122 .current_class_id = 3,
123 .update_pre_class = 2, // From 2
124 .update_post_class = 3, // To 3
125 .update_timestamp_of_change = 101, // At timestamp after current
126 .should_throw = true },
127 TestParams{ .original_class_id = 27,
128 .current_class_id = 3,
129 .update_pre_class = 2, // From 2
130 .update_post_class = 3, // To 3
131 .update_timestamp_of_change = 100, // At current (past) timestamp
132 .should_throw = false },
133 TestParams{ .original_class_id = 27,
134 .current_class_id = 2,
135 .update_pre_class = 2, // From 2
136 .update_post_class = 3, // To 3
137 .update_timestamp_of_change = 100, // At current (past) timestamp
138 .should_throw = true },
139 TestParams{ .original_class_id = 1,
140 .current_class_id = 1,
141 .update_pre_class = 0, // From original
142 .update_post_class = 3, // To 3
143 .update_timestamp_of_change = 101, // At timestamp after current
144 .should_throw = false },
145 TestParams{ .original_class_id = 1,
146 .current_class_id = 3,
147 .update_pre_class = 0, // From original
148 .update_post_class = 3, // To 3
149 .update_timestamp_of_change = 101, // At timestamp after current
150 .should_throw = true },
151};
152
153class UpdateCheckHashNonzeroTest : public TestWithParam<TestParams> {};
154
155TEST_P(UpdateCheckHashNonzeroTest, WithHash)
156{
157 const auto& param = GetParam();
158
159 uint64_t current_timestamp = 100;
160 ContractInstance instance = testing::random_contract_instance();
161 instance.current_class_id = param.current_class_id;
162 instance.original_class_id = param.original_class_id;
163
164 AztecAddress derived_address = compute_contract_address(instance);
165 FF delayed_public_mutable_slot = poseidon2::hash({ UPDATED_CLASS_IDS_SLOT, derived_address });
166 FF delayed_public_mutable_hash_slot = delayed_public_mutable_slot + UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN;
167 FF delayed_public_mutable_leaf_slot = poseidon2::hash({ GENERATOR_INDEX__PUBLIC_LEAF_INDEX,
169 delayed_public_mutable_hash_slot });
170
171 FF update_metadata = FF(static_cast<uint64_t>(123) << 32) + param.update_timestamp_of_change;
172 std::vector<FF> update_preimage = { update_metadata, param.update_pre_class, param.update_post_class };
173 std::vector<FF> update_preimage_slots;
174
175 for (size_t i = 0; i < update_preimage.size(); ++i) {
178 delayed_public_mutable_slot + i });
179 update_preimage_slots.push_back(leaf_slot);
180 }
181
182 FF update_hash = poseidon2::hash(update_preimage);
183
184 TreeStates tree_states = {};
185 tree_states.publicDataTree.tree.root = 42;
186
187 NiceMock<MockPoseidon2> poseidon2;
188 NiceMock<MockHighLevelMerkleDB> merkle_db;
189 NiceMock<MockLowLevelMerkleDB> mock_low_level_merkle_db;
190 NiceMock<MockRangeCheck> range_check;
191
192 EventEmitter<UpdateCheckEvent> event_emitter;
193 GlobalVariables globals{ .timestamp = current_timestamp };
194 UpdateCheck update_check(poseidon2, range_check, merkle_db, event_emitter, globals);
195
196 EXPECT_CALL(
197 merkle_db,
198 storage_read(AztecAddress(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS), delayed_public_mutable_hash_slot))
199 .WillRepeatedly(Return(update_hash));
200 EXPECT_CALL(merkle_db, get_tree_state()).WillRepeatedly(Return(tree_states));
201 EXPECT_CALL(merkle_db, as_unconstrained()).WillRepeatedly(ReturnRef(mock_low_level_merkle_db));
202
203 EXPECT_CALL(mock_low_level_merkle_db, get_low_indexed_leaf(world_state::MerkleTreeId::PUBLIC_DATA_TREE, _))
204 .WillRepeatedly([&](world_state::MerkleTreeId, const FF& leaf_slot) {
205 for (size_t i = 0; i < update_preimage_slots.size(); ++i) {
206 if (leaf_slot == update_preimage_slots[i]) {
207 return GetLowIndexedLeafResponse(true, static_cast<uint64_t>(i));
208 }
209 }
210 throw std::runtime_error("Leaf not found");
211 });
212
213 EXPECT_CALL(mock_low_level_merkle_db, get_leaf_preimage_public_data_tree(_))
214 .WillRepeatedly([&](const uint64_t& index) {
216 PublicDataLeafValue(FF(index) + delayed_public_mutable_leaf_slot, update_preimage[index]), 0, 0);
217 });
218
219 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& input) { return poseidon2::hash(input); });
220
221 EXPECT_CALL(range_check, assert_range(_, _)).WillRepeatedly([](const uint128_t& value, const uint8_t& range) {
222 if (range > 128) {
223 throw std::runtime_error("Range checks aren't supported for bit-sizes > 128");
224 }
225 if (range == 128) {
226 return;
227 }
228 if (value > (static_cast<uint128_t>(1) << range)) {
229 throw std::runtime_error("Value is out of range");
230 }
231 });
232
233 if (param.should_throw) {
234 EXPECT_THROW_WITH_MESSAGE(update_check.check_current_class_id(derived_address, instance),
235 "Current class id.*does not match expected class id.*");
236 EXPECT_THAT(event_emitter.dump_events(), SizeIs(0));
237 } else {
238 update_check.check_current_class_id(derived_address, instance);
239 EXPECT_THAT(event_emitter.dump_events(),
240 ElementsAre(UpdateCheckEvent{
241 .address = derived_address,
242 .current_class_id = instance.current_class_id,
243 .original_class_id = instance.original_class_id,
244 .public_data_tree_root = tree_states.publicDataTree.tree.root,
245 .current_timestamp = current_timestamp,
246 .update_hash = update_hash,
247 .update_preimage_metadata = update_metadata,
248 .update_preimage_pre_class_id = param.update_pre_class,
249 .update_preimage_post_class_id = param.update_post_class,
250 .delayed_public_mutable_slot = delayed_public_mutable_slot,
251 }));
252 }
253}
254
255INSTANTIATE_TEST_SUITE_P(AvmSimulationUpdateCheck, UpdateCheckHashNonzeroTest, ::testing::ValuesIn(hash_nonzero_tests));
256
257TEST(AvmSimulationUpdateCheck, HashMismatch)
258{
259 uint64_t current_timestamp = 100;
260 ContractInstance instance = testing::random_contract_instance();
261 instance.current_class_id = instance.original_class_id;
262 AztecAddress derived_address = compute_contract_address(instance);
263 FF delayed_public_mutable_slot = poseidon2::hash({ UPDATED_CLASS_IDS_SLOT, derived_address });
264 FF delayed_public_mutable_hash_slot = delayed_public_mutable_slot + UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN;
265 FF delayed_public_mutable_leaf_slot = poseidon2::hash({ GENERATOR_INDEX__PUBLIC_LEAF_INDEX,
267 delayed_public_mutable_hash_slot });
268
269 TreeSnapshots trees = {};
270
271 NiceMock<MockPoseidon2> poseidon2;
272 NiceMock<MockHighLevelMerkleDB> merkle_db;
273 NiceMock<MockLowLevelMerkleDB> mock_low_level_merkle_db;
274 StrictMock<MockRangeCheck> range_check;
275
276 EventEmitter<UpdateCheckEvent> event_emitter;
277 GlobalVariables globals{ .timestamp = current_timestamp };
278 UpdateCheck update_check(poseidon2, range_check, merkle_db, event_emitter, globals);
279
280 EXPECT_CALL(
281 merkle_db,
282 storage_read(AztecAddress(CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS), delayed_public_mutable_hash_slot))
283 .WillRepeatedly(Return(FF(27)));
284 EXPECT_CALL(mock_low_level_merkle_db, get_tree_roots()).WillRepeatedly(ReturnRef(trees));
285 EXPECT_CALL(merkle_db, as_unconstrained()).WillRepeatedly(ReturnRef(mock_low_level_merkle_db));
286
287 EXPECT_CALL(mock_low_level_merkle_db, get_low_indexed_leaf(world_state::MerkleTreeId::PUBLIC_DATA_TREE, _))
288 .WillRepeatedly([&](world_state::MerkleTreeId, const FF& leaf_slot) {
289 return GetLowIndexedLeafResponse(true, static_cast<uint64_t>(leaf_slot - delayed_public_mutable_leaf_slot));
290 });
291
292 EXPECT_CALL(mock_low_level_merkle_db, get_leaf_preimage_public_data_tree(_))
293 .WillRepeatedly([&](const uint64_t& index) {
295 PublicDataLeafValue(FF(index) + delayed_public_mutable_leaf_slot, 0), 0, 0);
296 });
297
298 EXPECT_CALL(poseidon2, hash(_)).WillRepeatedly([](const std::vector<FF>& input) { return poseidon2::hash(input); });
299
300 EXPECT_THROW_WITH_MESSAGE(update_check.check_current_class_id(derived_address, instance),
301 "Stored hash does not match preimage hash");
302 EXPECT_THAT(event_emitter.dump_events(), SizeIs(0));
303}
304
305} // namespace
306
307} // namespace bb::avm2::simulation
INSTANTIATE_TEST_SUITE_P(AcirTests, AcirIntegrationSingleTest, testing::Values("a_1327_concrete_in_generic", "a_1_mul", "a_2_div", "a_3_add", "a_4_sub", "a_5_over", "a_6", "a_6_array", "a_7", "a_7_function", "aes128_encrypt", "arithmetic_binary_operations", "array_dynamic", "array_dynamic_blackbox_input", "array_dynamic_main_output", "array_dynamic_nested_blackbox_input", "array_eq", "array_if_cond_simple", "array_len", "array_neq", "array_sort", "array_to_slice", "array_to_slice_constant_length", "assert", "assert_statement", "assign_ex", "bigint", "bit_and", "bit_not", "bit_shifts_comptime", "bit_shifts_runtime", "blake3", "bool_not", "bool_or", "break_and_continue", "brillig_acir_as_brillig", "brillig_array_eq", "brillig_array_to_slice", "brillig_arrays", "brillig_assert", "brillig_bit_shifts_runtime", "brillig_blake2s", "brillig_blake3", "brillig_calls", "brillig_calls_array", "brillig_calls_conditionals", "brillig_conditional", "brillig_cow", "brillig_cow_assign", "brillig_cow_regression", "brillig_ecdsa_secp256k1", "brillig_ecdsa_secp256r1", "brillig_embedded_curve", "brillig_fns_as_values", "brillig_hash_to_field", "brillig_identity_function", "brillig_keccak", "brillig_loop", "brillig_nested_arrays", "brillig_not", "brillig_oracle", "brillig_pedersen", "brillig_recursion", "brillig_references", "brillig_schnorr", "brillig_sha256", "brillig_signed_cmp", "brillig_signed_div", "brillig_slices", "brillig_to_be_bytes", "brillig_to_bits", "brillig_to_bytes_integration", "brillig_to_le_bytes", "brillig_top_level", "brillig_uninitialized_arrays", "brillig_wrapping", "cast_bool", "closures_mut_ref", "conditional_1", "conditional_2", "conditional_regression_421", "conditional_regression_547", "conditional_regression_661", "conditional_regression_short_circuit", "conditional_regression_underflow", "custom_entry", "databus", "debug_logs", "diamond_deps_0", "double_verify_nested_proof", "double_verify_proof", "ecdsa_secp256k1", "ecdsa_secp256r1", "ecdsa_secp256r1_3x", "eddsa", "embedded_curve_ops", "field_attribute", "generics", "global_consts", "hash_to_field", "hashmap", "higher_order_functions", "if_else_chain", "import", "inline_never_basic", "integer_array_indexing", "keccak256", "main_bool_arg", "main_return", "merkle_insert", "missing_closure_env", "modules", "modules_more", "modulus", "nested_array_dynamic", "nested_array_dynamic_simple", "nested_array_in_slice", "nested_arrays_from_brillig", "no_predicates_basic", "no_predicates_brillig", "no_predicates_numeric_generic_poseidon", "operator_overloading", "pedersen_check", "pedersen_commitment", "pedersen_hash", "poseidon_bn254_hash", "poseidonsponge_x5_254", "pred_eq", "prelude", "references", "regression", "regression_2660", "regression_3051", "regression_3394", "regression_3607", "regression_3889", "regression_4088", "regression_4124", "regression_4202", "regression_4449", "regression_4709", "regression_5045", "regression_capacity_tracker", "regression_mem_op_predicate", "regression_method_cannot_be_found", "regression_struct_array_conditional", "schnorr", "sha256", "sha2_byte", "side_effects_constrain_array", "signed_arithmetic", "signed_comparison", "signed_division", "simple_2d_array", "simple_add_and_ret_arr", "simple_array_param", "simple_bitwise", "simple_comparison", "simple_mut", "simple_not", "simple_print", "simple_program_addition", "simple_radix", "simple_shield", "simple_shift_left_right", "slice_coercion", "slice_dynamic_index", "slice_loop", "slices", "strings", "struct", "struct_array_inputs", "struct_fields_ordering", "struct_inputs", "submodules", "to_be_bytes", "to_bytes_consistent", "to_bytes_integration", "to_le_bytes", "trait_as_return_type", "trait_impl_base_type", "traits_in_crates_1", "traits_in_crates_2", "tuple_inputs", "tuples", "type_aliases", "u128", "u16_support", "unconstrained_empty", "unit_value", "unsafe_range_constraint", "witness_compression", "xor"))
TEST_P(AcirIntegrationSingleTest, DISABLED_ProveAndVerifyProgram)
#define GENERATOR_INDEX__PUBLIC_LEAF_INDEX
#define UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN
#define UPDATED_CLASS_IDS_SLOT
#define CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS
StrictMock< MockHighLevelMerkleDB > merkle_db
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
EventEmitter< DataCopyEvent > event_emitter
RangeCheck range_check
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void hash(State &state) noexcept
IndexedLeaf< PublicDataLeafValue > PublicDataTreeLeafPreimage
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
TEST(EmitUnencryptedLogTest, Basic)
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
FF compute_contract_address(const ContractInstance &contract_instance)
ContractInstance random_contract_instance()
Definition fixtures.cpp:158
typename Flavor::FF FF
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
bool should_throw
FF update_timestamp_of_change
FF original_class_id
FF current_class_id
FF update_pre_class
FF update_post_class