Skip to content

Commit

Permalink
chore(dsl): Update backend gateCount command to query a Program in a …
Browse files Browse the repository at this point in the history
…single request (#6228)

Resolves AztecProtocol/aztec-packages#6168

This PR does a very minor update to simply now go through the entire
list of functions contained inside of a Program and generate a basic
circuit report for each functions. Currently `nargo info` now takes in a
JSON report that it works with instead of an individual printed gate
count. This PR also does some initial work on making a gate report that
is ready for noir-lang/noir-gates-diff. This is yet to be updated but
has most of the initial skeleton needed to get a gates report for an
entire workspace.

Also, once noir-lang/noir#4975 is merged and
synced into this repo we can remove the `bb info` command and rename `bb
gates` -> `bb info`

Nargo info still works as expected:
<img width="662" alt="Screenshot 2024-05-08 at 2 55 32 PM"
src="https://github.com/AztecProtocol/aztec-packages/assets/43554004/dab5e819-18c4-4bbd-a727-d4bf42f7b95c">

---------

Co-authored-by: Tom French <tom@tomfren.ch>
  • Loading branch information
2 people authored and AztecBot committed May 10, 2024
1 parent a0f30c4 commit 1ae3380
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 7 deletions.
38 changes: 31 additions & 7 deletions cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(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<uint8_t> data(jsonData, jsonData + length);
writeRawBytesToStdout(data);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct AcirFormat {
// to be able to verify SNARKs on Ethereum.
bool recursive;

uint32_t num_acir_opcodes;

std::vector<uint32_t> public_inputs;

std::vector<LogicConstraint> logic_constraints;
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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<uint32_t, BlockConstraint> block_id_to_block_constraint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(witness.size() + 1),
.recursive = false,
.num_acir_opcodes = 5,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -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 = {},
Expand Down Expand Up @@ -292,6 +294,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(witness.size() + 1),
.recursive = false,
.num_acir_opcodes = 5,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -350,6 +353,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(witness.size() + 1),
.recursive = false,
.num_acir_opcodes = 5,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -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 = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 7,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ TEST_F(EcOperations, TestECOperations)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables + 1),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -141,6 +142,7 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -186,6 +188,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ TEST(ECDSASecp256r1, test_hardcoded)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -177,6 +178,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -227,6 +229,7 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down Expand Up @@ -272,6 +275,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail)
AcirFormat constraint_system{
.varnum = static_cast<uint32_t>(num_variables),
.recursive = false,
.num_acir_opcodes = 1,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -241,6 +242,7 @@ Builder create_outer_circuit(std::vector<Builder>& inner_circuits)

AcirFormat constraint_system{ .varnum = static_cast<uint32_t>(witness.size()),
.recursive = false,
.num_acir_opcodes = static_cast<uint32_t>(recursion_constraints.size()),
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down

0 comments on commit 1ae3380

Please sign in to comment.