14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
22using ::testing::ElementsAre;
23using ::testing::NiceMock;
24using ::testing::Return;
25using ::testing::ReturnRef;
26using ::testing::SizeIs;
27using ::testing::StrictMock;
28using ::testing::TestWithParam;
34TEST(AvmSimulationUpdateCheck, NeverWritten)
36 uint64_t current_timestamp = 100;
47 NiceMock<MockHighLevelMerkleDB>
merkle_db;
48 StrictMock<MockLowLevelMerkleDB> low_level_merkle_db;
59 .WillRepeatedly(Return(
FF(0)));
60 EXPECT_CALL(
merkle_db, get_tree_state()).WillRepeatedly(Return(tree_states));
68 .current_class_id =
instance.current_contract_class_id,
69 .original_class_id =
instance.original_contract_class_id,
71 .current_timestamp = current_timestamp,
73 .update_preimage_metadata = 0,
74 .update_preimage_pre_class_id = 0,
75 .update_preimage_post_class_id = 0,
76 .delayed_public_mutable_slot = delayed_public_mutable_slot,
82 "Current class id.*does not match expected class id.*");
97 .original_class_id = 27,
98 .current_class_id = 27,
99 .should_throw =
false },
102 .original_class_id = 27,
103 .current_class_id = 28,
104 .should_throw =
true },
105 TestParams{ .original_class_id = 27,
106 .current_class_id = 2,
107 .update_pre_class = 2,
108 .update_post_class = 3,
109 .update_timestamp_of_change = 101,
110 .should_throw =
false },
111 TestParams{ .original_class_id = 27,
112 .current_class_id = 2,
113 .update_pre_class = 2,
114 .update_post_class = 3,
115 .update_timestamp_of_change = 99,
116 .should_throw =
true },
117 TestParams{ .original_class_id = 27,
118 .current_class_id = 3,
119 .update_pre_class = 2,
120 .update_post_class = 3,
121 .update_timestamp_of_change = 99,
122 .should_throw =
false },
123 TestParams{ .original_class_id = 27,
124 .current_class_id = 3,
125 .update_pre_class = 2,
126 .update_post_class = 3,
127 .update_timestamp_of_change = 101,
128 .should_throw =
true },
129 TestParams{ .original_class_id = 27,
130 .current_class_id = 3,
131 .update_pre_class = 2,
132 .update_post_class = 3,
133 .update_timestamp_of_change = 100,
134 .should_throw =
false },
135 TestParams{ .original_class_id = 27,
136 .current_class_id = 2,
137 .update_pre_class = 2,
138 .update_post_class = 3,
139 .update_timestamp_of_change = 100,
140 .should_throw =
true },
141 TestParams{ .original_class_id = 1,
142 .current_class_id = 1,
143 .update_pre_class = 0,
144 .update_post_class = 3,
145 .update_timestamp_of_change = 101,
146 .should_throw =
false },
147 TestParams{ .original_class_id = 1,
148 .current_class_id = 3,
149 .update_pre_class = 0,
150 .update_post_class = 3,
151 .update_timestamp_of_change = 101,
152 .should_throw =
true },
155class UpdateCheckHashNonzeroTest :
public TestWithParam<TestParams> {};
157TEST_P(UpdateCheckHashNonzeroTest, WithHash)
159 const auto& param = GetParam();
161 uint64_t current_timestamp = 100;
163 instance.current_contract_class_id = param.current_class_id;
164 instance.original_contract_class_id = param.original_class_id;
172 FF update_metadata =
FF(
static_cast<uint64_t
>(123) << 32) + param.update_timestamp_of_change;
173 std::vector<FF> update_preimage = { update_metadata, param.update_pre_class, param.update_post_class };
174 std::vector<FF> update_preimage_slots;
176 for (
size_t i = 0; i < update_preimage.size(); ++i) {
179 delayed_public_mutable_slot + i });
180 update_preimage_slots.push_back(leaf_slot);
185 TreeStates tree_states = {};
186 tree_states.public_data_tree.tree.root = 42;
189 NiceMock<MockHighLevelMerkleDB>
merkle_db;
190 NiceMock<MockLowLevelMerkleDB> mock_low_level_merkle_db;
199 GlobalVariables globals{ .timestamp = current_timestamp };
205 .WillRepeatedly(Return(update_hash));
206 EXPECT_CALL(
merkle_db, get_tree_state()).WillRepeatedly(Return(tree_states));
207 EXPECT_CALL(
merkle_db, as_unconstrained()).WillRepeatedly(ReturnRef(mock_low_level_merkle_db));
211 for (
size_t i = 0; i < update_preimage_slots.size(); ++i) {
212 if (leaf_slot == update_preimage_slots[i]) {
216 throw std::runtime_error(
"Leaf not found");
219 EXPECT_CALL(mock_low_level_merkle_db, get_leaf_preimage_public_data_tree(_))
220 .WillRepeatedly([&](
const uint64_t&
index) {
227 EXPECT_CALL(range_check, assert_range(_, _)).WillRepeatedly([](
const uint128_t&
value,
const uint8_t& range) {
229 throw std::runtime_error(
"Range checks aren't supported for bit-sizes > 128");
235 throw std::runtime_error(
"Value is out of range");
239 if (param.should_throw) {
241 "Current class id.*does not match expected class id.*");
246 ElementsAre(UpdateCheckEvent{
247 .address = derived_address,
248 .current_class_id =
instance.current_contract_class_id,
249 .original_class_id =
instance.original_contract_class_id,
250 .public_data_tree_root = tree_states.public_data_tree.tree.root,
251 .current_timestamp = current_timestamp,
252 .update_hash = update_hash,
253 .update_preimage_metadata = update_metadata,
254 .update_preimage_pre_class_id = param.update_pre_class,
255 .update_preimage_post_class_id = param.update_post_class,
256 .delayed_public_mutable_slot = delayed_public_mutable_slot,
261INSTANTIATE_TEST_SUITE_P(AvmSimulationUpdateCheck, UpdateCheckHashNonzeroTest, ::testing::ValuesIn(hash_nonzero_tests));
263TEST(AvmSimulationUpdateCheck, HashMismatch)
265 uint64_t current_timestamp = 100;
274 TreeSnapshots trees = {};
277 NiceMock<MockHighLevelMerkleDB>
merkle_db;
278 NiceMock<MockLowLevelMerkleDB> mock_low_level_merkle_db;
283 GlobalVariables globals{ .timestamp = current_timestamp };
289 .WillRepeatedly(Return(
FF(27)));
290 EXPECT_CALL(mock_low_level_merkle_db, get_tree_roots()).WillRepeatedly(Return(trees));
291 EXPECT_CALL(
merkle_db, as_unconstrained()).WillRepeatedly(ReturnRef(mock_low_level_merkle_db));
298 EXPECT_CALL(mock_low_level_merkle_db, get_leaf_preimage_public_data_tree(_))
299 .WillRepeatedly([&](
const uint64_t&
index) {
307 "Stored hash does not match preimage hash");
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
std::shared_ptr< Napi::ThreadSafeFunction > instance
#define UPDATES_DELAYED_PUBLIC_MUTABLE_VALUES_LEN
#define UPDATED_CLASS_IDS_SLOT
#define DOM_SEP__PUBLIC_LEAF_INDEX
#define CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS
StrictMock< MockHighLevelMerkleDB > merkle_db
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
StrictMock< MockFieldGreaterThan > mock_field_gt
EventEmitter< DataCopyEvent > event_emitter
StrictMock< MockUpdateCheck > update_check
void hash(State &state) noexcept
INSTANTIATE_TEST_SUITE_P(PaddingVariants, AvmRecursiveTestsParameterized, ::testing::Values(false, true), [](const auto &info) { return info.param ? "Padded" :"Unpadded";})
TEST_P(AvmRecursiveTestsParameterized, GoblinRecursion)
A test of the Goblinized AVM recursive verifier.
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
IndexedLeaf< PublicDataLeafValue > PublicDataTreeLeafPreimage
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
FF compute_contract_address(const ContractInstance &contract_instance)
ContractInstance random_contract_instance()
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
unsigned __int128 uint128_t
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter
NoopEventEmitter< FieldGreaterThanEvent > field_gt_event_emitter
FF update_timestamp_of_change
AppendOnlyTreeSnapshot tree
TreeState public_data_tree