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>;
46
47TEST(NullifierExistsConstrainingTest, PositiveTest)
48{
49 TestTraceContainer trace({ { { C::execution_sel, 1 },
50 { C::execution_sel_execute_nullifier_exists, 1 },
51 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
52 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
53 { C::execution_register_2_, /*exists=*/1 },
54 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
55 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
56 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
57 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
58 { C::execution_sel_opcode_error, 0 },
59 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
60 check_relation<nullifier_exists>(trace);
61}
62
63TEST(NullifierExistsConstrainingTest, PositiveNullifierNotExists)
64{
65 TestTraceContainer trace({ { { C::execution_sel, 1 },
66 { C::execution_sel_execute_nullifier_exists, 1 },
67 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
68 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
69 { C::execution_register_2_, /*exists=*/0 }, // nullifier does not exist!
70 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
71 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
72 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
73 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
74 { C::execution_sel_opcode_error, 0 },
75 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
76 check_relation<nullifier_exists>(trace);
77}
78
79TEST(NullifierExistsConstrainingTest, NegativeInvalidOutputTag)
80{
81 TestTraceContainer trace({ { { C::execution_sel, 1 },
82 { C::execution_sel_execute_nullifier_exists, 1 },
83 { C::execution_register_0_, /*nullifier=*/FF(0x123456) },
84 { C::execution_register_1_, /*address=*/FF(0xdeadbeef) },
85 { C::execution_register_2_, /*exists=*/0 }, // nullifier does not exist!
86 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
87 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
88 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
89 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U8) }, // WRONG!
90 { C::execution_sel_opcode_error, 0 },
91 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
93 check_relation<nullifier_exists>(trace, nullifier_exists::SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG),
94 "NULLIFIER_EXISTS_U1_OUTPUT_TAG");
95}
96
97TEST(NullifierExistsConstrainingTest, NegativeNullifierExistsSuccess)
98{
99 TestTraceContainer trace({ {
100 { C::execution_sel_execute_nullifier_exists, 1 },
101 { C::execution_sel_opcode_error, 1 },
102 } });
103
105 "INFALLIBLE_OPCODES_SUCCESS");
106}
107
108TEST(NullifierExistsConstrainingTest, Interactions)
109{
110 NiceMock<MockPoseidon2> poseidon2;
111 NiceMock<MockMerkleCheck> merkle_check;
112
113 NiceMock<MockRangeCheck> range_check;
114 DeduplicatingEventEmitter<FieldGreaterThanEvent> event_emitter;
115 FieldGreaterThan field_gt(range_check, event_emitter);
116
117 EventEmitter<NullifierTreeCheckEvent> nullifier_tree_check_event_emitter;
118 NullifierTreeCheck nullifier_tree_check(poseidon2, merkle_check, field_gt, nullifier_tree_check_event_emitter);
119
120 FF nullifier = 42;
121 FF address = 43;
122
123 AppendOnlyTreeSnapshot nullifier_tree_snapshot = AppendOnlyTreeSnapshot{
124 .root = 42,
125 .next_available_leaf_index = 128,
126 };
127
128 nullifier_tree_check.assert_read(nullifier, address, true, {}, 0, {}, nullifier_tree_snapshot);
129
130 TestTraceContainer trace({ {
131 { C::execution_sel_execute_nullifier_exists, 1 },
132 { C::execution_register_0_, nullifier },
133 { C::execution_register_1_, address },
134 { C::execution_register_2_, /*exists=*/1 },
135 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
136 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::FF) },
137 { C::execution_mem_tag_reg_2_, static_cast<uint8_t>(MemoryTag::U1) },
138 { C::execution_prev_nullifier_tree_root, nullifier_tree_snapshot.root },
139 { C::execution_sel_opcode_error, 0 },
140 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS },
141 } });
142
143 NullifierTreeCheckTraceBuilder nullifier_tree_check_trace_builder;
144 nullifier_tree_check_trace_builder.process(nullifier_tree_check_event_emitter.dump_events(), trace);
145
146 check_relation<nullifier_exists>(trace);
147
148 check_interaction<ExecutionTraceBuilder, lookup_nullifier_exists_nullifier_exists_check_settings>(trace);
149}
150
151// TODO(dbanks12): interaction tests
152
153} // namespace
154} // namespace bb::avm2::constraining
FieldGreaterThan field_gt
#define AVM_EXEC_OP_ID_NULLIFIER_EXISTS
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:76
static constexpr size_t SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG
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:441
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
std::variant< NullifierTreeReadWriteEvent, CheckPointEventType > NullifierTreeCheckEvent
AvmFlavorSettings::FF FF
Definition field.hpp:10
NiceMock< MockExecution > execution