Skip to content

Commit

Permalink
feat(avm): deserialise execution hints in bb main (#6848)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: dbanks12 <david@aztecprotocol.com>
Co-authored-by: David Banks <47112877+dbanks12@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 4, 2024
1 parent 0e8e772 commit d3be85f
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 89 deletions.
25 changes: 22 additions & 3 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "barretenberg/dsl/acir_format/acir_format.hpp"
#include "barretenberg/honk/proof_system/types/proof.hpp"
#include "barretenberg/plonk/proof_system/proving_key/serialize.hpp"
#include "barretenberg/vm/avm_trace/avm_common.hpp"
#include "barretenberg/vm/avm_trace/avm_execution.hpp"
#include "config.hpp"
#include "get_bn254_crs.hpp"
Expand Down Expand Up @@ -513,6 +514,22 @@ void vk_as_fields(const std::string& vk_path, const std::string& output_path)
}
}

avm_trace::ExecutionHints deserialize_execution_hints(const std::vector<uint8_t>& hints)
{
avm_trace::ExecutionHints execution_hints;
if (hints.size() == 0) {
vinfo("no hints provided");
} else {
// Hints arrive serialised as a vector of <side effect counter, hint> pairs
using FF = avm_trace::FF;
std::vector<std::pair<FF, FF>> deser_hints = many_from_buffer<std::pair<FF, FF>>(hints);
for (auto& hint : deser_hints) {
execution_hints.side_effect_hints[static_cast<uint32_t>(hint.first)] = hint.second;
}
}
return execution_hints;
}

/**
* @brief Writes an avm proof and corresponding (incomplete) verification key to files.
*
Expand All @@ -536,9 +553,10 @@ void avm_prove(const std::filesystem::path& bytecode_path,
bytecode_path.extension() == ".gz" ? gunzip(bytecode_path) : read_file(bytecode_path);
std::vector<fr> const calldata = many_from_buffer<fr>(read_file(calldata_path));
std::vector<fr> const public_inputs_vec = many_from_buffer<fr>(read_file(public_inputs_path));
std::vector<uint8_t> avm_hints;

avm_trace::ExecutionHints avm_hints;
try {
avm_hints = read_file(hints_path);
avm_hints = deserialize_execution_hints(read_file(hints_path));
} catch (std::runtime_error const& err) {
vinfo("No hints were provided for avm proving.... Might be fine!");
}
Expand All @@ -547,7 +565,8 @@ void avm_prove(const std::filesystem::path& bytecode_path,
init_bn254_crs(1 << 17);

// Prove execution and return vk
auto const [verification_key, proof] = avm_trace::Execution::prove(bytecode, calldata, public_inputs_vec);
auto const [verification_key, proof] =
avm_trace::Execution::prove(bytecode, calldata, public_inputs_vec, avm_hints);
// TODO(ilyas): <#4887>: Currently we only need these two parts of the vk, look into pcs_verification key reqs
std::vector<uint64_t> vk_vector = { verification_key.circuit_size, verification_key.num_public_inputs };
std::vector<fr> vk_as_fields = { verification_key.circuit_size, verification_key.num_public_inputs };
Expand Down
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_common.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "barretenberg/serialize/msgpack.hpp"
#include "barretenberg/vm/avm_trace/constants.hpp"
#include "barretenberg/vm/generated/avm_flavor.hpp"

Expand Down Expand Up @@ -43,6 +44,7 @@ static const uint32_t MAX_SIZE_INTERNAL_STACK = 1 << 16;
// Side effect counter -> value
struct ExecutionHints {
std::unordered_map<uint32_t, FF> side_effect_hints;

std::vector<std::vector<FF>> returndata_hints;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,23 @@ const std::unordered_map<OpCode, std::vector<OperandType>> OPCODE_WIRE_FORMAT =
{ OpCode::EMITUNENCRYPTEDLOG, getter_format },
{ OpCode::SLOAD, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::SSTORE, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::NOTEHASHEXISTS, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::NULLIFIEREXISTS, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::L1TOL2MSGEXISTS, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
/*TODO: leafIndexOffset is not constrained*/
{ OpCode::NOTEHASHEXISTS,
{ OperandType::INDIRECT,
OperandType::UINT32,
/*TODO: leafIndexOffset is not constrained*/ OperandType::UINT32,
OperandType::UINT32 } },

{ OpCode::NULLIFIEREXISTS,
{ OperandType::INDIRECT,
OperandType::UINT32,
/*TODO: Address is not constrained*/ OperandType::UINT32,
OperandType::UINT32 } },
{ OpCode::L1TOL2MSGEXISTS,
{ OperandType::INDIRECT,
OperandType::UINT32,
/*TODO: leafIndexOffset is not constrained*/ OperandType::UINT32,
OperandType::UINT32 } },
// CONTRACTCALLDEPTH, -- not in simulator
// Execution Environment - Globals
{ OpCode::CHAINID, getter_format },
Expand Down
95 changes: 92 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,89 @@ VmPublicInputs Execution::convert_public_inputs(std::vector<FF> const& public_in
kernel_inputs[DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[DA_START_GAS_LEFT_PCPI_OFFSET];
kernel_inputs[L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET];

// Copy the output columns
std::array<FF, KERNEL_OUTPUTS_LENGTH>& ko_values = std::get<KERNEL_OUTPUTS_VALUE>(public_inputs);
std::array<FF, KERNEL_OUTPUTS_LENGTH>& ko_side_effect = std::get<KERNEL_OUTPUTS_SIDE_EFFECT_COUNTER>(public_inputs);
std::array<FF, KERNEL_OUTPUTS_LENGTH>& ko_metadata = std::get<KERNEL_OUTPUTS_METADATA>(public_inputs);

// We copy each type of the kernel outputs into their respective columns, each has differeing lengths / data
// For NOTEHASHEXISTS
for (size_t i = 0; i < MAX_NOTE_HASH_READ_REQUESTS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_NOTE_HASH_EXISTS_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_NOTE_HASH_EXISTS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For NULLIFIEREXISTS
for (size_t i = 0; i < MAX_NULLIFIER_READ_REQUESTS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_NULLIFIER_EXISTS_OFFSET + i;
size_t pcpi_offset = PCPI_NULLIFIER_EXISTS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For L1TOL2MSGEXISTS
for (size_t i = 0; i < MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_L1_TO_L2_MSG_EXISTS_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_L1_TO_L2_MSG_READ_REQUESTS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For SSTORE
for (size_t i = 0; i < MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_SSTORE_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_PUBLIC_DATA_UPDATE_OFFSET + (i * 3);

// slot, value, side effect
ko_metadata[dest_offset] = public_inputs_vec[pcpi_offset];
ko_values[dest_offset] = public_inputs_vec[pcpi_offset + 1];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 2];
}
// For SLOAD
for (size_t i = 0; i < MAX_PUBLIC_DATA_READS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_SLOAD_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_PUBLIC_DATA_READ_OFFSET + (i * 3);

// slot, value, side effect
ko_metadata[dest_offset] = public_inputs_vec[pcpi_offset];
ko_values[dest_offset] = public_inputs_vec[pcpi_offset + 1];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 2];
}
// For EMITNOTEHASH
for (size_t i = 0; i < MAX_NEW_NOTE_HASHES_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_EMIT_NOTE_HASH_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_NEW_NOTE_HASHES_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For EMITNULLIFIER
for (size_t i = 0; i < MAX_NEW_NULLIFIERS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_EMIT_NULLIFIER_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_NEW_NULLIFIERS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For EMITL2TOL1MSG
for (size_t i = 0; i < MAX_NEW_L2_TO_L1_MSGS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_L2_TO_L1_MSG_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_NEW_L2_TO_L1_MSGS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}
// For EMITUNENCRYPTEDLOG
for (size_t i = 0; i < MAX_UNENCRYPTED_LOGS_PER_CALL; i++) {
size_t dest_offset = AvmKernelTraceBuilder::START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET + i;
size_t pcpi_offset = PCPI_NEW_UNENCRYPTED_LOGS_OFFSET + (i * 2);

ko_values[dest_offset] = public_inputs_vec[pcpi_offset];
ko_side_effect[dest_offset] = public_inputs_vec[pcpi_offset + 1];
}

return public_inputs;
}

Expand Down Expand Up @@ -363,14 +446,18 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
break;
case OpCode::NOTEHASHEXISTS:
trace_builder.op_note_hash_exists(std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)));
// TODO: leaf offset exists
// std::get<uint32_t>(inst.operands.at(2))
std::get<uint32_t>(inst.operands.at(3)));
break;
case OpCode::EMITNOTEHASH:
trace_builder.op_emit_note_hash(std::get<uint32_t>(inst.operands.at(1)));
break;
case OpCode::NULLIFIEREXISTS:
trace_builder.op_nullifier_exists(std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)));
// std::get<uint32_t>(inst.operands.at(2))
/**TODO: Address offset for siloing */
std::get<uint32_t>(inst.operands.at(3)));
break;
case OpCode::EMITNULLIFIER:
trace_builder.op_emit_nullifier(std::get<uint32_t>(inst.operands.at(1)));
Expand All @@ -383,7 +470,9 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
break;
case OpCode::L1TOL2MSGEXISTS:
trace_builder.op_l1_to_l2_msg_exists(std::get<uint32_t>(inst.operands.at(1)),
std::get<uint32_t>(inst.operands.at(2)));
// TODO: leaf offset exists
// std::get<uint32_t>(inst.operands.at(2))
std::get<uint32_t>(inst.operands.at(3)));
break;
case OpCode::EMITUNENCRYPTEDLOG:
trace_builder.op_emit_unencrypted_log(std::get<uint32_t>(inst.operands.at(1)));
Expand Down
137 changes: 137 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,141 @@ std::string to_hex(OpCode opcode)
return to_hex(static_cast<uint8_t>(opcode));
}

// Utility function to print the string represenatation of an opcode
std::string to_string(OpCode opcode)
{
switch (opcode) {
case OpCode::ADD:
return "ADD";
case OpCode::SUB:
return "SUB";
case OpCode::MUL:
return "MUL";
case OpCode::DIV:
return "DIV";
case OpCode::FDIV:
return "FDIV";
case OpCode::EQ:
return "EQ";
case OpCode::LT:
return "LT";
case OpCode::LTE:
return "LTE";
case OpCode::AND:
return "AND";
case OpCode::OR:
return "OR";
case OpCode::XOR:
return "XOR";
case OpCode::NOT:
return "NOT";
case OpCode::SHL:
return "SHL";
case OpCode::SHR:
return "SHR";
case OpCode::CAST:
return "CAST";
case OpCode::ADDRESS:
return "ADDRESS";
case OpCode::STORAGEADDRESS:
return "STORAGEADDRESS";
case OpCode::SENDER:
return "SENDER";
case OpCode::FEEPERL2GAS:
return "FEEPERL2GAS";
case OpCode::FEEPERDAGAS:
return "FEEPERDAGAS";
case OpCode::TRANSACTIONFEE:
return "TRANSACTIONFEE";
case OpCode::CONTRACTCALLDEPTH:
return "CONTRACTCALLDEPTH";
case OpCode::CHAINID:
return "CHAINID";
case OpCode::VERSION:
return "VERSION";
case OpCode::BLOCKNUMBER:
return "BLOCKNUMBER";
case OpCode::TIMESTAMP:
return "TIMESTAMP";
case OpCode::COINBASE:
return "COINBASE";
case OpCode::BLOCKL2GASLIMIT:
return "BLOCKL2GASLIMIT";
case OpCode::BLOCKDAGASLIMIT:
return "BLOCKDAGASLIMIT";
case OpCode::CALLDATACOPY:
return "CALLDATACOPY";
case OpCode::L2GASLEFT:
return "L2GASLEFT";
case OpCode::DAGASLEFT:
return "DAGASLEFT";
case OpCode::JUMP:
return "JUMP";
case OpCode::JUMPI:
return "JUMPI";
case OpCode::INTERNALCALL:
return "INTERNALCALL";
case OpCode::INTERNALRETURN:
return "INTERNALRETURN";
case OpCode::SET:
return "SET";
case OpCode::MOV:
return "MOV";
case OpCode::CMOV:
return "CMOV";
case OpCode::SLOAD:
return "SLOAD";
case OpCode::SSTORE:
return "SSTORE";
case OpCode::NOTEHASHEXISTS:
return "NOTEHASHEXISTS";
case OpCode::EMITNOTEHASH:
return "EMITNOTEHASH";
case OpCode::NULLIFIEREXISTS:
return "NULLIFIEREXISTS";
case OpCode::EMITNULLIFIER:
return "EMITNULLIFIER";
case OpCode::L1TOL2MSGEXISTS:
return "L1TOL2MSGEXISTS";
case OpCode::HEADERMEMBER:
return "HEADERMEMBER";
case OpCode::GETCONTRACTINSTANCE:
return "GETCONTRACTINSTANCE";
case OpCode::EMITUNENCRYPTEDLOG:
return "EMITUNENCRYPTEDLOG";
case OpCode::SENDL2TOL1MSG:
return "SENDL2TOL1MSG";
case OpCode::CALL:
return "CALL";
case OpCode::STATICCALL:
return "STATICCALL";
case OpCode::DELEGATECALL:
return "DELEGATECALL";
case OpCode::RETURN:
return "RETURN";
case OpCode::REVERT:
return "REVERT";
case OpCode::DEBUGLOG:
return "DEBUGLOG";
case OpCode::KECCAK:
return "KECCAK";
case OpCode::POSEIDON2:
return "POSEIDON2";
case OpCode::SHA256:
return "SHA256";
case OpCode::PEDERSEN:
return "PEDERSEN";
case OpCode::TORADIXLE:
return "TORADIXLE";
case OpCode::SHA256COMPRESSION:
return "SHA256COMPRESSION";
case OpCode::KECCAKF1600:
return "KECCAKF1600";
case OpCode::LAST_OPCODE_SENTINEL:
return "LAST_OPCODE_SENTINEL";
default:
return "UNKNOWN";
}
}

} // namespace bb::avm_trace
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_opcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@ std::string to_hex(T value)
}
std::string to_hex(OpCode opcode);

std::string to_string(OpCode opcode);

} // namespace bb::avm_trace
Loading

0 comments on commit d3be85f

Please sign in to comment.