Skip to content

Commit bd48128

Browse files
authored
feat(avm): Add command to call avm proving in bb binary (AztecProtocol#4369)
Resolves AztecProtocol#4302 Resolves AztecProtocol#4039
1 parent 7424d23 commit bd48128

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

barretenberg/cpp/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ set(BARRETENBERG_TARGET_OBJECTS
137137
$<TARGET_OBJECTS:sumcheck_objects>
138138
$<TARGET_OBJECTS:transcript_objects>
139139
$<TARGET_OBJECTS:translator_vm_objects>
140-
$<TARGET_OBJECTS:ultra_honk_objects>)
140+
$<TARGET_OBJECTS:ultra_honk_objects>
141+
$<TARGET_OBJECTS:vm_objects>)
141142

142143
add_library(
143144
barretenberg

barretenberg/cpp/src/barretenberg/bb/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#include "barretenberg/bb/file_io.hpp"
2+
#include "barretenberg/common/serialize.hpp"
13
#include "barretenberg/dsl/types.hpp"
4+
#include "barretenberg/honk/proof_system/types/proof.hpp"
25
#include "barretenberg/plonk/proof_system/proving_key/serialize.hpp"
6+
#include "barretenberg/vm/avm_trace/AvmMini_execution.hpp"
37
#include "config.hpp"
48
#include "get_bn254_crs.hpp"
59
#include "get_bytecode.hpp"
@@ -12,6 +16,7 @@
1216
#include <barretenberg/dsl/acir_proofs/acir_composer.hpp>
1317
#include <barretenberg/dsl/acir_proofs/goblin_acir_composer.hpp>
1418
#include <barretenberg/srs/global_crs.hpp>
19+
#include <cstdint>
1520
#include <iostream>
1621
#include <stdexcept>
1722
#include <string>
@@ -506,6 +511,7 @@ int main(int argc, char* argv[])
506511
if (command == "prove_and_verify_goblin") {
507512
return proveAndVerifyGoblin(bytecode_path, witness_path) ? 0 : 1;
508513
}
514+
509515
if (command == "prove") {
510516
std::string output_path = get_option(args, "-o", "./proofs/proof");
511517
prove(bytecode_path, witness_path, output_path);
@@ -528,6 +534,23 @@ int main(int argc, char* argv[])
528534
} else if (command == "vk_as_fields") {
529535
std::string output_path = get_option(args, "-o", vk_path + "_fields.json");
530536
vk_as_fields(vk_path, output_path);
537+
} else if (command == "avm_prove") {
538+
std::string avm_bytecode_path = get_option(args, "-b", "./target/avm_bytecode.bin");
539+
std::string output_path = get_option(args, "-o", "./proofs/avm_proof");
540+
std::vector<uint8_t> call_data_bytes{};
541+
542+
if (flag_present(args, "-d")) {
543+
auto const call_data_path = get_option(args, "-d", "./target/call_data.bin");
544+
call_data_bytes = read_file(call_data_path);
545+
}
546+
547+
srs::init_crs_factory("../srs_db/ignition");
548+
549+
std::vector<fr> const call_data = many_from_buffer<fr>(call_data_bytes);
550+
auto const avm_bytecode = read_file(avm_bytecode_path);
551+
auto const proof = avm_trace::Execution::run_and_prove(avm_bytecode, call_data);
552+
std::vector<uint8_t> const proof_bytes = to_buffer(proof);
553+
write_file(output_path, proof_bytes);
531554
} else {
532555
std::cerr << "Unknown command: " << command << "\n";
533556
return 1;

barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "AvmMini_execution.hpp"
22
#include "barretenberg/common/serialize.hpp"
3+
#include "barretenberg/common/throw_or_abort.hpp"
34
#include "barretenberg/proof_system/circuit_builder/generated/AvmMini_circuit_builder.hpp"
45
#include "barretenberg/vm/avm_trace/AvmMini_common.hpp"
56
#include "barretenberg/vm/avm_trace/AvmMini_instructions.hpp"
@@ -56,20 +57,20 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
5657
pos += AVM_OPCODE_BYTE_LENGTH;
5758

5859
if (!Bytecode::is_valid(opcode_byte)) {
59-
throw std::runtime_error("Invalid opcode byte: " + std::to_string(opcode_byte));
60+
throw_or_abort("Invalid opcode byte: " + std::to_string(opcode_byte));
6061
}
6162

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

6566
if (Bytecode::has_in_tag(opcode)) {
6667
if (pos + AVM_IN_TAG_BYTE_LENGTH > length) {
67-
throw std::runtime_error("Instruction tag missing at position " + std::to_string(pos));
68+
throw_or_abort("Instruction tag missing at position " + std::to_string(pos));
6869
}
6970
in_tag_u8 = bytecode.at(pos);
7071
if (in_tag_u8 == static_cast<uint8_t>(AvmMemoryTag::U0) || in_tag_u8 > MAX_MEM_TAG) {
71-
throw std::runtime_error("Instruction tag is invalid at position " + std::to_string(pos) +
72-
" value: " + std::to_string(in_tag_u8));
72+
throw_or_abort("Instruction tag is invalid at position " + std::to_string(pos) +
73+
" value: " + std::to_string(in_tag_u8));
7374
}
7475
pos += AVM_IN_TAG_BYTE_LENGTH;
7576
}
@@ -109,8 +110,8 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
109110
operands_size = 20;
110111
break;
111112
default:
112-
throw std::runtime_error("Instruction tag for SET opcode is invalid at position " +
113-
std::to_string(pos) + " value: " + std::to_string(in_tag_u8));
113+
throw_or_abort("Instruction tag for SET opcode is invalid at position " + std::to_string(pos) +
114+
" value: " + std::to_string(in_tag_u8));
114115
break;
115116
}
116117
} else {
@@ -119,7 +120,7 @@ std::vector<Instruction> Execution::parse(std::vector<uint8_t> const& bytecode)
119120
}
120121

121122
if (pos + operands_size > length) {
122-
throw std::runtime_error("Operand is missing at position " + std::to_string(pos));
123+
throw_or_abort("Operand is missing at position " + std::to_string(pos));
123124
}
124125

125126
// We handle operands which are encoded with less than 4 bytes.

barretenberg/cpp/src/barretenberg/vm/avm_trace/AvmMini_execution.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Execution {
2222

2323
static std::vector<Instruction> parse(std::vector<uint8_t> const& bytecode);
2424
static std::vector<Row> gen_trace(std::vector<Instruction> const& instructions, std::vector<FF> const& calldata);
25-
static bb::HonkProof run_and_prove(std::vector<uint8_t> const& bytecode, std::vector<FF> const& calldata);
25+
static bb::HonkProof run_and_prove(std::vector<uint8_t> const& bytecode,
26+
std::vector<FF> const& calldata = std::vector<FF>{});
2627
};
2728

2829
} // namespace avm_trace

0 commit comments

Comments
 (0)