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 }},
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 }},
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 }},
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, TreeStateNotChanged)
85{
86 TestTraceContainer trace({
87 {
88 { C::precomputed_first_row, 1 },
89 },
90 {
91 { C::execution_sel, 1 },
92 { C::execution_prev_note_hash_tree_root, 10 },
93 { C::execution_prev_note_hash_tree_size, 9 },
94 { C::execution_prev_num_note_hashes_emitted, 8 },
95 { C::execution_prev_nullifier_tree_root, 7 },
96 { C::execution_prev_nullifier_tree_size, 6 },
97 { C::execution_prev_num_nullifiers_emitted, 5 },
98 { C::execution_prev_public_data_tree_root, 4 },
99 { C::execution_prev_public_data_tree_size, 3 },
100 { C::execution_prev_written_public_data_slots_tree_root, 2 },
101 { C::execution_prev_written_public_data_slots_tree_size, 1 },
102 { C::execution_prev_retrieved_bytecodes_tree_root, 12 },
103 { C::execution_prev_retrieved_bytecodes_tree_size, 13 },
104 { C::execution_note_hash_tree_root, 10 },
105 { C::execution_note_hash_tree_size, 9 },
106 { C::execution_num_note_hashes_emitted, 8 },
107 { C::execution_nullifier_tree_root, 7 },
108 { C::execution_nullifier_tree_size, 6 },
109 { C::execution_num_nullifiers_emitted, 5 },
110 { C::execution_public_data_tree_root, 4 },
111 { C::execution_public_data_tree_size, 3 },
112 { C::execution_written_public_data_slots_tree_root, 2 },
113 { C::execution_written_public_data_slots_tree_size, 1 },
114 { C::execution_retrieved_bytecodes_tree_root, 12 },
115 { C::execution_retrieved_bytecodes_tree_size, 13 },
116 },
117 });
118
119 check_relation<execution>(trace,
132
133 // Negative test: change note hash tree root
134 trace.set(C::execution_note_hash_tree_root, 1, 100);
136 "NOTE_HASH_TREE_ROOT_NOT_CHANGED");
137
138 // Negative test: change note hash tree size
139 trace.set(C::execution_note_hash_tree_size, 1, 100);
141 "NOTE_HASH_TREE_SIZE_NOT_CHANGED");
142
143 // Negative test: change num note hashes emitted
144 trace.set(C::execution_num_note_hashes_emitted, 1, 100);
146 "NUM_NOTE_HASHES_EMITTED_NOT_CHANGED");
147
148 // Negative test: change nullifier tree root
149 trace.set(C::execution_nullifier_tree_root, 1, 100);
151 "NULLIFIER_TREE_ROOT_NOT_CHANGED");
152
153 // Negative test: change nullifier tree size
154 trace.set(C::execution_nullifier_tree_size, 1, 100);
156 "NULLIFIER_TREE_SIZE_NOT_CHANGED");
157
158 // Negative test: change num nullifiers emitted
159 trace.set(C::execution_prev_num_nullifiers_emitted, 1, 100);
161 "NUM_NULLIFIERS_EMITTED_NOT_CHANGED");
162
163 // Negative test: change public data tree root
164 trace.set(C::execution_public_data_tree_root, 1, 100);
166 "PUBLIC_DATA_TREE_ROOT_NOT_CHANGED");
167
168 // Negative test: change public data tree size
169 trace.set(C::execution_public_data_tree_size, 1, 100);
171 "PUBLIC_DATA_TREE_SIZE_NOT_CHANGED");
172
173 // Negative test: change written public data slots tree root
174 trace.set(C::execution_written_public_data_slots_tree_root, 1, 100);
177 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_NOT_CHANGED");
178
179 // Negative test: change written public data slots tree size
180 trace.set(C::execution_written_public_data_slots_tree_size, 1, 100);
183 "WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_NOT_CHANGED");
184
185 // Negative test: change retrieved bytecodes tree root
186 trace.set(C::execution_retrieved_bytecodes_tree_root, 1, 100);
188 "RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED");
189
190 // Negative test: change retrieved bytecodes tree size
191 trace.set(C::execution_retrieved_bytecodes_tree_size, 1, 100);
193 "RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED");
194}
195
196TEST(ExecutionConstrainingTest, SideEffectStateNotChanged)
197{
198 TestTraceContainer trace({
199 {
200 { C::precomputed_first_row, 1 },
201 },
202 {
203 { C::execution_sel, 1 },
204 { C::execution_prev_num_unencrypted_log_fields, 10 },
205 { C::execution_prev_num_l2_to_l1_messages, 11 },
206 { C::execution_num_unencrypted_log_fields, 10 },
207 { C::execution_num_l2_to_l1_messages, 11 },
208 },
209 });
210
211 check_relation<execution>(
213
214 // Negative test: change num unencrypted logs
215 trace.set(C::execution_num_unencrypted_log_fields, 1, 100);
217 "NUM_UNENCRYPTED_LOGS_NOT_CHANGED");
218
219 // Negative test: change num l2 to l1 messages
220 trace.set(C::execution_num_l2_to_l1_messages, 1, 100);
222 "NUM_L2_TO_L1_MESSAGES_NOT_CHANGED");
223}
224
225TEST(ExecutionConstrainingTest, SubtraceIdDecomposition)
226{
230
231 TestTraceContainer trace;
232 const uint8_t enum_length = static_cast<uint8_t>(SubtraceSel::MAX) + 1;
233
234 for (uint8_t i = 0; i < enum_length; i++) {
235 SubtraceSel subtrace_sel = static_cast<SubtraceSel>(i);
236 const auto subtrace_id = get_subtrace_id(subtrace_sel);
237 const auto subtrace_selector = get_subtrace_selector(subtrace_sel);
238
239 trace.set(i,
240 { {
241 { subtrace_selector, 1 },
242 { C::execution_subtrace_id, subtrace_id },
243 { C::execution_sel_should_execute_opcode, 1 },
244 } });
245 }
246
247 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
248
249 for (uint8_t i = 0; i < enum_length; i++) {
250 const auto subtrace_selector = get_subtrace_selector(static_cast<SubtraceSel>(i));
251
252 // Negative test: de-activate the selector
253 trace.set(subtrace_selector, i, 0);
255 "SUBTRACE_ID_DECOMPOSITION");
256
257 // Negative test: activate the wrong selector
258 const auto wrong_selector = get_subtrace_selector(static_cast<SubtraceSel>((i + 1) % enum_length));
259 trace.set(wrong_selector, i, 1);
261 "SUBTRACE_ID_DECOMPOSITION");
262 // De-activate the wrong selector
263 trace.set(wrong_selector, i, 0);
264
265 // Re-activate the correct selector
266 trace.set(subtrace_selector, i, 1);
267
268 // Ensure we have a correct trace for the next iteration
269 check_relation<execution>(trace, execution::SR_SUBTRACE_ID_DECOMPOSITION);
270 }
271}
272
273} // namespace
274} // namespace bb::avm2::constraining
static constexpr size_t SR_NUM_L2_TO_L1_MESSAGES_NOT_CHANGED
Definition execution.hpp:73
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:74
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:65
static constexpr size_t SR_NULLIFIER_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:70
static constexpr size_t SR_NUM_NULLIFIERS_EMITTED_NOT_CHANGED
Definition execution.hpp:71
static constexpr size_t SR_PUBLIC_DATA_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:63
static constexpr size_t SR_NOTE_HASH_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:66
static constexpr size_t SR_TRACE_CONTINUITY
Definition execution.hpp:44
static constexpr size_t SR_SUBTRACE_ID_DECOMPOSITION
Definition execution.hpp:52
static constexpr size_t SR_NULLIFIER_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:69
static constexpr size_t SR_NUM_UNENCRYPTED_LOGS_NOT_CHANGED
Definition execution.hpp:72
static constexpr size_t SR_PUBLIC_DATA_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:62
static constexpr size_t SR_WRITTEN_PUBLIC_DATA_SLOTS_TREE_ROOT_NOT_CHANGED
Definition execution.hpp:64
static constexpr size_t SR_NOTE_HASH_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:67
static constexpr size_t SR_RETRIEVED_BYTECODES_TREE_SIZE_NOT_CHANGED
Definition execution.hpp:75
static constexpr size_t SR_NUM_NOTE_HASHES_EMITTED_NOT_CHANGED
Definition execution.hpp:68
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:441
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.
NiceMock< MockExecution > execution