Skip to content

Commit

Permalink
feat(avm): Add command to call avm proving in bb binary (AztecProtoco…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon authored Feb 5, 2024
1 parent 57824fe commit 4f6d607
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ set(BARRETENBERG_TARGET_OBJECTS
$<TARGET_OBJECTS:sumcheck_objects>
$<TARGET_OBJECTS:transcript_objects>
$<TARGET_OBJECTS:translator_vm_objects>
$<TARGET_OBJECTS:ultra_honk_objects>)
$<TARGET_OBJECTS:ultra_honk_objects>
$<TARGET_OBJECTS:vm_objects>)

add_library(
barretenberg
Expand Down
23 changes: 23 additions & 0 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "barretenberg/bb/file_io.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/honk/proof_system/types/proof.hpp"
#include "barretenberg/plonk/proof_system/proving_key/serialize.hpp"
#include "barretenberg/vm/avm_trace/AvmMini_execution.hpp"
#include "config.hpp"
#include "get_bn254_crs.hpp"
#include "get_bytecode.hpp"
Expand All @@ -12,6 +16,7 @@
#include <barretenberg/dsl/acir_proofs/acir_composer.hpp>
#include <barretenberg/dsl/acir_proofs/goblin_acir_composer.hpp>
#include <barretenberg/srs/global_crs.hpp>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -506,6 +511,7 @@ int main(int argc, char* argv[])
if (command == "prove_and_verify_goblin") {
return proveAndVerifyGoblin(bytecode_path, witness_path) ? 0 : 1;
}

if (command == "prove") {
std::string output_path = get_option(args, "-o", "./proofs/proof");
prove(bytecode_path, witness_path, output_path);
Expand All @@ -528,6 +534,23 @@ int main(int argc, char* argv[])
} else if (command == "vk_as_fields") {
std::string output_path = get_option(args, "-o", vk_path + "_fields.json");
vk_as_fields(vk_path, output_path);
} else if (command == "avm_prove") {
std::string avm_bytecode_path = get_option(args, "-b", "./target/avm_bytecode.bin");
std::string output_path = get_option(args, "-o", "./proofs/avm_proof");
std::vector<uint8_t> call_data_bytes{};

if (flag_present(args, "-d")) {
auto const call_data_path = get_option(args, "-d", "./target/call_data.bin");
call_data_bytes = read_file(call_data_path);
}

srs::init_crs_factory("../srs_db/ignition");

std::vector<fr> const call_data = many_from_buffer<fr>(call_data_bytes);
auto const avm_bytecode = read_file(avm_bytecode_path);
auto const proof = avm_trace::Execution::run_and_prove(avm_bytecode, call_data);
std::vector<uint8_t> const proof_bytes = to_buffer(proof);
write_file(output_path, proof_bytes);
} else {
std::cerr << "Unknown command: " << command << "\n";
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AvmMini_execution.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/common/throw_or_abort.hpp"
#include "barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp"
#include "barretenberg/vm/avm_trace/AvmMini_common.hpp"
#include "barretenberg/vm/avm_trace/AvmMini_instructions.hpp"
Expand Down Expand Up @@ -56,20 +57,20 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
pos += AVM_OPCODE_BYTE_LENGTH;

if (!Bytecode::is_valid(opcode_byte)) {
throw std::runtime_error("Invalid opcode byte: " + std::to_string(opcode_byte));
throw_or_abort("Invalid opcode byte: " + std::to_string(opcode_byte));
}

const auto opcode = static_cast<OpCode>(opcode_byte);
auto in_tag_u8 = static_cast<uint8_t>(AvmMemoryTag::U0);

if (Bytecode::has_in_tag(opcode)) {
if (pos + AVM_IN_TAG_BYTE_LENGTH > length) {
throw std::runtime_error("Instruction tag missing at position " + std::to_string(pos));
throw_or_abort("Instruction tag missing at position " + std::to_string(pos));
}
in_tag_u8 = bytecode.at(pos);
if (in_tag_u8 == static_cast<uint8_t>(AvmMemoryTag::U0) || in_tag_u8 > MAX_MEM_TAG) {
throw std::runtime_error("Instruction tag is invalid at position " + std::to_string(pos) +
" value: " + std::to_string(in_tag_u8));
throw_or_abort("Instruction tag is invalid at position " + std::to_string(pos) +
" value: " + std::to_string(in_tag_u8));
}
pos += AVM_IN_TAG_BYTE_LENGTH;
}
Expand Down Expand Up @@ -109,8 +110,8 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
operands_size = 20;
break;
default:
throw std::runtime_error("Instruction tag for SET opcode is invalid at position " +
std::to_string(pos) + " value: " + std::to_string(in_tag_u8));
throw_or_abort("Instruction tag for SET opcode is invalid at position " + std::to_string(pos) +
" value: " + std::to_string(in_tag_u8));
break;
}
} else {
Expand All @@ -119,7 +120,7 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
}

if (pos + operands_size > length) {
throw std::runtime_error("Operand is missing at position " + std::to_string(pos));
throw_or_abort("Operand is missing at position " + std::to_string(pos));
}

// We handle operands which are encoded with less than 4 bytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Execution {

static std::vector<Instruction> parse(std::vector<uint8_t> const& bytecode);
static std::vector<Row> gen_trace(std::vector<Instruction> const& instructions, std::vector<FF> const& calldata);
static bb::HonkProof run_and_prove(std::vector<uint8_t> const& bytecode, std::vector<FF> const& calldata);
static bb::HonkProof run_and_prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata = std::vector<FF>{});
};

} // namespace avm_trace

0 comments on commit 4f6d607

Please sign in to comment.