Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
notehash_exists.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
23
24namespace bb::avm2::constraining {
25namespace {
26
27using tracegen::ExecutionTraceBuilder;
28using tracegen::GreaterThanTraceBuilder;
29using tracegen::NoteHashTreeCheckTraceBuilder;
30using tracegen::TestTraceContainer;
31
32using simulation::EventEmitter;
33using simulation::GreaterThan;
34using simulation::GreaterThanEvent;
35using simulation::MockFieldGreaterThan;
36using simulation::MockMerkleCheck;
37using simulation::MockPoseidon2;
38using simulation::MockRangeCheck;
39using simulation::NoteHashTreeCheck;
41
42using testing::NiceMock;
43
45using C = Column;
46using notehash_exists = bb::avm2::notehash_exists<FF>;
48
49TEST(NoteHashExistsConstrainingTest, PositiveExists)
50{
51 TestTraceContainer trace({
52 { { C::execution_sel_execute_notehash_exists, 1 },
53 { C::execution_register_0_, /*unique_note_hash=*/42 },
54 { C::execution_register_1_, /*leaf_index=*/27 },
55 { C::execution_register_2_, /*dst=*/1 },
56 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
57 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
58 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
59 { C::execution_note_hash_leaf_in_range, 1 },
60 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
61 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS } },
62 });
63 check_relation<notehash_exists>(trace);
64}
65
66TEST(NoteHashExistsConstrainingTest, OutOfRange)
67{
68 TestTraceContainer trace({
69 { { C::execution_sel_execute_notehash_exists, 1 },
70 { C::execution_register_0_, /*unique_note_hash=*/42 },
71 { C::execution_register_1_, /*leaf_index=*/AVM_EXEC_OP_ID_NOTEHASH_EXISTS + 1 },
72 { C::execution_register_2_, /*dst=*/0 },
73 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
74 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
75 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
76 { C::execution_note_hash_leaf_in_range, 0 },
77 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
78 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS } },
79 });
80
81 check_relation<notehash_exists>(trace);
82
83 // Negative test: exists must be false
84 trace.set(C::execution_register_2_, 0, 1);
85 EXPECT_THROW_WITH_MESSAGE(check_relation<notehash_exists>(trace), "NOTE_HASH_EXISTS_OUT_OF_RANGE_FALSE");
86}
87
88TEST(NoteHashExistsConstrainingTest, NegativeInvalidOutputTag)
89{
90 TestTraceContainer trace({ {
91 { C::execution_sel_execute_notehash_exists, 1 },
92 { C::execution_register_0_, /*unique_note_hash=*/42 },
93 { C::execution_register_1_, /*leaf_index=*/27 },
94 { C::execution_register_2_, /*dst=*/1 },
95 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
96 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
97 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) },
98 } });
100 "NOTEHASH_EXISTS_U1_OUTPUT_TAG");
101}
102
103TEST(NoteHashExistsConstrainingTest, NegativeNoteHashExistsSuccess)
104{
105 TestTraceContainer trace({ {
106 { C::execution_sel_execute_notehash_exists, 1 },
107 { C::execution_sel_opcode_error, 1 },
108 } });
109
111 "NOTE_HASH_EXISTS_SUCCESS");
112}
113
114TEST(NoteHashExistsConstrainingTest, Interactions)
115{
116 NiceMock<MockPoseidon2> poseidon2;
117 NiceMock<MockMerkleCheck> merkle_check;
118 NiceMock<MockFieldGreaterThan> field_gt;
119 NiceMock<MockRangeCheck> range_check;
120
121 EventEmitter<GreaterThanEvent> greater_than_event_emitter;
122 GreaterThan greater_than(field_gt, range_check, greater_than_event_emitter);
123 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
124 NoteHashTreeCheck note_hash_tree_check(27, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
125
126 FF requested_note_hash = 42;
127 FF actual_leaf_value = 43;
128
129 uint64_t leaf_index = 27;
130
131 AppendOnlyTreeSnapshot note_hash_tree_snapshot = AppendOnlyTreeSnapshot{
132 .root = 42,
133 .nextAvailableLeafIndex = 128,
134 };
135
137 note_hash_tree_check.note_hash_exists(
138 requested_note_hash, actual_leaf_value, leaf_index, {}, note_hash_tree_snapshot);
139
140 TestTraceContainer trace({ {
141 { C::execution_sel_execute_notehash_exists, 1 },
142 { C::execution_register_0_, requested_note_hash },
143 { C::execution_register_1_, leaf_index },
144 { C::execution_register_2_, /*result=*/0 },
145 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
146 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
147 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
148 { C::execution_note_hash_leaf_in_range, 1 },
149 { C::execution_sel_opcode_error, 0 },
150 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
151 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS },
152 { C::execution_prev_note_hash_tree_root, note_hash_tree_snapshot.root },
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
158 GreaterThanTraceBuilder greater_than_trace_builder;
159 greater_than_trace_builder.process(greater_than_event_emitter.dump_events(), trace);
160
161 check_relation<notehash_exists>(trace);
162
163 check_interaction<ExecutionTraceBuilder,
166}
167
168} // namespace
169} // namespace bb::avm2::constraining
#define AVM_EXEC_OP_ID_NOTEHASH_EXISTS
#define NOTE_HASH_TREE_LEAF_COUNT
static constexpr size_t SR_NOTEHASH_EXISTS_U1_OUTPUT_TAG
static constexpr size_t SR_NOTE_HASH_EXISTS_SUCCESS
void set(Column col, uint32_t row, const FF &value)
RangeCheck range_check
TestTraceContainer trace
testing::StrictMock< MockGreaterThan > greater_than
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:402
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
std::variant< NoteHashTreeReadWriteEvent, CheckPointEventType > NoteHashTreeCheckEvent
lookup_settings< lookup_notehash_exists_note_hash_read_settings_ > lookup_notehash_exists_note_hash_read_settings
lookup_settings< lookup_notehash_exists_note_hash_leaf_index_in_range_settings_ > lookup_notehash_exists_note_hash_leaf_index_in_range_settings
typename Flavor::FF FF
FieldGreaterThan field_gt
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter