Skip to content

Commit

Permalink
6019: re-enable proof in execution tests and some code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Apr 26, 2024
1 parent 405bdf6 commit 36fcba2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 67 deletions.
19 changes: 8 additions & 11 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,14 @@ void avm_prove(const std::filesystem::path& bytecode_path,
bool avm_verify(const std::filesystem::path& proof_path)
{
std::filesystem::path vk_path = proof_path.parent_path() / "vk";

// Actual verification temporarily stopped (#4954)
// std::vector<fr> const proof = many_from_buffer<fr>(read_file(proof_path));
//
// std::vector<uint8_t> vk_bytes = read_file(vk_path);
// auto circuit_size = from_buffer<size_t>(vk_bytes, 0);
// auto _num_public_inputs = from_buffer<size_t>(vk_bytes, sizeof(size_t));
// auto vk = AvmFlavor::VerificationKey(circuit_size, num_public_inputs);
//
// std::cout << avm_trace::Execution::verify(vk, proof);
// return avm_trace::Execution::verify(vk, proof);
std::vector<fr> const proof = many_from_buffer<fr>(read_file(proof_path));
std::vector<uint8_t> vk_bytes = read_file(vk_path);
auto circuit_size = from_buffer<size_t>(vk_bytes, 0);
auto num_public_inputs = from_buffer<size_t>(vk_bytes, sizeof(size_t));
auto vk = AvmFlavor::VerificationKey(circuit_size, num_public_inputs);

std::cout << avm_trace::Execution::verify(vk, proof);
return avm_trace::Execution::verify(vk, proof);

std::cout << 1;
return true;
Expand Down
19 changes: 1 addition & 18 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,8 @@ namespace bb::avm_trace {
* @param bytecode A vector of bytes representing the bytecode to execute.
* @param calldata expressed as a vector of finite field elements.
* @throws runtime_error exception when the bytecode is invalid.
* @return A zk proof of the execution.
* @return The verifier key and zk proof of the execution.
*/
HonkProof Execution::run_and_prove(std::vector<uint8_t> const& bytecode, std::vector<FF> const& calldata)
{
auto instructions = Deserialization::parse(bytecode);
auto trace = gen_trace(instructions, calldata);
auto circuit_builder = bb::AvmCircuitBuilder();
circuit_builder.set_trace(std::move(trace));

auto composer = AvmComposer();
auto prover = composer.create_prover(circuit_builder);
auto verifier = composer.create_verifier(circuit_builder);
auto proof = prover.construct_proof();
return proof;
}

std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata)
{
Expand All @@ -51,9 +37,6 @@ std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<u
auto circuit_builder = bb::AvmCircuitBuilder();
circuit_builder.set_trace(std::move(trace));

// Temporarily use this until #4954 is resolved
assert(circuit_builder.check_circuit());

auto composer = AvmComposer();
auto prover = composer.create_prover(circuit_builder);
auto verifier = composer.create_verifier(circuit_builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Execution {

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 std::tuple<AvmFlavor::VerificationKey, bb::HonkProof> prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata = {});
static bool verify(AvmFlavor::VerificationKey vk, HonkProof const& proof);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cstddef>
#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <vector>
46 changes: 10 additions & 36 deletions barretenberg/cpp/src/barretenberg/vm/tests/avm_execution.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include "barretenberg/vm/avm_trace/avm_common.hpp"
#include "barretenberg/vm/avm_trace/avm_deserialization.hpp"
#include "barretenberg/vm/avm_trace/avm_opcode.hpp"
#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <utility>

namespace tests_avm {

Expand All @@ -17,26 +12,6 @@ using namespace testing;

using bb::utils::hex_to_bytes;

namespace {

void gen_proof_and_validate(std::vector<uint8_t> const& bytecode,
std::vector<Row>&& trace,
std::vector<FF> const& calldata)
{
auto circuit_builder = AvmCircuitBuilder();
circuit_builder.set_trace(std::move(trace));
EXPECT_TRUE(circuit_builder.check_circuit());

auto composer = AvmComposer();
auto verifier = composer.create_verifier(circuit_builder);

auto proof = avm_trace::Execution::run_and_prove(bytecode, calldata);

// TODO(#4944): uncomment the following line to revive full verification
// EXPECT_TRUE(verifier.verify_proof(proof));
}
} // namespace

class AvmExecutionTests : public ::testing::Test {
public:
AvmTraceBuilder trace_builder;
Expand Down Expand Up @@ -84,7 +59,7 @@ TEST_F(AvmExecutionTests, basicAddReturn)
ElementsAre(VariantWith<uint8_t>(0), VariantWith<uint32_t>(0), VariantWith<uint32_t>(0)))));

auto trace = Execution::gen_trace(instructions);
gen_proof_and_validate(bytecode, std::move(trace), {});
validate_trace(std::move(trace), true);
}

// Positive test for SET and SUB opcodes
Expand Down Expand Up @@ -149,8 +124,7 @@ TEST_F(AvmExecutionTests, setAndSubOpcodes)
// Find the first row enabling the subtraction selector
auto row = std::ranges::find_if(trace.begin(), trace.end(), [](Row r) { return r.avm_main_sel_op_sub == 1; });
EXPECT_EQ(row->avm_main_ic, 10000); // 47123 - 37123 = 10000

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

// Positive test for multiple MUL opcodes
Expand Down Expand Up @@ -230,7 +204,7 @@ TEST_F(AvmExecutionTests, powerWithMulOpcodes)
trace.begin(), trace.end(), [](Row r) { return r.avm_main_sel_op_mul == 1 && r.avm_main_pc == 13; });
EXPECT_EQ(row->avm_main_ic, 244140625); // 5^12 = 244140625

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

// Positive test about a single internal_call and internal_return
Expand Down Expand Up @@ -297,7 +271,7 @@ TEST_F(AvmExecutionTests, simpleInternalCall)
auto row = std::ranges::find_if(trace.begin(), trace.end(), [](Row r) { return r.avm_main_sel_op_add == 1; });
EXPECT_EQ(row->avm_main_ic, 345567789);

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

// Positive test with some nested internall calls
Expand Down Expand Up @@ -377,7 +351,7 @@ TEST_F(AvmExecutionTests, nestedInternalCalls)
EXPECT_EQ(row->avm_main_ic, 187);
EXPECT_EQ(row->avm_main_pc, 4);

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

// Positive test with JUMP and CALLDATACOPY
Expand Down Expand Up @@ -451,7 +425,7 @@ TEST_F(AvmExecutionTests, jumpAndCalldatacopy)
// It must have failed as subtraction was "jumped over".
EXPECT_EQ(row, trace.end());

gen_proof_and_validate(bytecode, std::move(trace), std::vector<FF>{ 13, 156 });
validate_trace(std::move(trace), true);
}

// Positive test with MOV.
Expand Down Expand Up @@ -499,7 +473,7 @@ TEST_F(AvmExecutionTests, movOpcode)
EXPECT_EQ(row->avm_main_ia, 19);
EXPECT_EQ(row->avm_main_ic, 19);

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

// Positive test with CMOV.
Expand Down Expand Up @@ -555,7 +529,7 @@ TEST_F(AvmExecutionTests, cmovOpcode)
EXPECT_EQ(row->avm_main_ic, 3);
EXPECT_EQ(row->avm_main_id, 5);

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

// Positive test with indirect MOV.
Expand Down Expand Up @@ -603,7 +577,7 @@ TEST_F(AvmExecutionTests, indMovOpcode)
EXPECT_EQ(row->avm_main_ia, 255);
EXPECT_EQ(row->avm_main_ic, 255);

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

// Positive test for SET and CAST opcodes
Expand Down Expand Up @@ -644,7 +618,7 @@ TEST_F(AvmExecutionTests, setAndCastOpcodes)
auto row = std::ranges::find_if(trace.begin(), trace.end(), [](Row r) { return r.avm_main_sel_op_cast == 1; });
EXPECT_EQ(row->avm_main_ic, 19); // 0XB813 --> 0X13 = 19

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

// Negative test detecting an invalid opcode byte.
Expand Down

0 comments on commit 36fcba2

Please sign in to comment.