Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
nullifier_exists.test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3#include <cstdint>
4
22
23namespace bb::avm2::constraining {
24namespace {
25
26using tracegen::ExecutionTraceBuilder;
27using tracegen::NullifierTreeCheckTraceBuilder;
28using tracegen::TestTraceContainer;
29
30using simulation::DeduplicatingEventEmitter;
31using simulation::EventEmitter;
32using simulation::FieldGreaterThan;
33using simulation::FieldGreaterThanEvent;
34using simulation::MockMerkleCheck;
35using simulation::MockPoseidon2;
36using simulation::MockRangeCheck;
37using simulation::NullifierTreeCheck;
39
40using testing::NiceMock;
41
43using C = Column;
44using nullifier_exists = bb::avm2::nullifier_exists<FF>;
45
46TEST(NullifierExistsConstrainingTest, PositiveTest)
47{
48 TestTraceContainer trace({ { { C::execution_sel, 1 },
49 { C::execution_sel_execute_nullifier_exists, 1 },
50 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
51 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
52 { C::execution_register_2_, /*exists=*/1 },
53 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
54 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
55 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
56 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
57 { C::execution_sel_opcode_error, 0 },
58 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
59 check_relation<nullifier_exists>(trace);
60}
61
62TEST(NullifierExistsConstrainingTest, PositiveNullifierNotExists)
63{
64 TestTraceContainer trace({ { { C::execution_sel, 1 },
65 { C::execution_sel_execute_nullifier_exists, 1 },
66 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
67 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
68 { C::execution_register_2_, /*exists=*/0 }, // nullifier does not exist!
69 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
70 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
71 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
72 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
73 { C::execution_sel_opcode_error, 0 },
74 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
75 check_relation<nullifier_exists>(trace);
76}
77
78TEST(NullifierExistsConstrainingTest, NegativeInvalidOutputTag)
79{
80 TestTraceContainer trace({ { { C::execution_sel, 1 },
81 { C::execution_sel_execute_nullifier_exists, 1 },
82 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
83 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
84 { C::execution_register_2_, /*exists=*/0 }, // nullifier does not exist!
85 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
86 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
87 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
88 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) }, // WRONG!
89 { C::execution_sel_opcode_error, 0 },
90 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
92 check_relation<nullifier_exists>(trace, nullifier_exists::SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG),
93 "NULLIFIER_EXISTS_U1_OUTPUT_TAG");
94}
95
96TEST(NullifierExistsConstrainingTest, NegativeNullifierExistsSuccess)
97{
98 TestTraceContainer trace({ {
99 { C::execution_sel_execute_nullifier_exists, 1 },
100 { C::execution_sel_opcode_error, 1 },
101 } });
102
104 "NULLIFIER_EXISTS_SUCCESS");
105}
106
107TEST(NullifierExistsConstrainingTest, Interactions)
108{
109 NiceMock<MockPoseidon2> poseidon2;
110 NiceMock<MockMerkleCheck> merkle_check;
111
112 NiceMock<MockRangeCheck> range_check;
113 DeduplicatingEventEmitter<FieldGreaterThanEvent> event_emitter;
114 FieldGreaterThan field_gt(range_check, event_emitter);
115
116 EventEmitter<NullifierTreeCheckEvent> nullifier_tree_check_event_emitter;
117 NullifierTreeCheck nullifier_tree_check(poseidon2, merkle_check, field_gt, nullifier_tree_check_event_emitter);
118
119 FF nullifier = 42;
120 FF address = 43;
121
122 AppendOnlyTreeSnapshot nullifier_tree_snapshot = AppendOnlyTreeSnapshot{
123 .root = 42,
124 .nextAvailableLeafIndex = 128,
125 };
126
127 nullifier_tree_check.assert_read(nullifier, address, true, {}, 0, {}, nullifier_tree_snapshot);
128
129 TestTraceContainer trace({ {
130 { C::execution_sel_execute_nullifier_exists, 1 },
131 { C::execution_register_0_, nullifier },
132 { C::execution_register_1_, address },
133 { C::execution_register_2_, /*exists=*/1 },
134 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
135 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
136 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
137 { C::execution_prev_nullifier_tree_root, nullifier_tree_snapshot.root },
138 { C::execution_sel_opcode_error, 0 },
139 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS },
140 } });
141
142 NullifierTreeCheckTraceBuilder nullifier_tree_check_trace_builder;
143 nullifier_tree_check_trace_builder.process(nullifier_tree_check_event_emitter.dump_events(), trace);
144
145 check_relation<nullifier_exists>(trace);
146
147 check_interaction<ExecutionTraceBuilder, lookup_nullifier_exists_nullifier_exists_check_settings>(trace);
148}
149
150// TODO(dbanks12): interaction tests
151
152} // namespace
153} // namespace bb::avm2::constraining
#define AVM_EXEC_OP_ID_NULLIFIER_EXISTS
static constexpr size_t SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG
static constexpr size_t SR_NULLIFIER_EXISTS_SUCCESS
EventEmitter< DataCopyEvent > event_emitter
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< NullifierTreeReadWriteEvent, CheckPointEventType > NullifierTreeCheckEvent
AvmFlavorSettings::FF FF
Definition field.hpp:10
typename Flavor::FF FF
FieldGreaterThan field_gt