1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
23using tracegen::ExecutionTraceBuilder;
24using tracegen::NoteHashTreeCheckTraceBuilder;
25using tracegen::TestTraceContainer;
27using simulation::EventEmitter;
28using simulation::MockMerkleCheck;
29using simulation::MockPoseidon2;
30using simulation::MockRangeCheck;
31using simulation::NoteHashTreeCheck;
35using testing::NiceMock;
43TEST(EmitNoteHashConstrainingTest, Positive)
46 TestTraceContainer
trace({ {
47 { C::execution_sel_execute_emit_notehash, 1 },
48 { C::execution_register_0_, 42 },
49 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
51 { C::execution_sel_write_note_hash, 1 },
52 { C::execution_sel_opcode_error, 0 },
54 { C::execution_prev_note_hash_tree_size, 1 },
55 { C::execution_note_hash_tree_size, 2 },
56 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
57 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
59 check_relation<emit_notehash>(
trace);
62TEST(EmitNoteHashConstrainingTest, LimitReached)
65 TestTraceContainer
trace({ {
66 { C::execution_sel_execute_emit_notehash, 1 },
67 { C::execution_register_0_, 42 },
68 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
69 { C::execution_sel_reached_max_note_hashes, 1 },
70 { C::execution_remaining_note_hashes_inv, 0 },
71 { C::execution_sel_opcode_error, 1 },
72 { C::execution_sel_write_note_hash, 0 },
74 { C::execution_prev_note_hash_tree_size, 1 },
75 { C::execution_prev_note_hash_tree_root, 27 },
76 { C::execution_note_hash_tree_size, 1 },
77 { C::execution_note_hash_tree_root, 27 },
78 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
79 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted },
81 check_relation<emit_notehash>(
trace);
84 trace.
set(C::execution_sel_reached_max_note_hashes, 0, 0);
86 "MAX_NOTE_HASHES_REACHED");
87 trace.
set(C::execution_sel_reached_max_note_hashes, 0, 1);
90 trace.
set(C::execution_sel_opcode_error, 0, 0);
93 "OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC");
94 trace.
set(C::execution_sel_opcode_error, 0, 1);
97 trace.
set(C::execution_note_hash_tree_root, 0, 28);
100 "EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED");
103 trace.
set(C::execution_note_hash_tree_size, 0, 2);
105 "EMIT_NOTEHASH_TREE_SIZE_INCREASE");
108 trace.
set(C::execution_num_note_hashes_emitted, 0, prev_num_note_hashes_emitted + 1);
111 "EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE");
114TEST(EmitNoteHashConstrainingTest, Interactions)
117 NiceMock<MockMerkleCheck> merkle_check;
120 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
121 NoteHashTreeCheck note_hash_tree_check(27,
poseidon2, merkle_check, note_hash_tree_check_event_emitter);
126 AppendOnlyTreeSnapshot prev_snapshot = AppendOnlyTreeSnapshot{
128 .nextAvailableLeafIndex = 128,
130 uint32_t prev_num_note_hashes_emitted = 2;
132 EXPECT_CALL(merkle_check,
write).WillOnce(Return(57));
134 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
135 note_hash,
contract_address, prev_num_note_hashes_emitted, {}, prev_snapshot);
137 TestTraceContainer
trace({ {
138 { C::execution_sel_execute_emit_notehash, 1 },
139 { C::execution_register_0_, note_hash },
140 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
142 { C::execution_sel_write_note_hash, 1 },
143 { C::execution_sel_opcode_error, 0 },
145 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
146 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
147 { C::execution_prev_note_hash_tree_root, prev_snapshot.root },
148 { C::execution_note_hash_tree_root, next_snapshot.root },
149 { C::execution_prev_note_hash_tree_size, prev_snapshot.nextAvailableLeafIndex },
150 { C::execution_note_hash_tree_size, next_snapshot.nextAvailableLeafIndex },
154 NoteHashTreeCheckTraceBuilder note_hash_tree_check_trace_builder;
155 note_hash_tree_check_trace_builder.process(note_hash_tree_check_event_emitter.dump_events(),
trace);
156 check_relation<emit_notehash>(
trace);
158 check_interaction<ExecutionTraceBuilder, lookup_emit_notehash_notehash_tree_write_settings>(
trace);
#define AVM_EXEC_OP_ID_EMIT_NOTEHASH
#define MAX_NOTE_HASHES_PER_TX
static constexpr size_t SR_EMIT_NOTEHASH_TREE_SIZE_INCREASE
static constexpr size_t SR_OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC
static constexpr size_t SR_EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED
static constexpr size_t SR_MAX_NOTE_HASHES_REACHED
static constexpr size_t SR_EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE
void set(Column col, uint32_t row, const FF &value)
AztecAddress contract_address
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
TEST(TxExecutionConstrainingTest, WriteTreeValue)
crypto::Poseidon2< crypto::Poseidon2Bn254ScalarFieldParams > poseidon2
std::variant< NoteHashTreeReadWriteEvent, CheckPointEventType > NoteHashTreeCheckEvent
void write(B &buf, field2< base_field, Params > const &value)