diff --git a/cpp/src/barretenberg/bb/main.cpp b/cpp/src/barretenberg/bb/main.cpp index 9db639ea0..820cc522a 100644 --- a/cpp/src/barretenberg/bb/main.cpp +++ b/cpp/src/barretenberg/bb/main.cpp @@ -296,19 +296,43 @@ void prove(const std::string& bytecodePath, const std::string& witnessPath, cons * @brief Computes the number of Barretenberg specific gates needed to create a proof for the specific ACIR circuit * * Communication: - * - stdout: The number of gates is written to stdout + * - stdout: A JSON string of the number of ACIR opcodes and final backend circuit size * * @param bytecodePath Path to the file containing the serialized circuit */ void gateCount(const std::string& bytecodePath) { - auto constraint_system = get_constraint_system(bytecodePath); - acir_proofs::AcirComposer acir_composer(0, verbose); - acir_composer.create_circuit(constraint_system); - auto gate_count = acir_composer.get_total_circuit_size(); + // All circuit reports will be built into the string below + std::string functions_string = "{\"functions\": [\n "; + auto constraint_systems = get_constraint_systems(bytecodePath); + size_t i = 0; + for (auto constraint_system : constraint_systems) { + acir_proofs::AcirComposer acir_composer(0, verbose); + acir_composer.create_circuit(constraint_system); + auto circuit_size = acir_composer.get_total_circuit_size(); + + // Build individual circuit report + auto result_string = format("{\n \"acir_opcodes\": ", + constraint_system.num_acir_opcodes, + ",\n \"circuit_size\": ", + circuit_size, + "\n }"); + + // Attach a comma if we still circuit reports to generate + if (i != (constraint_systems.size() - 1)) { + result_string = format(result_string, ","); + } - writeUint64AsRawBytesToStdout(static_cast(gate_count)); - vinfo("gate count: ", gate_count); + functions_string = format(functions_string, result_string); + + i++; + } + functions_string = format(functions_string, "\n]}"); + + const char* jsonData = functions_string.c_str(); + size_t length = strlen(jsonData); + std::vector data(jsonData, jsonData + length); + writeRawBytesToStdout(data); } /** diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 8b7823260..9add17a14 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -32,6 +32,8 @@ struct AcirFormat { // to be able to verify SNARKs on Ethereum. bool recursive; + uint32_t num_acir_opcodes; + std::vector public_inputs; std::vector logic_constraints; diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 2d23b057c..038db2a28 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -32,6 +32,7 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs) AcirFormat constraint_system{ .varnum = 4, .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -149,6 +150,7 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit) AcirFormat constraint_system{ .varnum = 6, .recursive = false, + .num_acir_opcodes = 7, .public_inputs = { 1 }, .logic_constraints = { logic_constraint }, .range_constraints = { range_a, range_b }, @@ -218,6 +220,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) }; AcirFormat constraint_system{ .varnum = 81, .recursive = false, + .num_acir_opcodes = 75, .public_inputs = {}, .logic_constraints = {}, .range_constraints = range_constraints, @@ -314,6 +317,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) AcirFormat constraint_system{ .varnum = 81, .recursive = false, + .num_acir_opcodes = 75, .public_inputs = {}, .logic_constraints = {}, .range_constraints = range_constraints, @@ -429,6 +433,7 @@ TEST_F(AcirFormatTests, TestVarKeccak) AcirFormat constraint_system{ .varnum = 36, .recursive = false, + .num_acir_opcodes = 6, .public_inputs = {}, .logic_constraints = {}, .range_constraints = { range_a, range_b, range_c, range_d }, @@ -477,6 +482,7 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) AcirFormat constraint_system{ .varnum = 51, .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp b/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp index 110087d40..3e77b60d6 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp @@ -477,6 +477,7 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit) // `varnum` is the true number of variables, thus we add one to the index which starts at zero af.varnum = circuit.current_witness_index + 1; af.recursive = circuit.recursive; + af.num_acir_opcodes = static_cast(circuit.opcodes.size()); af.public_inputs = join({ map(circuit.public_parameters.value, [](auto e) { return e.value; }), map(circuit.return_values.value, [](auto e) { return e.value; }) }); std::map block_id_to_block_constraint; diff --git a/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp index 863737703..1cc86262b 100644 --- a/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp @@ -169,6 +169,7 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple) AcirFormat constraint_system{ .varnum = static_cast(witness.size() + 1), .recursive = false, + .num_acir_opcodes = 5, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -238,6 +239,7 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple) AcirFormat constraint_system{ .varnum = 5, .recursive = false, + .num_acir_opcodes = 3, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -292,6 +294,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse) AcirFormat constraint_system{ .varnum = static_cast(witness.size() + 1), .recursive = false, + .num_acir_opcodes = 5, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -350,6 +353,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2) AcirFormat constraint_system{ .varnum = static_cast(witness.size() + 1), .recursive = false, + .num_acir_opcodes = 5, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -429,6 +433,7 @@ TEST_F(BigIntTests, TestBigIntDIV) AcirFormat constraint_system{ .varnum = 5, .recursive = false, + .num_acir_opcodes = 4, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp index 7cb3e5955..5d649d8fe 100644 --- a/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp @@ -111,6 +111,7 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 7, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp index fb676af0a..65be4aaae 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp @@ -51,6 +51,7 @@ TEST_F(EcOperations, TestECOperations) AcirFormat constraint_system{ .varnum = static_cast(num_variables + 1), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp index 20dddfe4a..61782002c 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp @@ -91,6 +91,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -141,6 +142,7 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -186,6 +188,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp index 6217149fd..de1d0931d 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp @@ -125,6 +125,7 @@ TEST(ECDSASecp256r1, test_hardcoded) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -177,6 +178,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -227,6 +229,7 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, @@ -272,6 +275,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail) AcirFormat constraint_system{ .varnum = static_cast(num_variables), .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp index d35a9d369..4922c63cd 100644 --- a/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp @@ -31,6 +31,7 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation) AcirFormat constraint_system{ .varnum = 9, .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp index 0b12a4119..b837f94ba 100644 --- a/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp @@ -83,6 +83,7 @@ Builder create_inner_circuit() AcirFormat constraint_system{ .varnum = 6, .recursive = true, + .num_acir_opcodes = 7, .public_inputs = { 1, 2 }, .logic_constraints = { logic_constraint }, .range_constraints = { range_a, range_b }, @@ -241,6 +242,7 @@ Builder create_outer_circuit(std::vector& inner_circuits) AcirFormat constraint_system{ .varnum = static_cast(witness.size()), .recursive = false, + .num_acir_opcodes = static_cast(recursion_constraints.size()), .public_inputs = {}, .logic_constraints = {}, .range_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp index 4b78a9550..5af032bed 100644 --- a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp @@ -33,6 +33,7 @@ TEST_F(Sha256Tests, TestSha256Compression) AcirFormat constraint_system{ .varnum = 34, .recursive = false, + .num_acir_opcodes = 1, .public_inputs = {}, .logic_constraints = {}, .range_constraints = {},