Skip to content

Commit

Permalink
feat(avm): Indirect memory support for arithmetic/bitwise opcodes (#5328
Browse files Browse the repository at this point in the history
)

Resolves #5273
  • Loading branch information
jeanmon authored Mar 20, 2024
1 parent d940356 commit d5ffa17
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ template <typename PCS> class ZeroMorphVerifier_ {
using Curve = typename PCS::Curve;
using FF = typename Curve::ScalarField;
using Commitment = typename Curve::AffineElement;
using VerifierAccumulator = PCS::VerifierAccumulator;
using VerifierAccumulator = typename PCS::VerifierAccumulator;

public:
/**
Expand Down
23 changes: 15 additions & 8 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,44 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
// Compute
// Compute - Arithmetic
case OpCode::ADD:
trace_builder.op_add(std::get<uint32_t>(inst.operands.at(2)),
trace_builder.op_add(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(3)),
std::get<uint32_t>(inst.operands.at(4)),
std::get<AvmMemoryTag>(inst.operands.at(1)));
break;
case OpCode::SUB:
trace_builder.op_sub(std::get<uint32_t>(inst.operands.at(2)),
trace_builder.op_sub(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(3)),
std::get<uint32_t>(inst.operands.at(4)),
std::get<AvmMemoryTag>(inst.operands.at(1)));
break;
case OpCode::MUL:
trace_builder.op_mul(std::get<uint32_t>(inst.operands.at(2)),
trace_builder.op_mul(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(3)),
std::get<uint32_t>(inst.operands.at(4)),
std::get<AvmMemoryTag>(inst.operands.at(1)));
break;
case OpCode::DIV:
trace_builder.op_div(std::get<uint32_t>(inst.operands.at(2)),
trace_builder.op_div(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(3)),
std::get<uint32_t>(inst.operands.at(4)),
std::get<AvmMemoryTag>(inst.operands.at(1)));
break;
// Compute - Bitwise
case OpCode::NOT:
trace_builder.op_not(std::get<uint32_t>(inst.operands.at(2)),
trace_builder.op_not(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(4)),
std::get<AvmMemoryTag>(inst.operands.at(1)));
break;
// Execution Environment - Calldata
case OpCode::CALLDATACOPY:
trace_builder.calldata_copy(std::get<uint32_t>(inst.operands.at(1)),
trace_builder.calldata_copy(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)),
std::get<uint32_t>(inst.operands.at(3)),
calldata);
Expand Down Expand Up @@ -185,8 +191,9 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
break;
// Control Flow - Contract Calls
case OpCode::RETURN:
// Skip indirect at index 0
trace_builder.return_op(std::get<uint32_t>(inst.operands.at(1)), std::get<uint32_t>(inst.operands.at(2)));
trace_builder.return_op(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)));
break;
default:
break;
Expand Down
9 changes: 9 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ void log_avm_trace(std::vector<Row> const& trace, size_t beg, size_t end, bool e
}
}

bool is_operand_indirect(uint8_t ind_value, uint8_t operand_idx)
{
if (operand_idx > 7) {
return false;
}

return static_cast<bool>((ind_value & (1 << operand_idx)) >> operand_idx);
}

} // namespace bb::avm_trace
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ namespace bb::avm_trace {

void log_avm_trace(std::vector<Row> const& trace, size_t beg, size_t end, bool enable_selectors = false);

bool is_operand_indirect(uint8_t ind_value, uint8_t operand_idx);

} // namespace bb::avm_trace
Loading

0 comments on commit d5ffa17

Please sign in to comment.