Skip to content

Commit

Permalink
refactor: Trace structure is an object (#10003)
Browse files Browse the repository at this point in the history
A trace structure is a set of block sizes that are known at compile
time, but previously there was no simple way to extract any of this
compile time information even at runtime without executing some heavier
code or perhaps by duplicating a switch statement and manually computing
a size a runtime. Motivated by this, I refactor the trace structure
(formerly an enum coupled loosely to some related structures) so that it
becomes a struct. At the same time I refactor related structures to use
more familiar patterns. I also do some renaming and related general
tidying.

`size` functions will be implemented in a follow-on, where they will
help to allocate and use a single commitment key throughout `ClientIVC`.

See PR description for more info.
  • Loading branch information
codygunton authored Nov 15, 2024
1 parent cae7279 commit 5f83755
Show file tree
Hide file tree
Showing 58 changed files with 823 additions and 964 deletions.
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ add_subdirectory(barretenberg/dsl)
add_subdirectory(barretenberg/ecc)
add_subdirectory(barretenberg/eccvm)
add_subdirectory(barretenberg/env)
add_subdirectory(barretenberg/execution_trace)
add_subdirectory(barretenberg/trace_to_polynomials)
add_subdirectory(barretenberg/examples)
add_subdirectory(barretenberg/flavor)
add_subdirectory(barretenberg/goblin)
Expand Down Expand Up @@ -128,7 +128,7 @@ set(BARRETENBERG_TARGET_OBJECTS
$<TARGET_OBJECTS:dsl_objects>
$<TARGET_OBJECTS:ecc_objects>
$<TARGET_OBJECTS:eccvm_objects>
$<TARGET_OBJECTS:execution_trace_objects>
$<TARGET_OBJECTS:trace_to_polynomials_objects>
$<TARGET_OBJECTS:simple_example_objects>
$<TARGET_OBJECTS:flavor_objects>
$<TARGET_OBJECTS:goblin_objects>
Expand Down
12 changes: 3 additions & 9 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ void client_ivc_prove_output_all_msgpack(const std::string& bytecodePath,
}
// TODO(#7371) dedupe this with the rest of the similar code
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): remove use of auto_verify_mode
ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::E2E_FULL_TEST;
ClientIVC ivc{ { E2E_FULL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

// Accumulate the entire program stack into the IVC
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1116): remove manual setting of is_kernel once databus
Expand Down Expand Up @@ -446,9 +444,7 @@ bool foldAndVerifyProgram(const std::string& bytecodePath, const std::string& wi
init_bn254_crs(1 << 22);
init_grumpkin_crs(1 << 16);

ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

auto program_stack = acir_format::get_acir_program_stack(
bytecodePath, witnessPath, false); // TODO(https://github.com/AztecProtocol/barretenberg/issues/1013): this
Expand Down Expand Up @@ -499,9 +495,7 @@ void client_ivc_prove_output_all(const std::string& bytecodePath,
init_grumpkin_crs(1 << 16);

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): remove use of auto_verify_mode
ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::E2E_FULL_TEST;
ClientIVC ivc{ { E2E_FULL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

auto program_stack = acir_format::get_acir_program_stack(
bytecodePath, witnessPath, false); // TODO(https://github.com/AztecProtocol/barretenberg/issues/1013): this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class ClientIVCBench : public benchmark::Fixture {
*/
BENCHMARK_DEFINE_F(ClientIVCBench, Full)(benchmark::State& state)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

auto total_num_circuits = 2 * static_cast<size_t>(state.range(0)); // 2x accounts for kernel circuits
auto mocked_vkeys = mock_verification_keys(total_num_circuits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ void fill_trace(State& state, TraceSettings settings)

void fill_trace_client_ivc_bench(State& state)
{
fill_trace(state, { TraceStructure::CLIENT_IVC_BENCH, /*overflow_capacity=*/0 });
fill_trace(state, { CLIENT_IVC_BENCH_STRUCTURE });
}

void fill_trace_e2e_full_test(State& state)
{
fill_trace(state, { TraceStructure::E2E_FULL_TEST, /*overflow_capacity=*/0 });
fill_trace(state, { E2E_FULL_TEST_STRUCTURE });
}

static void pk_mem(State& state, void (*test_circuit_function)(State&)) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace bb {

template <> auto UltraCircuitChecker::init_empty_values<UltraCircuitBuilder_<UltraArith<bb::fr>>>()
template <> auto UltraCircuitChecker::init_empty_values<UltraCircuitBuilder_<UltraExecutionTraceBlocks>>()
{
return UltraFlavor::AllValues{};
}
Expand Down Expand Up @@ -298,7 +298,7 @@ void UltraCircuitChecker::populate_values(
}

// Template method instantiations for each check method
template bool UltraCircuitChecker::check<UltraCircuitBuilder_<UltraArith<bb::fr>>>(
const UltraCircuitBuilder_<UltraArith<bb::fr>>& builder_in);
template bool UltraCircuitChecker::check<UltraCircuitBuilder_<UltraExecutionTraceBlocks>>(
const UltraCircuitBuilder_<UltraExecutionTraceBlocks>& builder_in);
template bool UltraCircuitChecker::check<MegaCircuitBuilder_<bb::fr>>(const MegaCircuitBuilder_<bb::fr>& builder_in);
} // namespace bb
9 changes: 7 additions & 2 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "barretenberg/goblin/goblin.hpp"
#include "barretenberg/goblin/mock_circuits.hpp"
#include "barretenberg/plonk_honk_shared/arithmetization/execution_trace_usage_tracker.hpp"
#include "barretenberg/plonk_honk_shared/execution_trace/execution_trace_usage_tracker.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/protogalaxy/protogalaxy_verifier.hpp"
#include "barretenberg/stdlib/honk_verifier/decider_recursive_verifier.hpp"
Expand Down Expand Up @@ -120,10 +120,15 @@ class ClientIVC {

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): eventually do away with this.
// Setting auto_verify_mode = true will cause kernel completion logic to be added to kernels automatically
bool auto_verify_mode = false;
bool auto_verify_mode;

bool initialized = false; // Is the IVC accumulator initialized

ClientIVC(TraceSettings trace_settings = {}, bool auto_verify_mode = false)
: trace_settings(trace_settings)
, auto_verify_mode(auto_verify_mode)
{}

void instantiate_stdlib_verification_queue(
ClientCircuit& circuit, const std::vector<std::shared_ptr<RecursiveVerificationKey>>& input_keys = {});

Expand Down
31 changes: 10 additions & 21 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ class ClientIVCTests : public ::testing::Test {
TraceSettings trace_settings,
size_t log2_num_gates = 16)
{
ClientIVC ivc; // temporary IVC instance needed to produce the complete kernel circuits
ivc.trace_settings = trace_settings;
ClientIVC ivc{ trace_settings }; // temporary IVC instance needed to produce the complete kernel circuits

std::vector<std::shared_ptr<VerificationKey>> vkeys;

Expand Down Expand Up @@ -162,8 +161,7 @@ TEST_F(ClientIVCTests, BadProofFailure)
{
// Confirm that the IVC verifies if nothing is tampered with
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand All @@ -178,8 +176,7 @@ TEST_F(ClientIVCTests, BadProofFailure)

// The IVC throws an exception if the FIRST fold proof is tampered with
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand All @@ -203,8 +200,7 @@ TEST_F(ClientIVCTests, BadProofFailure)

// The IVC fails if the SECOND fold proof is tampered with
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand All @@ -228,8 +224,7 @@ TEST_F(ClientIVCTests, BadProofFailure)

// The IVC fails if the 3rd/FINAL fold proof is tampered with
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand Down Expand Up @@ -277,8 +272,7 @@ TEST_F(ClientIVCTests, BasicLarge)
*/
TEST_F(ClientIVCTests, BasicStructured)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand Down Expand Up @@ -324,8 +318,7 @@ TEST_F(ClientIVCTests, PrecomputedVerificationKeys)
*/
TEST_F(ClientIVCTests, StructuredPrecomputedVKs)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

size_t NUM_CIRCUITS = 4;
size_t log2_num_gates = 5; // number of gates in baseline mocked circuit
Expand Down Expand Up @@ -356,8 +349,7 @@ TEST(ClientIVCBenchValidation, Full6)
bb::srs::init_crs_factory("../srs_db/ignition");
bb::srs::init_grumpkin_crs_factory("../srs_db/grumpkin");

ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };
size_t total_num_circuits{ 12 };
PrivateFunctionExecutionMockCircuitProducer circuit_producer;
auto precomputed_vkeys = circuit_producer.precompute_verification_keys(total_num_circuits, ivc.trace_settings);
Expand All @@ -376,8 +368,7 @@ TEST(ClientIVCBenchValidation, Full6MockedVKs)
bb::srs::init_crs_factory("../srs_db/ignition");
bb::srs::init_grumpkin_crs_factory("../srs_db/grumpkin");

ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };
size_t total_num_circuits{ 12 };
PrivateFunctionExecutionMockCircuitProducer circuit_producer;
auto mocked_vkeys = mock_verification_keys(total_num_circuits);
Expand All @@ -396,11 +387,9 @@ TEST(ClientIVCBenchValidation, Full6MockedVKs)
*/
TEST_F(ClientIVCTests, StructuredTraceOverflow)
{
ClientIVC ivc;

// Define trace settings with sufficient overflow capacity to accommodate each of the circuits to be accumulated
uint32_t overflow_capacity = 1 << 17;
ivc.trace_settings = { TraceStructure::SMALL_TEST, overflow_capacity };
ClientIVC ivc{ { SMALL_TEST_STRUCTURE, /*overflow_capacity=*/1 << 17 } };

MockCircuitProducer circuit_producer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class ClientIVCAutoVerifyTests : public ::testing::Test {
*/
TEST_F(ClientIVCAutoVerifyTests, Basic)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ClientIVC ivc{ {}, /*auto_verify_mode=*/true };

// Initialize the IVC with an arbitrary circuit
Builder circuit_0 = create_mock_circuit(ivc, /*is_kernel=*/false);
Expand All @@ -81,8 +80,7 @@ TEST_F(ClientIVCAutoVerifyTests, Basic)
*/
TEST_F(ClientIVCAutoVerifyTests, BasicOdd)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ClientIVC ivc{ {}, /*auto_verify_mode=*/true };

// Initialize the IVC with an arbitrary circuit
Builder circuit_0 = create_mock_circuit(ivc, /*is_kernel=*/false);
Expand All @@ -105,8 +103,7 @@ TEST_F(ClientIVCAutoVerifyTests, BasicOdd)
*/
TEST_F(ClientIVCAutoVerifyTests, BasicLarge)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ClientIVC ivc{ {}, /*auto_verify_mode=*/true };

// Construct a set of arbitrary circuits
size_t NUM_CIRCUITS = 6;
Expand All @@ -130,9 +127,7 @@ TEST_F(ClientIVCAutoVerifyTests, BasicLarge)
*/
TEST_F(ClientIVCAutoVerifyTests, BasicStructured)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

// Construct some circuits of varying size
Builder circuit_0 = create_mock_circuit(ivc, /*is_kernel=*/false, /*log2_num_gates=*/5);
Expand All @@ -155,8 +150,7 @@ TEST_F(ClientIVCAutoVerifyTests, BasicStructured)
*/
TEST_F(ClientIVCAutoVerifyTests, PrecomputedVerificationKeys)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ClientIVC ivc{ {}, /*auto_verify_mode=*/true };

// Construct a set of arbitrary circuits
size_t NUM_CIRCUITS = 4;
Expand All @@ -183,9 +177,7 @@ TEST_F(ClientIVCAutoVerifyTests, PrecomputedVerificationKeys)
*/
TEST_F(ClientIVCAutoVerifyTests, StructuredPrecomputedVKs)
{
ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

// Construct a set of arbitrary circuits
size_t NUM_CIRCUITS = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class ClientIVCIntegrationTests : public ::testing::Test {
*/
TEST_F(ClientIVCIntegrationTests, BenchmarkCaseSimple)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand All @@ -60,8 +59,7 @@ TEST_F(ClientIVCIntegrationTests, BenchmarkCaseSimple)
*/
TEST_F(ClientIVCIntegrationTests, ConsecutiveKernels)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand All @@ -88,8 +86,7 @@ TEST_F(ClientIVCIntegrationTests, ConsecutiveKernels)
*/
TEST_F(ClientIVCIntegrationTests, BenchmarkCasePrecomputedVKs)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

size_t NUM_CIRCUITS = 6;

Expand Down Expand Up @@ -122,8 +119,7 @@ TEST_F(ClientIVCIntegrationTests, BenchmarkCasePrecomputedVKs)
*/
TEST_F(ClientIVCIntegrationTests, DatabusFailure)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ class PrivateFunctionExecutionMockCircuitProducer {
*/
auto precompute_verification_keys(const size_t num_circuits, TraceSettings trace_settings)
{
ClientIVC ivc; // temporary IVC instance needed to produce the complete kernel circuits
ivc.trace_settings = trace_settings;
ClientIVC ivc{ trace_settings }; // temporary IVC instance needed to produce the complete kernel circuits

std::vector<std::shared_ptr<VerificationKey>> vkeys;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class MockKernelTest : public ::testing::Test {

TEST_F(MockKernelTest, PinFoldingKernelSizes)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::CLIENT_IVC_BENCH;
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE } };

MockCircuitProducer circuit_producer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ TEST_P(AcirIntegrationFoldingTest, DISABLED_FoldAndVerifyProgramStack)
test_name, /*honk_recursion=*/false); // TODO(https://github.com/AztecProtocol/barretenberg/issues/1013):
// Assumes Flavor is not UltraHonk

ClientIVC ivc;
ivc.auto_verify_mode = true;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

while (!program_stack.empty()) {
auto program = program_stack.back();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "barretenberg/common/container.hpp"
#include "barretenberg/dsl/acir_format/recursion_constraint.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/plonk_honk_shared/arithmetization/gate_data.hpp"
#include "barretenberg/plonk_honk_shared/execution_trace/gate_data.hpp"
#include <cstddef>
#include <cstdint>
#include <tuple>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include "barretenberg/ecc/groups/affine_element.hpp"
#include "barretenberg/plonk_honk_shared/arithmetization/gate_data.hpp"
#include "barretenberg/plonk_honk_shared/execution_trace/gate_data.hpp"
#include "barretenberg/stdlib/primitives/group/cycle_group.hpp"

namespace acir_format {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ using field_ct = stdlib::field_t<Builder>;

ClientIVC create_mock_ivc_from_constraints(const std::vector<RecursionConstraint>& constraints)
{
ClientIVC ivc;
ivc.trace_settings.structure = TraceStructure::SMALL_TEST;
ClientIVC ivc{ { SMALL_TEST_STRUCTURE } };

for (const auto& constraint : constraints) {
if (static_cast<uint32_t>(PROOF_TYPE::OINK) == constraint.proof_type) {
Expand Down Expand Up @@ -58,7 +57,7 @@ ClientIVC::VerifierInputs create_dummy_vkey_and_proof_oink(const TraceSettings&
using VerificationKey = ClientIVC::VerificationKey;
using FF = bb::fr;

MegaArith<FF>::TraceBlocks blocks;
MegaExecutionTraceBlocks blocks;
blocks.set_fixed_block_sizes(trace_settings);
blocks.compute_offsets(/*is_structured=*/true);
size_t structured_dyadic_size = blocks.get_structured_dyadic_size();
Expand Down
Loading

0 comments on commit 5f83755

Please sign in to comment.