Skip to content

Commit

Permalink
5159 - add deserialization and execution for MOV and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Mar 14, 2024
1 parent 4a88fe1 commit 153abee
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const std::unordered_map<OpCode, std::vector<OperandType>> OPCODE_WIRE_FORMAT =
{ OpCode::INTERNALRETURN, {} },
// Machine State - Memory
// OpCode::SET is handled differently
{ OpCode::MOV, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
// Control Flow - Contract Calls
{ OpCode::RETURN, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
trace_builder.set(val, dst_offset, in_tag);
break;
}
case OpCode::MOV:
trace_builder.op_mov(std::get<uint32_t>(inst.operands.at(1)), std::get<uint32_t>(inst.operands.at(2)));
break;
// Control Flow - Contract Calls
case OpCode::RETURN:
// Skip indirect at index 0
Expand Down
48 changes: 48 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/tests/avm_execution.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,54 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy)
gen_proof_and_validate(bytecode, std::move(trace), std::vector<FF>{ 13, 156 });
}

// Positive test with MOV.
TEST_F(AvmExecutionTests, movOpcode)
{
std::string bytecode_hex = to_hex(OpCode::SET) + // opcode SET
"00" // Indirect flag
"01" // U8
"13" // val 19
"000000AB" // dst_offset 171
+ to_hex(OpCode::MOV) + // opcode MOV
"00" // Indirect flag
"000000AB" // src_offset 171
"00000021" // dst_offset 33
+ to_hex(OpCode::RETURN) + // opcode RETURN
"00" // Indirect flag
"00000000" // ret offset 0
"00000000"; // ret size 0

auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

ASSERT_THAT(instructions, SizeIs(3));

// SET
EXPECT_THAT(instructions.at(0),
AllOf(Field(&Instruction::op_code, OpCode::SET),
Field(&Instruction::operands,
ElementsAre(VariantWith<uint8_t>(0),
VariantWith<AvmMemoryTag>(AvmMemoryTag::U8),
VariantWith<uint8_t>(19),
VariantWith<uint32_t>(171)))));

// MOV
EXPECT_THAT(
instructions.at(1),
AllOf(Field(&Instruction::op_code, OpCode::MOV),
Field(&Instruction::operands,
ElementsAre(VariantWith<uint8_t>(0), VariantWith<uint32_t>(171), VariantWith<uint32_t>(33)))));

auto trace = Execution::gen_trace(instructions);

// Find the first row enabling the MOV selector
auto row = std::ranges::find_if(trace.begin(), trace.end(), [](Row r) { return r.avm_main_sel_mov == 1; });
EXPECT_EQ(row->avm_main_ia, 19);
EXPECT_EQ(row->avm_main_ic, 19);

gen_proof_and_validate(bytecode, std::move(trace), {});
}

// Negative test detecting an invalid opcode byte.
TEST_F(AvmExecutionTests, invalidOpcode)
{
Expand Down

0 comments on commit 153abee

Please sign in to comment.