Skip to content

Commit

Permalink
chore(avm): simplify bb-prover and other AVM tests (#8627)
Browse files Browse the repository at this point in the history
Idea
* since this test is only proving TS -> BB and hints, just prove once.
* cpp tests run only check-circuit (and will run full proving nightly)

Problem: the test catches some bug, so I disable it for now. Ilyas is
working on a fix.
  • Loading branch information
fcarreiro committed Sep 18, 2024
1 parent b6bc7c3 commit 0d75363
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 201 deletions.
21 changes: 18 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/verifier.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// AUTOGENERATED FILE
#include "barretenberg/vm/avm/generated/verifier.hpp"

#include "barretenberg/common/log.hpp"
#include "barretenberg/vm/constants.hpp"

#include "barretenberg/commitment_schemes/zeromorph/zeromorph.hpp"
Expand Down Expand Up @@ -67,6 +68,7 @@ bool AvmVerifier::verify_proof(const HonkProof& proof,
const auto circuit_size = transcript->template receive_from_prover<uint32_t>("circuit_size");

if (circuit_size != key->circuit_size) {
vinfo("Circuit size mismatch: expected", key->circuit_size, " got ", circuit_size);
return false;
}

Expand Down Expand Up @@ -99,7 +101,8 @@ bool AvmVerifier::verify_proof(const HonkProof& proof,
sumcheck.verify(relation_parameters, alpha, gate_challenges);

// If Sumcheck did not verify, return false
if (sumcheck_verified.has_value() && !sumcheck_verified.value()) {
if (!sumcheck_verified.has_value() || !sumcheck_verified.value()) {
vinfo("Sumcheck failed");
return false;
}

Expand All @@ -109,28 +112,34 @@ bool AvmVerifier::verify_proof(const HonkProof& proof,

FF main_kernel_inputs_evaluation = evaluate_public_input_column(public_inputs[0], circuit_size, mle_challenge);
if (main_kernel_inputs_evaluation != claimed_evaluations.main_kernel_inputs) {
vinfo("main_kernel_inputs_evaluation failed");
return false;
}
FF main_kernel_value_out_evaluation = evaluate_public_input_column(public_inputs[1], circuit_size, mle_challenge);
if (main_kernel_value_out_evaluation != claimed_evaluations.main_kernel_value_out) {
vinfo("main_kernel_value_out_evaluation failed");
return false;
}
FF main_kernel_side_effect_out_evaluation =
evaluate_public_input_column(public_inputs[2], circuit_size, mle_challenge);
if (main_kernel_side_effect_out_evaluation != claimed_evaluations.main_kernel_side_effect_out) {
vinfo("main_kernel_side_effect_out_evaluation failed");
return false;
}
FF main_kernel_metadata_out_evaluation =
evaluate_public_input_column(public_inputs[3], circuit_size, mle_challenge);
if (main_kernel_metadata_out_evaluation != claimed_evaluations.main_kernel_metadata_out) {
vinfo("main_kernel_metadata_out_evaluation failed");
return false;
}
FF main_calldata_evaluation = evaluate_public_input_column(public_inputs[4], circuit_size, mle_challenge);
if (main_calldata_evaluation != claimed_evaluations.main_calldata) {
vinfo("main_calldata_evaluation failed");
return false;
}
FF main_returndata_evaluation = evaluate_public_input_column(public_inputs[5], circuit_size, mle_challenge);
if (main_returndata_evaluation != claimed_evaluations.main_returndata) {
vinfo("main_returndata_evaluation failed");
return false;
}

Expand All @@ -147,8 +156,14 @@ bool AvmVerifier::verify_proof(const HonkProof& proof,
transcript);

auto pairing_points = PCS::reduce_verify(opening_claim, transcript);
auto verified = key->pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);
return sumcheck_verified.value() && verified;
bool zeromoprh_verified = key->pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);

if (!zeromoprh_verified) {
vinfo("ZeroMorph failed");
return false;
}

return true;
}

} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ TEST_F(AvmArithmeticTestsFF, addition)

std::vector<FF> const returndata = { 37, 4, 11, 0, 41 };

validate_trace(std::move(trace), public_inputs, calldata, returndata, true);
validate_trace(std::move(trace), public_inputs, calldata, returndata);
}

// Test on basic subtraction over finite field type.
Expand Down Expand Up @@ -621,7 +621,7 @@ TEST_F(AvmArithmeticTestsFF, mixedOperationsWithError)
trace_builder.op_return(0, 0, 0);

auto trace = trace_builder.finalize();
validate_trace(std::move(trace), public_inputs, calldata, {}, true);
validate_trace(std::move(trace), public_inputs, calldata, {});
}

// Test of equality on FF elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ TEST_P(AvmBitwiseTestsAnd, AllAndTest)

auto trace = trace_builder.finalize();
common_validate_bit_op(trace, 0, a, b, output, FF(0), FF(1), FF(2), mem_tag);
validate_trace(std::move(trace), public_inputs, {}, { output }, true);
validate_trace(std::move(trace), public_inputs, {}, { output });
}
INSTANTIATE_TEST_SUITE_P(AvmBitwiseTests,
AvmBitwiseTestsAnd,
Expand Down
15 changes: 3 additions & 12 deletions barretenberg/cpp/src/barretenberg/vm/avm/tests/cast.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ class AvmCastTests : public ::testing::Test {
uint32_t src_address,
uint32_t dst_address,
AvmMemoryTag src_tag,
AvmMemoryTag dst_tag,
bool force_proof = false

)
AvmMemoryTag dst_tag)
{
auto const& row = trace.at(main_row_idx);
EXPECT_THAT(row,
Expand Down Expand Up @@ -106,13 +103,7 @@ class AvmCastTests : public ::testing::Test {
ALU_ROW_FIELD_EQ(in_tag, static_cast<uint32_t>(dst_tag)),
ALU_ROW_FIELD_EQ(sel_alu, 1)));

// We still want the ability to enable proving through the environment variable and therefore we do not pass
// the boolean variable force_proof to validate_trace second argument.
if (force_proof) {
validate_trace(std::move(trace), public_inputs, calldata, {}, true);
} else {
validate_trace(std::move(trace), public_inputs, calldata);
}
validate_trace(std::move(trace), public_inputs, calldata);
}
};

Expand Down Expand Up @@ -235,7 +226,7 @@ TEST_F(AvmCastTests, indirectAddrTruncationU64ToU8)
trace = trace_builder.finalize();
gen_indices();

validate_cast_trace(256'000'000'203UL, 203, 10, 11, AvmMemoryTag::U64, AvmMemoryTag::U8, true);
validate_cast_trace(256'000'000'203UL, 203, 10, 11, AvmMemoryTag::U64, AvmMemoryTag::U8);
}

TEST_F(AvmCastTests, indirectAddrWrongResolutionU64ToU8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TEST_F(AvmControlFlowTests, simpleCall)
EXPECT_EQ(halt_row->main_pc, FF(CALL_PC));
EXPECT_EQ(halt_row->main_internal_return_ptr, FF(1));
}
validate_trace(std::move(trace), public_inputs, {}, {}, true);
validate_trace(std::move(trace), public_inputs, {}, {});
}

TEST_F(AvmControlFlowTests, simpleJump)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ TEST_F(AvmExecutionTests, basicAddReturn)
ElementsAre(VariantWith<uint8_t>(0), VariantWith<uint32_t>(0), VariantWith<uint32_t>(0)))));

auto trace = gen_trace_from_instr(instructions);
validate_trace(std::move(trace), public_inputs, {}, {}, true);
validate_trace(std::move(trace), public_inputs, {}, {});
}

// Positive test for SET and SUB opcodes
Expand Down Expand Up @@ -167,7 +167,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.main_sel_op_sub == 1; });
EXPECT_EQ(row->main_ic, 10000); // 47123 - 37123 = 10000
validate_trace(std::move(trace), public_inputs, {}, {}, true);
validate_trace(std::move(trace), public_inputs, {}, {});
}

// Positive test for multiple MUL opcodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void validate_trace(std::vector<Row>&& trace,
bool with_proof,
bool expect_proof_failure)
{
// This is here for our nighly test runs.
with_proof |= std::getenv("AVM_ENABLE_FULL_PROVING") != nullptr;

const std::string avm_dump_trace_path =
std::getenv("AVM_DUMP_TRACE_PATH") != nullptr ? std::getenv("AVM_DUMP_TRACE_PATH") : "";
if (!avm_dump_trace_path.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void validate_trace(std::vector<Row>&& trace,
VmPublicInputs const& public_inputs = {},
std::vector<FF> const& calldata = {},
std::vector<FF> const& returndata = {},
bool with_proof = bb::avm_trace::ENABLE_PROVING,
bool with_proof = false,
bool expect_proof_failure = false);
void mutate_ic_in_trace(std::vector<Row>& trace,
std::function<bool(Row)>&& selectRow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TEST_F(AvmIndirectMemTests, allIndirectAdd)
EXPECT_EQ(row->main_sel_mem_op_b, FF(1));
EXPECT_EQ(row->main_sel_mem_op_c, FF(1));

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

// Testing a subtraction operation with direct input operands a, b, and an indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ TEST_F(AvmMemOpcodeTests, indirectMovInvalidAddressTag)
MEM_ROW_FIELD_EQ(r_in_tag, static_cast<uint32_t>(AvmMemoryTag::U32)),
MEM_ROW_FIELD_EQ(sel_resolve_ind_addr_c, 1)));

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

/******************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TEST_F(AvmMemoryTests, mismatchedTagAddOperation)
EXPECT_EQ(row->mem_r_in_tag, FF(static_cast<uint32_t>(AvmMemoryTag::U8)));
EXPECT_EQ(row->mem_tag, FF(static_cast<uint32_t>(AvmMemoryTag::FF)));

validate_trace(std::move(trace), public_inputs, calldata, {}, true);
validate_trace(std::move(trace), public_inputs, calldata, {});
}

// Testing an equality operation with a mismatched memory tag.
Expand Down
15 changes: 4 additions & 11 deletions barretenberg/cpp/src/barretenberg/vm/avm/tests/slice.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class AvmSliceTests : public ::testing::Test {
trace = trace_builder.finalize();
}

void validate_single_calldata_copy_trace(uint32_t col_offset,
uint32_t copy_size,
uint32_t dst_offset,
bool proof_verif = false)
void validate_single_calldata_copy_trace(uint32_t col_offset, uint32_t copy_size, uint32_t dst_offset)
{
// Find the first row enabling the calldata_copy selector
auto row = std::ranges::find_if(
Expand Down Expand Up @@ -111,11 +108,7 @@ class AvmSliceTests : public ::testing::Test {
SLICE_ROW_FIELD_EQ(sel_cd_cpy, 0),
SLICE_ROW_FIELD_EQ(sel_start, 0)));

if (proof_verif) {
validate_trace(std::move(trace), public_inputs, calldata, {}, true);
} else {
validate_trace(std::move(trace), public_inputs, calldata);
}
validate_trace(std::move(trace), public_inputs, calldata);
}

VmPublicInputs public_inputs;
Expand All @@ -131,7 +124,7 @@ class AvmSliceTests : public ::testing::Test {
TEST_F(AvmSliceTests, simpleCopyAllCDValues)
{
gen_single_calldata_copy(false, 12, 0, 12, 25);
validate_single_calldata_copy_trace(0, 12, 25, true);
validate_single_calldata_copy_trace(0, 12, 25);
}

TEST_F(AvmSliceTests, singleCopyCDElement)
Expand Down Expand Up @@ -329,7 +322,7 @@ TEST_F(AvmSliceNegativeTests, wrongCDValueInCalldataVerifier)
trace_builder.op_return(0, 0, 0);
trace = trace_builder.finalize();

validate_trace(std::move(trace), public_inputs, { 2, 3, 4, 5, 7 }, {}, true, true);
validate_trace(std::move(trace), public_inputs, { 2, 3, 4, 5, 7 }, {}, false, true);
}

TEST_F(AvmSliceNegativeTests, disableMemWriteEntry)
Expand Down
87 changes: 58 additions & 29 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,63 @@ std::unordered_map</*relation*/ std::string, /*degrees*/ std::string> get_relati
return relations_degrees;
}

void show_trace_info(const auto& trace)
{
vinfo("Built trace size: ", trace.size(), " (next power: 2^", std::bit_width(trace.size()), ")");
vinfo("Number of columns: ", trace.front().SIZE);
vinfo("Relation degrees: ", []() {
std::string result;
for (const auto& [key, value] : sorted_entries(get_relations_degrees())) {
result += "\n\t" + key + ": [" + value + "]";
}
return result;
}());

// The following computations are expensive, so we only do them in verbose mode.
if (verbose_logging) {
return;
}

const size_t total_elements = trace.front().SIZE * trace.size();
const size_t nonzero_elements = [&]() {
size_t count = 0;
for (auto const& row : trace) {
for (const auto& ff : row.as_vector()) {
if (!ff.is_zero()) {
count++;
}
}
}
return count;
}();
vinfo("Number of non-zero elements: ",
nonzero_elements,
"/",
total_elements,
" (",
100 * nonzero_elements / total_elements,
"%)");
const size_t non_zero_columns = [&]() {
bool column_is_nonzero[trace.front().SIZE];
for (auto const& row : trace) {
const auto row_vec = row.as_vector();
for (size_t col = 0; col < row.SIZE; col++) {
if (!row_vec[col].is_zero()) {
column_is_nonzero[col] = true;
}
}
}
return static_cast<size_t>(std::count(column_is_nonzero, column_is_nonzero + trace.front().SIZE, true));
}();
vinfo("Number of non-zero columns: ",
non_zero_columns,
"/",
trace.front().SIZE,
" (",
100 * non_zero_columns / trace.front().SIZE,
"%)");
}

} // namespace

/**
Expand Down Expand Up @@ -935,35 +992,7 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
}

auto trace = trace_builder.finalize();
vinfo("Built trace size: ", trace.size());
vinfo("Number of columns: ", trace.front().SIZE);
const size_t total_elements = trace.front().SIZE * trace.size();
const size_t nonzero_elements = [&]() {
size_t count = 0;
for (auto const& row : trace) {
for (const auto& ff : row.as_vector()) {
if (!ff.is_zero()) {
count++;
}
}
}
return count;
}();
vinfo("Number of non-zero elements: ",
nonzero_elements,
"/",
total_elements,
" (",
100 * nonzero_elements / total_elements,
"%)");
vinfo("Relation degrees: ", []() {
std::string result;
for (const auto& [key, value] : sorted_entries(get_relations_degrees())) {
result += "\n\t" + key + ": [" + value + "]";
}
return result;
}());

show_trace_info(trace);
return trace;
}

Expand Down
Loading

0 comments on commit 0d75363

Please sign in to comment.