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;
41
42using testing::NiceMock;
43
45using C = Column;
46using nullifier_exists = bb::avm2::nullifier_exists<FF>;
48
49TEST(NullifierExistsConstrainingTest, PositiveTest)
50{
51 TestTraceContainer trace({ { { C::execution_sel, 1 },
52 { C::execution_sel_execute_nullifier_exists, 1 },
53 { C::execution_register_0_, /*siloed_nullifier=*/FF(0x123456) },
54 { C::execution_register_1_, /*exists=*/1 },
55 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
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::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_, /*siloed_nullifier=*/FF(0x123456) },
68 { C::execution_register_1_, /*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::U1) },
72 { C::execution_sel_opcode_error, 0 },
73 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
74 check_relation<nullifier_exists>(trace);
75}
76
77TEST(NullifierExistsConstrainingTest, NegativeInvalidOutputTag)
78{
79 TestTraceContainer trace({ { { C::execution_sel, 1 },
80 { C::execution_sel_execute_nullifier_exists, 1 },
81 { C::execution_register_0_, /*siloed_nullifier=*/FF(0x123456) },
82 { C::execution_register_1_, /*exists=*/0 }, // nullifier does not exist!
83 { C::execution_prev_nullifier_tree_root, FF(0xabc) },
84 { C::execution_mem_tag_reg_0_, static_cast<uint8_t>(MemoryTag::FF) },
85 { C::execution_mem_tag_reg_1_, static_cast<uint8_t>(MemoryTag::U8) }, // WRONG!
86 { C::execution_sel_opcode_error, 0 },
87 { C::execution_subtrace_operation_id, AVM_EXEC_OP_ID_NULLIFIER_EXISTS } } });
89 check_relation<nullifier_exists>(trace, nullifier_exists::SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG),
90 "NULLIFIER_EXISTS_U1_OUTPUT_TAG");
91}
92
93TEST(NullifierExistsConstrainingTest, NegativeNullifierExistsSuccess)
94{
95 TestTraceContainer trace({ {
96 { C::execution_sel_execute_nullifier_exists, 1 },
97 { C::execution_sel_opcode_error, 1 },
98 } });
99
101 "INFALLIBLE_OPCODES_SUCCESS");
102}
103
104TEST(NullifierExistsConstrainingTest, Interactions)
105{
106 NiceMock<MockPoseidon2> poseidon2;
107 NiceMock<MockMerkleCheck> merkle_check;
108
109 NiceMock<MockRangeCheck> range_check;
110 DeduplicatingEventEmitter<FieldGreaterThanEvent> event_emitter;
111 FieldGreaterThan field_gt(range_check, event_emitter);
112
113 EventEmitter<NullifierTreeCheckEvent> nullifier_tree_check_event_emitter;
114 NullifierTreeCheck nullifier_tree_check(poseidon2, merkle_check, field_gt, nullifier_tree_check_event_emitter);
115
116 // Siloed nullifier (no siloing happens in the opcode now)
117 FF siloed_nullifier = 42;
118
119 // For exists=true, the low leaf's nullifier must match the searched nullifier
121
122 AppendOnlyTreeSnapshot nullifier_tree_snapshot = AppendOnlyTreeSnapshot{
123 .root = 42,
124 .next_available_leaf_index = 128,
125 };
126
127 // sel_silo=false (address unused when not siloing)
128 nullifier_tree_check.assert_read(
129 siloed_nullifier, /*contract_address=*/std::nullopt, /*exists=*/true, low_leaf, 0, {}, nullifier_tree_snapshot);
130
131 TestTraceContainer trace({ {
132 { C::execution_sel_execute_nullifier_exists, 1 },
133 { C::execution_register_0_, siloed_nullifier },
134 { C::execution_register_1_, /*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::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 EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:193
#define AVM_EXEC_OP_ID_NULLIFIER_EXISTS
FieldGreaterThan field_gt
MerkleCheck merkle_check
RangeCheck range_check
static constexpr size_t SR_INFALLIBLE_OPCODES_SUCCESS
Definition execution.hpp:78
static constexpr size_t SR_NULLIFIER_EXISTS_U1_OUTPUT_TAG
Native Poseidon2 hash function implementation.
Definition poseidon2.hpp:22
EventEmitter< DataCopyEvent > event_emitter
TestTraceContainer trace
NullifierTreeLeafPreimage low_leaf
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > NullifierTreeLeafPreimage
Definition db_types.hpp:12
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
::bb::crypto::merkle_tree::NullifierLeafValue NullifierLeafValue
Definition db.hpp:39
std::variant< NullifierTreeReadWriteEvent, CheckPointEventType > NullifierTreeCheckEvent
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
NiceMock< MockExecution > execution