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
20
21namespace bb::avm2::constraining {
22namespace {
23
24using tracegen::ExecutionTraceBuilder;
25using tracegen::NoteHashTreeCheckTraceBuilder;
26using tracegen::TestTraceContainer;
27
28using simulation::EventEmitter;
29using simulation::MockMerkleCheck;
30using simulation::MockPoseidon2;
31using simulation::MockRangeCheck;
32using simulation::NoteHashTreeCheck;
34
35using testing::_;
36using testing::NiceMock;
37using testing::Return;
38
40using C = Column;
41using emit_notehash = bb::avm2::emit_notehash<FF>;
43
44TEST(EmitNoteHashConstrainingTest, Positive)
45{
46 uint64_t prev_num_note_hashes_emitted = MAX_NOTE_HASHES_PER_TX - 1;
47 TestTraceContainer trace({ {
48 { C::execution_sel_execute_emit_notehash, 1 },
49 { C::execution_register_0_, /*note_hash=*/42 },
50 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
51 { C::execution_remaining_note_hashes_inv, FF(MAX_NOTE_HASHES_PER_TX - prev_num_note_hashes_emitted).invert() },
52 { C::execution_sel_write_note_hash, 1 },
53 { C::execution_sel_opcode_error, 0 },
54 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
55 { C::execution_prev_note_hash_tree_size, 1 },
56 { C::execution_note_hash_tree_size, 2 },
57 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
58 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
59 } });
60 check_relation<emit_notehash>(trace);
61}
62
63TEST(EmitNoteHashConstrainingTest, LimitReached)
64{
65 uint64_t prev_num_note_hashes_emitted = MAX_NOTE_HASHES_PER_TX;
66 TestTraceContainer trace({ {
67 { C::execution_sel_execute_emit_notehash, 1 },
68 { C::execution_register_0_, /*note_hash=*/42 },
69 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
70 { C::execution_sel_reached_max_note_hashes, 1 },
71 { C::execution_remaining_note_hashes_inv, 0 },
72 { C::execution_sel_opcode_error, 1 },
73 { C::execution_sel_write_note_hash, 0 },
74 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
75 { C::execution_prev_note_hash_tree_size, 1 },
76 { C::execution_prev_note_hash_tree_root, 27 },
77 { C::execution_note_hash_tree_size, 1 },
78 { C::execution_note_hash_tree_root, 27 },
79 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
80 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted },
81 } });
82 check_relation<emit_notehash>(trace);
83
84 // Negative test: sel_reached_max_note_hashes must be 1
85 trace.set(C::execution_sel_reached_max_note_hashes, 0, 0);
87 "MAX_NOTE_HASHES_REACHED");
88 trace.set(C::execution_sel_reached_max_note_hashes, 0, 1);
89
90 // Negative test: sel_opcode_error must be on
91 trace.set(C::execution_sel_opcode_error, 0, 0);
94 "OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC");
95 trace.set(C::execution_sel_opcode_error, 0, 1);
96
97 // Negative test: note hash tree root must be the same
98 trace.set(C::execution_note_hash_tree_root, 0, 28);
100 check_relation<emit_notehash>(trace, emit_notehash::SR_EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED),
101 "EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED");
102
103 // Negative test: tree size must be the same
104 trace.set(C::execution_note_hash_tree_size, 0, 2);
106 "EMIT_NOTEHASH_TREE_SIZE_INCREASE");
107
108 // Negative test: num note hashes emitted must be the same
109 trace.set(C::execution_num_note_hashes_emitted, 0, prev_num_note_hashes_emitted + 1);
112 "EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE");
113}
114
115TEST(EmitNoteHashConstrainingTest, Interactions)
116{
117 NiceMock<MockPoseidon2> poseidon2;
118 NiceMock<MockMerkleCheck> merkle_check;
119 NiceMock<MockRangeCheck> range_check;
120
121 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
122 NoteHashTreeCheck note_hash_tree_check(27, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
123
124 FF note_hash = 42;
125 AztecAddress contract_address = 0xdeadbeef;
126
127 AppendOnlyTreeSnapshot prev_snapshot = AppendOnlyTreeSnapshot{
128 .root = 27,
129 .nextAvailableLeafIndex = 128,
130 };
131 uint32_t prev_num_note_hashes_emitted = 2;
132
133 EXPECT_CALL(merkle_check, write).WillOnce(Return(57));
134
135 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
136 note_hash, contract_address, prev_num_note_hashes_emitted, {}, prev_snapshot);
137
138 TestTraceContainer trace({ {
139 { C::execution_sel_execute_emit_notehash, 1 },
140 { C::execution_register_0_, note_hash },
141 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
142 { C::execution_remaining_note_hashes_inv, FF(MAX_NOTE_HASHES_PER_TX - prev_num_note_hashes_emitted).invert() },
143 { C::execution_sel_write_note_hash, 1 },
144 { C::execution_sel_opcode_error, 0 },
145 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_EMIT_NOTEHASH },
146 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
147 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
148 { C::execution_prev_note_hash_tree_root, prev_snapshot.root },
149 { C::execution_note_hash_tree_root, next_snapshot.root },
150 { C::execution_prev_note_hash_tree_size, prev_snapshot.nextAvailableLeafIndex },
151 { C::execution_note_hash_tree_size, next_snapshot.nextAvailableLeafIndex },
152 { C::execution_contract_address, contract_address },
153 } });
154
155 NoteHashTreeCheckTraceBuilder note_hash_tree_check_trace_builder;
156 note_hash_tree_check_trace_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
157 check_relation<emit_notehash>(trace);
158
159 check_interaction<ExecutionTraceBuilder, lookup_emit_notehash_notehash_tree_write_settings>(trace);
160}
161
162} // namespace
163} // 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
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:402
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
std::variant< NoteHashTreeReadWriteEvent, CheckPointEventType > NoteHashTreeCheckEvent
AvmFlavorSettings::FF FF
Definition field.hpp:10
void write(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)
typename Flavor::FF FF