1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
24using tracegen::ExecutionTraceBuilder;
25using tracegen::NoteHashTreeCheckTraceBuilder;
26using tracegen::TestTraceContainer;
28using simulation::EventEmitter;
29using simulation::MockMerkleCheck;
30using simulation::MockPoseidon2;
31using simulation::MockRangeCheck;
32using simulation::NoteHashTreeCheck;
36using testing::NiceMock;
44TEST(EmitNoteHashConstrainingTest, Positive)
47 TestTraceContainer
trace({ {
48 { C::execution_sel_execute_emit_notehash, 1 },
49 { C::execution_register_0_, 42 },
50 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
52 { C::execution_sel_write_note_hash, 1 },
53 { C::execution_sel_opcode_error, 0 },
55 { C::execution_prev_note_hash_tree_size, 1 },
56 { C::execution_note_hash_tree_size, 2 },
57 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
58 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
60 check_relation<emit_notehash>(
trace);
63TEST(EmitNoteHashConstrainingTest, LimitReached)
66 TestTraceContainer
trace({ {
67 { C::execution_sel_execute_emit_notehash, 1 },
68 { C::execution_register_0_, 42 },
69 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
70 { C::execution_sel_reached_max_note_hashes, 1 },
71 { C::execution_remaining_note_hashes_inv, 0 },
72 { C::execution_sel_opcode_error, 1 },
73 { C::execution_sel_write_note_hash, 0 },
75 { C::execution_prev_note_hash_tree_size, 1 },
76 { C::execution_prev_note_hash_tree_root, 27 },
77 { C::execution_note_hash_tree_size, 1 },
78 { C::execution_note_hash_tree_root, 27 },
79 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
80 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted },
82 check_relation<emit_notehash>(
trace);
85 trace.
set(C::execution_sel_reached_max_note_hashes, 0, 0);
87 "MAX_NOTE_HASHES_REACHED");
88 trace.
set(C::execution_sel_reached_max_note_hashes, 0, 1);
91 trace.
set(C::execution_sel_opcode_error, 0, 0);
94 "OPCODE_ERROR_IF_MAX_NOTE_HASHES_REACHED_OR_STATIC");
95 trace.
set(C::execution_sel_opcode_error, 0, 1);
98 trace.
set(C::execution_note_hash_tree_root, 0, 28);
101 "EMIT_NOTEHASH_TREE_ROOT_NOT_CHANGED");
104 trace.
set(C::execution_note_hash_tree_size, 0, 2);
106 "EMIT_NOTEHASH_TREE_SIZE_INCREASE");
109 trace.
set(C::execution_num_note_hashes_emitted, 0, prev_num_note_hashes_emitted + 1);
112 "EMIT_NOTEHASH_NUM_NOTE_HASHES_EMITTED_INCREASE");
115TEST(EmitNoteHashConstrainingTest, Interactions)
118 NiceMock<MockMerkleCheck> merkle_check;
121 EventEmitter<NoteHashTreeCheckEvent> note_hash_tree_check_event_emitter;
122 NoteHashTreeCheck note_hash_tree_check(27,
poseidon2, merkle_check, note_hash_tree_check_event_emitter);
127 AppendOnlyTreeSnapshot prev_snapshot = AppendOnlyTreeSnapshot{
129 .nextAvailableLeafIndex = 128,
131 uint32_t prev_num_note_hashes_emitted = 2;
133 EXPECT_CALL(merkle_check,
write).WillOnce(Return(57));
135 AppendOnlyTreeSnapshot next_snapshot = note_hash_tree_check.append_note_hash(
136 note_hash,
contract_address, prev_num_note_hashes_emitted, {}, prev_snapshot);
138 TestTraceContainer
trace({ {
139 { C::execution_sel_execute_emit_notehash, 1 },
140 { C::execution_register_0_, note_hash },
141 { C::execution_mem_tag_reg_0_,
static_cast<uint8_t
>(
MemoryTag::FF) },
143 { C::execution_sel_write_note_hash, 1 },
144 { C::execution_sel_opcode_error, 0 },
146 { C::execution_prev_num_note_hashes_emitted, prev_num_note_hashes_emitted },
147 { C::execution_num_note_hashes_emitted, prev_num_note_hashes_emitted + 1 },
148 { C::execution_prev_note_hash_tree_root, prev_snapshot.root },
149 { C::execution_note_hash_tree_root, next_snapshot.root },
150 { C::execution_prev_note_hash_tree_size, prev_snapshot.nextAvailableLeafIndex },
151 { C::execution_note_hash_tree_size, next_snapshot.nextAvailableLeafIndex },
155 NoteHashTreeCheckTraceBuilder note_hash_tree_check_trace_builder;
156 note_hash_tree_check_trace_builder.process(note_hash_tree_check_event_emitter.dump_events(),
trace);
157 check_relation<emit_notehash>(
trace);
159 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(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)