Skip to content

Commit

Permalink
Format C++
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Oct 28, 2024
1 parent 4d659ba commit e0f0fe7
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 62 deletions.
34 changes: 25 additions & 9 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ bool proveAndVerifyHonkAcirFormat(acir_format::AcirFormat constraint_system, aci
honk_recursion = true;
}
// Construct a bberg circuit from the acir representation
auto builder = acir_format::create_circuit<Builder>(constraint_system, /*recursive=*/false, 0, witness, honk_recursion);
auto builder =
acir_format::create_circuit<Builder>(constraint_system, /*recursive=*/false, 0, witness, honk_recursion);

// Construct Honk proof
Prover prover{ builder };
Expand Down Expand Up @@ -367,7 +368,8 @@ void client_ivc_prove_output_all_msgpack(const std::string& bytecodePath,
bool is_kernel = false;
for (Program& program : folding_stack) {
// Construct a bberg circuit from the acir representation then accumulate it into the IVC
auto circuit = create_circuit<Builder>(program.constraints, recursive, 0, program.witness, false, ivc.goblin.op_queue);
auto circuit =
create_circuit<Builder>(program.constraints, recursive, 0, program.witness, false, ivc.goblin.op_queue);

// Set the internal is_kernel flag based on the local mechanism only if it has not already been set to true
if (!circuit.databus_propagation_data.is_kernel) {
Expand Down Expand Up @@ -464,8 +466,12 @@ bool foldAndVerifyProgram(const std::string& bytecodePath, const std::string& wi
auto stack_item = program_stack.back();

// Construct a bberg circuit from the acir representation
auto builder = acir_format::create_circuit<Builder>(
stack_item.constraints, /*recursive=*/false, 0, stack_item.witness, /*honk_recursion=*/false, ivc.goblin.op_queue);
auto builder = acir_format::create_circuit<Builder>(stack_item.constraints,
/*recursive=*/false,
0,
stack_item.witness,
/*honk_recursion=*/false,
ivc.goblin.op_queue);

// Set the internal is_kernel flag to trigger automatic appending of kernel logic if true
builder.databus_propagation_data.is_kernel = is_kernel;
Expand Down Expand Up @@ -658,7 +664,10 @@ void prove_tube(const std::string& output_path)
* @param outputPath Path to write the proof to
* @param recursive Whether to use recursive proof generation of non-recursive
*/
void prove(const std::string& bytecodePath, const std::string& witnessPath, const std::string& outputPath, const bool recursive)
void prove(const std::string& bytecodePath,
const std::string& witnessPath,
const std::string& outputPath,
const bool recursive)
{
auto constraint_system = get_constraint_system(bytecodePath, /*honk_recursion=*/false);
auto witness = get_witness(witnessPath);
Expand Down Expand Up @@ -688,7 +697,8 @@ void prove(const std::string& bytecodePath, const std::string& witnessPath, cons
*
* @param bytecodePath Path to the file containing the serialized circuit
*/
template <typename Builder = UltraCircuitBuilder> void gateCount(const std::string& bytecodePath, bool recursive, bool honk_recursion)
template <typename Builder = UltraCircuitBuilder>
void gateCount(const std::string& bytecodePath, bool recursive, bool honk_recursion)
{
// All circuit reports will be built into the string below
std::string functions_string = "{\"functions\": [\n ";
Expand Down Expand Up @@ -1073,7 +1083,9 @@ bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::
* @return UltraProver_<Flavor>
*/
template <typename Flavor>
UltraProver_<Flavor> compute_valid_prover(const std::string& bytecodePath, const std::string& witnessPath, const bool recursive)
UltraProver_<Flavor> compute_valid_prover(const std::string& bytecodePath,
const std::string& witnessPath,
const bool recursive)
{
using Builder = Flavor::CircuitBuilder;
using Prover = UltraProver_<Flavor>;
Expand Down Expand Up @@ -1106,7 +1118,10 @@ UltraProver_<Flavor> compute_valid_prover(const std::string& bytecodePath, const
* @param outputPath Path to write the proof to
*/
template <IsUltraFlavor Flavor>
void prove_honk(const std::string& bytecodePath, const std::string& witnessPath, const std::string& outputPath, const bool recursive)
void prove_honk(const std::string& bytecodePath,
const std::string& witnessPath,
const std::string& outputPath,
const bool recursive)
{
// using Builder = Flavor::CircuitBuilder;
using Prover = UltraProver_<Flavor>;
Expand Down Expand Up @@ -1174,7 +1189,8 @@ template <IsUltraFlavor Flavor> bool verify_honk(const std::string& proof_path,
* @param bytecodePath Path to the file containing the serialized circuit
* @param outputPath Path to write the verification key to
*/
template <IsUltraFlavor Flavor> void write_vk_honk(const std::string& bytecodePath, const std::string& outputPath, const bool recursive)
template <IsUltraFlavor Flavor>
void write_vk_honk(const std::string& bytecodePath, const std::string& outputPath, const bool recursive)
{
using Prover = UltraProver_<Flavor>;
using VerificationKey = Flavor::VerificationKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system,
[[maybe_unused]] std::shared_ptr<ECCOpQueue>,
bool collect_gates_per_opcode)
{
Builder builder{
size_hint, witness, constraint_system.public_inputs, constraint_system.varnum, recursive
};
Builder builder{ size_hint, witness, constraint_system.public_inputs, constraint_system.varnum, recursive };

bool has_valid_witness_assignments = !witness.empty();
build_constraints(
Expand Down Expand Up @@ -472,7 +470,7 @@ MegaCircuitBuilder create_kernel_circuit(AcirFormat& constraint_system,

// Construct the main kernel circuit logic excluding recursive verifiers
auto circuit = create_circuit<MegaCircuitBuilder>(constraint_system,
/*recursive=*/ false,
/*recursive=*/false,
size_hint,
witness,
/*honk_recursion=*/false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ Builder create_circuit(AcirFormat& constraint_system,
// Specifies whether a prover that produces SNARK recursion friendly proofs should be used.
// The proof produced when this flag is true should be friendly for recursive verification inside
// of another SNARK. For example, a recursive friendly proof may use Blake3Pedersen for
// hashing in its transcript, while we still want a prove that uses Keccak for its transcript in order
// to be able to verify SNARKs on Ethereum.
// hashing in its transcript, while we still want a prove that uses Keccak for its transcript in
// order to be able to verify SNARKs on Ethereum.
bool recursive,
const size_t size_hint = 0,
const WitnessVector& witness = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,13 @@ TEST_F(AcirFormatTests, TestCollectsGateCounts)
};
mock_opcode_indices(constraint_system);
WitnessVector witness{ 5, 27, 32 };
auto builder =
create_circuit(constraint_system, /*recursive*/ false, /*size_hint*/ 0, witness, false, std::make_shared<bb::ECCOpQueue>(), true);
auto builder = create_circuit(constraint_system,
/*recursive*/ false,
/*size_hint*/ 0,
witness,
false,
std::make_shared<bb::ECCOpQueue>(),
true);

EXPECT_EQ(constraint_system.gates_per_opcode, std::vector<size_t>({ 2, 1 }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ TEST_P(AcirIntegrationSingleTest, DISABLED_ProveAndVerifyProgram)
false); // TODO(https://github.com/AztecProtocol/barretenberg/issues/1013): Assumes Flavor is not UltraHonk

// Construct a bberg circuit from the acir representation
Builder builder = acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);
Builder builder =
acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);

// Construct and verify Honk proof
if constexpr (IsPlonkFlavor<Flavor>) {
Expand Down Expand Up @@ -381,7 +382,8 @@ TEST_P(AcirIntegrationFoldingTest, DISABLED_ProveAndVerifyProgramStack)
auto program = program_stack.back();

// Construct a bberg circuit from the acir representation
auto builder = acir_format::create_circuit<Builder>(program.constraints, /*recursive*/ false, 0, program.witness);
auto builder =
acir_format::create_circuit<Builder>(program.constraints, /*recursive*/ false, 0, program.witness);

// Construct and verify Honk proof for the individidual circuit
EXPECT_TRUE(prove_and_verify_honk<Flavor>(builder));
Expand All @@ -408,8 +410,8 @@ TEST_P(AcirIntegrationFoldingTest, DISABLED_FoldAndVerifyProgramStack)
auto program = program_stack.back();

// Construct a bberg circuit from the acir representation
auto circuit =
acir_format::create_circuit<Builder>(program.constraints, /*recursive*/ false, 0, program.witness, false, ivc.goblin.op_queue);
auto circuit = acir_format::create_circuit<Builder>(
program.constraints, /*recursive*/ false, 0, program.witness, false, ivc.goblin.op_queue);

ivc.accumulate(circuit);

Expand Down Expand Up @@ -440,7 +442,8 @@ TEST_F(AcirIntegrationTest, DISABLED_Databus)
acir_format::AcirProgram acir_program = get_program_data_from_test_file(test_name);

// Construct a bberg circuit from the acir representation
Builder builder = acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);
Builder builder =
acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);

// This prints a summary of the types of gates in the circuit
builder.blocks.summarize();
Expand All @@ -464,7 +467,8 @@ TEST_F(AcirIntegrationTest, DISABLED_DatabusTwoCalldata)
acir_format::AcirProgram acir_program = get_program_data_from_test_file(test_name);

// Construct a bberg circuit from the acir representation
Builder builder = acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);
Builder builder =
acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);

// Check that the databus columns in the builder have been populated as expected
const auto& calldata = builder.get_calldata();
Expand Down Expand Up @@ -518,7 +522,8 @@ TEST_F(AcirIntegrationTest, DISABLED_UpdateAcirCircuit)
// Assumes Flavor is not UltraHonk

// Construct a bberg circuit from the acir representation
auto circuit = acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);
auto circuit =
acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);

EXPECT_TRUE(CircuitChecker::check(circuit));

Expand Down Expand Up @@ -557,7 +562,8 @@ TEST_F(AcirIntegrationTest, DISABLED_HonkRecursion)
/*honk_recursion=*/false);

// Construct a bberg circuit from the acir representation
auto circuit = acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);
auto circuit =
acir_format::create_circuit<Builder>(acir_program.constraints, /*recursive*/ false, 0, acir_program.witness);

EXPECT_TRUE(CircuitChecker::check(circuit));
EXPECT_TRUE(prove_and_verify_honk<Flavor>(circuit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class AcirAvmRecursionConstraint : public ::testing::Test {
constraint_system.original_opcode_indices = create_empty_original_opcode_indices();

mock_opcode_indices(constraint_system);
auto outer_circuit = create_circuit(constraint_system, /*recursive*/ false, /*size_hint*/ 0, witness, /*honk_recursion=*/true);
auto outer_circuit =
create_circuit(constraint_system, /*recursive*/ false, /*size_hint*/ 0, witness, /*honk_recursion=*/true);
return outer_circuit;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class AcirHonkRecursionConstraint : public ::testing::Test {
WitnessVector witness{
5, 10, 15, 5, inverse_of_five, 1,
};
auto builder = create_circuit(constraint_system, /*recursive*/ true, /*size_hint*/ 0, witness, /*honk recursion*/ true);
auto builder =
create_circuit(constraint_system, /*recursive*/ true, /*size_hint*/ 0, witness, /*honk recursion*/ true);

return builder;
}
Expand Down Expand Up @@ -174,7 +175,8 @@ class AcirHonkRecursionConstraint : public ::testing::Test {
constraint_system.original_opcode_indices = create_empty_original_opcode_indices();

mock_opcode_indices(constraint_system);
auto outer_circuit = create_circuit(constraint_system, /*recursive*/ true, /*size_hint*/ 0, witness, /*honk recursion*/ true);
auto outer_circuit =
create_circuit(constraint_system, /*recursive*/ true, /*size_hint*/ 0, witness, /*honk recursion*/ true);

return outer_circuit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ void AcirComposer::create_finalized_circuit(acir_format::AcirFormat& constraint_
bool collect_gates_per_opcode)
{
vinfo("building circuit...");
builder_ = acir_format::create_circuit<Builder>(
constraint_system, recursive, size_hint_, witness, false, std::make_shared<bb::ECCOpQueue>(), collect_gates_per_opcode);
builder_ = acir_format::create_circuit<Builder>(constraint_system,
recursive,
size_hint_,
witness,
false,
std::make_shared<bb::ECCOpQueue>(),
collect_gates_per_opcode);
finalize_circuit();
vinfo("gates: ", builder_.get_estimated_total_circuit_size());
vinfo("circuit is recursive friendly: ", builder_.is_recursive_circuit);
Expand Down
Loading

0 comments on commit e0f0fe7

Please sign in to comment.