Skip to content

Commit 3e8eb4d

Browse files
authored
feat: merge-train/avm (#19017)
BEGIN_COMMIT_OVERRIDE chore(avm): move constraining test feat(avm_fuzzing): sendl2tol1msg and emitpubliclog opcodes implementations (#19019) refactor(avm): simplify hinting db (#18984) END_COMMIT_OVERRIDE
2 parents 02d8409 + af855d1 commit 3e8eb4d

File tree

20 files changed

+375
-801
lines changed

20 files changed

+375
-801
lines changed

barretenberg/cpp/src/barretenberg/avm_fuzzer/common/interfaces/dbs.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,10 @@ simulation::SequentialInsertionResult<NullifierLeafValue> FuzzerLowLevelDB::inse
160160
return {};
161161
}
162162

163-
std::vector<AppendLeafResult> FuzzerLowLevelDB::append_leaves([[maybe_unused]] MerkleTreeId tree_id,
164-
std::span<const FF> leaves)
163+
void FuzzerLowLevelDB::append_leaves([[maybe_unused]] MerkleTreeId tree_id, std::span<const FF> leaves)
165164
{
166165
note_hash_leaves.insert(note_hash_leaves.end(), leaves.begin(), leaves.end());
167166
next_available_note_hash_index += leaves.size();
168-
return {};
169167
}
170168
void FuzzerLowLevelDB::pad_tree([[maybe_unused]] MerkleTreeId tree_id, [[maybe_unused]] size_t num_leaves) {}
171169

barretenberg/cpp/src/barretenberg/avm_fuzzer/common/interfaces/dbs.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class FuzzerLowLevelDB : public bb::avm2::simulation::LowLevelMerkleDBInterface
3333
simulation::SequentialInsertionResult<NullifierLeafValue> insert_indexed_leaves_nullifier_tree(
3434
const NullifierLeafValue& leaf_value) override;
3535

36-
std::vector<simulation::AppendLeafResult> append_leaves(simulation::MerkleTreeId tree_id,
37-
std::span<const FF> leaves) override;
36+
void append_leaves(simulation::MerkleTreeId tree_id, std::span<const FF> leaves) override;
3837

3938
void pad_tree(simulation::MerkleTreeId tree_id, size_t num_leaves) override;
4039
void create_checkpoint() override;

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const FF CONTRACT_ADDRESS = 42;
3838
const FF TRANSACTION_FEE = 0;
3939
const bool IS_STATIC_CALL = false;
4040
const Gas GAS_LIMIT = Gas{ .l2_gas = 1000000, .da_gas = 1000000 };
41-
4241
// Fuzzer-specific logging macro (similar to vinfo but uses AVM_FUZZER_LOGGING env var)
4342
// Uses lazy evaluation to avoid string formatting overhead when logging is disabled
4443
inline void fuzz_info_(std::function<std::string()> func)

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/fuzz.test.cpp

Lines changed: 124 additions & 459 deletions
Large diffs are not rendered by default.

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/instruction.hpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ struct CALLDATACOPY_Instruction {
505505
MSGPACK_FIELDS(dst_address, copy_size, copy_size_address, cd_start, cd_start_address);
506506
};
507507

508+
struct SENDL2TOL1MSG_Instruction {
509+
bb::avm2::FF recipient;
510+
ResultAddressRef recipient_address;
511+
bb::avm2::FF content;
512+
ResultAddressRef content_address;
513+
MSGPACK_FIELDS(recipient, recipient_address, content, content_address);
514+
};
515+
516+
struct EMITUNENCRYPTEDLOG_Instruction {
517+
uint8_t log_size;
518+
ResultAddressRef log_size_address;
519+
std::vector<bb::avm2::FF> log_values;
520+
uint16_t log_values_address_start;
521+
MSGPACK_FIELDS(log_size, log_size_address, log_values);
522+
};
523+
508524
using FuzzInstruction = std::variant<ADD_8_Instruction,
509525
FDIV_8_Instruction,
510526
SET_8_Instruction,
@@ -550,7 +566,9 @@ using FuzzInstruction = std::variant<ADD_8_Instruction,
550566
NULLIFIEREXISTS_Instruction,
551567
EMITNOTEHASH_Instruction,
552568
NOTEHASHEXISTS_Instruction,
553-
CALLDATACOPY_Instruction>;
569+
CALLDATACOPY_Instruction,
570+
SENDL2TOL1MSG_Instruction,
571+
EMITUNENCRYPTEDLOG_Instruction>;
554572

555573
template <class... Ts> struct overloaded_instruction : Ts... {
556574
using Ts::operator()...;
@@ -717,6 +735,17 @@ inline std::ostream& operator<<(std::ostream& os, const FuzzInstruction& instruc
717735
os << "CALLDATACOPY_Instruction " << arg.dst_address << " " << static_cast<int>(arg.copy_size) << " "
718736
<< arg.copy_size_address << " " << arg.cd_start_address << " " << arg.cd_start_address;
719737
},
738+
[&](SENDL2TOL1MSG_Instruction arg) {
739+
os << "SENDL2TOL1MSG_Instruction " << arg.recipient << " " << arg.recipient_address << " "
740+
<< arg.content << " " << arg.content_address;
741+
},
742+
[&](EMITUNENCRYPTEDLOG_Instruction arg) {
743+
os << "EMITUNENCRYPTEDLOG_Instruction " << arg.log_size << " " << arg.log_size_address << " ";
744+
for (const auto& value : arg.log_values) {
745+
os << value << " ";
746+
}
747+
os << std::endl;
748+
},
720749
[&](auto) { os << "Unknown instruction"; },
721750
},
722751
instruction);

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/program_block.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,61 @@ void ProgramBlock::process_calldatacopy_instruction(CALLDATACOPY_Instruction ins
10101010
}
10111011
}
10121012

1013+
void ProgramBlock::process_sendl2tol1msg_instruction(SENDL2TOL1MSG_Instruction instruction)
1014+
{
1015+
auto set_recipient_instruction = SET_FF_Instruction{ .value_tag = bb::avm2::MemoryTag::FF,
1016+
.result_address = instruction.recipient_address,
1017+
.value = instruction.recipient };
1018+
this->process_set_ff_instruction(set_recipient_instruction);
1019+
auto set_content_instruction = SET_FF_Instruction{ .value_tag = bb::avm2::MemoryTag::FF,
1020+
.result_address = instruction.content_address,
1021+
.value = instruction.content };
1022+
this->process_set_ff_instruction(set_content_instruction);
1023+
1024+
auto recipient_address_operand = memory_manager.get_memory_address_and_operand_16(instruction.recipient_address);
1025+
auto content_address_operand = memory_manager.get_memory_address_and_operand_16(instruction.content_address);
1026+
if (!recipient_address_operand.has_value() || !content_address_operand.has_value()) {
1027+
return;
1028+
}
1029+
preprocess_memory_addresses(instruction.recipient_address, recipient_address_operand.value().first);
1030+
preprocess_memory_addresses(instruction.content_address, content_address_operand.value().first);
1031+
auto sendl2tol1msg_instruction = bb::avm2::testing::InstructionBuilder(bb::avm2::WireOpCode::SENDL2TOL1MSG)
1032+
.operand(recipient_address_operand.value().second)
1033+
.operand(content_address_operand.value().second)
1034+
.build();
1035+
instructions.push_back(sendl2tol1msg_instruction);
1036+
}
1037+
1038+
void ProgramBlock::process_emitunencryptedlog_instruction(EMITUNENCRYPTEDLOG_Instruction instruction)
1039+
{
1040+
auto log_size_set_instruction = SET_32_Instruction{ .value_tag = bb::avm2::MemoryTag::U32,
1041+
.result_address = instruction.log_size_address,
1042+
.value = instruction.log_size };
1043+
this->process_set_32_instruction(log_size_set_instruction);
1044+
size_t counter = 0;
1045+
for (const auto& value : instruction.log_values) {
1046+
auto log_values_address =
1047+
ResultAddressRef{ .address = static_cast<uint32_t>(instruction.log_values_address_start + counter),
1048+
.mode = AddressingMode::Direct };
1049+
auto set_value_instruction = SET_FF_Instruction{ .value_tag = bb::avm2::MemoryTag::FF,
1050+
.result_address = log_values_address,
1051+
.value = value };
1052+
this->process_set_ff_instruction(set_value_instruction);
1053+
counter++;
1054+
}
1055+
auto log_size_address_operand = memory_manager.get_memory_address_and_operand_16(instruction.log_size_address);
1056+
if (!log_size_address_operand.has_value()) {
1057+
return;
1058+
}
1059+
preprocess_memory_addresses(instruction.log_size_address, log_size_address_operand.value().first);
1060+
auto emitunencryptedlog_instruction =
1061+
bb::avm2::testing::InstructionBuilder(bb::avm2::WireOpCode::EMITUNENCRYPTEDLOG)
1062+
.operand(log_size_address_operand.value().second)
1063+
.operand(instruction.log_values_address_start)
1064+
.build();
1065+
instructions.push_back(emitunencryptedlog_instruction);
1066+
}
1067+
10131068
void ProgramBlock::finalize_with_return(uint8_t return_size,
10141069
MemoryTagWrapper return_value_tag,
10151070
uint16_t return_value_offset_index)
@@ -1176,6 +1231,12 @@ void ProgramBlock::process_instruction(FuzzInstruction instruction)
11761231
[this](CALLDATACOPY_Instruction instruction) {
11771232
return this->process_calldatacopy_instruction(instruction);
11781233
},
1234+
[this](SENDL2TOL1MSG_Instruction instruction) {
1235+
return this->process_sendl2tol1msg_instruction(instruction);
1236+
},
1237+
[this](EMITUNENCRYPTEDLOG_Instruction instruction) {
1238+
return this->process_emitunencryptedlog_instruction(instruction);
1239+
},
11791240
[](auto) { throw std::runtime_error("Unknown instruction"); },
11801241
},
11811242
instruction);

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/program_block.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class ProgramBlock {
9999
void process_emitnotehash_instruction(EMITNOTEHASH_Instruction instruction);
100100
void process_notehashexists_instruction(NOTEHASHEXISTS_Instruction instruction);
101101
void process_calldatacopy_instruction(CALLDATACOPY_Instruction instruction);
102+
void process_sendl2tol1msg_instruction(SENDL2TOL1MSG_Instruction instruction);
103+
void process_emitunencryptedlog_instruction(EMITUNENCRYPTEDLOG_Instruction instruction);
102104

103105
public:
104106
std::vector<ProgramBlock*> successors;

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/configuration.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ enum class InstructionGenerationOptions {
260260
EMITNOTEHASH,
261261
NOTEHASHEXISTS,
262262
CALLDATACOPY,
263+
SENDL2TOL1MSG,
264+
EMITUNENCRYPTEDLOG,
263265
};
264266

265-
using InstructionGenerationConfig = WeightedSelectionConfig<InstructionGenerationOptions, 42>;
267+
using InstructionGenerationConfig = WeightedSelectionConfig<InstructionGenerationOptions, 44>;
266268

267269
constexpr InstructionGenerationConfig BASIC_INSTRUCTION_GENERATION_CONFIGURATION = InstructionGenerationConfig({
268270
{ InstructionGenerationOptions::ADD_8, 1 },
@@ -307,6 +309,8 @@ constexpr InstructionGenerationConfig BASIC_INSTRUCTION_GENERATION_CONFIGURATION
307309
{ InstructionGenerationOptions::EMITNOTEHASH, 1 },
308310
{ InstructionGenerationOptions::NOTEHASHEXISTS, 1 },
309311
{ InstructionGenerationOptions::CALLDATACOPY, 1 },
312+
{ InstructionGenerationOptions::SENDL2TOL1MSG, 1 },
313+
{ InstructionGenerationOptions::EMITUNENCRYPTEDLOG, 1 },
310314
});
311315

312316
enum class SStoreMutationOptions { src_address, result_address, slot };
@@ -373,6 +377,27 @@ constexpr CalldataCopyMutationConfig BASIC_CALLDATACOPY_MUTATION_CONFIGURATION =
373377
{ CalldataCopyMutationOptions::cd_start_address, 1 },
374378
});
375379

380+
enum class SendL2ToL1MsgMutationOptions { recipient, recipient_address, content, content_address };
381+
using SendL2ToL1MsgMutationConfig = WeightedSelectionConfig<SendL2ToL1MsgMutationOptions, 4>;
382+
383+
constexpr SendL2ToL1MsgMutationConfig BASIC_SENDL2TOL1MSG_MUTATION_CONFIGURATION = SendL2ToL1MsgMutationConfig({
384+
{ SendL2ToL1MsgMutationOptions::recipient, 1 },
385+
{ SendL2ToL1MsgMutationOptions::recipient_address, 1 },
386+
{ SendL2ToL1MsgMutationOptions::content, 1 },
387+
{ SendL2ToL1MsgMutationOptions::content_address, 1 },
388+
});
389+
390+
enum class EmitUnencryptedLogMutationOptions { log_size, log_size_address, log_values, log_values_address_start };
391+
using EmitUnencryptedLogMutationConfig = WeightedSelectionConfig<EmitUnencryptedLogMutationOptions, 4>;
392+
393+
constexpr EmitUnencryptedLogMutationConfig BASIC_EMITUNENCRYPTEDLOG_MUTATION_CONFIGURATION =
394+
EmitUnencryptedLogMutationConfig({
395+
{ EmitUnencryptedLogMutationOptions::log_size, 1 },
396+
{ EmitUnencryptedLogMutationOptions::log_size_address, 1 },
397+
{ EmitUnencryptedLogMutationOptions::log_values, 1 },
398+
{ EmitUnencryptedLogMutationOptions::log_values_address_start, 1 },
399+
});
400+
376401
enum class ReturnOptionsMutationOptions { return_size, return_value_tag, return_value_offset_index };
377402

378403
using ReturnOptionsMutationConfig = WeightedSelectionConfig<ReturnOptionsMutationOptions, 3>;

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/instructions/instruction.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "barretenberg/avm_fuzzer/mutations/basic_types/uint32_t.hpp"
77
#include "barretenberg/avm_fuzzer/mutations/basic_types/uint64_t.hpp"
88
#include "barretenberg/avm_fuzzer/mutations/basic_types/uint8_t.hpp"
9+
#include "barretenberg/avm_fuzzer/mutations/basic_types/vector.hpp"
910
#include "barretenberg/avm_fuzzer/mutations/configuration.hpp"
1011

1112
AddressingMode generate_addressing_mode(std::mt19937_64& rng)
@@ -209,9 +210,18 @@ FuzzInstruction generate_instruction(std::mt19937_64& rng)
209210
.copy_size_address = generate_result_address_ref(rng),
210211
.cd_start = generate_random_uint16(rng),
211212
.cd_start_address = generate_result_address_ref(rng) };
213+
case InstructionGenerationOptions::SENDL2TOL1MSG:
214+
return SENDL2TOL1MSG_Instruction{ .recipient = generate_random_field(rng),
215+
.recipient_address = generate_result_address_ref(rng),
216+
.content = generate_random_field(rng),
217+
.content_address = generate_result_address_ref(rng) };
218+
case InstructionGenerationOptions::EMITUNENCRYPTEDLOG:
219+
return EMITUNENCRYPTEDLOG_Instruction{ .log_size = generate_random_uint8(rng),
220+
.log_size_address = generate_result_address_ref(rng),
221+
.log_values = { generate_random_field(rng) },
222+
.log_values_address_start = generate_random_uint16(rng) };
212223
}
213224
}
214-
215225
/// Most of the tags will be equal to the default tag
216226
void mutate_address_ref(AddressRef& address, std::mt19937_64& rng, std::optional<MemoryTag> default_tag)
217227
{
@@ -587,6 +597,50 @@ void mutate_calldatacopy_instruction(CALLDATACOPY_Instruction& instruction, std:
587597
}
588598
}
589599

600+
void mutate_sendl2tol1msg_instruction(SENDL2TOL1MSG_Instruction& instruction, std::mt19937_64& rng)
601+
{
602+
SendL2ToL1MsgMutationOptions option = BASIC_SENDL2TOL1MSG_MUTATION_CONFIGURATION.select(rng);
603+
switch (option) {
604+
case SendL2ToL1MsgMutationOptions::recipient:
605+
mutate_field(instruction.recipient, rng, BASIC_FIELD_MUTATION_CONFIGURATION);
606+
break;
607+
case SendL2ToL1MsgMutationOptions::recipient_address:
608+
mutate_result_address_ref(instruction.recipient_address, rng);
609+
break;
610+
case SendL2ToL1MsgMutationOptions::content:
611+
mutate_field(instruction.content, rng, BASIC_FIELD_MUTATION_CONFIGURATION);
612+
break;
613+
case SendL2ToL1MsgMutationOptions::content_address:
614+
mutate_result_address_ref(instruction.content_address, rng);
615+
break;
616+
}
617+
}
618+
619+
void mutate_emitunencryptedlog_instruction(EMITUNENCRYPTEDLOG_Instruction& instruction, std::mt19937_64& rng)
620+
{
621+
EmitUnencryptedLogMutationOptions option = BASIC_EMITUNENCRYPTEDLOG_MUTATION_CONFIGURATION.select(rng);
622+
switch (option) {
623+
case EmitUnencryptedLogMutationOptions::log_size:
624+
mutate_uint8_t(instruction.log_size, rng, BASIC_UINT8_T_MUTATION_CONFIGURATION);
625+
break;
626+
case EmitUnencryptedLogMutationOptions::log_size_address:
627+
mutate_result_address_ref(instruction.log_size_address, rng);
628+
break;
629+
case EmitUnencryptedLogMutationOptions::log_values:
630+
mutate_vec<bb::avm2::FF>(
631+
instruction.log_values,
632+
rng,
633+
[](bb::avm2::FF& value, std::mt19937_64& rng) {
634+
mutate_field(value, rng, BASIC_FIELD_MUTATION_CONFIGURATION);
635+
},
636+
generate_random_field,
637+
BASIC_VEC_MUTATION_CONFIGURATION);
638+
break;
639+
case EmitUnencryptedLogMutationOptions::log_values_address_start:
640+
mutate_uint16_t(instruction.log_values_address_start, rng, BASIC_UINT16_T_MUTATION_CONFIGURATION);
641+
break;
642+
}
643+
}
590644
void mutate_instruction(FuzzInstruction& instruction, std::mt19937_64& rng)
591645
{
592646
std::visit(overloaded_instruction{
@@ -634,6 +688,7 @@ void mutate_instruction(FuzzInstruction& instruction, std::mt19937_64& rng)
634688
[&rng](EMITNOTEHASH_Instruction& instr) { mutate_emit_note_hash_instruction(instr, rng); },
635689
[&rng](NOTEHASHEXISTS_Instruction& instr) { mutate_note_hash_exists_instruction(instr, rng); },
636690
[&rng](CALLDATACOPY_Instruction& instr) { mutate_calldatacopy_instruction(instr, rng); },
691+
[&rng](SENDL2TOL1MSG_Instruction& instr) { mutate_sendl2tol1msg_instruction(instr, rng); },
637692
[](auto&) { throw std::runtime_error("Unknown instruction"); } },
638693
instruction);
639694
}

barretenberg/cpp/src/barretenberg/vm2/constraining/nullifier_exists.test.cpp renamed to barretenberg/cpp/src/barretenberg/vm2/constraining/relations/nullifier_exists.test.cpp

File renamed without changes.

0 commit comments

Comments
 (0)