Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
emit_notehash.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
19
20namespace bb::avm2::constraining {
21namespace {
22
23using tracegen::ExecutionTraceBuilder;
24using tracegen::NoteHashTreeCheckTraceBuilder;
25using tracegen::TestTraceContainer;
26
27using simulation::EventEmitter;
28using simulation::MockMerkleCheck;
29using simulation::MockPoseidon2;
30using simulation::MockRangeCheck;
31using simulation::NoteHashTreeCheck;
33
34using testing::_;
35using testing::NiceMock;
36using testing::Return;
37
39using C = Column;
40using emit_notehash = bb::avm2::emit_notehash<FF>;
42
43TEST(EmitNoteHashConstrainingTest, Positive)
44{
45 uint64_t prev_num_note_hashes_emitted = MAX_NOTE_HASHES_PER_TX - 1;
46 TestTraceContainer trace({ {
47 { C::execution_sel_execute_emit_notehash, 1 },
48 { C::execution_register_0_, /*note_hash=*/42 },
49 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
50 { C::execution_remaining_note_hashes_inv, FF(MAX_NOTE_HASHES_PER_TX - prev_num_note_hashes_emitted).invert() },
51 { C::execution_sel_write_note_hash, 1 },
52 { C::execution_sel_opcode_error, 0 },
53 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
54 { C::execution_prev_note_hash_tree_size, 1 },
55 { C::execution_note_hash_tree_size, 2 },
56 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
57 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
58 } });
59 check_relation<emit_notehash>(trace);
60}
61
62TEST(EmitNoteHashConstrainingTest, LimitReached)
63{
64 uint64_t prev_num_note_hashes_emitted = MAX_NOTE_HASHES_PER_TX;
65 TestTraceContainer trace({ {
66 { C::execution_sel_execute_emit_notehash, 1 },
67 { C::execution_register_0_, /*note_hash=*/42 },
68 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
69 { C::execution_sel_reached_max_note_hashes, 1 },
70 { C::execution_remaining_note_hashes_inv, 0 },
71 { C::execution_sel_opcode_error, 1 },
72 { C::execution_sel_write_note_hash, 0 },
73 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
74 { C::execution_prev_note_hash_tree_size, 1 },
75 { C::execution_prev_note_hash_tree_root, 27 },
76 { C::execution_note_hash_tree_size, 1 },
77 { C::execution_note_hash_tree_root, 27 },
78 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
79 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted },
80 } });
81 check_relation<emit_notehash>(trace);
82
83 // Negative test: sel_reached_max_note_hashes must be 1
84 trace.set(C::execution_sel_reached_max_note_hashes, 0, 0);
86 "MAX_NOTE_HASHES_REACHED");
87 trace.set(C::execution_sel_reached_max_note_hashes, 0, 1);
88
89 // Negative test: sel_opcode_error must be on
90 trace.set(C::execution_sel_opcode_error, 0, 0);
93 "OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC");
94 trace.set(C::execution_sel_opcode_error, 0, 1);
95
96 // Negative test: note hash tree root must be the same
97 trace.set(C::execution_note_hash_tree_root, 0, 28);
100 "EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED");
101
102 // Negative test: tree size must be the same
103 trace.set(C::execution_note_hash_tree_size, 0, 2);
105 "EMIT_NOTEHASH_TREE_SIZE_INCREASE");
106
107 // Negative test: num note hashes emitted must be the same
108 trace.set(C::execution_num_note_hashes_emitted, 0, prev_num_note_hashes_emitted + 1);
111 "EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE");
112}
113
114TEST(EmitNoteHashConstrainingTest, Interactions)
115{
116 NiceMock<MockPoseidon2> poseidon2;
117 NiceMock<MockMerkleCheck> merkle_check;
118 NiceMock<MockRangeCheck> range_check;
119
120 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
121 NoteHashTreeCheck note_hash_tree_check(27, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
122
123 FF note_hash = 42;
124 AztecAddress contract_address = 0xdeadbeef;
125
126 AppendOnlyTreeSnapshot prev_snapshot = AppendOnlyTreeSnapshot{
127 .root = 27,
128 .nextAvailableLeafIndex = 128,
129 };
130 uint32_t prev_num_note_hashes_emitted = 2;
131
132 EXPECT_CALL(merkle_check, write).WillOnce(Return(57));
133
134 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
135 note_hash, contract_address, prev_num_note_hashes_emitted, {}, prev_snapshot);
136
137 TestTraceContainer trace({ {
138 { C::execution_sel_execute_emit_notehash, 1 },
139 { C::execution_register_0_, note_hash },
140 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
141 { C::execution_remaining_note_hashes_inv, FF(MAX_NOTE_HASHES_PER_TX - prev_num_note_hashes_emitted).invert() },
142 { C::execution_sel_write_note_hash, 1 },
143 { C::execution_sel_opcode_error, 0 },
144 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
145 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
146 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
147 { C::execution_prev_note_hash_tree_root, prev_snapshot.root },
148 { C::execution_note_hash_tree_root, next_snapshot.root },
149 { C::execution_prev_note_hash_tree_size, prev_snapshot.nextAvailableLeafIndex },
150 { C::execution_note_hash_tree_size, next_snapshot.nextAvailableLeafIndex },
151 { C::execution_contract_address, contract_address },
152 } });
153
154 NoteHashTreeCheckTraceBuilder note_hash_tree_check_trace_builder;
155 note_hash_tree_check_trace_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
156 check_relation<emit_notehash>(trace);
157
158 check_interaction<ExecutionTraceBuilder, lookup_emit_notehash_notehash_tree_write_settings>(trace);
159}
160
161} // namespace
162} // namespace bb::avm2::constraining
#define AVM_EXEC_OP_ID_EMIT_NOTEHASH
#define MAX_NOTE_HASHES_PER_TX
static constexpr size_t SR_EMIT_NOTEHASH_TREE_SIZE_INCREASE
static constexpr size_t SR_OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC
static constexpr size_t SR_EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED
static constexpr size_t SR_MAX_NOTE_HASHES_REACHED
static constexpr size_t SR_EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE
void set(Column col, uint32_t row, const FF &value)
RangeCheck range_check
TestTraceContainer trace
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
AvmFlavorSettings::FF FF
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:508
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
std::variant< NoteHashTreeReadWriteEvent, CheckPointEventType > NoteHashTreeCheckEvent
typename Flavor::FF FF
void write(B &buf, field2< base_field, Params > const &value)