Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
execution.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
15
16namespace bb::avm2::constraining {
17namespace {
18
19using tracegen::TestTraceContainer;
21using C = Column;
23
24TEST(ExecutionConstrainingTest, EmptyRow)
25{
26 check_relation<execution>(testing::empty_trace());
27}
28
29// DO NOT SUBMIT: add full flow tests
30// TEST(ExecutionConstrainingTest, Basic)
31// {
32// // clang-format off
33// TestTraceContainer trace({
34// {{ C::execution_sel, 1 }, { C::execution_pc, 0 }},
35// {{ C::execution_sel, 1 }, { C::execution_pc, 20 }, { C::execution_last, 1 }}
36// });
37// // clang-format on
38
39// check_relation<execution>(trace);
40// }
41
42TEST(ExecutionConstrainingTest, Continuity)
43{
44 // clang-format off
45 TestTraceContainer trace({
46 {{ C::precomputed_first_row, 1 }},
47 {{ C::execution_sel, 1 }},
48 {{ C::execution_sel, 1 }},
49 {{ C::execution_sel, 1 }, {C::execution_last, 1}},
50 });
51 // clang-format on
52
53 check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY);
54}
55
56TEST(ExecutionConstrainingTest, ContinuityBrokenFirstRow)
57{
58 // clang-format off
59 TestTraceContainer trace({
60 {{ C::execution_sel, 0 }}, // End of trace!
61 {{ C::execution_sel, 1 }},
62 {{ C::execution_sel, 1 }},
63 {{ C::execution_sel, 1 }, {C::execution_last, 1}},
64 });
65 // clang-format on
66
67 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY), "TRACE_CONTINUITY");
68}
69
70TEST(ExecutionConstrainingTest, ContinuityBrokenInMiddle)
71{
72 // clang-format off
73 TestTraceContainer trace({
74 {{ C::execution_sel, 1 }},
75 {{ C::execution_sel, 0 }}, // End of trace!
76 {{ C::execution_sel, 1 }},
77 {{ C::execution_sel, 1 }, {C::execution_last, 1}},
78 });
79 // clang-format on
80
81 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_TRACE_CONTINUITY), "TRACE_CONTINUITY");
82}
83
84TEST(ExecutionConstrainingTest, ContinuityMultipleLast)
85{
86 // clang-format off
87 TestTraceContainer trace({
88 {{ C::execution_sel, 1 }},
89 {{ C::execution_sel, 1 }},
90 {{ C::execution_sel, 1 }},
91 {{ C::execution_sel, 1 }, {C::execution_last, 1}},
92 });
93 // clang-format on
94
95 // Last is correct.
96 check_relation<execution>(trace, execution::SR_LAST_IS_LAST);
97 // If we add another last, it should fail.
98 trace.set(C::execution_last, /*row=*/1, /*value=*/1);
99 EXPECT_THROW_WITH_MESSAGE(check_relation<execution>(trace, execution::SR_LAST_IS_LAST), "LAST_IS_LAST.*row 1");
100}
101
102TEST(ExecutionConstrainingTest, TreeStateNotChanged)
103{
104 TestTraceContainer trace({
105 {
106 { C::precomputed_first_row, 1 },
107 },
108 {
109 { C::execution_sel, 1 },
110 { C::execution_prev_note_hash_tree_root, 10 },
111 { C::execution_prev_note_hash_tree_size, 9 },
112 { C::execution_prev_num_note_hashes_emitted, 8 },
113 { C::execution_prev_nullifier_tree_root, 7 },
114 { C::execution_prev_nullifier_tree_size, 6 },
115 { C::execution_prev_num_nullifiers_emitted, 5 },
116 { C::execution_prev_public_data_tree_root, 4 },
117 { C::execution_prev_public_data_tree_size, 3 },
118 { C::execution_prev_written_public_data_slots_tree_root, 2 },
119 { C::execution_prev_written_public_data_slots_tree_size, 1 },
120 { C::execution_prev_retrieved_bytecodes_tree_root, 12 },
121 { C::execution_prev_retrieved_bytecodes_tree_size, 13 },
122 { C::execution_note_hash_tree_root, 10 },
123 { C::execution_note_hash_tree_size, 9 },
124 { C::execution_num_note_hashes_emitted, 8 },
125 { C::execution_nullifier_tree_root, 7 },
126 { C::execution_nullifier_tree_size, 6 },
127 { C::execution_num_nullifiers_emitted, 5 },
128 { C::execution_public_data_tree_root, 4 },
129 { C::execution_public_data_tree_size, 3 },
130 { C::execution_written_public_data_slots_tree_root, 2 },
131 { C::execution_written_public_data_slots_tree_size, 1 },
132 { C::execution_retrieved_bytecodes_tree_root, 12 },
133 { C::execution_retrieved_bytecodes_tree_size, 13 },
134 },
135 });
136
137 check_relation<execution>(trace,
150
151 // Negative test: change note hash tree root
152 trace.set(C::execution_note_hash_tree_root, 1, 100);
154 "NOTE_HASH_TREE_ROOT_NOT_CHANGED");
155
156 // Negative test: change note hash tree size
157 trace.set(C::execution_note_hash_tree_size, 1, 100);
159 "NOTE_HASH_TREE_SIZE_NOT_CHANGED");
160
161 // Negative test: change num note hashes emitted
162 trace.set(C::execution_num_note_hashes_emitted, 1, 100);
164 "NUM_NOTE_HASHES_EMITTED_NOT_CHANGED");
165
166 // Negative test: change nullifier tree root
167 trace.set(C::execution_nullifier_tree_root, 1, 100);
169 "NULLIFIER_TREE_ROOT_NOT_CHANGED");
170
171 // Negative test: change nullifier tree size
172 trace.set(C::execution_nullifier_tree_size, 1, 100);
174 "NULLIFIER_TREE_SIZE_NOT_CHANGED");
175
176 // Negative test: change num nullifiers emitted
177 trace.set(C::execution_prev_num_nullifiers_emitted, 1, 100);
179 "NUM_NULLIFIERS_EMITTED_NOT_CHANGED");
180
181 // Negative test: change public data tree root
182 trace.set(C::execution_public_data_tree_root, 1, 100);
184 "PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
185
186 // Negative test: change public data tree size
187 trace.set(C::execution_public_data_tree_size, 1, 100);
189 "PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
190
191 // Negative test: change written public data slots tree root
192 trace.set(C::execution_written_public_data_slots_tree_root, 1, 100);
195 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_NOT_CHANGED");
196
197 // Negative test: change written public data slots tree size
198 trace.set(C::execution_written_public_data_slots_tree_size, 1, 100);
201 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_NOT_CHANGED");
202
203 // Negative test: change retrieved bytecodes tree root
204 trace.set(C::execution_retrieved_bytecodes_tree_root, 1, 100);
206 "RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED");
207
208 // Negative test: change retrieved bytecodes tree size
209 trace.set(C::execution_retrieved_bytecodes_tree_size, 1, 100);
211 "RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED");
212}
213
214TEST(ExecutionConstrainingTest, SideEffectStateNotChanged)
215{
216 TestTraceContainer trace({
217 {
218 { C::precomputed_first_row, 1 },
219 },
220 {
221 { C::execution_sel, 1 },
222 { C::execution_prev_num_unencrypted_log_fields, 10 },
223 { C::execution_prev_num_l2_to_l1_messages, 11 },
224 { C::execution_num_unencrypted_log_fields, 10 },
225 { C::execution_num_l2_to_l1_messages, 11 },
226 },
227 });
228
229 check_relation<execution>(
231
232 // Negative test: change num unencrypted logs
233 trace.set(C::execution_num_unencrypted_log_fields, 1, 100);
235 "NUM_UNENCRYPTED_LOGS_NOT_CHANGED");
236
237 // Negative test: change num l2 to l1 messages
238 trace.set(C::execution_num_l2_to_l1_messages, 1, 100);
240 "NUM_L2_TO_L1_MESSAGES_NOT_CHANGED");
241}
242
243TEST(ExecutionConstrainingTest, SubtraceIdDecomposition)
244{
248
249 TestTraceContainer trace;
250 const uint8_t enum_length = static_cast<uint8_t>(SubtraceSel::MAX) + 1;
251
252 for (uint8_t i = 0; i < enum_length; i++) {
253 SubtraceSel subtrace_sel = static_cast<SubtraceSel>(i);
254 const auto subtrace_id = get_subtrace_id(subtrace_sel);
255 const auto subtrace_selector = get_subtrace_selector(subtrace_sel);
256
257 trace.set(i,
258 { {
259 { subtrace_selector, 1 },
260 { C::execution_subtrace_id, subtrace_id },
261 { C::execution_sel_should_execute_opcode, 1 },
262 } });
263 }
264
265 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
266
267 for (uint8_t i = 0; i < enum_length; i++) {
268 const auto subtrace_selector = get_subtrace_selector(static_cast<SubtraceSel>(i));
269
270 // Negative test: de-activate the selector
271 trace.set(subtrace_selector, i, 0);
273 "SUBTRACE_ID_DECOMPOSITION");
274
275 // Negative test: activate the wrong selector
276 const auto wrong_selector = get_subtrace_selector(static_cast<SubtraceSel>((i + 1) % enum_length));
277 trace.set(wrong_selector, i, 1);
279 "SUBTRACE_ID_DECOMPOSITION");
280 // De-activate the wrong selector
281 trace.set(wrong_selector, i, 0);
282
283 // Re-activate the correct selector
284 trace.set(subtrace_selector, i, 1);
285
286 // Ensure we have a correct trace for the next iteration
287 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
288 }
289}
290
291} // namespace
292} // namespace bb::avm2::constraining
static constexpr size_t SR_NUM_L2_TO_L1_MESSAGES_NOT_CHANGED
Definition execution.hpp:71
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:72
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:63
static constexpr size_t SR_NULLIFIER_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:68
static constexpr size_t SR_LAST_IS_LAST
Definition execution.hpp:45
static constexpr size_t SR_NUM_NULLIFIERS_EMITTED_NOT_CHANGED
Definition execution.hpp:69
static constexpr size_t SR_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:61
static constexpr size_t SR_NOTE_HASH_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:64
static constexpr size_t SR_TRACE_CONTINUITY
Definition execution.hpp:44
static constexpr size_t SR_SUBTRACE_ID_DECOMPOSITION
Definition execution.hpp:49
static constexpr size_t SR_NULLIFIER_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:67
static constexpr size_t SR_NUM_UNENCRYPTED_LOGS_NOT_CHANGED
Definition execution.hpp:70
static constexpr size_t SR_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:60
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:62
static constexpr size_t SR_NOTE_HASH_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:65
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:73
static constexpr size_t SR_NUM_NOTE_HASHES_EMITTED_NOT_CHANGED
Definition execution.hpp:66
void set(Column col, uint32_t row, const FF &value)
TestTraceContainer trace
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:402
TestTraceContainer empty_trace()
Definition fixtures.cpp:153
Column get_subtrace_selector(SubtraceSel subtrace_sel)
Get the column selector for a given subtrace selector.
FF get_subtrace_id(SubtraceSel subtrace_sel)
Get the subtrace ID for a given subtrace enum.
typename Flavor::FF FF
NiceMock< MockExecution > execution