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>;
49
50TEST(NoteHashExistsConstrainingTest, PositiveExists)
51{
52 TestTraceContainer trace({
53 { { C::execution_sel_execute_notehash_exists, 1 },
54 { C::execution_register_0_, /*unique_note_hash=*/42 },
55 { C::execution_register_1_, /*leaf_index=*/27 },
56 { C::execution_register_2_, /*dst=*/1 },
57 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
58 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
59 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
60 { C::execution_note_hash_leaf_in_range, 1 },
61 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
62 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS } },
63 });
64 check_relation<notehash_exists>(trace);
65}
66
67TEST(NoteHashExistsConstrainingTest, OutOfRange)
68{
69 TestTraceContainer trace({
70 { { C::execution_sel_execute_notehash_exists, 1 },
71 { C::execution_register_0_, /*unique_note_hash=*/42 },
72 { C::execution_register_1_, /*leaf_index=*/AVM_EXEC_OP_ID_NOTEHASH_EXISTS + 1 },
73 { C::execution_register_2_, /*dst=*/0 },
74 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
75 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
76 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
77 { C::execution_note_hash_leaf_in_range, 0 },
78 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
79 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS } },
80 });
81
82 check_relation<notehash_exists>(trace);
83
84 // Negative test: exists must be false
85 trace.set(C::execution_register_2_, 0, 1);
86 EXPECT_THROW_WITH_MESSAGE(check_relation<notehash_exists>(trace), "NOTE_HASH_EXISTS_OUT_OF_RANGE_FALSE");
87}
88
89TEST(NoteHashExistsConstrainingTest, NegativeInvalidOutputTag)
90{
91 TestTraceContainer trace({ {
92 { C::execution_sel_execute_notehash_exists, 1 },
93 { C::execution_register_0_, /*unique_note_hash=*/42 },
94 { C::execution_register_1_, /*leaf_index=*/27 },
95 { C::execution_register_2_, /*dst=*/1 },
96 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
97 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
98 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) },
99 } });
101 "NOTEHASH_EXISTS_U1_OUTPUT_TAG");
102}
103
104TEST(NoteHashExistsConstrainingTest, NegativeNoteHashExistsSuccess)
105{
106 TestTraceContainer trace({ {
107 { C::execution_sel_execute_notehash_exists, 1 },
108 { C::execution_sel_opcode_error, 1 },
109 } });
110
112 "INFALLIBLE_OPCODES_SUCCESS");
113}
114
115TEST(NoteHashExistsConstrainingTest, Interactions)
116{
117 NiceMock<MockPoseidon2> poseidon2;
118 NiceMock<MockMerkleCheck> merkle_check;
119 NiceMock<MockFieldGreaterThan> field_gt;
120 NiceMock<MockRangeCheck> range_check;
121
122 EventEmitter<GreaterThanEvent> greater_than_event_emitter;
123 GreaterThan greater_than(field_gt, range_check, greater_than_event_emitter);
124 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
125 NoteHashTreeCheck note_hash_tree_check(27, poseidon2, merkle_check, note_hash_tree_check_event_emitter);
126
127 FF requested_note_hash = 42;
128 FF actual_leaf_value = 43;
129
130 uint64_t leaf_index = 27;
131
132 AppendOnlyTreeSnapshot note_hash_tree_snapshot = AppendOnlyTreeSnapshot{
133 .root = 42,
134 .next_available_leaf_index = 128,
135 };
136
138 note_hash_tree_check.note_hash_exists(
139 requested_note_hash, actual_leaf_value, leaf_index, {}, note_hash_tree_snapshot);
140
141 TestTraceContainer trace({ {
142 { C::execution_sel_execute_notehash_exists, 1 },
143 { C::execution_register_0_, requested_note_hash },
144 { C::execution_register_1_, leaf_index },
145 { C::execution_register_2_, /*result=*/0 },
146 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
147 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U64) },
148 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
149 { C::execution_note_hash_leaf_in_range, 1 },
150 { C::execution_sel_opcode_error, 0 },
151 { C::execution_note_hash_tree_leaf_count, static_cast<uint64_t>(NOTE_HASH_TREE_LEAF_COUNT) },
152 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NOTEHASH_EXISTS },
153 { C::execution_prev_note_hash_tree_root, note_hash_tree_snapshot.root },
154 } });
155
156 NoteHashTreeCheckTraceBuilder note_hash_tree_check_trace_builder;
157 note_hash_tree_check_trace_builder.process(note_hash_tree_check_event_emitter.dump_events(), trace);
158
159 GreaterThanTraceBuilder greater_than_trace_builder;
160 greater_than_trace_builder.process(greater_than_event_emitter.dump_events(), trace);
161
162 check_relation<notehash_exists>(trace);
163
164 check_interaction<ExecutionTraceBuilder,
167}
168
169} // namespace
170} // namespace bb::avm2::constraining
GreaterThan greater_than
FieldGreaterThan field_gt
#define AVM_EXEC_OP_ID_NOTEHASH_EXISTS
#define NOTE_HASH_TREE_LEAF_COUNT
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:76
static constexpr size_t SR_NOTEHASH_EXISTS_U1_OUTPUT_TAG
bool gt(const FF &a, const FF &b) override
Definition gt.cpp:11
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
void check_interaction(tracegen::TestTraceContainer &trace)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
Definition tx.test.cpp:441
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
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter
NiceMock< MockExecution > execution