From f6d65afe6820d0d6e55ecae3bf8693b5ad9a9c48 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 6 Jun 2024 15:24:02 +0000 Subject: [PATCH 01/43] initial version of reporter of gates Co-authored-by: Leila Wang Co-authored-by: Michael Connor --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 2 + .../dsl/acir_format/acir_format.cpp | 132 ++++++++++++++---- .../dsl/acir_format/acir_format.hpp | 38 ++++- .../acir_format/acir_to_constraint_buf.cpp | 78 ++++++++--- trickty/Nargo.toml | 7 + trickty/src/main.nr | 9 ++ trickty/target/trickty.json | 1 + 7 files changed, 221 insertions(+), 46 deletions(-) create mode 100644 trickty/Nargo.toml create mode 100644 trickty/src/main.nr create mode 100644 trickty/target/trickty.json diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 03cb6b24c44..7bf1f94d6f6 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -323,6 +323,8 @@ void gateCount(const std::string& bytecodePath, bool honk_recursion) constraint_system.num_acir_opcodes, ",\n \"circuit_size\": ", circuit_size, + ",\n \"gates_per_opcode\": ", + constraint_system.gates_per_opcode, "\n }"); // Attach a comma if we still circuit reports to generate diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index bee50d76742..2e683fec218 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -14,114 +14,192 @@ template class DSLBigInts; template void build_constraints(Builder& builder, - AcirFormat const& constraint_system, + AcirFormat& constraint_system, bool has_valid_witness_assignments, bool honk_recursion) { + constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); + size_t prev_gate_count = 0; + + auto compute_gate_diff = [&]() { + size_t new_gate_count = builder.get_total_circuit_size(); + size_t diff = new_gate_count - prev_gate_count; + prev_gate_count = new_gate_count; + return diff; + }; + // Add arithmetic gates - for (const auto& constraint : constraint_system.poly_triple_constraints) { + for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { + const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); + constraint_system.gates_per_opcode[constraint_system.poly_triple_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.quad_constraints) { + for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { + const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); + constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = compute_gate_diff(); } // Add logic constraint - for (const auto& constraint : constraint_system.logic_constraints) { + for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { + const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); + constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = compute_gate_diff(); } // Add range constraint - for (const auto& constraint : constraint_system.range_constraints) { + for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { + const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); + constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = compute_gate_diff(); } // Add aes128 constraints - for (const auto& constraint : constraint_system.aes128_constraints) { + for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { + const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.aes128_constraints_original_index[i]] = + compute_gate_diff(); } // Add sha256 constraints - for (const auto& constraint : constraint_system.sha256_constraints) { + for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { + const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.sha256_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.sha256_compression) { + for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { + const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = + compute_gate_diff(); } // Add schnorr constraints - for (const auto& constraint : constraint_system.schnorr_constraints) { + for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { + const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.schnorr_constraints_original_index[i]] = + compute_gate_diff(); } // Add ECDSA k1 constraints - for (const auto& constraint : constraint_system.ecdsa_k1_constraints) { + for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { + const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); + constraint_system.gates_per_opcode[constraint_system.ecdsa_k1_constraints_original_index[i]] = + compute_gate_diff(); } // Add ECDSA r1 constraints - for (const auto& constraint : constraint_system.ecdsa_r1_constraints) { + for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { + const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); + constraint_system.gates_per_opcode[constraint_system.ecdsa_r1_constraints_original_index[i]] = + compute_gate_diff(); } // Add blake2s constraints - for (const auto& constraint : constraint_system.blake2s_constraints) { + for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { + const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.blake2s_constraints_original_index[i]] = + compute_gate_diff(); } // Add blake3 constraints - for (const auto& constraint : constraint_system.blake3_constraints) { + for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { + const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.blake3_constraints_original_index[i]] = + compute_gate_diff(); } // Add keccak constraints - for (const auto& constraint : constraint_system.keccak_constraints) { + for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { + const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.keccak_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.keccak_permutations) { + for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { + const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.keccak_permutations_original_index[i]] = + compute_gate_diff(); } // Add pedersen constraints - for (const auto& constraint : constraint_system.pedersen_constraints) { + for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { + const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.pedersen_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.pedersen_hash_constraints) { + for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { + const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.pedersen_hash_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.poseidon2_constraints) { + for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { + const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.poseidon2_constraints_original_index[i]] = + compute_gate_diff(); } // Add multi scalar mul constraints - for (const auto& constraint : constraint_system.multi_scalar_mul_constraints) { + for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { + const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); + constraint_system.gates_per_opcode[constraint_system.multi_scalar_mul_constraints_original_index[i]] = + compute_gate_diff(); } // Add ec add constraints - for (const auto& constraint : constraint_system.ec_add_constraints) { + for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { + const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); + constraint_system.gates_per_opcode[constraint_system.ec_add_constraints_original_index[i]] = + compute_gate_diff(); } // Add block constraints - for (const auto& constraint : constraint_system.block_constraints) { + for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { + const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); + size_t delta_gates = compute_gate_diff(); + size_t avg_gates_per_opcode = delta_gates / constraint_system.block_constraints_indices[i].size(); + for (size_t opcode_index : constraint_system.block_constraints_indices[i]) { + constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; + } } // Add big_int constraints DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); - for (const auto& constraint : constraint_system.bigint_from_le_bytes_constraints) { + for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { + const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); + constraint_system.gates_per_opcode[constraint_system.bigint_from_le_bytes_constraints_original_index[i]] = + compute_gate_diff(); } - for (const auto& constraint : constraint_system.bigint_operations) { + for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { + const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); + constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = compute_gate_diff(); } - for (const auto& constraint : constraint_system.bigint_to_le_bytes_constraints) { + for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { + const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); + constraint_system.gates_per_opcode[constraint_system.bigint_to_le_bytes_constraints_original_index[i]] = + compute_gate_diff(); } // RecursionConstraint @@ -319,7 +397,7 @@ void build_constraints(Builder& builder, * @return Builder */ template <> -UltraCircuitBuilder create_circuit(const AcirFormat& constraint_system, +UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, size_t size_hint, WitnessVector const& witness, bool honk_recursion, @@ -345,7 +423,7 @@ UltraCircuitBuilder create_circuit(const AcirFormat& constraint_system, * @return Builder */ template <> -MegaCircuitBuilder create_circuit(const AcirFormat& constraint_system, +MegaCircuitBuilder create_circuit(AcirFormat& constraint_system, [[maybe_unused]] size_t size_hint, WitnessVector const& witness, bool honk_recursion, @@ -361,6 +439,6 @@ MegaCircuitBuilder create_circuit(const AcirFormat& constraint_system, return builder; }; -template void build_constraints(MegaCircuitBuilder&, AcirFormat const&, bool, bool); +template void build_constraints(MegaCircuitBuilder&, AcirFormat&, bool, bool); } // namespace acir_format diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 493cfe84061..bdcc25be859 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -39,27 +39,51 @@ struct AcirFormat { std::vector public_inputs; std::vector logic_constraints; + // the index (in the opcodes array) of each constraint in logic_constraints. + // maybe rename to : "opcode_index_of_logic_constraints" + std::vector logic_constraints_original_index; std::vector range_constraints; + std::vector range_constraints_original_index; std::vector aes128_constraints; + std::vector aes128_constraints_original_index; std::vector sha256_constraints; + std::vector sha256_constraints_original_index; std::vector sha256_compression; + std::vector sha256_compression_original_index; std::vector schnorr_constraints; + std::vector schnorr_constraints_original_index; std::vector ecdsa_k1_constraints; + std::vector ecdsa_k1_constraints_original_index; std::vector ecdsa_r1_constraints; + std::vector ecdsa_r1_constraints_original_index; std::vector blake2s_constraints; + std::vector blake2s_constraints_original_index; std::vector blake3_constraints; + std::vector blake3_constraints_original_index; std::vector keccak_constraints; + std::vector keccak_constraints_original_index; std::vector keccak_permutations; + std::vector keccak_permutations_original_index; std::vector pedersen_constraints; + std::vector pedersen_constraints_original_index; std::vector pedersen_hash_constraints; + std::vector pedersen_hash_constraints_original_index; std::vector poseidon2_constraints; + std::vector poseidon2_constraints_original_index; std::vector multi_scalar_mul_constraints; + std::vector multi_scalar_mul_constraints_original_index; std::vector ec_add_constraints; + std::vector ec_add_constraints_original_index; std::vector recursion_constraints; + std::vector recursion_constraints_original_index; std::vector honk_recursion_constraints; + std::vector honk_recursion_constraints_original_index; std::vector bigint_from_le_bytes_constraints; + std::vector bigint_from_le_bytes_constraints_original_index; std::vector bigint_to_le_bytes_constraints; + std::vector bigint_to_le_bytes_constraints_original_index; std::vector bigint_operations; + std::vector bigint_operations_original_index; // A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values // for q_M,q_L,q_R,q_O,q_C and indices of three variables taking the role of left, right and output wire @@ -67,10 +91,20 @@ struct AcirFormat { std::vector, bb::ContainerSlabAllocator>> poly_triple_constraints; + std::vector poly_triple_constraints_original_index; + std::vector, bb::ContainerSlabAllocator>> quad_constraints; + std::vector quad_constraints_original_index; + std::vector block_constraints; + // vec of blocks => vec of opcode indices: + std::vector> block_constraints_indices; + + // Number of gates added to the circuit per original opcode. + // Has the same length as the opcode vector. + std::vector gates_per_opcode; // For serialization, update with any new fields MSGPACK_FIELDS(varnum, @@ -142,7 +176,7 @@ struct AcirProgramStack { }; template -Builder create_circuit(const AcirFormat& constraint_system, +Builder create_circuit(AcirFormat& constraint_system, size_t size_hint = 0, WitnessVector const& witness = {}, bool honk_recursion = false, @@ -150,7 +184,7 @@ Builder create_circuit(const AcirFormat& constraint_system, template void build_constraints(Builder& builder, - AcirFormat const& constraint_system, + AcirFormat& constraint_system, bool has_valid_witness_assignments, bool honk_recursion = false); // honk_recursion means we will honk to recursively verify this // circuit. This distinction is needed to not add the default diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp index e292766d78f..288aad02a5a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp @@ -1,5 +1,8 @@ #include "acir_to_constraint_buf.hpp" #include "barretenberg/common/container.hpp" +#include +#include +#include #ifndef __wasm__ #include "barretenberg/bb/get_bytecode.hpp" #endif @@ -164,7 +167,7 @@ mul_quad_ serialize_mul_quad_gate(Program::Expression const& arg) return quad; } -void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af) +void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af, size_t opcode_index) { if (arg.value.linear_combinations.size() <= 3) { poly_triple pt = serialize_arithmetic_gate(arg.value); @@ -174,15 +177,22 @@ void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af) // gate instead. We could probably always use a width-4 gate in fact. if (pt == poly_triple{ 0, 0, 0, 0, 0, 0, 0, 0 }) { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); + af.quad_constraints_original_index.push_back(opcode_index); + } else { af.poly_triple_constraints.push_back(pt); + af.poly_triple_constraints_original_index.push_back(opcode_index); } } else { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); + af.quad_constraints_original_index.push_back(opcode_index); } } -void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, AcirFormat& af, bool honk_recursion) +void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, + AcirFormat& af, + bool honk_recursion, + size_t opcode_index) { std::visit( [&](auto&& arg) { @@ -195,6 +205,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .num_bits = arg.lhs.num_bits, .is_xor_gate = false, }); + af.logic_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.logic_constraints.push_back(LogicConstraint{ .a = arg.lhs.witness.value, @@ -203,11 +214,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .num_bits = arg.lhs.num_bits, .is_xor_gate = true, }); + af.logic_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.range_constraints.push_back(RangeConstraint{ .witness = arg.input.witness.value, .num_bits = arg.input.num_bits, }); + af.range_constraints_original_index.push_back(opcode_index); + } else if constexpr (std::is_same_v) { af.aes128_constraints.push_back(AES128Constraint{ .inputs = map(arg.inputs, @@ -233,6 +247,8 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci }), .outputs = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.aes128_constraints_original_index.push_back(opcode_index); + } else if constexpr (std::is_same_v) { af.sha256_constraints.push_back(Sha256Constraint{ .inputs = map(arg.inputs, @@ -244,6 +260,8 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.sha256_constraints_original_index.push_back(opcode_index); + } else if constexpr (std::is_same_v) { af.sha256_compression.push_back(Sha256Compression{ .inputs = map(arg.inputs, @@ -262,6 +280,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.sha256_compression_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.blake2s_constraints.push_back(Blake2sConstraint{ .inputs = map(arg.inputs, @@ -273,6 +292,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.blake2s_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.blake3_constraints.push_back(Blake3Constraint{ .inputs = map(arg.inputs, @@ -284,6 +304,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.blake3_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.schnorr_constraints.push_back(SchnorrConstraint{ .message = map(arg.message, [](auto& e) { return e.witness.value; }), @@ -292,6 +313,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); + af.schnorr_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.pedersen_constraints.push_back(PedersenConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), @@ -299,12 +321,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result_x = arg.outputs[0].value, .result_y = arg.outputs[1].value, }); + af.pedersen_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.pedersen_hash_constraints.push_back(PedersenHashConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), .hash_index = arg.domain_separator, .result = arg.output.value, }); + af.pedersen_hash_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ecdsa_k1_constraints.push_back(EcdsaSecp256k1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -313,6 +337,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .pub_y_indices = map(arg.public_key_y, [](auto& e) { return e.witness.value; }), .result = arg.output.value, }); + af.ecdsa_k1_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ecdsa_r1_constraints.push_back(EcdsaSecp256r1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -321,6 +346,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); + af.ecdsa_r1_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.multi_scalar_mul_constraints.push_back(MultiScalarMul{ .points = map(arg.points, [](auto& e) { return e.witness.value; }), @@ -329,6 +355,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .out_point_y = arg.outputs[1].value, .out_point_is_infinite = arg.outputs[2].value, }); + af.multi_scalar_mul_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ec_add_constraints.push_back(EcAdd{ .input1_x = arg.input1[0].witness.value, @@ -341,6 +368,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result_y = arg.outputs[1].value, .result_infinite = arg.outputs[2].value, }); + af.ec_add_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.keccak_constraints.push_back(KeccakConstraint{ .inputs = map(arg.inputs, @@ -353,11 +381,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = map(arg.outputs, [](auto& e) { return e.value; }), .var_message_size = arg.var_message_size.witness.value, }); + af.keccak_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.keccak_permutations.push_back(Keccakf1600{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.keccak_permutations_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { if (honk_recursion) { // if we're using the honk recursive verifier auto c = HonkRecursionConstraint{ @@ -366,6 +396,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .public_inputs = map(arg.public_inputs, [](auto& e) { return e.witness.value; }), }; af.honk_recursion_constraints.push_back(c); + af.honk_recursion_constraints_original_index.push_back(opcode_index); } else { auto c = RecursionConstraint{ .key = map(arg.verification_key, [](auto& e) { return e.witness.value; }), @@ -374,6 +405,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .key_hash = arg.key_hash.witness.value, }; af.recursion_constraints.push_back(c); + af.recursion_constraints_original_index.push_back(opcode_index); } } else if constexpr (std::is_same_v) { af.bigint_from_le_bytes_constraints.push_back(BigIntFromLeBytes{ @@ -381,11 +413,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .modulus = map(arg.modulus, [](auto& e) -> uint32_t { return e; }), .result = arg.output, }); + af.bigint_from_le_bytes_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_to_le_bytes_constraints.push_back(BigIntToLeBytes{ .input = arg.input, .result = map(arg.outputs, [](auto& e) { return e.value; }), }); + af.bigint_to_le_bytes_constraints_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -393,6 +427,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output, .opcode = BigIntOperationType::Add, }); + af.bigint_operations_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -400,6 +435,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output, .opcode = BigIntOperationType::Sub, }); + af.bigint_operations_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -407,6 +443,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output, .opcode = BigIntOperationType::Mul, }); + af.bigint_operations_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -414,12 +451,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci .result = arg.output, .opcode = BigIntOperationType::Div, }); + af.bigint_operations_original_index.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.poseidon2_constraints.push_back(Poseidon2Constraint{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), .len = arg.len, }); + af.poseidon2_constraints_original_index.push_back(opcode_index); } }, arg.value.value); @@ -445,7 +484,8 @@ BlockConstraint handle_memory_init(Program::Opcode::MemoryInit const& mem_init) }); } - // Databus is only supported for Goblin, non Goblin builders will treat call_data and return_data as normal array. + // Databus is only supported for Goblin, non Goblin builders will treat call_data and return_data as normal + // array. if (IsMegaBuilder) { if (std::holds_alternative(mem_init.block_type.value)) { block.type = BlockType::CallData; @@ -490,32 +530,36 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit, bool ho 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; - for (auto gate : circuit.opcodes) { + // Map to a pair of: BlockConstraint, and list of opcodes associated with that BlockConstraint + std::map>> block_id_to_block_constraint; + for (size_t i = 0; i < circuit.opcodes.size(); ++i) { + auto gate = circuit.opcodes[i]; std::visit( [&](auto&& arg) { using T = std::decay_t; if constexpr (std::is_same_v) { - handle_arithmetic(arg, af); + handle_arithmetic(arg, af, i); } else if constexpr (std::is_same_v) { - handle_blackbox_func_call(arg, af, honk_recursion); + handle_blackbox_func_call(arg, af, honk_recursion, i); } else if constexpr (std::is_same_v) { auto block = handle_memory_init(arg); uint32_t block_id = arg.block_id.value; - block_id_to_block_constraint[block_id] = block; + block_id_to_block_constraint[block_id] = std::make_pair(block, std::vector()); } else if constexpr (std::is_same_v) { auto block = block_id_to_block_constraint.find(arg.block_id.value); if (block == block_id_to_block_constraint.end()) { throw_or_abort("unitialized MemoryOp"); } - handle_memory_op(arg, block->second); + handle_memory_op(arg, block->second.first); + block->second.second.push_back(i); } }, gate.value); } for (const auto& [block_id, block] : block_id_to_block_constraint) { - if (!block.trace.empty()) { - af.block_constraints.push_back(block); + if (!block.first.trace.empty()) { + af.block_constraints.push_back(block.first); + af.block_constraints_indices.push_back(block.second); } } return af; @@ -523,9 +567,9 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit, bool ho AcirFormat circuit_buf_to_acir_format(std::vector const& buf, bool honk_recursion) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/927): Move to using just `program_buf_to_acir_format` - // once Honk fully supports all ACIR test flows - // For now the backend still expects to work with a single ACIR function + // TODO(https://github.com/AztecProtocol/barretenberg/issues/927): Move to using just + // `program_buf_to_acir_format` once Honk fully supports all ACIR test flows For now the backend still expects + // to work with a single ACIR function auto circuit = Program::Program::bincodeDeserialize(buf).functions[0]; return circuit_serde_to_acir_format(circuit, honk_recursion); @@ -567,9 +611,9 @@ WitnessVector witness_map_to_witness_vector(WitnessStack::WitnessMap const& witn */ WitnessVector witness_buf_to_witness_data(std::vector const& buf) { - // TODO(https://github.com/AztecProtocol/barretenberg/issues/927): Move to using just `witness_buf_to_witness_stack` - // once Honk fully supports all ACIR test flows. - // For now the backend still expects to work with the stop of the `WitnessStack`. + // TODO(https://github.com/AztecProtocol/barretenberg/issues/927): Move to using just + // `witness_buf_to_witness_stack` once Honk fully supports all ACIR test flows. For now the backend still + // expects to work with the stop of the `WitnessStack`. auto witness_stack = WitnessStack::WitnessStack::bincodeDeserialize(buf); auto w = witness_stack.stack[witness_stack.stack.size() - 1].witness; diff --git a/trickty/Nargo.toml b/trickty/Nargo.toml new file mode 100644 index 00000000000..4c2af8c81b2 --- /dev/null +++ b/trickty/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "trickty" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] \ No newline at end of file diff --git a/trickty/src/main.nr b/trickty/src/main.nr new file mode 100644 index 00000000000..58b75f10ad9 --- /dev/null +++ b/trickty/src/main.nr @@ -0,0 +1,9 @@ +fn main(x: Field) { + let mut acc = x; + for i in 0..100 { + acc = dep::std::hash::pedersen_hash([acc]); + } + assert_eq(acc * 2, 28); + assert_eq(acc * 3, 42); +} + diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json new file mode 100644 index 00000000000..e4b0103f464 --- /dev/null +++ b/trickty/target/trickty.json @@ -0,0 +1 @@ +{"noir_version":"0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file From 14aa792f17649ff483ff64c1321c58b37ff601d9 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Fri, 7 Jun 2024 13:52:31 +0000 Subject: [PATCH 02/43] amortising table gates over all opcodes which use the table... ish --- .../dsl/acir_format/acir_format.cpp | 135 ++++++++++++++---- trickty/target/trickty.json | 2 +- 2 files changed, 108 insertions(+), 29 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 2e683fec218..22db381c9b7 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -1,5 +1,6 @@ #include "acir_format.hpp" #include "barretenberg/common/log.hpp" +#include "barretenberg/common/throw_or_abort.hpp" #include "barretenberg/stdlib/primitives/field/field_conversion.hpp" #include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp" #include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" @@ -20,161 +21,211 @@ void build_constraints(Builder& builder, { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); size_t prev_gate_count = 0; + size_t prev_table_size = 0; - auto compute_gate_diff = [&]() { + auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { size_t new_gate_count = builder.get_total_circuit_size(); - size_t diff = new_gate_count - prev_gate_count; + size_t new_table_size = builder.get_tables_size(); + size_t gate_diff = new_gate_count - prev_gate_count; + size_t table_diff = new_table_size - prev_table_size; + + if (table_diff > 0 && table_size_of_this_type > 0) { + throw_or_abort( + "My current understanding is that tables shouldn't grow in size if the opcode has already been " + "encountered earlier."); + } + + if (gate_diff < table_diff) { + throw_or_abort("Unexpected error"); + } + + table_size_of_this_type += table_diff; + size_t amortised_gate_diff = + (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type); + prev_gate_count = new_gate_count; - return diff; + prev_table_size = new_table_size; + return amortised_gate_diff; }; // Add arithmetic gates + size_t table_size_for_poly_triple_constraints = 0; for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); constraint_system.gates_per_opcode[constraint_system.poly_triple_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.poly_triple_constraints.size(), table_size_for_poly_triple_constraints); } + + size_t table_size_for_quad_constraints = 0; for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = + compute_gate_diff(constraint_system.quad_constraints.size(), table_size_for_quad_constraints); } // Add logic constraint + size_t table_size_for_logic_constraints = 0; for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = + compute_gate_diff(constraint_system.logic_constraints.size(), table_size_for_logic_constraints); } // Add range constraint + size_t table_size_for_range_constraints = 0; for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = + compute_gate_diff(constraint_system.range_constraints.size(), table_size_for_range_constraints); } // Add aes128 constraints + size_t table_size_for_aes128_constraints = 0; for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.aes128_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.aes128_constraints.size(), table_size_for_aes128_constraints); } // Add sha256 constraints + size_t table_size_for_sha256_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.sha256_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.sha256_constraints.size(), table_size_for_sha256_constraints); } + + size_t table_size_for_sha256_compression_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = - compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = compute_gate_diff( + constraint_system.sha256_compression.size(), table_size_for_sha256_compression_constraints); } // Add schnorr constraints + size_t table_size_for_schnorr_constraints = 0; for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.schnorr_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.schnorr_constraints.size(), table_size_for_schnorr_constraints); } // Add ECDSA k1 constraints + size_t table_size_for_ecdsa_k1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_k1_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ecdsa_k1_constraints.size(), table_size_for_ecdsa_k1_constraints); } // Add ECDSA r1 constraints + size_t table_size_for_ecdsa_r1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_r1_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ecdsa_r1_constraints.size(), table_size_for_ecdsa_r1_constraints); } // Add blake2s constraints + size_t table_size_for_blake2s_constraints = 0; for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake2s_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.blake2s_constraints.size(), table_size_for_blake2s_constraints); } // Add blake3 constraints + size_t table_size_for_blake3_constraints = 0; for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake3_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.blake3_constraints.size(), table_size_for_blake3_constraints); } // Add keccak constraints + size_t table_size_for_keccak_constraints = 0; for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.keccak_constraints.size(), table_size_for_keccak_constraints); } + + size_t table_size_for_keccak_permutations = 0; for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_permutations_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.keccak_permutations.size(), table_size_for_keccak_permutations); } // Add pedersen constraints + size_t table_size_for_pedersen_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.pedersen_constraints.size(), table_size_for_pedersen_constraints); } + size_t table_size_for_pedersen_hash_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_hash_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.pedersen_hash_constraints.size(), + table_size_for_pedersen_hash_constraints); } + size_t table_size_for_poseidon2_constraints = 0; for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.poseidon2_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.poseidon2_constraints.size(), table_size_for_poseidon2_constraints); } // Add multi scalar mul constraints + size_t table_size_for_multi_scalar_mul_constraints = 0; for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.multi_scalar_mul_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.multi_scalar_mul_constraints.size(), + table_size_for_multi_scalar_mul_constraints); } // Add ec add constraints + size_t table_size_for_ec_add_constraints = 0; for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ec_add_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ec_add_constraints.size(), table_size_for_ec_add_constraints); } // Add block constraints - for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { + size_t block_constraints_size = constraint_system.block_constraints.size(); + std::vector table_sizes_for_block_constraints(block_constraints_size, size_t(0)); + for (size_t i = 0; i < block_constraints_size; ++i) { const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); - size_t delta_gates = compute_gate_diff(); + // Note sure about taking the size of `init`, like this. Got confused with all the vectors. + size_t delta_gates = + compute_gate_diff(constraint_system.block_constraints[i].init.size(), table_sizes_for_block_constraints[i]); size_t avg_gates_per_opcode = delta_gates / constraint_system.block_constraints_indices[i].size(); for (size_t opcode_index : constraint_system.block_constraints_indices[i]) { constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; @@ -184,22 +235,30 @@ void build_constraints(Builder& builder, // Add big_int constraints DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); + size_t table_size_for_bigint_from_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_from_le_bytes_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.bigint_from_le_bytes_constraints.size(), + table_size_for_bigint_from_le_bytes_constraints); } + + size_t table_size_for_bigint_operations = 0; for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = + compute_gate_diff(constraint_system.bigint_operations.size(), table_size_for_bigint_operations); } + + size_t table_size_for_bigint_to_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_to_le_bytes_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.bigint_to_le_bytes_constraints.size(), + table_size_for_bigint_to_le_bytes_constraints); } // RecursionConstraint @@ -230,6 +289,7 @@ void build_constraints(Builder& builder, auto proof_size_no_pub_inputs = recursion_proof_size_without_public_inputs(); // Add recursion constraints + size_t table_size_for_recursion_constraints = 0; for (auto constraint : constraint_system.recursion_constraints) { // A proof passed into the constraint should be stripped of its public inputs, except in the case where a // proof contains an aggregation object itself. We refer to this as the `nested_aggregation_object`. The @@ -265,12 +325,15 @@ void build_constraints(Builder& builder, constraint.proof.begin() + static_cast(RecursionConstraint::AGGREGATION_OBJECT_SIZE)); } + current_output_aggregation_object = create_recursion_constraints(builder, constraint, current_input_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; + + compute_gate_diff(constraint_system.recursion_constraints.size(), table_size_for_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -310,6 +373,7 @@ void build_constraints(Builder& builder, }; // Add recursion constraints + size_t table_size_for_honk_recursion_constraints = 0; for (auto constraint : constraint_system.honk_recursion_constraints) { // A proof passed into the constraint should be stripped of its inner public inputs, but not the nested // aggregation object itself. The verifier circuit requires that the indices to a nested proof aggregation @@ -334,6 +398,9 @@ void build_constraints(Builder& builder, current_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); + + compute_gate_diff(constraint_system.honk_recursion_constraints.size(), + table_size_for_honk_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -410,6 +477,18 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, bool has_valid_witness_assignments = !witness.empty(); build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); + // I was doing this to check whether the final gate count matches the sum of the amortised gate counts. It doesn't. + // size_t sum = 0; + // for (size_t i : constraint_system.gates_per_opcode) { + // sum += i; + // } + // // if (builder.get_total_circuit_size() != sum) { + // // throw_or_abort("Badly counted num gates!!"); + // // } + // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 1] = + // builder.get_total_circuit_size(); + // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 2] = sum; + return builder; }; diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json index e4b0103f464..8afcf98ed9c 100644 --- a/trickty/target/trickty.json +++ b/trickty/target/trickty.json @@ -1 +1 @@ -{"noir_version":"0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file +{"noir_version":"0.30.0+f6d65afe6820d0d6e55ecae3bf8693b5ad9a9c48","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/mike/packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file From 455dcbb73900b9568135813e676e84b492fdb40a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 7 Jun 2024 13:55:20 +0000 Subject: [PATCH 03/43] feat: flamegraph generator --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 14 +- noir/noir-repo/Cargo.lock | 58 +++++- noir/noir-repo/Cargo.toml | 1 + .../tooling/nargo/src/artifacts/program.rs | 31 +++ .../tooling/noir_flamegraph/Cargo.toml | 47 +++++ .../src/cli/create_flamegraph_cmd.rs | 186 ++++++++++++++++++ .../tooling/noir_flamegraph/src/cli/mod.rs | 41 ++++ .../tooling/noir_flamegraph/src/main.rs | 35 ++++ 8 files changed, 407 insertions(+), 6 deletions(-) create mode 100644 noir/noir-repo/tooling/noir_flamegraph/Cargo.toml create mode 100644 noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs create mode 100644 noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs create mode 100644 noir/noir-repo/tooling/noir_flamegraph/src/main.rs diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 7bf1f94d6f6..fd023e81ee2 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -319,13 +319,21 @@ void gateCount(const std::string& bytecodePath, bool honk_recursion) auto circuit_size = acir_composer.get_total_circuit_size(); // Build individual circuit report + std::string gates_per_opcode_str; + for (size_t j = 0; j < constraint_system.gates_per_opcode.size(); j++) { + gates_per_opcode_str += std::to_string(constraint_system.gates_per_opcode[j]); + if (j != constraint_system.gates_per_opcode.size() - 1) { + gates_per_opcode_str += ","; + } + } + auto result_string = format("{\n \"acir_opcodes\": ", constraint_system.num_acir_opcodes, ",\n \"circuit_size\": ", circuit_size, - ",\n \"gates_per_opcode\": ", - constraint_system.gates_per_opcode, - "\n }"); + ",\n \"gates_per_opcode\": [", + gates_per_opcode_str, + "]\n }"); // Attach a comma if we still circuit reports to generate if (i != (constraint_systems.size() - 1)) { diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index 4b140a513f2..e6a88f36ebd 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -1185,6 +1185,19 @@ dependencies = [ "syn 2.0.64", ] +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if 1.0.0", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core 0.9.8", +] + [[package]] name = "debugid" version = "0.8.0" @@ -1381,6 +1394,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -2016,12 +2038,17 @@ dependencies = [ [[package]] name = "inferno" -version = "0.11.15" +version = "0.11.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fb7c1b80a1dfa604bb4a649a5c5aeef3d913f7c520cb42b40e534e8a61bcdfc" +checksum = "321f0f839cd44a4686e9504b0a62b4d69a50b62072144c71c68f5873c167b8d9" dependencies = [ "ahash 0.8.11", - "indexmap 1.9.3", + "clap", + "crossbeam-channel", + "crossbeam-utils", + "dashmap", + "env_logger", + "indexmap 2.2.6", "is-terminal", "itoa", "log", @@ -2658,6 +2685,31 @@ dependencies = [ "thiserror", ] +[[package]] +name = "noir_flamegraph" +version = "0.40.0" +dependencies = [ + "acir", + "clap", + "codespan-reporting", + "color-eyre", + "const_format", + "fm", + "im", + "inferno", + "nargo", + "noirc_frontend", + "paste", + "proptest", + "rand 0.8.5", + "serde", + "serde_json", + "thiserror", + "toml 0.7.6", + "tracing-appender", + "tracing-subscriber", +] + [[package]] name = "noir_grumpkin" version = "0.1.0" diff --git a/noir/noir-repo/Cargo.toml b/noir/noir-repo/Cargo.toml index 182580f8d67..9bbcd17f533 100644 --- a/noir/noir-repo/Cargo.toml +++ b/noir/noir-repo/Cargo.toml @@ -22,6 +22,7 @@ members = [ "tooling/noirc_abi", "tooling/noirc_abi_wasm", "tooling/acvm_cli", + "tooling/noir_flamegraph", # ACVM "acvm-repo/acir_field", "acvm-repo/acir", diff --git a/noir/noir-repo/tooling/nargo/src/artifacts/program.rs b/noir/noir-repo/tooling/nargo/src/artifacts/program.rs index 91f02157414..fb723592d06 100644 --- a/noir/noir-repo/tooling/nargo/src/artifacts/program.rs +++ b/noir/noir-repo/tooling/nargo/src/artifacts/program.rs @@ -2,12 +2,15 @@ use std::collections::BTreeMap; use acvm::acir::circuit::Program; use acvm::FieldElement; +use codespan_reporting::files::{Error, Files, SimpleFile}; use fm::FileId; +use fm::PathString; use noirc_abi::Abi; use noirc_driver::CompiledProgram; use noirc_driver::DebugFile; use noirc_errors::debug_info::ProgramDebugInfo; use serde::{Deserialize, Serialize}; +use std::ops::Range; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct ProgramArtifact { @@ -67,3 +70,31 @@ impl From for CompiledProgram { } } } + +impl<'a> Files<'a> for ProgramArtifact { + type FileId = FileId; + type Name = PathString; + type Source = &'a str; + + fn name(&self, file_id: Self::FileId) -> Result { + self.file_map.get(&file_id).ok_or(Error::FileMissing).map(|file| file.path.clone().into()) + } + + fn source(&'a self, file_id: Self::FileId) -> Result { + self.file_map.get(&file_id).ok_or(Error::FileMissing).map(|file| file.source.as_ref()) + } + + fn line_index(&self, file_id: Self::FileId, byte_index: usize) -> Result { + self.file_map.get(&file_id).ok_or(Error::FileMissing).and_then(|file| { + SimpleFile::new(PathString::from(file.path.clone()), file.source.clone()) + .line_index((), byte_index) + }) + } + + fn line_range(&self, file_id: Self::FileId, line_index: usize) -> Result, Error> { + self.file_map.get(&file_id).ok_or(Error::FileMissing).and_then(|file| { + SimpleFile::new(PathString::from(file.path.clone()), file.source.clone()) + .line_range((), line_index) + }) + } +} diff --git a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml new file mode 100644 index 00000000000..6ef107df341 --- /dev/null +++ b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml @@ -0,0 +1,47 @@ +[package] +name = "noir_flamegraph" +description = "The hackity hack hack hack" +# x-release-please-start-version +version = "0.40.0" +# x-release-please-end +authors.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "noir_flamegraph" +path = "src/main.rs" + +[dependencies] +thiserror.workspace = true +toml.workspace = true +color-eyre = "0.6.2" +clap.workspace = true +nargo.workspace = true +const_format.workspace = true +serde.workspace = true +serde_json.workspace = true +fm.workspace = true +codespan-reporting.workspace = true +inferno = "0.11.19" +im.workspace = true +noirc_frontend.workspace = true +acir.workspace = true + +# Logs +tracing-subscriber.workspace = true +tracing-appender = "0.2.3" + +[dev-dependencies] +rand = "0.8.5" +proptest = "1.2.0" +paste = "1.0.14" + + +[features] +default = ["bn254"] +bn254 = ["noirc_frontend/bn254", "acir/bn254"] diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs new file mode 100644 index 00000000000..8faf3222dc5 --- /dev/null +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -0,0 +1,186 @@ +use acir::circuit::OpcodeLocation; +use clap::Args; +use codespan_reporting::files::Files; +use nargo::errors::Location; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::io::BufWriter; +use std::path::Path; +use std::process::Command; + +use inferno::flamegraph::{from_lines, Options}; +use nargo::artifacts::program::ProgramArtifact; + +#[derive(Debug, Clone, Args)] +pub(crate) struct CreateFlamegraphCommand { + /// The path to the artifact file + #[clap(long, short)] + artifact_path: String, + + /// Path to the binary of noir's backend + #[clap(long, short)] + backend_path: String, + + /// The output file for the flamegraph + #[clap(long, short)] + output: String, +} + +pub(crate) fn read_program_from_file>( + circuit_path: P, +) -> Result { + let file_path = circuit_path.as_ref().with_extension("json"); + + let input_string = std::fs::read(&file_path).map_err(|err| err.to_string())?; + let program = serde_json::from_slice(&input_string).map_err(|err| err.to_string())?; + + Ok(program) +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct BBGatesReport { + acir_opcodes: usize, + circuit_size: usize, + gates_per_opcode: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct BBGatesResponse { + functions: Vec, +} + +fn read_location<'files>( + location: Location, + files: &'files impl Files<'files, FileId = fm::FileId>, +) -> String { + let path = files.name(location.file).expect("should get file path"); + let source = files.source(location.file).expect("should get file source"); + + let code_slice = source + .as_ref() + .chars() + .skip(location.span.start() as usize) + .take(location.span.end() as usize - location.span.start() as usize) + .collect::(); + + let (line, column) = line_and_column_from_span(source.as_ref(), location.span.start()); + + format!("{}", code_slice) +} + +fn line_and_column_from_span(source: &str, span_start: u32) -> (u32, u32) { + let mut line = 1; + let mut column = 0; + + for (i, char) in source.chars().enumerate() { + column += 1; + + if char == '\n' { + line += 1; + column = 0; + } + + if span_start <= i as u32 { + break; + } + } + + (line, column) +} + +struct FoldedStackItem { + total_gates: usize, + nested_items: HashMap, +} + +fn add_locations_to_folded_stack_items( + stack_items: &mut HashMap, + locations: Vec, + gates: usize, +) { + let mut child_map = stack_items; + for location in locations { + let current_item = child_map + .entry(location) + .or_insert(FoldedStackItem { total_gates: 0, nested_items: HashMap::new() }); + current_item.total_gates += gates; + child_map = &mut current_item.nested_items; + } +} + +fn to_folded_lines( + folded_stack_items: &HashMap, + parent_stacks: im::Vector, +) -> Vec { + folded_stack_items + .iter() + .flat_map(move |(location, folded_stack_item)| { + let frame_list: Vec = + std::iter::once(location.clone()).chain(parent_stacks.clone()).collect(); + let line: String = + format!("{} {}", frame_list.join(";"), folded_stack_item.total_gates); + + let mut new_parent_stacks = parent_stacks.clone(); + new_parent_stacks.push_back(location.clone()); + + let child_lines: Vec = + to_folded_lines(&folded_stack_item.nested_items, new_parent_stacks); + + std::iter::once(line).chain(child_lines.into_iter()) + }) + .collect() +} + +pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { + let program = read_program_from_file(&args.artifact_path)?; + // println!("Got program"); + let bb_gates_response = Command::new(args.backend_path) + .arg("gates") + .arg("-b") + .arg(args.artifact_path) + .output() + .expect("failed to execute process"); + + // println!("BB gates response {}", String::from_utf8(bb_gates_response.stdout.clone()).unwrap()); + // Parse the bb gates stdout as json + let bb_gates_response: BBGatesResponse = + serde_json::from_slice(&bb_gates_response.stdout).map_err(|err| err.to_string())?; + + // Consume first + let gates: BBGatesReport = bb_gates_response.functions.into_iter().next().unwrap(); + + let mut folded_stack_items = HashMap::new(); + + gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { + let call_stack = &program.debug_symbols.debug_infos[0] + .locations + .get(&OpcodeLocation::Acir(opcode_index)); + let location_names = if let Some(call_stack) = call_stack { + call_stack + .iter() + .map(|location| read_location(*location, &program)) + .collect::>() + } else { + vec!["unknown".to_string()] + }; + + add_locations_to_folded_stack_items(&mut folded_stack_items, location_names, gates); + }); + + // println!("writing flamegraph to {:?}", args.output); + let flamegraph_file = std::fs::File::create(&args.output).map_err(|err| err.to_string())?; + + let flamegraph_writer = BufWriter::new(flamegraph_file); + + let folded_lines = to_folded_lines(&folded_stack_items, Default::default()); + // println!("folded lines {}", folded_lines.join("\n")); + + from_lines( + &mut Options::default(), + folded_lines.iter().map(|as_string| as_string.as_str()), + flamegraph_writer, + ) + .map_err(|err| err.to_string())?; + + Ok(()) +} diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs new file mode 100644 index 00000000000..26d187d7a95 --- /dev/null +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs @@ -0,0 +1,41 @@ +use clap::{Parser, Subcommand}; +use color_eyre::eyre; +use const_format::formatcp; + +mod create_flamegraph_cmd; + +const ACVM_VERSION: &str = env!("CARGO_PKG_VERSION"); + +static VERSION_STRING: &str = formatcp!("version = {}\n", ACVM_VERSION,); + +#[derive(Parser, Debug)] +#[command(name="acvm", author, version=VERSION_STRING, about, long_about = None)] +struct CreateFlamegraphCli { + #[command(subcommand)] + command: CreateFlamegraphCommand, +} + +#[non_exhaustive] +#[derive(Subcommand, Clone, Debug)] +enum CreateFlamegraphCommand { + CreateFlamegraph(create_flamegraph_cmd::CreateFlamegraphCommand), +} + +#[cfg(not(feature = "codegen-docs"))] +pub(crate) fn start_cli() -> eyre::Result<()> { + let CreateFlamegraphCli { command } = CreateFlamegraphCli::parse(); + + match command { + CreateFlamegraphCommand::CreateFlamegraph(args) => create_flamegraph_cmd::run(args), + } + .map_err(|err| eyre::eyre!("{}", err))?; + + Ok(()) +} + +#[cfg(feature = "codegen-docs")] +pub(crate) fn start_cli() -> eyre::Result<()> { + let markdown: String = clap_markdown::help_markdown::(); + println!("{markdown}"); + Ok(()) +} diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/main.rs b/noir/noir-repo/tooling/noir_flamegraph/src/main.rs new file mode 100644 index 00000000000..9ad3d694003 --- /dev/null +++ b/noir/noir-repo/tooling/noir_flamegraph/src/main.rs @@ -0,0 +1,35 @@ +#![forbid(unsafe_code)] +#![warn(unreachable_pub)] +#![warn(clippy::semicolon_if_nothing_returned)] +#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] + +mod cli; + +use std::env; + +use tracing_appender::rolling; +use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; + +fn main() { + // Setup tracing + if let Ok(log_dir) = env::var("ACVM_LOG_DIR") { + let debug_file = rolling::daily(log_dir, "acvm-log"); + tracing_subscriber::fmt() + .with_span_events(FmtSpan::ACTIVE) + .with_writer(debug_file) + .with_ansi(false) + .with_env_filter(EnvFilter::from_default_env()) + .init(); + } else { + tracing_subscriber::fmt() + .with_span_events(FmtSpan::ACTIVE) + .with_ansi(true) + .with_env_filter(EnvFilter::from_env("NOIR_LOG")) + .init(); + } + + if let Err(report) = cli::start_cli() { + eprintln!("{report}"); + std::process::exit(1); + } +} From 7a20a944556c3cef86e5f45f2f8a93b03e7ce24a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 7 Jun 2024 14:13:21 +0000 Subject: [PATCH 04/43] Revert "amortising table gates over all opcodes which use the table... ish" This reverts commit 14aa792f17649ff483ff64c1321c58b37ff601d9. --- .../dsl/acir_format/acir_format.cpp | 135 ++++-------------- trickty/target/trickty.json | 2 +- 2 files changed, 29 insertions(+), 108 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 22db381c9b7..2e683fec218 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -1,6 +1,5 @@ #include "acir_format.hpp" #include "barretenberg/common/log.hpp" -#include "barretenberg/common/throw_or_abort.hpp" #include "barretenberg/stdlib/primitives/field/field_conversion.hpp" #include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp" #include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" @@ -21,211 +20,161 @@ void build_constraints(Builder& builder, { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); size_t prev_gate_count = 0; - size_t prev_table_size = 0; - auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { + auto compute_gate_diff = [&]() { size_t new_gate_count = builder.get_total_circuit_size(); - size_t new_table_size = builder.get_tables_size(); - size_t gate_diff = new_gate_count - prev_gate_count; - size_t table_diff = new_table_size - prev_table_size; - - if (table_diff > 0 && table_size_of_this_type > 0) { - throw_or_abort( - "My current understanding is that tables shouldn't grow in size if the opcode has already been " - "encountered earlier."); - } - - if (gate_diff < table_diff) { - throw_or_abort("Unexpected error"); - } - - table_size_of_this_type += table_diff; - size_t amortised_gate_diff = - (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type); - + size_t diff = new_gate_count - prev_gate_count; prev_gate_count = new_gate_count; - prev_table_size = new_table_size; - return amortised_gate_diff; + return diff; }; // Add arithmetic gates - size_t table_size_for_poly_triple_constraints = 0; for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); constraint_system.gates_per_opcode[constraint_system.poly_triple_constraints_original_index[i]] = - compute_gate_diff(constraint_system.poly_triple_constraints.size(), table_size_for_poly_triple_constraints); + compute_gate_diff(); } - - size_t table_size_for_quad_constraints = 0; for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = - compute_gate_diff(constraint_system.quad_constraints.size(), table_size_for_quad_constraints); + constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = compute_gate_diff(); } // Add logic constraint - size_t table_size_for_logic_constraints = 0; for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = - compute_gate_diff(constraint_system.logic_constraints.size(), table_size_for_logic_constraints); + constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = compute_gate_diff(); } // Add range constraint - size_t table_size_for_range_constraints = 0; for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = - compute_gate_diff(constraint_system.range_constraints.size(), table_size_for_range_constraints); + constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = compute_gate_diff(); } // Add aes128 constraints - size_t table_size_for_aes128_constraints = 0; for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.aes128_constraints_original_index[i]] = - compute_gate_diff(constraint_system.aes128_constraints.size(), table_size_for_aes128_constraints); + compute_gate_diff(); } // Add sha256 constraints - size_t table_size_for_sha256_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.sha256_constraints_original_index[i]] = - compute_gate_diff(constraint_system.sha256_constraints.size(), table_size_for_sha256_constraints); + compute_gate_diff(); } - - size_t table_size_for_sha256_compression_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = compute_gate_diff( - constraint_system.sha256_compression.size(), table_size_for_sha256_compression_constraints); + constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = + compute_gate_diff(); } // Add schnorr constraints - size_t table_size_for_schnorr_constraints = 0; for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.schnorr_constraints_original_index[i]] = - compute_gate_diff(constraint_system.schnorr_constraints.size(), table_size_for_schnorr_constraints); + compute_gate_diff(); } // Add ECDSA k1 constraints - size_t table_size_for_ecdsa_k1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_k1_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ecdsa_k1_constraints.size(), table_size_for_ecdsa_k1_constraints); + compute_gate_diff(); } // Add ECDSA r1 constraints - size_t table_size_for_ecdsa_r1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_r1_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ecdsa_r1_constraints.size(), table_size_for_ecdsa_r1_constraints); + compute_gate_diff(); } // Add blake2s constraints - size_t table_size_for_blake2s_constraints = 0; for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake2s_constraints_original_index[i]] = - compute_gate_diff(constraint_system.blake2s_constraints.size(), table_size_for_blake2s_constraints); + compute_gate_diff(); } // Add blake3 constraints - size_t table_size_for_blake3_constraints = 0; for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake3_constraints_original_index[i]] = - compute_gate_diff(constraint_system.blake3_constraints.size(), table_size_for_blake3_constraints); + compute_gate_diff(); } // Add keccak constraints - size_t table_size_for_keccak_constraints = 0; for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_constraints_original_index[i]] = - compute_gate_diff(constraint_system.keccak_constraints.size(), table_size_for_keccak_constraints); + compute_gate_diff(); } - - size_t table_size_for_keccak_permutations = 0; for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_permutations_original_index[i]] = - compute_gate_diff(constraint_system.keccak_permutations.size(), table_size_for_keccak_permutations); + compute_gate_diff(); } // Add pedersen constraints - size_t table_size_for_pedersen_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_constraints_original_index[i]] = - compute_gate_diff(constraint_system.pedersen_constraints.size(), table_size_for_pedersen_constraints); + compute_gate_diff(); } - size_t table_size_for_pedersen_hash_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_hash_constraints_original_index[i]] = - compute_gate_diff(constraint_system.pedersen_hash_constraints.size(), - table_size_for_pedersen_hash_constraints); + compute_gate_diff(); } - size_t table_size_for_poseidon2_constraints = 0; for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.poseidon2_constraints_original_index[i]] = - compute_gate_diff(constraint_system.poseidon2_constraints.size(), table_size_for_poseidon2_constraints); + compute_gate_diff(); } // Add multi scalar mul constraints - size_t table_size_for_multi_scalar_mul_constraints = 0; for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.multi_scalar_mul_constraints_original_index[i]] = - compute_gate_diff(constraint_system.multi_scalar_mul_constraints.size(), - table_size_for_multi_scalar_mul_constraints); + compute_gate_diff(); } // Add ec add constraints - size_t table_size_for_ec_add_constraints = 0; for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ec_add_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ec_add_constraints.size(), table_size_for_ec_add_constraints); + compute_gate_diff(); } // Add block constraints - size_t block_constraints_size = constraint_system.block_constraints.size(); - std::vector table_sizes_for_block_constraints(block_constraints_size, size_t(0)); - for (size_t i = 0; i < block_constraints_size; ++i) { + for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); - // Note sure about taking the size of `init`, like this. Got confused with all the vectors. - size_t delta_gates = - compute_gate_diff(constraint_system.block_constraints[i].init.size(), table_sizes_for_block_constraints[i]); + size_t delta_gates = compute_gate_diff(); size_t avg_gates_per_opcode = delta_gates / constraint_system.block_constraints_indices[i].size(); for (size_t opcode_index : constraint_system.block_constraints_indices[i]) { constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; @@ -235,30 +184,22 @@ void build_constraints(Builder& builder, // Add big_int constraints DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); - size_t table_size_for_bigint_from_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_from_le_bytes_constraints_original_index[i]] = - compute_gate_diff(constraint_system.bigint_from_le_bytes_constraints.size(), - table_size_for_bigint_from_le_bytes_constraints); + compute_gate_diff(); } - - size_t table_size_for_bigint_operations = 0; for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = - compute_gate_diff(constraint_system.bigint_operations.size(), table_size_for_bigint_operations); + constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = compute_gate_diff(); } - - size_t table_size_for_bigint_to_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_to_le_bytes_constraints_original_index[i]] = - compute_gate_diff(constraint_system.bigint_to_le_bytes_constraints.size(), - table_size_for_bigint_to_le_bytes_constraints); + compute_gate_diff(); } // RecursionConstraint @@ -289,7 +230,6 @@ void build_constraints(Builder& builder, auto proof_size_no_pub_inputs = recursion_proof_size_without_public_inputs(); // Add recursion constraints - size_t table_size_for_recursion_constraints = 0; for (auto constraint : constraint_system.recursion_constraints) { // A proof passed into the constraint should be stripped of its public inputs, except in the case where a // proof contains an aggregation object itself. We refer to this as the `nested_aggregation_object`. The @@ -325,15 +265,12 @@ void build_constraints(Builder& builder, constraint.proof.begin() + static_cast(RecursionConstraint::AGGREGATION_OBJECT_SIZE)); } - current_output_aggregation_object = create_recursion_constraints(builder, constraint, current_input_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; - - compute_gate_diff(constraint_system.recursion_constraints.size(), table_size_for_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -373,7 +310,6 @@ void build_constraints(Builder& builder, }; // Add recursion constraints - size_t table_size_for_honk_recursion_constraints = 0; for (auto constraint : constraint_system.honk_recursion_constraints) { // A proof passed into the constraint should be stripped of its inner public inputs, but not the nested // aggregation object itself. The verifier circuit requires that the indices to a nested proof aggregation @@ -398,9 +334,6 @@ void build_constraints(Builder& builder, current_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); - - compute_gate_diff(constraint_system.honk_recursion_constraints.size(), - table_size_for_honk_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -477,18 +410,6 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, bool has_valid_witness_assignments = !witness.empty(); build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); - // I was doing this to check whether the final gate count matches the sum of the amortised gate counts. It doesn't. - // size_t sum = 0; - // for (size_t i : constraint_system.gates_per_opcode) { - // sum += i; - // } - // // if (builder.get_total_circuit_size() != sum) { - // // throw_or_abort("Badly counted num gates!!"); - // // } - // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 1] = - // builder.get_total_circuit_size(); - // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 2] = sum; - return builder; }; diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json index 8afcf98ed9c..e4b0103f464 100644 --- a/trickty/target/trickty.json +++ b/trickty/target/trickty.json @@ -1 +1 @@ -{"noir_version":"0.30.0+f6d65afe6820d0d6e55ecae3bf8693b5ad9a9c48","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/mike/packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file +{"noir_version":"0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file From 319c9808c6274747508f5960bb22a982c6766622 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 7 Jun 2024 14:56:29 +0000 Subject: [PATCH 05/43] it's workiiiiiiiiing --- .../dsl/acir_format/acir_format.cpp | 19 ++++++--- .../src/cli/create_flamegraph_cmd.rs | 17 +++++--- trickty/src/main.nr | 20 ++++++--- trickty/target/trickty.json | 42 ++++++++++++++++++- 4 files changed, 80 insertions(+), 18 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 2e683fec218..694ac4a51c6 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -230,7 +230,9 @@ void build_constraints(Builder& builder, auto proof_size_no_pub_inputs = recursion_proof_size_without_public_inputs(); // Add recursion constraints - for (auto constraint : constraint_system.recursion_constraints) { + for (size_t constraint_idx = 0; constraint_idx < constraint_system.recursion_constraints.size(); + ++constraint_idx) { + auto constraint = constraint_system.recursion_constraints[constraint_idx]; // A proof passed into the constraint should be stripped of its public inputs, except in the case where a // proof contains an aggregation object itself. We refer to this as the `nested_aggregation_object`. The // verifier circuit requires that the indices to a nested proof aggregation state are a circuit constant. @@ -271,6 +273,8 @@ void build_constraints(Builder& builder, nested_aggregation_object, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; + constraint_system.gates_per_opcode[constraint_system.recursion_constraints_original_index[constraint_idx]] = + compute_gate_diff(); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -310,11 +314,12 @@ void build_constraints(Builder& builder, }; // Add recursion constraints - for (auto constraint : constraint_system.honk_recursion_constraints) { - // A proof passed into the constraint should be stripped of its inner public inputs, but not the nested - // aggregation object itself. The verifier circuit requires that the indices to a nested proof aggregation - // state are a circuit constant. The user tells us they how they want these constants set by keeping the - // nested aggregation object attached to the proof as public inputs. + for (size_t i = 0; i < constraint_system.honk_recursion_constraints.size(); ++i) { + auto constraint = constraint_system.honk_recursion_constraints[i]; + // A proof passed into the constraint should be stripped of its inner public inputs, but not the + // nested aggregation object itself. The verifier circuit requires that the indices to a nested + // proof aggregation state are a circuit constant. The user tells us they how they want these + // constants set by keeping the nested aggregation object attached to the proof as public inputs. std::array nested_aggregation_object = {}; for (size_t i = 0; i < HonkRecursionConstraint::AGGREGATION_OBJECT_SIZE; ++i) { // Set the nested aggregation object indices to witness indices from the proof @@ -334,6 +339,8 @@ void build_constraints(Builder& builder, current_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); + constraint_system.gates_per_opcode[constraint_system.honk_recursion_constraints_original_index[i]] = + compute_gate_diff(); } // Now that the circuit has been completely built, we add the output aggregation as public diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index 8faf3222dc5..ce82dad085b 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -99,12 +99,16 @@ fn add_locations_to_folded_stack_items( gates: usize, ) { let mut child_map = stack_items; - for location in locations { + for (index, location) in locations.iter().enumerate() { let current_item = child_map - .entry(location) + .entry(location.clone()) .or_insert(FoldedStackItem { total_gates: 0, nested_items: HashMap::new() }); - current_item.total_gates += gates; + child_map = &mut current_item.nested_items; + + if index == locations.len() - 1 { + current_item.total_gates += gates; + } } } @@ -116,7 +120,7 @@ fn to_folded_lines( .iter() .flat_map(move |(location, folded_stack_item)| { let frame_list: Vec = - std::iter::once(location.clone()).chain(parent_stacks.clone()).collect(); + parent_stacks.iter().cloned().chain(std::iter::once(location.clone())).collect(); let line: String = format!("{} {}", frame_list.join(";"), folded_stack_item.total_gates); @@ -140,8 +144,9 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { .arg(args.artifact_path) .output() .expect("failed to execute process"); + println!("BB gates raw response {:?}", bb_gates_response); - // println!("BB gates response {}", String::from_utf8(bb_gates_response.stdout.clone()).unwrap()); + println!("BB gates response {}", String::from_utf8(bb_gates_response.stdout.clone()).unwrap()); // Parse the bb gates stdout as json let bb_gates_response: BBGatesResponse = serde_json::from_slice(&bb_gates_response.stdout).map_err(|err| err.to_string())?; @@ -151,6 +156,8 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { let mut folded_stack_items = HashMap::new(); + println!("Total gates {}", gates.gates_per_opcode.iter().sum::()); + gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { let call_stack = &program.debug_symbols.debug_infos[0] .locations diff --git a/trickty/src/main.nr b/trickty/src/main.nr index 58b75f10ad9..90ae232fde4 100644 --- a/trickty/src/main.nr +++ b/trickty/src/main.nr @@ -1,9 +1,17 @@ fn main(x: Field) { - let mut acc = x; - for i in 0..100 { - acc = dep::std::hash::pedersen_hash([acc]); - } - assert_eq(acc * 2, 28); - assert_eq(acc * 3, 42); + assert_eq( + foo(x)+bar(x)+baz(x), 27 + ); } +fn foo(x: Field) -> Field { + dep::std::hash::pedersen_hash([x]) + 1 +} + +fn bar(x: Field) -> Field { + dep::std::hash::pedersen_hash([x]) + 2 +} + +fn baz(x: Field) -> Field { + dep::std::hash::pedersen_hash([x]) + 3 +} \ No newline at end of file diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json index e4b0103f464..18886b51f06 100644 --- a/trickty/target/trickty.json +++ b/trickty/target/trickty.json @@ -1 +1,41 @@ -{"noir_version":"0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f","hash":13623433748087479528,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/7XU1W5bQRRA0YaZmZnJ9jVch5mZGRw7//8FVRM1lXaj/VCp7ZaOZqQljc7T5H37WfZ93j7vee9T9nn+6jvsq+fB88Xz4QXiBfBC8UJ4kXgRvFi8GF4iXgIvFS+Fl4mXwcvFy+EV4hXwSvFKeJV4FbxavBpeI14DrxWvhdeJ18HrxevhDeIN8EbxRniTeBO8WbwZ3iLeAm8Vb4W3ibfB28Xb4R3iHfBO8U54l3gXvFu8G94j3gPvFe+F94n3wfvF++ED4gPwQfFB+JD4EHxYfBg+Ij4CHxUfhY+Jj8HHxcfhE+IT8EnxSfiU+BR8WnwaPiM+A4+IR+BR8Sg8Jh6DB+IBPC4ehyfEE/CkeBKeEk/BQ/EQnhZPw2fFZ+Fz4nPwefF5+IL4AnxRfBG+JL4EXxZfhq+Ir8BXxVfha+Jr8HXxdfiG+AZ8U3wTviW+Bd8W34bviO/Ad8V34Xvie/B98X34gfgB/FD8EH4kfgQ/Fj+Gn4ifwE/FT+Fn4mfwc/Fz+IX4BfxS/BJ+JX4Fvxa/ht+I38BvxW/hd+J38Hvxe/iD+AP8UfwR/iT+BH8Wf4a/iL/AM+IZ+Kv4Kzz77ff4B30U+btiWbwVRJLxeC4Vy0WD6Eskls6EiUg8kUmG0TCaCBPZWBgEuTAeptKZdCqSjsaDXPQtkQ5ybx/lEv9xz+Df7ZlNfexW+GXPP+oHWpQ5Cx0OAAA=","debug_symbols":"tdrPasJAEMfxd9lzDp3ZnZ3dvEopJWosAYmisVDEd29isVLq9XucZOZ3+5A/O5ew6Vfnj/dh3O5PoX29hN1+3U3DfpyrS3i5XTodunGpTlN3nELr3oR+3IRWJF6bsB12fWiTX5t/nSKPXhX7bdZ6fWuCkOFKhkcyPJHhRoZnMtzJ8EKGVxQRSxQ1KihSQZUKylRQp4JCFVSqoFQFtaqoVWWfp6hVRa0qalVRq4paVdSqolYVtRpRqxG1GtmXX9RqRK1G1GpErUbUakStRtRqQq0m1GpCrSb2SxW1mlCrCbWaUKsJtZpQq4ZaNdSqoVYNtWqoVUOtGmrVUKuGWjXUakatZtRqRq1m1GpGrWb2HzBqNaNWM2o1o1YdteqoVUetOmrVUauOWnX2wAa16qhVR60W1GpBrRbUakGtFtRqQa0W1GphT1dRqwW1WlGrFbVaUasVtVpRqxW1WlGrFbVa2VUIeBfiKVZRu88soY/828hTgWLxPuL2Z2QuPrvj0K12/bIhtdw7j+v7wtRcTl+Hnztz7zc=","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n let mut acc = x;\n for i in 0..100 {\n acc = dep::std::hash::pedersen_hash([acc]);\n }\n assert_eq(acc * 2, 28);\n assert_eq(acc * 3, 42);\n}\n\n","path":"/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file +{ + "noir_version": "0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f", + "hash": 10799808187588243059, + "abi": { + "parameters": [ + { + "name": "x", + "type": { + "kind": "field" + }, + "visibility": "private" + } + ], + "param_witnesses": { + "x": [ + { + "start": 0, + "end": 1 + } + ] + }, + "return_type": null, + "return_witnesses": [], + "error_types": {} + }, + "bytecode": "H4sIAAAAAAAA/62RUQqDQAxEXbc/vU2ySdzkr1fptvH+JyhVXEH9UEEfDJMwMD8Tmok46FHvMOhZfea3yPby9iCPzZr5f1WHa2C4sau9sSsuugg6Zs/JkfANyYoKsJROUVFUvkmJXFmzFctgyOTYi5H3I/4Jm61O8wcxl0My7QEAAA==", + "debug_symbols": "pZHBCoMwDIbfJecebGyt+ipDRtU6ClJF62CI7752s4OxMg/e8jdfwxeyQqvq5XbVphtmKC8r9EMjrR6MSyskr6d5lMan2crJQslSAsq0rig2Ap3ulSvFRn5ASpOdpIwdoUIEFin/wFhsFQEa0+BhOM8OZudsJ5HmZzQwqhE+ZOn/2cjzoFHgGY00poFhR/GlUblwl5OWda/8RX1vMU04sIv2Mb47jn0C", + "file_map": { + "29": { + "source": "mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n", + "path": "std/hash.nr" + }, + "47": { + "source": "fn main(x: Field) {\n assert_eq(\n foo(x)+bar(x)+baz(x), 27\n );\n}\n\nfn foo(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 1\n}\n\nfn bar(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 2\n}\n\nfn baz(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 3\n}", + "path": "/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr" + } + }, + "names": [ + "main" + ] +} \ No newline at end of file From 64453b89ad6b235d7a5360df9bf59381ac47488f Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Fri, 7 Jun 2024 15:58:58 +0000 Subject: [PATCH 06/43] Reapply "amortising table gates over all opcodes which use the table... ish" This reverts commit 7a20a944556c3cef86e5f45f2f8a93b03e7ce24a. --- .../dsl/acir_format/acir_format.cpp | 136 ++++++++++++++---- trickty/target/trickty.json | 2 +- 2 files changed, 107 insertions(+), 31 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 694ac4a51c6..8eb9a57df7c 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -1,5 +1,6 @@ #include "acir_format.hpp" #include "barretenberg/common/log.hpp" +#include "barretenberg/common/throw_or_abort.hpp" #include "barretenberg/stdlib/primitives/field/field_conversion.hpp" #include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp" #include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" @@ -20,161 +21,211 @@ void build_constraints(Builder& builder, { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); size_t prev_gate_count = 0; + size_t prev_table_size = 0; - auto compute_gate_diff = [&]() { + auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { size_t new_gate_count = builder.get_total_circuit_size(); - size_t diff = new_gate_count - prev_gate_count; + size_t new_table_size = builder.get_tables_size(); + size_t gate_diff = new_gate_count - prev_gate_count; + size_t table_diff = new_table_size - prev_table_size; + + if (table_diff > 0 && table_size_of_this_type > 0) { + throw_or_abort( + "My current understanding is that tables shouldn't grow in size if the opcode has already been " + "encountered earlier."); + } + + if (gate_diff < table_diff) { + throw_or_abort("Unexpected error"); + } + + table_size_of_this_type += table_diff; + size_t amortised_gate_diff = + (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type); + prev_gate_count = new_gate_count; - return diff; + prev_table_size = new_table_size; + return amortised_gate_diff; }; // Add arithmetic gates + size_t table_size_for_poly_triple_constraints = 0; for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); constraint_system.gates_per_opcode[constraint_system.poly_triple_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.poly_triple_constraints.size(), table_size_for_poly_triple_constraints); } + + size_t table_size_for_quad_constraints = 0; for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = + compute_gate_diff(constraint_system.quad_constraints.size(), table_size_for_quad_constraints); } // Add logic constraint + size_t table_size_for_logic_constraints = 0; for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = + compute_gate_diff(constraint_system.logic_constraints.size(), table_size_for_logic_constraints); } // Add range constraint + size_t table_size_for_range_constraints = 0; for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = + compute_gate_diff(constraint_system.range_constraints.size(), table_size_for_range_constraints); } // Add aes128 constraints + size_t table_size_for_aes128_constraints = 0; for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.aes128_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.aes128_constraints.size(), table_size_for_aes128_constraints); } // Add sha256 constraints + size_t table_size_for_sha256_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.sha256_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.sha256_constraints.size(), table_size_for_sha256_constraints); } + + size_t table_size_for_sha256_compression_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = - compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = compute_gate_diff( + constraint_system.sha256_compression.size(), table_size_for_sha256_compression_constraints); } // Add schnorr constraints + size_t table_size_for_schnorr_constraints = 0; for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.schnorr_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.schnorr_constraints.size(), table_size_for_schnorr_constraints); } // Add ECDSA k1 constraints + size_t table_size_for_ecdsa_k1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_k1_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ecdsa_k1_constraints.size(), table_size_for_ecdsa_k1_constraints); } // Add ECDSA r1 constraints + size_t table_size_for_ecdsa_r1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ecdsa_r1_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ecdsa_r1_constraints.size(), table_size_for_ecdsa_r1_constraints); } // Add blake2s constraints + size_t table_size_for_blake2s_constraints = 0; for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake2s_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.blake2s_constraints.size(), table_size_for_blake2s_constraints); } // Add blake3 constraints + size_t table_size_for_blake3_constraints = 0; for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.blake3_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.blake3_constraints.size(), table_size_for_blake3_constraints); } // Add keccak constraints + size_t table_size_for_keccak_constraints = 0; for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.keccak_constraints.size(), table_size_for_keccak_constraints); } + + size_t table_size_for_keccak_permutations = 0; for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.keccak_permutations_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.keccak_permutations.size(), table_size_for_keccak_permutations); } // Add pedersen constraints + size_t table_size_for_pedersen_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.pedersen_constraints.size(), table_size_for_pedersen_constraints); } + size_t table_size_for_pedersen_hash_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.pedersen_hash_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.pedersen_hash_constraints.size(), + table_size_for_pedersen_hash_constraints); } + size_t table_size_for_poseidon2_constraints = 0; for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); constraint_system.gates_per_opcode[constraint_system.poseidon2_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.poseidon2_constraints.size(), table_size_for_poseidon2_constraints); } // Add multi scalar mul constraints + size_t table_size_for_multi_scalar_mul_constraints = 0; for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); constraint_system.gates_per_opcode[constraint_system.multi_scalar_mul_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.multi_scalar_mul_constraints.size(), + table_size_for_multi_scalar_mul_constraints); } // Add ec add constraints + size_t table_size_for_ec_add_constraints = 0; for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.ec_add_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.ec_add_constraints.size(), table_size_for_ec_add_constraints); } // Add block constraints - for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { + size_t block_constraints_size = constraint_system.block_constraints.size(); + std::vector table_sizes_for_block_constraints(block_constraints_size, size_t(0)); + for (size_t i = 0; i < block_constraints_size; ++i) { const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); - size_t delta_gates = compute_gate_diff(); + // Note sure about taking the size of `init`, like this. Got confused with all the vectors. + size_t delta_gates = + compute_gate_diff(constraint_system.block_constraints[i].init.size(), table_sizes_for_block_constraints[i]); size_t avg_gates_per_opcode = delta_gates / constraint_system.block_constraints_indices[i].size(); for (size_t opcode_index : constraint_system.block_constraints_indices[i]) { constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; @@ -184,22 +235,30 @@ void build_constraints(Builder& builder, // Add big_int constraints DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); + size_t table_size_for_bigint_from_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_from_le_bytes_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.bigint_from_le_bytes_constraints.size(), + table_size_for_bigint_from_le_bytes_constraints); } + + size_t table_size_for_bigint_operations = 0; for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = compute_gate_diff(); + constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = + compute_gate_diff(constraint_system.bigint_operations.size(), table_size_for_bigint_operations); } + + size_t table_size_for_bigint_to_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); constraint_system.gates_per_opcode[constraint_system.bigint_to_le_bytes_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.bigint_to_le_bytes_constraints.size(), + table_size_for_bigint_to_le_bytes_constraints); } // RecursionConstraint @@ -230,6 +289,7 @@ void build_constraints(Builder& builder, auto proof_size_no_pub_inputs = recursion_proof_size_without_public_inputs(); // Add recursion constraints + size_t table_size_for_recursion_constraints = 0; for (size_t constraint_idx = 0; constraint_idx < constraint_system.recursion_constraints.size(); ++constraint_idx) { auto constraint = constraint_system.recursion_constraints[constraint_idx]; @@ -267,6 +327,7 @@ void build_constraints(Builder& builder, constraint.proof.begin() + static_cast(RecursionConstraint::AGGREGATION_OBJECT_SIZE)); } + current_output_aggregation_object = create_recursion_constraints(builder, constraint, current_input_aggregation_object, @@ -274,7 +335,7 @@ void build_constraints(Builder& builder, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; constraint_system.gates_per_opcode[constraint_system.recursion_constraints_original_index[constraint_idx]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.recursion_constraints.size(), table_size_for_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -314,6 +375,8 @@ void build_constraints(Builder& builder, }; // Add recursion constraints + + size_t table_size_for_honk_recursion_constraints = 0; for (size_t i = 0; i < constraint_system.honk_recursion_constraints.size(); ++i) { auto constraint = constraint_system.honk_recursion_constraints[i]; // A proof passed into the constraint should be stripped of its inner public inputs, but not the @@ -340,7 +403,8 @@ void build_constraints(Builder& builder, nested_aggregation_object, has_valid_witness_assignments); constraint_system.gates_per_opcode[constraint_system.honk_recursion_constraints_original_index[i]] = - compute_gate_diff(); + compute_gate_diff(constraint_system.honk_recursion_constraints.size(), + table_size_for_honk_recursion_constraints); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -417,6 +481,18 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, bool has_valid_witness_assignments = !witness.empty(); build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); + // I was doing this to check whether the final gate count matches the sum of the amortised gate counts. It doesn't. + // size_t sum = 0; + // for (size_t i : constraint_system.gates_per_opcode) { + // sum += i; + // } + // // if (builder.get_total_circuit_size() != sum) { + // // throw_or_abort("Badly counted num gates!!"); + // // } + // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 1] = + // builder.get_total_circuit_size(); + // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 2] = sum; + return builder; }; diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json index 18886b51f06..49f11eaa28f 100644 --- a/trickty/target/trickty.json +++ b/trickty/target/trickty.json @@ -38,4 +38,4 @@ "names": [ "main" ] -} \ No newline at end of file +} From fc7b22a902a3207efd68e206097d01e498a68966 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Fri, 7 Jun 2024 16:04:10 +0000 Subject: [PATCH 07/43] better spreading --- .../dsl/acir_format/acir_format.cpp | 35 ++++++++++++---- trickty/target/trickty.json | 42 +------------------ 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 8eb9a57df7c..be04e768834 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -20,14 +20,25 @@ void build_constraints(Builder& builder, bool honk_recursion) { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); - size_t prev_gate_count = 0; + size_t prev_total_gate_count = 0; + size_t prev_normal_gate_count = 0; // excludes tables, lookup gates, public inputs + size_t prev_public_input_count = 0; size_t prev_table_size = 0; + size_t prev_lookups_gate_count = 0; + // This should be refactored auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { - size_t new_gate_count = builder.get_total_circuit_size(); + size_t new_total_gate_count = builder.get_total_circuit_size(); + size_t new_normal_gate_count = builder.get_num_gates(); + size_t new_public_input_count = builder.public_inputs.size(); size_t new_table_size = builder.get_tables_size(); - size_t gate_diff = new_gate_count - prev_gate_count; + size_t new_lookups_gate_count = builder.get_lookups_size(); + + size_t total_gate_diff = new_total_gate_count - prev_total_gate_count; + size_t normal_gate_diff = new_normal_gate_count - prev_normal_gate_count; + size_t public_input_diff = new_public_input_count - prev_public_input_count; size_t table_diff = new_table_size - prev_table_size; + size_t lookups_diff = new_lookups_gate_count - prev_lookups_gate_count; if (table_diff > 0 && table_size_of_this_type > 0) { throw_or_abort( @@ -35,16 +46,26 @@ void build_constraints(Builder& builder, "encountered earlier."); } - if (gate_diff < table_diff) { - throw_or_abort("Unexpected error"); + size_t gate_diff = total_gate_diff; + + if (total_gate_diff < table_diff) { + // This means that before this constraint was added, the number of normal gates exceeded the (table size + + // num lookup gates). After this constraint has been added, the table size + num lookup gates is the + // dominating size. + gate_diff = std::max(normal_gate_diff + public_input_diff, lookups_diff); } table_size_of_this_type += table_diff; + size_t remainder = table_diff > 0 ? table_size_of_this_type % num_constraints_of_this_type : 0; size_t amortised_gate_diff = - (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type); + (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; - prev_gate_count = new_gate_count; + prev_total_gate_count = new_total_gate_count; + prev_normal_gate_count = new_normal_gate_count; + prev_public_input_count = new_public_input_count; prev_table_size = new_table_size; + prev_lookups_gate_count = new_lookups_gate_count; + return amortised_gate_diff; }; diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json index 49f11eaa28f..d7b969aa81f 100644 --- a/trickty/target/trickty.json +++ b/trickty/target/trickty.json @@ -1,41 +1 @@ -{ - "noir_version": "0.30.0+06f03fdf73d9374b5d18d33cf1480f7748db016f", - "hash": 10799808187588243059, - "abi": { - "parameters": [ - { - "name": "x", - "type": { - "kind": "field" - }, - "visibility": "private" - } - ], - "param_witnesses": { - "x": [ - { - "start": 0, - "end": 1 - } - ] - }, - "return_type": null, - "return_witnesses": [], - "error_types": {} - }, - "bytecode": "H4sIAAAAAAAA/62RUQqDQAxEXbc/vU2ySdzkr1fptvH+JyhVXEH9UEEfDJMwMD8Tmok46FHvMOhZfea3yPby9iCPzZr5f1WHa2C4sau9sSsuugg6Zs/JkfANyYoKsJROUVFUvkmJXFmzFctgyOTYi5H3I/4Jm61O8wcxl0My7QEAAA==", - "debug_symbols": "pZHBCoMwDIbfJecebGyt+ipDRtU6ClJF62CI7752s4OxMg/e8jdfwxeyQqvq5XbVphtmKC8r9EMjrR6MSyskr6d5lMan2crJQslSAsq0rig2Ap3ulSvFRn5ASpOdpIwdoUIEFin/wFhsFQEa0+BhOM8OZudsJ5HmZzQwqhE+ZOn/2cjzoFHgGY00poFhR/GlUblwl5OWda/8RX1vMU04sIv2Mb47jn0C", - "file_map": { - "29": { - "source": "mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n", - "path": "std/hash.nr" - }, - "47": { - "source": "fn main(x: Field) {\n assert_eq(\n foo(x)+bar(x)+baz(x), 27\n );\n}\n\nfn foo(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 1\n}\n\nfn bar(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 2\n}\n\nfn baz(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 3\n}", - "path": "/mnt/user-data/alvaro/aztec-packages/trickty/src/main.nr" - } - }, - "names": [ - "main" - ] -} +{"noir_version":"0.30.0+f6d65afe6820d0d6e55ecae3bf8693b5ad9a9c48","hash":10799808187588243059,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/62RUQqDQAxEXbc/vU2ySdzkr1fptvH+JyhVXEH9UEEfDJMwMD8Tmok46FHvMOhZfea3yPby9iCPzZr5f1WHa2C4sau9sSsuugg6Zs/JkfANyYoKsJROUVFUvkmJXFmzFctgyOTYi5H3I/4Jm61O8wcxl0My7QEAAA==","debug_symbols":"pZHBCoMwDIbfJecebGyt+ipDRtU6ClJF62CI7752s4OxMg/e8jdfwxeyQqvq5XbVphtmKC8r9EMjrR6MSyskr6d5lMan2crJQslSAsq0rig2Ap3ulSvFRn5ASpOdpIwdoUIEFin/wFhsFQEa0+BhOM8OZudsJ5HmZzQwqhE+ZOn/2cjzoFHgGY00poFhR/GlUblwl5OWda/8RX1vMU04sIv2Mb47jn0C","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n assert_eq(\n foo(x)+bar(x)+baz(x), 27\n );\n}\n\nfn foo(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 1\n}\n\nfn bar(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 2\n}\n\nfn baz(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 3\n}","path":"/mnt/user-data/mike/packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file From 5da26643a4ffe8341a5a7a22e97db941a5643769 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Fri, 7 Jun 2024 17:23:31 +0000 Subject: [PATCH 08/43] try again... it's over-computing gates --- .../dsl/acir_format/acir_format.cpp | 33 +++++++++---------- .../src/cli/create_flamegraph_cmd.rs | 9 +++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index be04e768834..2d51552ab42 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -21,24 +21,17 @@ void build_constraints(Builder& builder, { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); size_t prev_total_gate_count = 0; - size_t prev_normal_gate_count = 0; // excludes tables, lookup gates, public inputs - size_t prev_public_input_count = 0; size_t prev_table_size = 0; - size_t prev_lookups_gate_count = 0; // This should be refactored auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { + bool first = table_size_of_this_type == 0; + size_t new_total_gate_count = builder.get_total_circuit_size(); - size_t new_normal_gate_count = builder.get_num_gates(); - size_t new_public_input_count = builder.public_inputs.size(); size_t new_table_size = builder.get_tables_size(); - size_t new_lookups_gate_count = builder.get_lookups_size(); size_t total_gate_diff = new_total_gate_count - prev_total_gate_count; - size_t normal_gate_diff = new_normal_gate_count - prev_normal_gate_count; - size_t public_input_diff = new_public_input_count - prev_public_input_count; size_t table_diff = new_table_size - prev_table_size; - size_t lookups_diff = new_lookups_gate_count - prev_lookups_gate_count; if (table_diff > 0 && table_size_of_this_type > 0) { throw_or_abort( @@ -48,23 +41,27 @@ void build_constraints(Builder& builder, size_t gate_diff = total_gate_diff; + table_size_of_this_type += table_diff; + + size_t remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; + size_t amortised_gate_diff = + (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; + if (total_gate_diff < table_diff) { // This means that before this constraint was added, the number of normal gates exceeded the (table size + // num lookup gates). After this constraint has been added, the table size + num lookup gates is the // dominating size. - gate_diff = std::max(normal_gate_diff + public_input_diff, lookups_diff); - } - table_size_of_this_type += table_diff; - size_t remainder = table_diff > 0 ? table_size_of_this_type % num_constraints_of_this_type : 0; - size_t amortised_gate_diff = - (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; + // treat the whole diff as though it's a table + table_size_of_this_type += total_gate_diff; + + remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; + + amortised_gate_diff = 0 + (table_size_of_this_type / num_constraints_of_this_type) + remainder; + } prev_total_gate_count = new_total_gate_count; - prev_normal_gate_count = new_normal_gate_count; - prev_public_input_count = new_public_input_count; prev_table_size = new_table_size; - prev_lookups_gate_count = new_lookups_gate_count; return amortised_gate_diff; }; diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index ce82dad085b..438a5ccf480 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -156,7 +156,16 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { let mut folded_stack_items = HashMap::new(); + gates.gates_per_opcode.iter().enumerate().for_each(|(opcode_idx, gate_count)| { + if *gate_count > 1000000 { + let opcode = &program.bytecode.functions[0].opcodes[opcode_idx]; + println!("PROBLEM {}", opcode); + } + }); + + println!("GONNA PRINT TOTAL GATES"); println!("Total gates {}", gates.gates_per_opcode.iter().sum::()); + println!("GONNA PRINT TOTAL GATES"); gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { let call_stack = &program.debug_symbols.debug_infos[0] From 159605d1b28b54e16514cc3e07235aaf73637a43 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Fri, 7 Jun 2024 17:29:16 +0000 Subject: [PATCH 09/43] and again... --- .../cpp/src/barretenberg/dsl/acir_format/acir_format.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 2d51552ab42..27c4cee4d2c 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -39,13 +39,11 @@ void build_constraints(Builder& builder, "encountered earlier."); } - size_t gate_diff = total_gate_diff; - table_size_of_this_type += table_diff; size_t remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; size_t amortised_gate_diff = - (gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; + (total_gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; if (total_gate_diff < table_diff) { // This means that before this constraint was added, the number of normal gates exceeded the (table size + @@ -53,7 +51,7 @@ void build_constraints(Builder& builder, // dominating size. // treat the whole diff as though it's a table - table_size_of_this_type += total_gate_diff; + table_size_of_this_type = table_size_of_this_type - table_diff + total_gate_diff; remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; From 1ba3b388c5f3bc98b92a4779e4fea784a76fc4c7 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 10 Jun 2024 09:59:34 +0000 Subject: [PATCH 10/43] flamegraph options --- .../noir_flamegraph/src/cli/create_flamegraph_cmd.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index ce82dad085b..ead316ae3f8 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -141,7 +141,7 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { let bb_gates_response = Command::new(args.backend_path) .arg("gates") .arg("-b") - .arg(args.artifact_path) + .arg(&args.artifact_path) .output() .expect("failed to execute process"); println!("BB gates raw response {:?}", bb_gates_response); @@ -182,8 +182,16 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { let folded_lines = to_folded_lines(&folded_stack_items, Default::default()); // println!("folded lines {}", folded_lines.join("\n")); + let mut options = Options::default(); + options.hash = true; + options.deterministic = true; + options.title = format!("{}-{}", &args.artifact_path, program.names[0].clone()); + options.subtitle = Some("Sample = Gate".to_string()); + options.frame_height = 24; + options.color_diffusion = true; + from_lines( - &mut Options::default(), + &mut options, folded_lines.iter().map(|as_string| as_string.as_str()), flamegraph_writer, ) From 0b3d59e18a22ad1ddd4366ba95d3050c76a0a03e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 10 Jun 2024 10:00:57 +0000 Subject: [PATCH 11/43] remove simple test --- trickty/Nargo.toml | 7 ------- trickty/src/main.nr | 17 ----------------- trickty/target/trickty.json | 1 - 3 files changed, 25 deletions(-) delete mode 100644 trickty/Nargo.toml delete mode 100644 trickty/src/main.nr delete mode 100644 trickty/target/trickty.json diff --git a/trickty/Nargo.toml b/trickty/Nargo.toml deleted file mode 100644 index 4c2af8c81b2..00000000000 --- a/trickty/Nargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "trickty" -type = "bin" -authors = [""] -compiler_version = ">=0.30.0" - -[dependencies] \ No newline at end of file diff --git a/trickty/src/main.nr b/trickty/src/main.nr deleted file mode 100644 index 90ae232fde4..00000000000 --- a/trickty/src/main.nr +++ /dev/null @@ -1,17 +0,0 @@ -fn main(x: Field) { - assert_eq( - foo(x)+bar(x)+baz(x), 27 - ); -} - -fn foo(x: Field) -> Field { - dep::std::hash::pedersen_hash([x]) + 1 -} - -fn bar(x: Field) -> Field { - dep::std::hash::pedersen_hash([x]) + 2 -} - -fn baz(x: Field) -> Field { - dep::std::hash::pedersen_hash([x]) + 3 -} \ No newline at end of file diff --git a/trickty/target/trickty.json b/trickty/target/trickty.json deleted file mode 100644 index d7b969aa81f..00000000000 --- a/trickty/target/trickty.json +++ /dev/null @@ -1 +0,0 @@ -{"noir_version":"0.30.0+f6d65afe6820d0d6e55ecae3bf8693b5ad9a9c48","hash":10799808187588243059,"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}]},"return_type":null,"return_witnesses":[],"error_types":{}},"bytecode":"H4sIAAAAAAAA/62RUQqDQAxEXbc/vU2ySdzkr1fptvH+JyhVXEH9UEEfDJMwMD8Tmok46FHvMOhZfea3yPby9iCPzZr5f1WHa2C4sau9sSsuugg6Zs/JkfANyYoKsJROUVFUvkmJXFmzFctgyOTYi5H3I/4Jm61O8wcxl0My7QEAAA==","debug_symbols":"pZHBCoMwDIbfJecebGyt+ipDRtU6ClJF62CI7752s4OxMg/e8jdfwxeyQqvq5XbVphtmKC8r9EMjrR6MSyskr6d5lMan2crJQslSAsq0rig2Ap3ulSvFRn5ASpOdpIwdoUIEFin/wFhsFQEa0+BhOM8OZudsJ5HmZzQwqhE+ZOn/2cjzoFHgGY00poFhR/GlUblwl5OWda/8RX1vMU04sIv2Mb47jn0C","file_map":{"29":{"source":"mod poseidon;\nmod mimc;\nmod poseidon2;\n\nuse crate::default::Default;\nuse crate::uint128::U128;\nuse crate::sha256::{digest, sha256_var};\n\n#[foreign(sha256)]\n// docs:start:sha256\npub fn sha256(input: [u8; N]) -> [u8; 32]\n// docs:end:sha256\n{}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n#[foreign(blake3)]\n// docs:start:blake3\npub fn blake3(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{}\n\n// docs:start:pedersen_commitment\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(input: [Field; N], separator: u32) -> Field {}\n\npub fn hash_to_field(inputs: [Field]) -> Field {\n let mut sum = 0;\n\n for input in inputs {\n let input_bytes: [u8; 32] = input.to_le_bytes(32).as_array();\n sum += crate::field::bytes32_to_field(blake2s(input_bytes));\n }\n\n sum\n}\n\n#[foreign(keccak256)]\n// docs:start:keccak256\npub fn keccak256(input: [u8; N], message_size: u32) -> [u8; 32]\n// docs:end:keccak256\n{}\n\n#[foreign(poseidon2_permutation)]\npub fn poseidon2_permutation(_input: [Field; N], _state_length: u32) -> [Field; N] {}\n\n#[foreign(sha256_compression)]\npub fn sha256_compression(_input: [u32; 16], _state: [u32; 8]) -> [u32; 8] {}\n\n// Generic hashing support. \n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\ntrait Hash{\n fn hash(self, state: &mut H) where H: Hasher;\n}\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\ntrait Hasher{\n fn finish(self) -> Field;\n \n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\ntrait BuildHasher where H: Hasher{\n fn build_hasher(self) -> H;\n}\n\nstruct BuildHasherDefault;\n\nimpl BuildHasher for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn build_hasher(_self: Self) -> H{\n H::default()\n }\n}\n\nimpl Default for BuildHasherDefault\nwhere \n H: Hasher + Default\n{\n fn default() -> Self{\n BuildHasherDefault{}\n } \n}\n\nimpl Hash for Field {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self);\n }\n}\n\nimpl Hash for u8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash(_self: Self, _state: &mut H) where H: Hasher {}\n}\n\nimpl Hash for U128 {\n fn hash(self, state: &mut H) where H: Hasher{\n H::write(state, self.lo as Field);\n H::write(state, self.hi as Field);\n }\n}\n\nimpl Hash for [T; N] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for [T] where T: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl Hash for (A, B) where A: Hash, B: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {\n fn hash(self, state: &mut H) where H: Hasher{\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n","path":"std/hash.nr"},"47":{"source":"fn main(x: Field) {\n assert_eq(\n foo(x)+bar(x)+baz(x), 27\n );\n}\n\nfn foo(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 1\n}\n\nfn bar(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 2\n}\n\nfn baz(x: Field) -> Field {\n dep::std::hash::pedersen_hash([x]) + 3\n}","path":"/mnt/user-data/mike/packages/trickty/src/main.nr"}},"names":["main"]} \ No newline at end of file From 987c68313d3ce484e12dd2de985c71f36955b8a6 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 10 Jun 2024 11:57:08 +0000 Subject: [PATCH 12/43] add smol script for extract a contract function as a regular artifact --- .../extractFunctionAsNoirArtifact.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 noir-projects/extractFunctionAsNoirArtifact.js diff --git a/noir-projects/extractFunctionAsNoirArtifact.js b/noir-projects/extractFunctionAsNoirArtifact.js new file mode 100644 index 00000000000..56c45fe264d --- /dev/null +++ b/noir-projects/extractFunctionAsNoirArtifact.js @@ -0,0 +1,47 @@ +const path = require("path"); +const fs = require("fs").promises; + +async function main() { + let [contractArtifactPath, functionName] = process.argv.slice(2); + if (!contractArtifactPath || !functionName) { + console.log( + "Usage: node extractFunctionAsNoirArtifact.js " + ); + return; + } + + const contractArtifact = JSON.parse( + await fs.readFile(contractArtifactPath, "utf8") + ); + const func = contractArtifact.functions.find((f) => f.name === functionName); + if (!func) { + console.error( + `Function ${functionName} not found in ${contractArtifactPath}` + ); + return; + } + + const artifact = { + noir_version: contractArtifact.noir_version, + hash: 0, + abi: func.abi, + bytecode: func.bytecode, + debug_symbols: func.debug_symbols, + file_map: contractArtifact.file_map, + names: ["main"], + }; + + const outputDir = path.dirname(contractArtifactPath); + const outputName = + path.basename(contractArtifactPath, ".json") + `-${functionName}.json`; + + await fs.writeFile( + path.join(outputDir, outputName), + JSON.stringify(artifact, null, 2) + ); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); From 15f2c2b28830d7dec3e71773d2afd2444f351a9a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 10 Jun 2024 14:40:30 +0000 Subject: [PATCH 13/43] refactor create flamegraph cmd --- noir/noir-repo/Cargo.lock | 2 - .../tooling/noir_flamegraph/Cargo.toml | 2 - .../src/cli/create_flamegraph_cmd.rs | 199 +++++++++--------- 3 files changed, 99 insertions(+), 104 deletions(-) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index e6a88f36ebd..56525af204d 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2704,8 +2704,6 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "thiserror", - "toml 0.7.6", "tracing-appender", "tracing-subscriber", ] diff --git a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml index 6ef107df341..405a86e7ded 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml +++ b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml @@ -17,8 +17,6 @@ name = "noir_flamegraph" path = "src/main.rs" [dependencies] -thiserror.workspace = true -toml.workspace = true color-eyre = "0.6.2" clap.workspace = true nargo.workspace = true diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index d8bc95a4ade..bc446232297 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -1,15 +1,17 @@ -use acir::circuit::OpcodeLocation; -use clap::Args; -use codespan_reporting::files::Files; -use nargo::errors::Location; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::io::BufWriter; use std::path::Path; use std::process::Command; +use clap::Args; +use codespan_reporting::files::Files; +use color_eyre::eyre::{self}; use inferno::flamegraph::{from_lines, Options}; +use serde::{Deserialize, Serialize}; + +use acir::circuit::OpcodeLocation; use nargo::artifacts::program::ProgramArtifact; +use nargo::errors::Location; #[derive(Debug, Clone, Args)] pub(crate) struct CreateFlamegraphCommand { @@ -21,22 +23,11 @@ pub(crate) struct CreateFlamegraphCommand { #[clap(long, short)] backend_path: String, - /// The output file for the flamegraph + /// The output folder for the flamegraph #[clap(long, short)] output: String, } -pub(crate) fn read_program_from_file>( - circuit_path: P, -) -> Result { - let file_path = circuit_path.as_ref().with_extension("json"); - - let input_string = std::fs::read(&file_path).map_err(|err| err.to_string())?; - let program = serde_json::from_slice(&input_string).map_err(|err| err.to_string())?; - - Ok(program) -} - #[derive(Debug, Clone, Serialize, Deserialize)] struct BBGatesReport { acir_opcodes: usize, @@ -49,11 +40,98 @@ struct BBGatesResponse { functions: Vec, } -fn read_location<'files>( +struct FoldedStackItem { + total_gates: usize, + nested_items: HashMap, +} + +pub(crate) fn run(args: CreateFlamegraphCommand) -> eyre::Result<()> { + let program = read_program_from_file(&args.artifact_path)?; + let bb_gates_response = Command::new(args.backend_path) + .arg("gates") + .arg("-b") + .arg(&args.artifact_path) + .output() + .expect("failed to execute process"); + + // Parse the bb gates stdout as json + let bb_gates_response: BBGatesResponse = serde_json::from_slice(&bb_gates_response.stdout)?; + + for (func_idx, func_gates) in bb_gates_response.functions.into_iter().enumerate() { + let mut folded_stack_items = HashMap::new(); + + println!( + "Total gates in the {} opcodes {} of total gates {}", + func_gates.acir_opcodes, + func_gates.gates_per_opcode.iter().sum::(), + func_gates.circuit_size + ); + + func_gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { + let call_stack = &program.debug_symbols.debug_infos[func_idx] + .locations + .get(&OpcodeLocation::Acir(opcode_index)); + let location_names = if let Some(call_stack) = call_stack { + call_stack + .iter() + .map(|location| location_to_callsite_label(*location, &program)) + .collect::>() + } else { + // println!( + // "No call stack found for opcode {} with index {}", + // program.bytecode.functions[func_idx].opcodes[opcode_index], opcode_index + // ); + vec!["unknown".to_string()] + }; + + add_locations_to_folded_stack_items(&mut folded_stack_items, location_names, gates); + }); + + let output_path = + Path::new(&args.output).join(Path::new(&format!("{}.svg", &program.names[func_idx]))); + let flamegraph_file = std::fs::File::create(output_path)?; + + let flamegraph_writer = BufWriter::new(flamegraph_file); + + let folded_lines = to_folded_lines(&folded_stack_items, Default::default()); + + let mut options = Options::default(); + options.hash = true; + options.deterministic = true; + options.title = format!("{}-{}", &args.artifact_path, program.names[func_idx].clone()); + options.subtitle = Some("Sample = Gate".to_string()); + options.frame_height = 24; + options.color_diffusion = true; + + from_lines( + &mut options, + folded_lines.iter().map(|as_string| as_string.as_str()), + flamegraph_writer, + )?; + } + + Ok(()) +} + +pub(crate) fn read_program_from_file>( + circuit_path: P, +) -> eyre::Result { + let file_path = circuit_path.as_ref().with_extension("json"); + + let input_string = std::fs::read(file_path)?; + let program = serde_json::from_slice(&input_string)?; + + Ok(program) +} + +fn location_to_callsite_label<'files>( location: Location, files: &'files impl Files<'files, FileId = fm::FileId>, ) -> String { - let path = files.name(location.file).expect("should get file path"); + let filename = Path::new(&files.name(location.file).expect("should get file path").to_string()) + .file_name() + .map(|os_str| os_str.to_string_lossy().to_string()) + .unwrap_or("unknown".to_string()); let source = files.source(location.file).expect("should get file source"); let code_slice = source @@ -65,7 +143,7 @@ fn read_location<'files>( let (line, column) = line_and_column_from_span(source.as_ref(), location.span.start()); - format!("{}", code_slice) + format!("{}:{}:{}::{}", filename, line, column, code_slice) } fn line_and_column_from_span(source: &str, span_start: u32) -> (u32, u32) { @@ -88,11 +166,6 @@ fn line_and_column_from_span(source: &str, span_start: u32) -> (u32, u32) { (line, column) } -struct FoldedStackItem { - total_gates: usize, - nested_items: HashMap, -} - fn add_locations_to_folded_stack_items( stack_items: &mut HashMap, locations: Vec, @@ -130,81 +203,7 @@ fn to_folded_lines( let child_lines: Vec = to_folded_lines(&folded_stack_item.nested_items, new_parent_stacks); - std::iter::once(line).chain(child_lines.into_iter()) + std::iter::once(line).chain(child_lines) }) .collect() } - -pub(crate) fn run(args: CreateFlamegraphCommand) -> Result<(), String> { - let program = read_program_from_file(&args.artifact_path)?; - // println!("Got program"); - let bb_gates_response = Command::new(args.backend_path) - .arg("gates") - .arg("-b") - .arg(&args.artifact_path) - .output() - .expect("failed to execute process"); - println!("BB gates raw response {:?}", bb_gates_response); - - println!("BB gates response {}", String::from_utf8(bb_gates_response.stdout.clone()).unwrap()); - // Parse the bb gates stdout as json - let bb_gates_response: BBGatesResponse = - serde_json::from_slice(&bb_gates_response.stdout).map_err(|err| err.to_string())?; - - // Consume first - let gates: BBGatesReport = bb_gates_response.functions.into_iter().next().unwrap(); - - let mut folded_stack_items = HashMap::new(); - - gates.gates_per_opcode.iter().enumerate().for_each(|(opcode_idx, gate_count)| { - if *gate_count > 1000000 { - let opcode = &program.bytecode.functions[0].opcodes[opcode_idx]; - println!("PROBLEM {}", opcode); - } - }); - - println!("GONNA PRINT TOTAL GATES"); - println!("Total gates {}", gates.gates_per_opcode.iter().sum::()); - println!("GONNA PRINT TOTAL GATES"); - - gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { - let call_stack = &program.debug_symbols.debug_infos[0] - .locations - .get(&OpcodeLocation::Acir(opcode_index)); - let location_names = if let Some(call_stack) = call_stack { - call_stack - .iter() - .map(|location| read_location(*location, &program)) - .collect::>() - } else { - vec!["unknown".to_string()] - }; - - add_locations_to_folded_stack_items(&mut folded_stack_items, location_names, gates); - }); - - // println!("writing flamegraph to {:?}", args.output); - let flamegraph_file = std::fs::File::create(&args.output).map_err(|err| err.to_string())?; - - let flamegraph_writer = BufWriter::new(flamegraph_file); - - let folded_lines = to_folded_lines(&folded_stack_items, Default::default()); - // println!("folded lines {}", folded_lines.join("\n")); - - let mut options = Options::default(); - options.hash = true; - options.deterministic = true; - options.title = format!("{}-{}", &args.artifact_path, program.names[0].clone()); - options.subtitle = Some("Sample = Gate".to_string()); - options.frame_height = 24; - options.color_diffusion = true; - - from_lines( - &mut options, - folded_lines.iter().map(|as_string| as_string.as_str()), - flamegraph_writer, - ) - .map_err(|err| err.to_string())?; - - Ok(()) -} From e98efed0fe35b7597608493ec8f2312c04ed0d1d Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 10 Jun 2024 14:43:14 +0000 Subject: [PATCH 14/43] remove some extraneous boilerplate --- noir/noir-repo/tooling/noir_flamegraph/Cargo.toml | 2 +- noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml index 405a86e7ded..ab317e8debd 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml +++ b/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "noir_flamegraph" -description = "The hackity hack hack hack" +description = "Creates a flamegraph of gates for noir circuits" # x-release-please-start-version version = "0.40.0" # x-release-please-end diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs index 26d187d7a95..c93b28c5a16 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs @@ -4,13 +4,13 @@ use const_format::formatcp; mod create_flamegraph_cmd; -const ACVM_VERSION: &str = env!("CARGO_PKG_VERSION"); +const FLAMEGRAPH_VERSION: &str = env!("CARGO_PKG_VERSION"); -static VERSION_STRING: &str = formatcp!("version = {}\n", ACVM_VERSION,); +static VERSION_STRING: &str = formatcp!("version = {}\n", FLAMEGRAPH_VERSION,); #[derive(Parser, Debug)] -#[command(name="acvm", author, version=VERSION_STRING, about, long_about = None)] -struct CreateFlamegraphCli { +#[command(name="Gates flamegraph", author, version=VERSION_STRING, about, long_about = None)] +struct GatesFlamegraphCli { #[command(subcommand)] command: CreateFlamegraphCommand, } @@ -23,7 +23,7 @@ enum CreateFlamegraphCommand { #[cfg(not(feature = "codegen-docs"))] pub(crate) fn start_cli() -> eyre::Result<()> { - let CreateFlamegraphCli { command } = CreateFlamegraphCli::parse(); + let GatesFlamegraphCli { command } = GatesFlamegraphCli::parse(); match command { CreateFlamegraphCommand::CreateFlamegraph(args) => create_flamegraph_cmd::run(args), From 12b9c173c8213b52a0f3994ef179942a5f0d4e50 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 06:54:39 +0000 Subject: [PATCH 15/43] rename --- .../noir_flamegraph/src/cli/create_flamegraph_cmd.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index bc446232297..c7764a35b5c 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -93,7 +93,7 @@ pub(crate) fn run(args: CreateFlamegraphCommand) -> eyre::Result<()> { let flamegraph_writer = BufWriter::new(flamegraph_file); - let folded_lines = to_folded_lines(&folded_stack_items, Default::default()); + let folded_lines = to_folded_sorted_lines(&folded_stack_items, Default::default()); let mut options = Options::default(); options.hash = true; @@ -185,7 +185,7 @@ fn add_locations_to_folded_stack_items( } } -fn to_folded_lines( +fn to_folded_sorted_lines( folded_stack_items: &HashMap, parent_stacks: im::Vector, ) -> Vec { @@ -201,7 +201,7 @@ fn to_folded_lines( new_parent_stacks.push_back(location.clone()); let child_lines: Vec = - to_folded_lines(&folded_stack_item.nested_items, new_parent_stacks); + to_folded_sorted_lines(&folded_stack_item.nested_items, new_parent_stacks); std::iter::once(line).chain(child_lines) }) From 75885c82472cf799fa1713b67ec445cac0cbabac Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 07:06:53 +0000 Subject: [PATCH 16/43] update help --- .../noir_flamegraph/src/cli/create_flamegraph_cmd.rs | 6 +++--- noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs index c7764a35b5c..5909ab419f0 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs @@ -15,15 +15,15 @@ use nargo::errors::Location; #[derive(Debug, Clone, Args)] pub(crate) struct CreateFlamegraphCommand { - /// The path to the artifact file + /// The path to the artifact JSON file #[clap(long, short)] artifact_path: String, - /// Path to the binary of noir's backend + /// Path to the noir backend binary #[clap(long, short)] backend_path: String, - /// The output folder for the flamegraph + /// The output folder for the flamegraph svg files #[clap(long, short)] output: String, } diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs index c93b28c5a16..d9f7046c113 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs +++ b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs @@ -1,6 +1,7 @@ use clap::{Parser, Subcommand}; use color_eyre::eyre; use const_format::formatcp; +use noirc_frontend as _; mod create_flamegraph_cmd; From 24564422701f051ea39ba367cb67bae6d93487fa Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 07:38:00 +0000 Subject: [PATCH 17/43] simplify bb changes to gate counter --- .../dsl/acir_format/acir_format.cpp | 188 ++++++------------ .../dsl/acir_format/acir_format.hpp | 81 +++++--- .../acir_format/acir_to_constraint_buf.cpp | 60 +++--- 3 files changed, 147 insertions(+), 182 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 27c4cee4d2c..56727c2cc4d 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -20,230 +20,170 @@ void build_constraints(Builder& builder, bool honk_recursion) { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); - size_t prev_total_gate_count = 0; - size_t prev_table_size = 0; + size_t prev_gate_count = 0; - // This should be refactored - auto compute_gate_diff = [&](size_t const num_constraints_of_this_type, size_t& table_size_of_this_type) -> size_t { - bool first = table_size_of_this_type == 0; - - size_t new_total_gate_count = builder.get_total_circuit_size(); - size_t new_table_size = builder.get_tables_size(); - - size_t total_gate_diff = new_total_gate_count - prev_total_gate_count; - size_t table_diff = new_table_size - prev_table_size; - - if (table_diff > 0 && table_size_of_this_type > 0) { - throw_or_abort( - "My current understanding is that tables shouldn't grow in size if the opcode has already been " - "encountered earlier."); - } - - table_size_of_this_type += table_diff; - - size_t remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; - size_t amortised_gate_diff = - (total_gate_diff - table_diff) + (table_size_of_this_type / num_constraints_of_this_type) + remainder; - - if (total_gate_diff < table_diff) { - // This means that before this constraint was added, the number of normal gates exceeded the (table size + - // num lookup gates). After this constraint has been added, the table size + num lookup gates is the - // dominating size. - - // treat the whole diff as though it's a table - table_size_of_this_type = table_size_of_this_type - table_diff + total_gate_diff; - - remainder = first ? table_size_of_this_type % num_constraints_of_this_type : 0; - - amortised_gate_diff = 0 + (table_size_of_this_type / num_constraints_of_this_type) + remainder; - } - - prev_total_gate_count = new_total_gate_count; - prev_table_size = new_table_size; - - return amortised_gate_diff; + auto compute_gate_diff = [&]() { + size_t new_gate_count = builder.get_num_gates(); + size_t diff = new_gate_count - prev_gate_count; + prev_gate_count = new_gate_count; + return diff; }; // Add arithmetic gates - size_t table_size_for_poly_triple_constraints = 0; for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.poly_triple_constraints_original_index[i]] = - compute_gate_diff(constraint_system.poly_triple_constraints.size(), table_size_for_poly_triple_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.poly_triple_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_quad_constraints = 0; for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.quad_constraints_original_index[i]] = - compute_gate_diff(constraint_system.quad_constraints.size(), table_size_for_quad_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.quad_constraints[i]] = + compute_gate_diff(); } // Add logic constraint - size_t table_size_for_logic_constraints = 0; for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - constraint_system.gates_per_opcode[constraint_system.logic_constraints_original_index[i]] = - compute_gate_diff(constraint_system.logic_constraints.size(), table_size_for_logic_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.logic_constraints[i]] = + compute_gate_diff(); } // Add range constraint - size_t table_size_for_range_constraints = 0; for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - constraint_system.gates_per_opcode[constraint_system.range_constraints_original_index[i]] = - compute_gate_diff(constraint_system.range_constraints.size(), table_size_for_range_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.range_constraints[i]] = + compute_gate_diff(); } // Add aes128 constraints - size_t table_size_for_aes128_constraints = 0; for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.aes128_constraints_original_index[i]] = - compute_gate_diff(constraint_system.aes128_constraints.size(), table_size_for_aes128_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.aes128_constraints[i]] = + compute_gate_diff(); } // Add sha256 constraints - size_t table_size_for_sha256_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.sha256_constraints_original_index[i]] = - compute_gate_diff(constraint_system.sha256_constraints.size(), table_size_for_sha256_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.sha256_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_sha256_compression_constraints = 0; for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.sha256_compression_original_index[i]] = compute_gate_diff( - constraint_system.sha256_compression.size(), table_size_for_sha256_compression_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.sha256_compression[i]] = + compute_gate_diff(); } // Add schnorr constraints - size_t table_size_for_schnorr_constraints = 0; for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.schnorr_constraints_original_index[i]] = - compute_gate_diff(constraint_system.schnorr_constraints.size(), table_size_for_schnorr_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.schnorr_constraints[i]] = + compute_gate_diff(); } // Add ECDSA k1 constraints - size_t table_size_for_ecdsa_k1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.ecdsa_k1_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ecdsa_k1_constraints.size(), table_size_for_ecdsa_k1_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ecdsa_k1_constraints[i]] = + compute_gate_diff(); } // Add ECDSA r1 constraints - size_t table_size_for_ecdsa_r1_constraints = 0; for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.ecdsa_r1_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ecdsa_r1_constraints.size(), table_size_for_ecdsa_r1_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ecdsa_r1_constraints[i]] = + compute_gate_diff(); } // Add blake2s constraints - size_t table_size_for_blake2s_constraints = 0; for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.blake2s_constraints_original_index[i]] = - compute_gate_diff(constraint_system.blake2s_constraints.size(), table_size_for_blake2s_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.blake2s_constraints[i]] = + compute_gate_diff(); } // Add blake3 constraints - size_t table_size_for_blake3_constraints = 0; for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.blake3_constraints_original_index[i]] = - compute_gate_diff(constraint_system.blake3_constraints.size(), table_size_for_blake3_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.blake3_constraints[i]] = + compute_gate_diff(); } // Add keccak constraints - size_t table_size_for_keccak_constraints = 0; for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.keccak_constraints_original_index[i]] = - compute_gate_diff(constraint_system.keccak_constraints.size(), table_size_for_keccak_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.keccak_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_keccak_permutations = 0; for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.keccak_permutations_original_index[i]] = - compute_gate_diff(constraint_system.keccak_permutations.size(), table_size_for_keccak_permutations); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.keccak_permutations[i]] = + compute_gate_diff(); } // Add pedersen constraints - size_t table_size_for_pedersen_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.pedersen_constraints_original_index[i]] = - compute_gate_diff(constraint_system.pedersen_constraints.size(), table_size_for_pedersen_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.pedersen_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_pedersen_hash_constraints = 0; for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.pedersen_hash_constraints_original_index[i]] = - compute_gate_diff(constraint_system.pedersen_hash_constraints.size(), - table_size_for_pedersen_hash_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.pedersen_hash_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_poseidon2_constraints = 0; for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.poseidon2_constraints_original_index[i]] = - compute_gate_diff(constraint_system.poseidon2_constraints.size(), table_size_for_poseidon2_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.poseidon2_constraints[i]] = + compute_gate_diff(); } // Add multi scalar mul constraints - size_t table_size_for_multi_scalar_mul_constraints = 0; for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.multi_scalar_mul_constraints_original_index[i]] = - compute_gate_diff(constraint_system.multi_scalar_mul_constraints.size(), - table_size_for_multi_scalar_mul_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.multi_scalar_mul_constraints[i]] = + compute_gate_diff(); } // Add ec add constraints - size_t table_size_for_ec_add_constraints = 0; for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.ec_add_constraints_original_index[i]] = - compute_gate_diff(constraint_system.ec_add_constraints.size(), table_size_for_ec_add_constraints); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ec_add_constraints[i]] = + compute_gate_diff(); } // Add block constraints - size_t block_constraints_size = constraint_system.block_constraints.size(); - std::vector table_sizes_for_block_constraints(block_constraints_size, size_t(0)); - for (size_t i = 0; i < block_constraints_size; ++i) { + for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); - // Note sure about taking the size of `init`, like this. Got confused with all the vectors. - size_t delta_gates = - compute_gate_diff(constraint_system.block_constraints[i].init.size(), table_sizes_for_block_constraints[i]); - size_t avg_gates_per_opcode = delta_gates / constraint_system.block_constraints_indices[i].size(); - for (size_t opcode_index : constraint_system.block_constraints_indices[i]) { + size_t avg_gates_per_opcode = + compute_gate_diff() / constraint_system.original_opcode_indices.block_constraints[i].size(); + for (size_t opcode_index : constraint_system.original_opcode_indices.block_constraints[i]) { constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; } } @@ -251,30 +191,27 @@ void build_constraints(Builder& builder, // Add big_int constraints DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); - size_t table_size_for_bigint_from_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); - constraint_system.gates_per_opcode[constraint_system.bigint_from_le_bytes_constraints_original_index[i]] = - compute_gate_diff(constraint_system.bigint_from_le_bytes_constraints.size(), - table_size_for_bigint_from_le_bytes_constraints); + constraint_system + .gates_per_opcode[constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints[i]] = + compute_gate_diff(); } - size_t table_size_for_bigint_operations = 0; for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.bigint_operations_original_index[i]] = - compute_gate_diff(constraint_system.bigint_operations.size(), table_size_for_bigint_operations); + constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.bigint_operations[i]] = + compute_gate_diff(); } - size_t table_size_for_bigint_to_le_bytes_constraints = 0; for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); - constraint_system.gates_per_opcode[constraint_system.bigint_to_le_bytes_constraints_original_index[i]] = - compute_gate_diff(constraint_system.bigint_to_le_bytes_constraints.size(), - table_size_for_bigint_to_le_bytes_constraints); + constraint_system + .gates_per_opcode[constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints[i]] = + compute_gate_diff(); } // RecursionConstraint @@ -305,7 +242,6 @@ void build_constraints(Builder& builder, auto proof_size_no_pub_inputs = recursion_proof_size_without_public_inputs(); // Add recursion constraints - size_t table_size_for_recursion_constraints = 0; for (size_t constraint_idx = 0; constraint_idx < constraint_system.recursion_constraints.size(); ++constraint_idx) { auto constraint = constraint_system.recursion_constraints[constraint_idx]; @@ -350,8 +286,9 @@ void build_constraints(Builder& builder, nested_aggregation_object, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; - constraint_system.gates_per_opcode[constraint_system.recursion_constraints_original_index[constraint_idx]] = - compute_gate_diff(constraint_system.recursion_constraints.size(), table_size_for_recursion_constraints); + constraint_system + .gates_per_opcode[constraint_system.original_opcode_indices.recursion_constraints[constraint_idx]] = + compute_gate_diff(); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -392,7 +329,6 @@ void build_constraints(Builder& builder, // Add recursion constraints - size_t table_size_for_honk_recursion_constraints = 0; for (size_t i = 0; i < constraint_system.honk_recursion_constraints.size(); ++i) { auto constraint = constraint_system.honk_recursion_constraints[i]; // A proof passed into the constraint should be stripped of its inner public inputs, but not the @@ -418,9 +354,9 @@ void build_constraints(Builder& builder, current_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.honk_recursion_constraints_original_index[i]] = - compute_gate_diff(constraint_system.honk_recursion_constraints.size(), - table_size_for_honk_recursion_constraints); + constraint_system + .gates_per_opcode[constraint_system.original_opcode_indices.honk_recursion_constraints[i]] = + compute_gate_diff(); } // Now that the circuit has been completely built, we add the output aggregation as public diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index bdcc25be859..039071d9e50 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -24,6 +24,38 @@ namespace acir_format { +struct AcirFormatOriginalOpcodeIndices { + std::vector logic_constraints; + std::vector range_constraints; + std::vector aes128_constraints; + std::vector sha256_constraints; + std::vector sha256_compression; + std::vector schnorr_constraints; + std::vector ecdsa_k1_constraints; + std::vector ecdsa_r1_constraints; + std::vector blake2s_constraints; + std::vector blake3_constraints; + std::vector keccak_constraints; + std::vector keccak_permutations; + std::vector pedersen_constraints; + std::vector pedersen_hash_constraints; + std::vector poseidon2_constraints; + std::vector multi_scalar_mul_constraints; + std::vector ec_add_constraints; + std::vector recursion_constraints; + std::vector honk_recursion_constraints; + std::vector bigint_from_le_bytes_constraints; + std::vector bigint_to_le_bytes_constraints; + std::vector bigint_operations; + std::vector poly_triple_constraints; + std::vector quad_constraints; + // Multiple opcode indices per block: + std::vector> block_constraints; + + friend bool operator==(AcirFormatOriginalOpcodeIndices const& lhs, + AcirFormatOriginalOpcodeIndices const& rhs) = default; +}; + struct AcirFormat { // The number of witnesses in the circuit uint32_t varnum; @@ -41,49 +73,48 @@ struct AcirFormat { std::vector logic_constraints; // the index (in the opcodes array) of each constraint in logic_constraints. // maybe rename to : "opcode_index_of_logic_constraints" - std::vector logic_constraints_original_index; + std::vector range_constraints; - std::vector range_constraints_original_index; + std::vector aes128_constraints; - std::vector aes128_constraints_original_index; + std::vector sha256_constraints; - std::vector sha256_constraints_original_index; + std::vector sha256_compression; - std::vector sha256_compression_original_index; + std::vector schnorr_constraints; - std::vector schnorr_constraints_original_index; + std::vector ecdsa_k1_constraints; - std::vector ecdsa_k1_constraints_original_index; + std::vector ecdsa_r1_constraints; - std::vector ecdsa_r1_constraints_original_index; + std::vector blake2s_constraints; - std::vector blake2s_constraints_original_index; + std::vector blake3_constraints; - std::vector blake3_constraints_original_index; + std::vector keccak_constraints; - std::vector keccak_constraints_original_index; + std::vector keccak_permutations; - std::vector keccak_permutations_original_index; + std::vector pedersen_constraints; - std::vector pedersen_constraints_original_index; + std::vector pedersen_hash_constraints; - std::vector pedersen_hash_constraints_original_index; + std::vector poseidon2_constraints; - std::vector poseidon2_constraints_original_index; + std::vector multi_scalar_mul_constraints; - std::vector multi_scalar_mul_constraints_original_index; + std::vector ec_add_constraints; - std::vector ec_add_constraints_original_index; + std::vector recursion_constraints; - std::vector recursion_constraints_original_index; + std::vector honk_recursion_constraints; - std::vector honk_recursion_constraints_original_index; + std::vector bigint_from_le_bytes_constraints; - std::vector bigint_from_le_bytes_constraints_original_index; + std::vector bigint_to_le_bytes_constraints; - std::vector bigint_to_le_bytes_constraints_original_index; + std::vector bigint_operations; - std::vector bigint_operations_original_index; // A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values // for q_M,q_L,q_R,q_O,q_C and indices of three variables taking the role of left, right and output wire @@ -91,21 +122,19 @@ struct AcirFormat { std::vector, bb::ContainerSlabAllocator>> poly_triple_constraints; - std::vector poly_triple_constraints_original_index; std::vector, bb::ContainerSlabAllocator>> quad_constraints; - std::vector quad_constraints_original_index; std::vector block_constraints; - // vec of blocks => vec of opcode indices: - std::vector> block_constraints_indices; // Number of gates added to the circuit per original opcode. // Has the same length as the opcode vector. std::vector gates_per_opcode; + AcirFormatOriginalOpcodeIndices original_opcode_indices; + // For serialization, update with any new fields MSGPACK_FIELDS(varnum, public_inputs, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp index 288aad02a5a..3e375b79a8a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp @@ -177,15 +177,15 @@ void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af, s // gate instead. We could probably always use a width-4 gate in fact. if (pt == poly_triple{ 0, 0, 0, 0, 0, 0, 0, 0 }) { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); - af.quad_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.quad_constraints.push_back((opcode_index)); } else { af.poly_triple_constraints.push_back(pt); - af.poly_triple_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.poly_triple_constraints.push_back((opcode_index)); } } else { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); - af.quad_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.quad_constraints.push_back((opcode_index)); } } @@ -205,7 +205,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .num_bits = arg.lhs.num_bits, .is_xor_gate = false, }); - af.logic_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.logic_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.logic_constraints.push_back(LogicConstraint{ .a = arg.lhs.witness.value, @@ -214,13 +214,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .num_bits = arg.lhs.num_bits, .is_xor_gate = true, }); - af.logic_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.logic_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.range_constraints.push_back(RangeConstraint{ .witness = arg.input.witness.value, .num_bits = arg.input.num_bits, }); - af.range_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.range_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.aes128_constraints.push_back(AES128Constraint{ @@ -247,7 +247,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .outputs = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.aes128_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.aes128_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.sha256_constraints.push_back(Sha256Constraint{ @@ -260,7 +260,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.sha256_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.sha256_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.sha256_compression.push_back(Sha256Compression{ @@ -280,7 +280,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.sha256_compression_original_index.push_back(opcode_index); + af.original_opcode_indices.sha256_compression.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.blake2s_constraints.push_back(Blake2sConstraint{ .inputs = map(arg.inputs, @@ -292,7 +292,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.blake2s_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.blake2s_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.blake3_constraints.push_back(Blake3Constraint{ .inputs = map(arg.inputs, @@ -304,7 +304,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.blake3_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.blake3_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.schnorr_constraints.push_back(SchnorrConstraint{ .message = map(arg.message, [](auto& e) { return e.witness.value; }), @@ -313,7 +313,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); - af.schnorr_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.schnorr_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.pedersen_constraints.push_back(PedersenConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), @@ -321,14 +321,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result_x = arg.outputs[0].value, .result_y = arg.outputs[1].value, }); - af.pedersen_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.pedersen_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.pedersen_hash_constraints.push_back(PedersenHashConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), .hash_index = arg.domain_separator, .result = arg.output.value, }); - af.pedersen_hash_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.pedersen_hash_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.ecdsa_k1_constraints.push_back(EcdsaSecp256k1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -337,7 +337,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .pub_y_indices = map(arg.public_key_y, [](auto& e) { return e.witness.value; }), .result = arg.output.value, }); - af.ecdsa_k1_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.ecdsa_k1_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.ecdsa_r1_constraints.push_back(EcdsaSecp256r1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -346,7 +346,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); - af.ecdsa_r1_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.ecdsa_r1_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.multi_scalar_mul_constraints.push_back(MultiScalarMul{ .points = map(arg.points, [](auto& e) { return e.witness.value; }), @@ -355,7 +355,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .out_point_y = arg.outputs[1].value, .out_point_is_infinite = arg.outputs[2].value, }); - af.multi_scalar_mul_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.multi_scalar_mul_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.ec_add_constraints.push_back(EcAdd{ .input1_x = arg.input1[0].witness.value, @@ -368,7 +368,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result_y = arg.outputs[1].value, .result_infinite = arg.outputs[2].value, }); - af.ec_add_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.ec_add_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.keccak_constraints.push_back(KeccakConstraint{ .inputs = map(arg.inputs, @@ -381,13 +381,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = map(arg.outputs, [](auto& e) { return e.value; }), .var_message_size = arg.var_message_size.witness.value, }); - af.keccak_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.keccak_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.keccak_permutations.push_back(Keccakf1600{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.keccak_permutations_original_index.push_back(opcode_index); + af.original_opcode_indices.keccak_permutations.push_back((opcode_index)); } else if constexpr (std::is_same_v) { if (honk_recursion) { // if we're using the honk recursive verifier auto c = HonkRecursionConstraint{ @@ -396,7 +396,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .public_inputs = map(arg.public_inputs, [](auto& e) { return e.witness.value; }), }; af.honk_recursion_constraints.push_back(c); - af.honk_recursion_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.honk_recursion_constraints.push_back((opcode_index)); } else { auto c = RecursionConstraint{ .key = map(arg.verification_key, [](auto& e) { return e.witness.value; }), @@ -405,7 +405,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .key_hash = arg.key_hash.witness.value, }; af.recursion_constraints.push_back(c); - af.recursion_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.recursion_constraints.push_back((opcode_index)); } } else if constexpr (std::is_same_v) { af.bigint_from_le_bytes_constraints.push_back(BigIntFromLeBytes{ @@ -413,13 +413,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .modulus = map(arg.modulus, [](auto& e) -> uint32_t { return e; }), .result = arg.output, }); - af.bigint_from_le_bytes_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_from_le_bytes_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.bigint_to_le_bytes_constraints.push_back(BigIntToLeBytes{ .input = arg.input, .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.bigint_to_le_bytes_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_to_le_bytes_constraints.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -427,7 +427,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Add, }); - af.bigint_operations_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_operations.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -435,7 +435,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Sub, }); - af.bigint_operations_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_operations.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -443,7 +443,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Mul, }); - af.bigint_operations_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_operations.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -451,14 +451,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Div, }); - af.bigint_operations_original_index.push_back(opcode_index); + af.original_opcode_indices.bigint_operations.push_back((opcode_index)); } else if constexpr (std::is_same_v) { af.poseidon2_constraints.push_back(Poseidon2Constraint{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), .len = arg.len, }); - af.poseidon2_constraints_original_index.push_back(opcode_index); + af.original_opcode_indices.poseidon2_constraints.push_back((opcode_index)); } }, arg.value.value); @@ -559,7 +559,7 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit, bool ho for (const auto& [block_id, block] : block_id_to_block_constraint) { if (!block.first.trace.empty()) { af.block_constraints.push_back(block.first); - af.block_constraints_indices.push_back(block.second); + af.original_opcode_indices.block_constraints.push_back(block.second); } } return af; From c6d0e77efa128af52141ad39c413ba03909007cc Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 07:55:06 +0000 Subject: [PATCH 18/43] refactoring --- noir/noir-repo/Cargo.lock | 46 +++++++++---------- noir/noir-repo/Cargo.toml | 2 +- .../tooling/noir_flamegraph/src/cli/mod.rs | 42 ----------------- .../{noir_flamegraph => profiler}/Cargo.toml | 6 +-- .../src/cli/gates_flamegraph_cmd.rs} | 38 ++++++++------- .../noir-repo/tooling/profiler/src/cli/mod.rs | 42 +++++++++++++++++ .../{noir_flamegraph => profiler}/src/main.rs | 0 7 files changed, 87 insertions(+), 89 deletions(-) delete mode 100644 noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs rename noir/noir-repo/tooling/{noir_flamegraph => profiler}/Cargo.toml (88%) rename noir/noir-repo/tooling/{noir_flamegraph/src/cli/create_flamegraph_cmd.rs => profiler/src/cli/gates_flamegraph_cmd.rs} (84%) create mode 100644 noir/noir-repo/tooling/profiler/src/cli/mod.rs rename noir/noir-repo/tooling/{noir_flamegraph => profiler}/src/main.rs (100%) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index 56525af204d..4d815af0d73 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2685,29 +2685,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "noir_flamegraph" -version = "0.40.0" -dependencies = [ - "acir", - "clap", - "codespan-reporting", - "color-eyre", - "const_format", - "fm", - "im", - "inferno", - "nargo", - "noirc_frontend", - "paste", - "proptest", - "rand 0.8.5", - "serde", - "serde_json", - "tracing-appender", - "tracing-subscriber", -] - [[package]] name = "noir_grumpkin" version = "0.1.0" @@ -2746,6 +2723,29 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "noir_profiler" +version = "0.40.0" +dependencies = [ + "acir", + "clap", + "codespan-reporting", + "color-eyre", + "const_format", + "fm", + "im", + "inferno", + "nargo", + "noirc_frontend", + "paste", + "proptest", + "rand 0.8.5", + "serde", + "serde_json", + "tracing-appender", + "tracing-subscriber", +] + [[package]] name = "noir_wasm" version = "0.30.0" diff --git a/noir/noir-repo/Cargo.toml b/noir/noir-repo/Cargo.toml index 9bbcd17f533..98afe9274c5 100644 --- a/noir/noir-repo/Cargo.toml +++ b/noir/noir-repo/Cargo.toml @@ -22,7 +22,7 @@ members = [ "tooling/noirc_abi", "tooling/noirc_abi_wasm", "tooling/acvm_cli", - "tooling/noir_flamegraph", + "tooling/profiler", # ACVM "acvm-repo/acir_field", "acvm-repo/acir", diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs b/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs deleted file mode 100644 index d9f7046c113..00000000000 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/mod.rs +++ /dev/null @@ -1,42 +0,0 @@ -use clap::{Parser, Subcommand}; -use color_eyre::eyre; -use const_format::formatcp; -use noirc_frontend as _; - -mod create_flamegraph_cmd; - -const FLAMEGRAPH_VERSION: &str = env!("CARGO_PKG_VERSION"); - -static VERSION_STRING: &str = formatcp!("version = {}\n", FLAMEGRAPH_VERSION,); - -#[derive(Parser, Debug)] -#[command(name="Gates flamegraph", author, version=VERSION_STRING, about, long_about = None)] -struct GatesFlamegraphCli { - #[command(subcommand)] - command: CreateFlamegraphCommand, -} - -#[non_exhaustive] -#[derive(Subcommand, Clone, Debug)] -enum CreateFlamegraphCommand { - CreateFlamegraph(create_flamegraph_cmd::CreateFlamegraphCommand), -} - -#[cfg(not(feature = "codegen-docs"))] -pub(crate) fn start_cli() -> eyre::Result<()> { - let GatesFlamegraphCli { command } = GatesFlamegraphCli::parse(); - - match command { - CreateFlamegraphCommand::CreateFlamegraph(args) => create_flamegraph_cmd::run(args), - } - .map_err(|err| eyre::eyre!("{}", err))?; - - Ok(()) -} - -#[cfg(feature = "codegen-docs")] -pub(crate) fn start_cli() -> eyre::Result<()> { - let markdown: String = clap_markdown::help_markdown::(); - println!("{markdown}"); - Ok(()) -} diff --git a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml similarity index 88% rename from noir/noir-repo/tooling/noir_flamegraph/Cargo.toml rename to noir/noir-repo/tooling/profiler/Cargo.toml index ab317e8debd..f1fc55f2d75 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "noir_flamegraph" -description = "Creates a flamegraph of gates for noir circuits" +name = "noir_profiler" +description = "Analyzer for noir circuits" # x-release-please-start-version version = "0.40.0" # x-release-please-end @@ -13,7 +13,7 @@ repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[bin]] -name = "noir_flamegraph" +name = "noir_profiler" path = "src/main.rs" [dependencies] diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs similarity index 84% rename from noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs rename to noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index 5909ab419f0..0f17d4f2394 100644 --- a/noir/noir-repo/tooling/noir_flamegraph/src/cli/create_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -14,7 +14,7 @@ use nargo::artifacts::program::ProgramArtifact; use nargo::errors::Location; #[derive(Debug, Clone, Args)] -pub(crate) struct CreateFlamegraphCommand { +pub(crate) struct GatesFlamegraphCommand { /// The path to the artifact JSON file #[clap(long, short)] artifact_path: String, @@ -29,15 +29,15 @@ pub(crate) struct CreateFlamegraphCommand { } #[derive(Debug, Clone, Serialize, Deserialize)] -struct BBGatesReport { +struct BackendGatesReport { acir_opcodes: usize, circuit_size: usize, gates_per_opcode: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] -struct BBGatesResponse { - functions: Vec, +struct BackendGatesResponse { + functions: Vec, } struct FoldedStackItem { @@ -45,23 +45,20 @@ struct FoldedStackItem { nested_items: HashMap, } -pub(crate) fn run(args: CreateFlamegraphCommand) -> eyre::Result<()> { +pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { let program = read_program_from_file(&args.artifact_path)?; - let bb_gates_response = Command::new(args.backend_path) - .arg("gates") - .arg("-b") - .arg(&args.artifact_path) - .output() - .expect("failed to execute process"); + let backend_gates_response = + Command::new(args.backend_path).arg("gates").arg("-b").arg(&args.artifact_path).output()?; - // Parse the bb gates stdout as json - let bb_gates_response: BBGatesResponse = serde_json::from_slice(&bb_gates_response.stdout)?; + // Parse the backend gates command stdout as json + let backend_gates_response: BackendGatesResponse = + serde_json::from_slice(&backend_gates_response.stdout)?; - for (func_idx, func_gates) in bb_gates_response.functions.into_iter().enumerate() { + for (func_idx, func_gates) in backend_gates_response.functions.into_iter().enumerate() { let mut folded_stack_items = HashMap::new(); println!( - "Total gates in the {} opcodes {} of total gates {}", + "Opcode count: {}, Total gates by opcodes: {}, Circuit size: {}", func_gates.acir_opcodes, func_gates.gates_per_opcode.iter().sum::(), func_gates.circuit_size @@ -128,11 +125,12 @@ fn location_to_callsite_label<'files>( location: Location, files: &'files impl Files<'files, FileId = fm::FileId>, ) -> String { - let filename = Path::new(&files.name(location.file).expect("should get file path").to_string()) - .file_name() - .map(|os_str| os_str.to_string_lossy().to_string()) - .unwrap_or("unknown".to_string()); - let source = files.source(location.file).expect("should get file source"); + let filename = + Path::new(&files.name(location.file).expect("should have a file path").to_string()) + .file_name() + .map(|os_str| os_str.to_string_lossy().to_string()) + .unwrap_or("invalid_path".to_string()); + let source = files.source(location.file).expect("should have a file source"); let code_slice = source .as_ref() diff --git a/noir/noir-repo/tooling/profiler/src/cli/mod.rs b/noir/noir-repo/tooling/profiler/src/cli/mod.rs new file mode 100644 index 00000000000..f6a5637ed86 --- /dev/null +++ b/noir/noir-repo/tooling/profiler/src/cli/mod.rs @@ -0,0 +1,42 @@ +use clap::{Parser, Subcommand}; +use color_eyre::eyre; +use const_format::formatcp; +use noirc_frontend as _; + +mod gates_flamegraph_cmd; + +const PROFILER_VERSION: &str = env!("CARGO_PKG_VERSION"); + +static VERSION_STRING: &str = formatcp!("version = {}\n", PROFILER_VERSION,); + +#[derive(Parser, Debug)] +#[command(name="Noir profiler", author, version=VERSION_STRING, about, long_about = None)] +struct ProfilerCli { + #[command(subcommand)] + command: GatesFlamegraphCommand, +} + +#[non_exhaustive] +#[derive(Subcommand, Clone, Debug)] +enum GatesFlamegraphCommand { + GatesFlamegraph(gates_flamegraph_cmd::GatesFlamegraphCommand), +} + +#[cfg(not(feature = "codegen-docs"))] +pub(crate) fn start_cli() -> eyre::Result<()> { + let ProfilerCli { command } = ProfilerCli::parse(); + + match command { + GatesFlamegraphCommand::GatesFlamegraph(args) => gates_flamegraph_cmd::run(args), + } + .map_err(|err| eyre::eyre!("{}", err))?; + + Ok(()) +} + +#[cfg(feature = "codegen-docs")] +pub(crate) fn start_cli() -> eyre::Result<()> { + let markdown: String = clap_markdown::help_markdown::(); + println!("{markdown}"); + Ok(()) +} diff --git a/noir/noir-repo/tooling/noir_flamegraph/src/main.rs b/noir/noir-repo/tooling/profiler/src/main.rs similarity index 100% rename from noir/noir-repo/tooling/noir_flamegraph/src/main.rs rename to noir/noir-repo/tooling/profiler/src/main.rs From 3180711d9024ac63e39d4603d6fd02e0823441ae Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 08:05:47 +0000 Subject: [PATCH 19/43] preparing for PR --- .../dsl/acir_format/acir_format.cpp | 12 ------- .../dsl/acir_format/acir_format.hpp | 32 ++++--------------- .../extractFunctionAsNoirArtifact.js | 3 ++ noir/noir-repo/tooling/profiler/Cargo.toml | 2 +- .../profiler/src/cli/gates_flamegraph_cmd.rs | 4 --- 5 files changed, 11 insertions(+), 42 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 56727c2cc4d..7a384be60ad 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -433,18 +433,6 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, bool has_valid_witness_assignments = !witness.empty(); build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); - // I was doing this to check whether the final gate count matches the sum of the amortised gate counts. It doesn't. - // size_t sum = 0; - // for (size_t i : constraint_system.gates_per_opcode) { - // sum += i; - // } - // // if (builder.get_total_circuit_size() != sum) { - // // throw_or_abort("Badly counted num gates!!"); - // // } - // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 1] = - // builder.get_total_circuit_size(); - // constraint_system.gates_per_opcode[constraint_system.gates_per_opcode.size() - 2] = sum; - return builder; }; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 039071d9e50..0aec0fbc270 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -24,6 +24,12 @@ namespace acir_format { +/** + * @brief Indices of the original opcode that originated each constraint in AcirFormat. + * @details Contains one array of indices per opcode type. The length of each array is equal to the number of + * constraints of that type. The relationship between the opcodes and constraints is assumed to be one to one, except + * for block constraints. + */ struct AcirFormatOriginalOpcodeIndices { std::vector logic_constraints; std::vector range_constraints; @@ -71,49 +77,26 @@ struct AcirFormat { std::vector public_inputs; std::vector logic_constraints; - // the index (in the opcodes array) of each constraint in logic_constraints. - // maybe rename to : "opcode_index_of_logic_constraints" - std::vector range_constraints; - std::vector aes128_constraints; - std::vector sha256_constraints; - std::vector sha256_compression; - std::vector schnorr_constraints; - std::vector ecdsa_k1_constraints; - std::vector ecdsa_r1_constraints; - std::vector blake2s_constraints; - std::vector blake3_constraints; - std::vector keccak_constraints; - std::vector keccak_permutations; - std::vector pedersen_constraints; - std::vector pedersen_hash_constraints; - std::vector poseidon2_constraints; - std::vector multi_scalar_mul_constraints; - std::vector ec_add_constraints; - std::vector recursion_constraints; - std::vector honk_recursion_constraints; - std::vector bigint_from_le_bytes_constraints; - std::vector bigint_to_le_bytes_constraints; - std::vector bigint_operations; // A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values @@ -122,17 +105,16 @@ struct AcirFormat { std::vector, bb::ContainerSlabAllocator>> poly_triple_constraints; - std::vector, bb::ContainerSlabAllocator>> quad_constraints; - std::vector block_constraints; // Number of gates added to the circuit per original opcode. // Has the same length as the opcode vector. std::vector gates_per_opcode; + // Indices of the original opcode that originated each constraint in AcirFormat. AcirFormatOriginalOpcodeIndices original_opcode_indices; // For serialization, update with any new fields diff --git a/noir-projects/extractFunctionAsNoirArtifact.js b/noir-projects/extractFunctionAsNoirArtifact.js index 56c45fe264d..d25c5bd98a4 100644 --- a/noir-projects/extractFunctionAsNoirArtifact.js +++ b/noir-projects/extractFunctionAsNoirArtifact.js @@ -1,6 +1,9 @@ const path = require("path"); const fs = require("fs").promises; +// Simple script to extract a contract function as a separate Noir artifact. +// We need to use this since the transpiling that we do on public functions make the contract artifacts +// unreadable by noir tooling, since they are no longer following the noir artifact format. async function main() { let [contractArtifactPath, functionName] = process.argv.slice(2); if (!contractArtifactPath || !functionName) { diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index f1fc55f2d75..59645b50846 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "noir_profiler" -description = "Analyzer for noir circuits" +description = "Profiler for noir circuits" # x-release-please-start-version version = "0.40.0" # x-release-please-end diff --git a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index 0f17d4f2394..88eb7d968a1 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -74,10 +74,6 @@ pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { .map(|location| location_to_callsite_label(*location, &program)) .collect::>() } else { - // println!( - // "No call stack found for opcode {} with index {}", - // program.bytecode.functions[func_idx].opcodes[opcode_index], opcode_index - // ); vec!["unknown".to_string()] }; From 0a18f3acc114a04f53bfecc8909c32c0ef76fdde Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 08:14:57 +0000 Subject: [PATCH 20/43] build profiler bin --- noir/noir-repo/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir/noir-repo/Cargo.toml b/noir/noir-repo/Cargo.toml index 98afe9274c5..e99d13b6a60 100644 --- a/noir/noir-repo/Cargo.toml +++ b/noir/noir-repo/Cargo.toml @@ -35,7 +35,7 @@ members = [ # Utility crates "utils/iter-extended", ] -default-members = ["tooling/nargo_cli", "tooling/acvm_cli"] +default-members = ["tooling/nargo_cli", "tooling/acvm_cli", "tooling/profiler"] resolver = "2" [workspace.package] From dba410efbe68e0cadf916cca6fe596a82fcf3edc Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 08:17:21 +0000 Subject: [PATCH 21/43] move script --- .../{ => noir-contracts}/extractFunctionAsNoirArtifact.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename noir-projects/{ => noir-contracts}/extractFunctionAsNoirArtifact.js (100%) diff --git a/noir-projects/extractFunctionAsNoirArtifact.js b/noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js similarity index 100% rename from noir-projects/extractFunctionAsNoirArtifact.js rename to noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js From 12370a795c6929b61d0b2f30861a327866272b6b Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 08:24:29 +0000 Subject: [PATCH 22/43] print the path written in extractor --- .../noir-contracts/extractFunctionAsNoirArtifact.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js b/noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js index d25c5bd98a4..842645d860f 100644 --- a/noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js +++ b/noir-projects/noir-contracts/extractFunctionAsNoirArtifact.js @@ -38,10 +38,11 @@ async function main() { const outputName = path.basename(contractArtifactPath, ".json") + `-${functionName}.json`; - await fs.writeFile( - path.join(outputDir, outputName), - JSON.stringify(artifact, null, 2) - ); + const outPath = path.join(outputDir, outputName); + + console.log(`Writing to ${outPath}`); + + await fs.writeFile(outPath, JSON.stringify(artifact, null, 2)); } main().catch((err) => { From a932bb144de33fa6dac86fe62140b71f5b143cd3 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 11:10:43 +0000 Subject: [PATCH 23/43] update tests --- .../dsl/acir_format/acir_format.hpp | 2 +- .../dsl/acir_format/acir_format.test.cpp | 318 ++++++++++++---- .../acir_format/bigint_constraint.test.cpp | 58 ++- .../dsl/acir_format/block_constraint.test.cpp | 261 ++++++++----- .../dsl/acir_format/ec_operations.test.cpp | 174 +++++---- .../dsl/acir_format/ecdsa_secp256k1.test.cpp | 261 ++++++++----- .../dsl/acir_format/ecdsa_secp256r1.test.cpp | 348 +++++++++++------- .../honk_recursion_constraint.test.cpp | 61 ++- .../acir_format/poseidon2_constraint.test.cpp | 29 +- .../acir_format/recursion_constraint.test.cpp | 61 ++- .../acir_format/sha256_constraint.test.cpp | 29 +- 11 files changed, 1152 insertions(+), 450 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 0aec0fbc270..de8b22d8f1d 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -112,7 +112,7 @@ struct AcirFormat { // Number of gates added to the circuit per original opcode. // Has the same length as the opcode vector. - std::vector gates_per_opcode; + std::vector gates_per_opcode = {}; // Indices of the original opcode that originated each constraint in AcirFormat. AcirFormatOriginalOpcodeIndices original_opcode_indices; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 0d1ef31040f..906c52fd863 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -64,6 +64,34 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs) .poly_triple_constraints = { constraint }, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = + AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 0 }, + .quad_constraints = {}, + .block_constraints = {}, + }, }; WitnessVector witness{ 0, 0, 1 }; @@ -154,35 +182,65 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit) // EXPR [ (1, _4, _6) (-1, _4) 0 ] // EXPR [ (-1, _6) 1 ] - AcirFormat constraint_system{ .varnum = 6, - .recursive = false, - .num_acir_opcodes = 7, - .public_inputs = { 1 }, - .logic_constraints = { logic_constraint }, - .range_constraints = { range_a, range_b }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, - .quad_constraints = {}, - .block_constraints = {} }; + AcirFormat constraint_system{ + .varnum = 6, + .recursive = false, + .num_acir_opcodes = 7, + .public_inputs = { 1 }, + .logic_constraints = { logic_constraint }, + .range_constraints = { range_a, range_b }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = + AcirFormatOriginalOpcodeIndices{ + .logic_constraints = { 0 }, + .range_constraints = { 1, 2 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 3, 4, 5, 6 }, + .quad_constraints = {}, + .block_constraints = {}, + }, + }; uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -202,11 +260,14 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit) TEST_F(AcirFormatTests, TestSchnorrVerifyPass) { std::vector range_constraints; + std::vector range_opcode_indices; + size_t current_opcode = 0; for (uint32_t i = 0; i < 10; i++) { range_constraints.push_back(RangeConstraint{ .witness = i, .num_bits = 15, }); + range_opcode_indices.push_back(current_opcode++); } std::array signature; @@ -216,6 +277,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) .witness = value, .num_bits = 15, }); + range_opcode_indices.push_back(current_opcode++); } SchnorrConstraint schnorr_constraint{ @@ -225,44 +287,74 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) .result = 76, .signature = signature, }; - AcirFormat constraint_system{ .varnum = 81, - .recursive = false, - .num_acir_opcodes = 75, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = range_constraints, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = { schnorr_constraint }, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { poly_triple{ - .a = schnorr_constraint.result, - .b = schnorr_constraint.result, - .c = schnorr_constraint.result, - .q_m = 0, - .q_l = 0, - .q_r = 0, - .q_o = 1, - .q_c = fr::neg_one(), - } }, - .quad_constraints = {}, - .block_constraints = {} }; + AcirFormat constraint_system{ + .varnum = 81, + .recursive = false, + .num_acir_opcodes = 76, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = range_constraints, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = { schnorr_constraint }, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { poly_triple{ + .a = schnorr_constraint.result, + .b = schnorr_constraint.result, + .c = schnorr_constraint.result, + .q_m = 0, + .q_l = 0, + .q_r = 0, + .q_o = 1, + .q_c = fr::neg_one(), + } }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = + AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = range_opcode_indices, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = { current_opcode++ }, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { current_opcode++ }, + .quad_constraints = {}, + .block_constraints = {}, + }, + }; std::string message_string = "tenletters"; schnorr_key_pair account; @@ -299,11 +391,15 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) { std::vector range_constraints; + std::vector range_opcode_indices; + size_t current_opcode = 0; + for (uint32_t i = 0; i < 10; i++) { range_constraints.push_back(RangeConstraint{ .witness = i, .num_bits = 8, }); + range_opcode_indices.push_back(current_opcode++); } std::array signature; @@ -313,6 +409,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) .witness = value, .num_bits = 8, }); + range_opcode_indices.push_back(current_opcode++); } SchnorrConstraint schnorr_constraint{ @@ -325,7 +422,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) AcirFormat constraint_system{ .varnum = 81, .recursive = false, - .num_acir_opcodes = 75, + .num_acir_opcodes = 76, .public_inputs = {}, .logic_constraints = {}, .range_constraints = range_constraints, @@ -361,6 +458,34 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) } }, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = + AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = range_opcode_indices, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = { current_opcode++ }, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { current_opcode++ }, + .quad_constraints = {}, + .block_constraints = {}, + }, }; std::string message_string = "tenletters"; @@ -469,6 +594,34 @@ TEST_F(AcirFormatTests, TestVarKeccak) .poly_triple_constraints = { dummy }, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = + AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = { 0, 1, 2, 3 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = { 4 }, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 5 }, + .quad_constraints = {}, + .block_constraints = {}, + }, }; WitnessVector witness{ 4, 2, 6, 2 }; @@ -518,7 +671,34 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) .bigint_operations = {}, .poly_triple_constraints = {}, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = { 0 }, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; WitnessVector witness{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp index 69d40ffe965..d9f040c9918 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp @@ -160,6 +160,52 @@ std::tuple generate_big_int_op_constraint_with return { constraint, to_bytes }; } +AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices() +{ + return AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + }; +} + +void mock_opcode_indices(AcirFormat& constraint_system) +{ + size_t current_opcode = 0; + for (size_t i = 0; i < constraint_system.bigint_operations.size(); i++) { + constraint_system.original_opcode_indices.bigint_operations.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); i++) { + constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); i++) { + constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints.push_back(current_opcode++); + } + constraint_system.num_acir_opcodes = static_cast(current_opcode); +} + // Based on TestBigIntConstraintSimple, we generate constraints for multiple operations at the same time. TEST_F(BigIntTests, TestBigIntConstraintMultiple) { @@ -199,12 +245,14 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple) .poly_triple_constraints = {}, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), }; apply_constraints(constraint_system, contraints); apply_constraints(constraint_system, contraints2); apply_constraints(constraint_system, contraints3); apply_constraints(constraint_system, contraints4); apply_constraints(constraint_system, contraints5); + mock_opcode_indices(constraint_system); constraint_system.varnum = static_cast(witness.size() + 1); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness); @@ -270,8 +318,9 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple) .poly_triple_constraints = {}, .quad_constraints = {}, .block_constraints = {}, - + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 0, 3, 6, 3, 0, @@ -326,6 +375,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse) .poly_triple_constraints = {}, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), }; apply_constraints(constraint_system, contraints); apply_constraints(constraint_system, contraints2); @@ -336,6 +386,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse) constraint_system.bigint_to_le_bytes_constraints.push_back(get<1>(contraints5)); constraint_system.bigint_operations.push_back(get<0>(contraints5)); constraint_system.varnum = static_cast(witness.size() + 1); + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness); @@ -386,6 +437,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2) .poly_triple_constraints = {}, .quad_constraints = {}, .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), }; apply_constraints(constraint_system, contraints); apply_constraints(constraint_system, contraints2); @@ -396,6 +448,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2) constraint_system.bigint_to_le_bytes_constraints.push_back(get<1>(contraints5)); constraint_system.bigint_operations.push_back(get<0>(contraints5)); constraint_system.varnum = static_cast(witness.size() + 1); + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness); @@ -467,8 +520,9 @@ TEST_F(BigIntTests, TestBigIntDIV) .poly_triple_constraints = {}, .quad_constraints = {}, .block_constraints = {}, - + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 0, 6, 3, 2, 0, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp index 5da46acc95e..cbb206de08a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp @@ -137,37 +137,62 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint) BlockConstraint block; WitnessVector witness_values; size_t num_variables = generate_block_constraint(block, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 7, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { block }, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 7, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { { 0, 1, 2, 3, 4, 5, 6 } }, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -186,37 +211,62 @@ TEST_F(MegaHonk, Databus) size_t num_variables = generate_block_constraint(block, witness_values); block.type = BlockType::CallData; - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { block }, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { { 0 } }, + } }; // Construct a bberg circuit from the acir representation auto circuit = acir_format::create_circuit(constraint_system, 0, witness_values); @@ -290,37 +340,62 @@ TEST_F(MegaHonk, DatabusReturn) .q_c = 0, }; - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { assert_equal }, - .quad_constraints = {}, - .block_constraints = { block }, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 2, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { assert_equal }, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 0 }, + .quad_constraints = {}, + .block_constraints = { { 1 } }, + } }; // Construct a bberg circuit from the acir representation auto circuit = acir_format::create_circuit(constraint_system, 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp index c006186494d..e6b73692c95 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp @@ -59,37 +59,62 @@ TEST_F(EcOperations, TestECOperations) WitnessVector witness_values; size_t num_variables = generate_ec_add_constraint(ec_add_constraint, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables + 1), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = { ec_add_constraint }, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables + 1), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = { ec_add_constraint }, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = { 0 }, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -145,37 +170,62 @@ TEST_F(EcOperations, TestECMultiScalarMul) }; size_t num_variables = witness_values.size(); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables + 1), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = { msm_constrain }, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { assert_equal }, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables + 1), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = { msm_constrain }, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { assert_equal }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = { 0 }, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 1 }, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp index 88140210210..8afb8b2dd7e 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp @@ -91,37 +91,62 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed) EcdsaSecp256k1Constraint ecdsa_k1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_k1_constraint, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { 0 }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -143,37 +168,62 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier) EcdsaSecp256k1Constraint ecdsa_k1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_k1_constraint, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { 0 }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system); } @@ -190,37 +240,62 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail) // tamper with signature witness_values[witness_values.size() - 20] += 1; - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { 0 }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); EXPECT_EQ(builder.get_variable(ecdsa_k1_constraint.result), 0); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp index ac0d9a31938..0e7ade8106b 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp @@ -125,37 +125,62 @@ TEST(ECDSASecp256r1, test_hardcoded) size_t num_variables = generate_r1_constraints(ecdsa_r1_constraint, witness_values, pub_key_x, pub_key_y, hashed_message, signature); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { 0 }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; secp256r1::g1::affine_element pub_key = { pub_key_x, pub_key_y }; bool we_ballin = @@ -179,37 +204,62 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed) EcdsaSecp256r1Constraint ecdsa_r1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_r1_constraint, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { 0 }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -231,37 +281,62 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier) EcdsaSecp256r1Constraint ecdsa_r1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_r1_constraint, witness_values); - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { 0 }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system); } @@ -278,37 +353,62 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail) // tamper with signature witness_values[witness_values.size() - 20] += 1; - AcirFormat constraint_system{ - .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + AcirFormat constraint_system{ .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { 0 }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp index 3b653fecc18..dab8d7ba666 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp @@ -114,7 +114,34 @@ class AcirHonkRecursionConstraint : public ::testing::Test { .bigint_operations = {}, .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = { 0 }, + .range_constraints = { 1, 2 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 3, 4, 5, 6 }, + .quad_constraints = {}, + .block_constraints = {}, + } }; uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -244,6 +271,9 @@ class AcirHonkRecursionConstraint : public ::testing::Test { witness_offset = key_indices_start_idx + key_witnesses.size(); } + std::vector honk_recursion_opcode_indices(honk_recursion_constraints.size()); + std::iota(honk_recursion_opcode_indices.begin(), honk_recursion_opcode_indices.end(), 0); + AcirFormat constraint_system{ .varnum = static_cast(witness.size()), .recursive = false, .num_acir_opcodes = static_cast(honk_recursion_constraints.size()), @@ -272,7 +302,34 @@ class AcirHonkRecursionConstraint : public ::testing::Test { .bigint_operations = {}, .poly_triple_constraints = {}, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = honk_recursion_opcode_indices, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto outer_circuit = create_circuit(constraint_system, /*size_hint*/ 0, witness, /*honk recursion*/ true); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp index dd01d0b9b3e..2ca3e1f2ef6 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp @@ -61,7 +61,34 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation) .bigint_operations = {}, .poly_triple_constraints = {}, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = { 0 }, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; WitnessVector witness{ 1, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp index 2715bc873a9..92b692eadcb 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp @@ -112,7 +112,34 @@ Builder create_inner_circuit() .bigint_operations = {}, .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = { 0 }, + .range_constraints = { 1, 2 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { 3, 4, 5, 6 }, + .quad_constraints = {}, + .block_constraints = {}, + } }; uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -239,6 +266,9 @@ Builder create_outer_circuit(std::vector& inner_circuits) witness_offset = key_indices_start_idx + key_witnesses.size(); } + std::vector recursion_opcode_indices(recursion_constraints.size()); + std::iota(recursion_opcode_indices.begin(), recursion_opcode_indices.end(), 0); + AcirFormat constraint_system{ .varnum = static_cast(witness.size()), .recursive = false, .num_acir_opcodes = static_cast(recursion_constraints.size()), @@ -267,7 +297,34 @@ Builder create_outer_circuit(std::vector& inner_circuits) .bigint_operations = {}, .poly_triple_constraints = {}, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = recursion_opcode_indices, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; auto outer_circuit = create_circuit(constraint_system, /*size_hint*/ 0, witness); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp index 902ac2944e9..d501143c2c3 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp @@ -62,7 +62,34 @@ TEST_F(Sha256Tests, TestSha256Compression) .bigint_operations = {}, .poly_triple_constraints = {}, .quad_constraints = {}, - .block_constraints = {} }; + .block_constraints = {}, + .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = { 0 }, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + } }; WitnessVector witness{ 0, 0, From 073039d0cb38f6c52fa2e132cb8c9a0e4753403e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 14:32:15 +0000 Subject: [PATCH 24/43] refactor: extract to a mock file --- .../dsl/acir_format/acir_format.test.cpp | 241 +++--------- .../dsl/acir_format/acir_format_mocks.cpp | 118 ++++++ .../dsl/acir_format/acir_format_mocks.hpp | 5 + .../acir_format/bigint_constraint.test.cpp | 47 +-- .../dsl/acir_format/block_constraint.test.cpp | 268 +++++-------- .../dsl/acir_format/ec_operations.test.cpp | 179 ++++----- .../dsl/acir_format/ecdsa_secp256k1.test.cpp | 268 +++++-------- .../dsl/acir_format/ecdsa_secp256r1.test.cpp | 358 +++++++----------- .../honk_recursion_constraint.test.cpp | 180 ++++----- .../acir_format/poseidon2_constraint.test.cpp | 90 ++--- .../acir_format/recursion_constraint.test.cpp | 179 ++++----- .../acir_format/sha256_constraint.test.cpp | 90 ++--- 12 files changed, 771 insertions(+), 1252 deletions(-) create mode 100644 barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp create mode 100644 barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.hpp diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 906c52fd863..3658f8375f4 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -2,6 +2,7 @@ #include #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/common/streams.hpp" #include "barretenberg/crypto/schnorr/schnorr.hpp" #include "barretenberg/plonk/composer/standard_composer.hpp" @@ -64,36 +65,9 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs) .poly_triple_constraints = { constraint }, .quad_constraints = {}, .block_constraints = {}, - .original_opcode_indices = - AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 0 }, - .quad_constraints = {}, - .block_constraints = {}, - }, + .original_opcode_indices = create_empty_original_opcode_indices(), }; - + mock_opcode_indices(constraint_system); WitnessVector witness{ 0, 0, 1 }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness); @@ -212,35 +186,9 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit) .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, .quad_constraints = {}, .block_constraints = {}, - .original_opcode_indices = - AcirFormatOriginalOpcodeIndices{ - .logic_constraints = { 0 }, - .range_constraints = { 1, 2 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 3, 4, 5, 6 }, - .quad_constraints = {}, - .block_constraints = {}, - }, + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -326,35 +274,9 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) } }, .quad_constraints = {}, .block_constraints = {}, - .original_opcode_indices = - AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = range_opcode_indices, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = { current_opcode++ }, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { current_opcode++ }, - .quad_constraints = {}, - .block_constraints = {}, - }, + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); std::string message_string = "tenletters"; schnorr_key_pair account; @@ -458,35 +380,9 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) } }, .quad_constraints = {}, .block_constraints = {}, - .original_opcode_indices = - AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = range_opcode_indices, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = { current_opcode++ }, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { current_opcode++ }, - .quad_constraints = {}, - .block_constraints = {}, - }, + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); std::string message_string = "tenletters"; schnorr_key_pair account; @@ -594,35 +490,9 @@ TEST_F(AcirFormatTests, TestVarKeccak) .poly_triple_constraints = { dummy }, .quad_constraints = {}, .block_constraints = {}, - .original_opcode_indices = - AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = { 0, 1, 2, 3 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = { 4 }, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 5 }, - .quad_constraints = {}, - .block_constraints = {}, - }, + .original_opcode_indices = create_empty_original_opcode_indices(), }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 4, 2, 6, 2 }; auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness); @@ -643,62 +513,39 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 }, }; - AcirFormat constraint_system{ .varnum = 51, - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = { keccak_permutation }, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = { 0 }, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = 51, + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = { keccak_permutation }, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp new file mode 100644 index 00000000000..f53ff85d6e7 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp @@ -0,0 +1,118 @@ +#include "acir_format.hpp" + +acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices() +{ + return acir_format::AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + }; +} + +void mock_opcode_indices(acir_format::AcirFormat& constraint_system) +{ + size_t current_opcode = 0; + for (size_t i = 0; i < constraint_system.logic_constraints.size(); i++) { + constraint_system.original_opcode_indices.logic_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.range_constraints.size(); i++) { + constraint_system.original_opcode_indices.range_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.aes128_constraints.size(); i++) { + constraint_system.original_opcode_indices.aes128_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.sha256_constraints.size(); i++) { + constraint_system.original_opcode_indices.sha256_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.sha256_compression.size(); i++) { + constraint_system.original_opcode_indices.sha256_compression.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); i++) { + constraint_system.original_opcode_indices.schnorr_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); i++) { + constraint_system.original_opcode_indices.ecdsa_k1_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); i++) { + constraint_system.original_opcode_indices.ecdsa_r1_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); i++) { + constraint_system.original_opcode_indices.blake2s_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.blake3_constraints.size(); i++) { + constraint_system.original_opcode_indices.blake3_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.keccak_constraints.size(); i++) { + constraint_system.original_opcode_indices.keccak_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.keccak_permutations.size(); i++) { + constraint_system.original_opcode_indices.keccak_permutations.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); i++) { + constraint_system.original_opcode_indices.pedersen_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); i++) { + constraint_system.original_opcode_indices.pedersen_hash_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); i++) { + constraint_system.original_opcode_indices.poseidon2_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); i++) { + constraint_system.original_opcode_indices.multi_scalar_mul_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); i++) { + constraint_system.original_opcode_indices.ec_add_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.recursion_constraints.size(); i++) { + constraint_system.original_opcode_indices.recursion_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.honk_recursion_constraints.size(); i++) { + constraint_system.original_opcode_indices.honk_recursion_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); i++) { + constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); i++) { + constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.bigint_operations.size(); i++) { + constraint_system.original_opcode_indices.bigint_operations.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); i++) { + constraint_system.original_opcode_indices.poly_triple_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.quad_constraints.size(); i++) { + constraint_system.original_opcode_indices.quad_constraints.push_back(current_opcode++); + } + for (size_t i = 0; i < constraint_system.block_constraints.size(); i++) { + std::vector block_indices; + for (size_t j = 0; j < constraint_system.block_constraints[i].trace.size(); j++) { + block_indices.push_back(current_opcode++); + } + constraint_system.original_opcode_indices.block_constraints.push_back(block_indices); + } + + constraint_system.num_acir_opcodes = static_cast(current_opcode); +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.hpp new file mode 100644 index 00000000000..6a58c34aa0d --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.hpp @@ -0,0 +1,5 @@ +#include "acir_format.hpp" + +acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices(); + +void mock_opcode_indices(acir_format::AcirFormat& constraint_system); \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp index d9f040c9918..4ce4f77b934 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp @@ -1,5 +1,6 @@ #include "bigint_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/circuit_checker/circuit_checker.hpp" #include "barretenberg/numeric/uint256/uint256.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" @@ -160,52 +161,6 @@ std::tuple generate_big_int_op_constraint_with return { constraint, to_bytes }; } -AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices() -{ - return AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; -} - -void mock_opcode_indices(AcirFormat& constraint_system) -{ - size_t current_opcode = 0; - for (size_t i = 0; i < constraint_system.bigint_operations.size(); i++) { - constraint_system.original_opcode_indices.bigint_operations.push_back(current_opcode++); - } - for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); i++) { - constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints.push_back(current_opcode++); - } - for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); i++) { - constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints.push_back(current_opcode++); - } - constraint_system.num_acir_opcodes = static_cast(current_opcode); -} - // Based on TestBigIntConstraintSimple, we generate constraints for multiple operations at the same time. TEST_F(BigIntTests, TestBigIntConstraintMultiple) { diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp index cbb206de08a..9affaf9f9f5 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp @@ -1,5 +1,6 @@ #include "block_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp" @@ -137,62 +138,39 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint) BlockConstraint block; WitnessVector witness_values; size_t num_variables = generate_block_constraint(block, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 7, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { block }, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { { 0, 1, 2, 3, 4, 5, 6 } }, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 7, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -211,62 +189,39 @@ TEST_F(MegaHonk, Databus) size_t num_variables = generate_block_constraint(block, witness_values); block.type = BlockType::CallData; - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { block }, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = { { 0 } }, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); // Construct a bberg circuit from the acir representation auto circuit = acir_format::create_circuit(constraint_system, 0, witness_values); @@ -340,62 +295,39 @@ TEST_F(MegaHonk, DatabusReturn) .q_c = 0, }; - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 2, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { assert_equal }, - .quad_constraints = {}, - .block_constraints = { block }, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 0 }, - .quad_constraints = {}, - .block_constraints = { { 1 } }, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 2, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { assert_equal }, + .quad_constraints = {}, + .block_constraints = { block }, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); // Construct a bberg circuit from the acir representation auto circuit = acir_format::create_circuit(constraint_system, 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp index e6b73692c95..1dacfe85a0f 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp @@ -1,5 +1,6 @@ #include "ec_operations.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/circuit_checker/circuit_checker.hpp" #include "barretenberg/plonk/composer/standard_composer.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" @@ -59,62 +60,39 @@ TEST_F(EcOperations, TestECOperations) WitnessVector witness_values; size_t num_variables = generate_ec_add_constraint(ec_add_constraint, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables + 1), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = { ec_add_constraint }, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = { 0 }, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables + 1), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = { ec_add_constraint }, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -170,62 +148,39 @@ TEST_F(EcOperations, TestECMultiScalarMul) }; size_t num_variables = witness_values.size(); - AcirFormat constraint_system{ .varnum = static_cast(num_variables + 1), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = { msm_constrain }, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { assert_equal }, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = { 0 }, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 1 }, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables + 1), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = { msm_constrain }, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { assert_equal }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp index 8afb8b2dd7e..6026f0cf31e 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp @@ -1,5 +1,6 @@ #include "ecdsa_secp256k1.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/crypto/ecdsa/ecdsa.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" @@ -91,62 +92,39 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed) EcdsaSecp256k1Constraint ecdsa_k1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_k1_constraint, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { 0 }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -168,62 +146,39 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier) EcdsaSecp256k1Constraint ecdsa_k1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_k1_constraint, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { 0 }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system); } @@ -240,62 +195,39 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail) // tamper with signature witness_values[witness_values.size() - 20] += 1; - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { ecdsa_k1_constraint }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = { 0 }, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = { ecdsa_k1_constraint }, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); EXPECT_EQ(builder.get_variable(ecdsa_k1_constraint.result), 0); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp index 0e7ade8106b..68f0f416dbe 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp @@ -1,5 +1,6 @@ #include "ecdsa_secp256r1.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/crypto/ecdsa/ecdsa.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" @@ -125,62 +126,39 @@ TEST(ECDSASecp256r1, test_hardcoded) size_t num_variables = generate_r1_constraints(ecdsa_r1_constraint, witness_values, pub_key_x, pub_key_y, hashed_message, signature); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { 0 }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); secp256r1::g1::affine_element pub_key = { pub_key_x, pub_key_y }; bool we_ballin = @@ -204,62 +182,39 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed) EcdsaSecp256r1Constraint ecdsa_r1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_r1_constraint, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { 0 }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); @@ -281,62 +236,40 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier) EcdsaSecp256r1Constraint ecdsa_r1_constraint; WitnessVector witness_values; size_t num_variables = generate_ecdsa_constraint(ecdsa_r1_constraint, witness_values); - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { 0 }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); + auto builder = create_circuit(constraint_system); } @@ -353,62 +286,39 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail) // tamper with signature witness_values[witness_values.size() - 20] += 1; - AcirFormat constraint_system{ .varnum = static_cast(num_variables), - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { ecdsa_r1_constraint }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = { 0 }, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(num_variables), + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = { ecdsa_r1_constraint }, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto builder = create_circuit(constraint_system, /*size_hint*/ 0, witness_values); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp index dab8d7ba666..61259910828 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp @@ -1,5 +1,6 @@ #include "honk_recursion_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/sumcheck/instance/prover_instance.hpp" #include "barretenberg/ultra_honk/ultra_prover.hpp" #include "barretenberg/ultra_honk/ultra_verifier.hpp" @@ -86,62 +87,39 @@ class AcirHonkRecursionConstraint : public ::testing::Test { .q_c = 1, }; - 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 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = { 0 }, - .range_constraints = { 1, 2 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 3, 4, 5, 6 }, - .quad_constraints = {}, - .block_constraints = {}, - } }; + 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 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -274,63 +252,39 @@ class AcirHonkRecursionConstraint : public ::testing::Test { std::vector honk_recursion_opcode_indices(honk_recursion_constraints.size()); std::iota(honk_recursion_opcode_indices.begin(), honk_recursion_opcode_indices.end(), 0); - AcirFormat constraint_system{ .varnum = static_cast(witness.size()), - .recursive = false, - .num_acir_opcodes = static_cast(honk_recursion_constraints.size()), - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = honk_recursion_constraints, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = honk_recursion_opcode_indices, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; - + AcirFormat constraint_system{ + .varnum = static_cast(witness.size()), + .recursive = false, + .num_acir_opcodes = static_cast(honk_recursion_constraints.size()), + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = honk_recursion_constraints, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto outer_circuit = create_circuit(constraint_system, /*size_hint*/ 0, witness, /*honk recursion*/ true); return outer_circuit; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp index 2ca3e1f2ef6..cc144fa6fd9 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp @@ -1,5 +1,6 @@ #include "poseidon2_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/numeric/uint256/uint256.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" @@ -33,62 +34,39 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation) .len = 4, }; - AcirFormat constraint_system{ .varnum = 9, - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = { poseidon2_constraint }, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = { 0 }, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = 9, + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = { poseidon2_constraint }, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 1, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp index 92b692eadcb..72ea7e939ac 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp @@ -1,5 +1,6 @@ #include "recursion_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp" @@ -84,62 +85,39 @@ Builder create_inner_circuit() .q_c = 1, }; - 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 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = { 0 }, - .range_constraints = { 1, 2 }, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = { 3, 4, 5, 6 }, - .quad_constraints = {}, - .block_constraints = {}, - } }; + 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 }, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); uint256_t inverse_of_five = fr(5).invert(); WitnessVector witness{ @@ -269,62 +247,39 @@ Builder create_outer_circuit(std::vector& inner_circuits) std::vector recursion_opcode_indices(recursion_constraints.size()); std::iota(recursion_opcode_indices.begin(), recursion_opcode_indices.end(), 0); - AcirFormat constraint_system{ .varnum = static_cast(witness.size()), - .recursive = false, - .num_acir_opcodes = static_cast(recursion_constraints.size()), - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = recursion_constraints, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = recursion_opcode_indices, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = static_cast(witness.size()), + .recursive = false, + .num_acir_opcodes = static_cast(recursion_constraints.size()), + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = recursion_constraints, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); auto outer_circuit = create_circuit(constraint_system, /*size_hint*/ 0, witness); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp index d501143c2c3..72936adc776 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp @@ -1,5 +1,6 @@ #include "sha256_constraint.hpp" #include "acir_format.hpp" +#include "acir_format_mocks.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp" @@ -34,62 +35,39 @@ TEST_F(Sha256Tests, TestSha256Compression) .result = { 25, 26, 27, 28, 29, 30, 31, 32 }, }; - AcirFormat constraint_system{ .varnum = 34, - .recursive = false, - .num_acir_opcodes = 1, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = { sha256_compression }, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - .original_opcode_indices = AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = { 0 }, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - } }; + AcirFormat constraint_system{ + .varnum = 34, + .recursive = false, + .num_acir_opcodes = 1, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = { sha256_compression }, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); WitnessVector witness{ 0, 0, From 553272a5f0e5e5d03043304bfd9005b7ddc0e09c Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 11 Jun 2024 14:37:27 +0000 Subject: [PATCH 25/43] update comment --- .../cpp/src/barretenberg/dsl/acir_format/acir_format.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index de8b22d8f1d..1c8ac4afb51 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -111,7 +111,7 @@ struct AcirFormat { std::vector block_constraints; // Number of gates added to the circuit per original opcode. - // Has the same length as the opcode vector. + // Has length equal to num_acir_opcodes. std::vector gates_per_opcode = {}; // Indices of the original opcode that originated each constraint in AcirFormat. From f2eec54fa6e611ce1cd562e5272917128f4a7ef9 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 09:36:55 +0000 Subject: [PATCH 26/43] conditional gate counting --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 2 +- .../dsl/acir_format/acir_format.cpp | 22 ++++-- .../dsl/acir_format/acir_format.hpp | 17 +++-- .../dsl/acir_format/acir_format.test.cpp | 69 +++++++++++++++++++ .../dsl/acir_proofs/acir_composer.cpp | 10 ++- .../dsl/acir_proofs/acir_composer.hpp | 4 +- 6 files changed, 105 insertions(+), 19 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 91771a4c8a1..507cf9dfaa7 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -446,7 +446,7 @@ void gateCount(const std::string& bytecodePath, bool honk_recursion) size_t i = 0; for (auto constraint_system : constraint_systems) { acir_proofs::AcirComposer acir_composer(0, verbose_logging); - acir_composer.create_circuit(constraint_system); + acir_composer.create_circuit(constraint_system, {}, true); auto circuit_size = acir_composer.get_total_circuit_size(); // Build individual circuit report diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 7a384be60ad..3e9464916bf 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -17,12 +17,16 @@ template void build_constraints(Builder& builder, AcirFormat& constraint_system, bool has_valid_witness_assignments, - bool honk_recursion) + bool honk_recursion, + bool collect_gates_per_opcode) { constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); size_t prev_gate_count = 0; - auto compute_gate_diff = [&]() { + auto compute_gate_diff = [&]() -> size_t { + if (!collect_gates_per_opcode) { + return 0; + } size_t new_gate_count = builder.get_num_gates(); size_t diff = new_gate_count - prev_gate_count; prev_gate_count = new_gate_count; @@ -424,14 +428,16 @@ UltraCircuitBuilder create_circuit(AcirFormat& constraint_system, size_t size_hint, WitnessVector const& witness, bool honk_recursion, - [[maybe_unused]] std::shared_ptr) + [[maybe_unused]] std::shared_ptr, + bool collect_gates_per_opcode) { Builder builder{ size_hint, witness, constraint_system.public_inputs, constraint_system.varnum, constraint_system.recursive }; bool has_valid_witness_assignments = !witness.empty(); - build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); + build_constraints( + builder, constraint_system, has_valid_witness_assignments, honk_recursion, collect_gates_per_opcode); return builder; }; @@ -450,18 +456,20 @@ MegaCircuitBuilder create_circuit(AcirFormat& constraint_system, [[maybe_unused]] size_t size_hint, WitnessVector const& witness, bool honk_recursion, - std::shared_ptr op_queue) + std::shared_ptr op_queue, + bool collect_gates_per_opcode) { // Construct a builder using the witness and public input data from acir and with the goblin-owned op_queue auto builder = MegaCircuitBuilder{ op_queue, witness, constraint_system.public_inputs, constraint_system.varnum }; // Populate constraints in the builder via the data in constraint_system bool has_valid_witness_assignments = !witness.empty(); - acir_format::build_constraints(builder, constraint_system, has_valid_witness_assignments, honk_recursion); + acir_format::build_constraints( + builder, constraint_system, has_valid_witness_assignments, honk_recursion, collect_gates_per_opcode); return builder; }; -template void build_constraints(MegaCircuitBuilder&, AcirFormat&, bool, bool); +template void build_constraints(MegaCircuitBuilder&, AcirFormat&, bool, bool, bool); } // namespace acir_format diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 1c8ac4afb51..80edf92270b 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -191,14 +191,17 @@ Builder create_circuit(AcirFormat& constraint_system, size_t size_hint = 0, WitnessVector const& witness = {}, bool honk_recursion = false, - std::shared_ptr op_queue = std::make_shared()); + std::shared_ptr op_queue = std::make_shared(), + bool collect_gates_per_opcode = false); template -void build_constraints(Builder& builder, - AcirFormat& constraint_system, - bool has_valid_witness_assignments, - bool honk_recursion = false); // honk_recursion means we will honk to recursively verify this - // circuit. This distinction is needed to not add the default - // aggregation object when we're not using the honk RV. +void build_constraints( + Builder& builder, + AcirFormat& constraint_system, + bool has_valid_witness_assignments, + bool honk_recursion = false, + bool collect_gates_per_opcode = false); // honk_recursion means we will honk to recursively verify this + // circuit. This distinction is needed to not add the default + // aggregation object when we're not using the honk RV. } // namespace acir_format diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 3658f8375f4..6085671fb0a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "acir_format.hpp" @@ -9,6 +10,7 @@ #include "barretenberg/plonk/composer/ultra_composer.hpp" #include "barretenberg/plonk/proof_system/types/proof.hpp" #include "barretenberg/serialize/test_helper.hpp" +#include "barretenberg/stdlib_circuit_builders/op_queue/ecc_op_queue.hpp" #include "ecdsa_secp256k1.hpp" using namespace bb; @@ -561,3 +563,70 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) EXPECT_EQ(verifier.verify_proof(proof), true); } + +TEST_F(AcirFormatTests, TestCollectsGateCounts) +{ + + // Witness 0 + witness 1 = witness 2 + poly_triple first_gate{ + .a = 0, + .b = 1, + .c = 2, + .q_m = 0, + .q_l = 1, + .q_r = 1, + .q_o = -1, + .q_c = 0, + }; + + // Witness 1 = 27 + poly_triple second_gate{ + .a = 1, + .b = 0, + .c = 0, + .q_m = 0, + .q_l = 1, + .q_r = 0, + .q_o = 0, + .q_c = -27, + }; + + AcirFormat constraint_system{ + .varnum = 4, + .recursive = false, + .num_acir_opcodes = 2, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = { first_gate, second_gate }, + .quad_constraints = {}, + .block_constraints = {}, + .original_opcode_indices = create_empty_original_opcode_indices(), + }; + mock_opcode_indices(constraint_system); + WitnessVector witness{ 5, 27, 32 }; + auto builder = + create_circuit(constraint_system, /*size_hint*/ 0, witness, false, std::make_shared(), true); + + EXPECT_EQ(constraint_system.gates_per_opcode, std::vector({ 2, 1 })); +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index b54244cf9bb..3775ac65f83 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -28,10 +28,13 @@ AcirComposer::AcirComposer(size_t size_hint, bool verbose) * @param witness */ template -void AcirComposer::create_circuit(acir_format::AcirFormat& constraint_system, WitnessVector const& witness) +void AcirComposer::create_circuit(acir_format::AcirFormat& constraint_system, + WitnessVector const& witness, + bool collect_gates_per_opcode) { vinfo("building circuit..."); - builder_ = acir_format::create_circuit(constraint_system, size_hint_, witness); + builder_ = acir_format::create_circuit( + constraint_system, size_hint_, witness, false, std::make_shared(), collect_gates_per_opcode); vinfo("gates: ", builder_.get_total_circuit_size()); vinfo("circuit is recursive friendly: ", builder_.is_recursive_circuit); } @@ -144,6 +147,7 @@ std::vector AcirComposer::serialize_verification_key_into_fields() } template void AcirComposer::create_circuit(acir_format::AcirFormat& constraint_system, - WitnessVector const& witness); + WitnessVector const& witness, + bool collect_gates_per_opcode); } // namespace acir_proofs diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index e15c59fa60d..f53011ec374 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -17,7 +17,9 @@ class AcirComposer { AcirComposer(size_t size_hint = 0, bool verbose = true); template - void create_circuit(acir_format::AcirFormat& constraint_system, WitnessVector const& witness = {}); + void create_circuit(acir_format::AcirFormat& constraint_system, + WitnessVector const& witness = {}, + bool collect_gates_per_opcode = false); std::shared_ptr init_proving_key(); From a48d748954ef2c777cb6e2cc3cea452250d497c2 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 10:14:37 +0000 Subject: [PATCH 27/43] better resizing --- .../cpp/src/barretenberg/dsl/acir_format/acir_format.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 3e9464916bf..b827eff2ea4 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -20,7 +20,7 @@ void build_constraints(Builder& builder, bool honk_recursion, bool collect_gates_per_opcode) { - constraint_system.gates_per_opcode = std::vector(constraint_system.num_acir_opcodes); + constraint_system.gates_per_opcode.resize(constraint_system.num_acir_opcodes, 0); size_t prev_gate_count = 0; auto compute_gate_diff = [&]() -> size_t { From 86e008ecd93c879614f64a7432ff2ebe85745a01 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 11:12:38 +0000 Subject: [PATCH 28/43] better conditioning of the gate tracking --- .../dsl/acir_format/acir_format.cpp | 120 +++++++++--------- 1 file changed, 63 insertions(+), 57 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index b827eff2ea4..91617d4ebcb 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -20,7 +20,9 @@ void build_constraints(Builder& builder, bool honk_recursion, bool collect_gates_per_opcode) { - constraint_system.gates_per_opcode.resize(constraint_system.num_acir_opcodes, 0); + if (collect_gates_per_opcode) { + constraint_system.gates_per_opcode.resize(constraint_system.num_acir_opcodes, 0); + } size_t prev_gate_count = 0; auto compute_gate_diff = [&]() -> size_t { @@ -33,19 +35,25 @@ void build_constraints(Builder& builder, return diff; }; + auto track_gate_diff = [&](std::vector& gates_per_opcode, size_t opcode_index) -> void { + if (collect_gates_per_opcode) { + gates_per_opcode[opcode_index] = compute_gate_diff(); + } + }; + // Add arithmetic gates for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { const auto& constraint = constraint_system.poly_triple_constraints[i]; builder.create_poly_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.poly_triple_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.poly_triple_constraints[i]); } for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { const auto& constraint = constraint_system.quad_constraints[i]; builder.create_big_mul_gate(constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.quad_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.quad_constraints[i]); } // Add logic constraint @@ -53,142 +61,144 @@ void build_constraints(Builder& builder, const auto& constraint = constraint_system.logic_constraints[i]; create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.logic_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.logic_constraints[i]); } // Add range constraint for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { const auto& constraint = constraint_system.range_constraints[i]; builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.range_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.range_constraints[i]); } // Add aes128 constraints for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { const auto& constraint = constraint_system.aes128_constraints[i]; create_aes128_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.aes128_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.aes128_constraints[i]); } // Add sha256 constraints for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { const auto& constraint = constraint_system.sha256_constraints[i]; create_sha256_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.sha256_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.sha256_constraints[i]); } for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.sha256_compression[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.sha256_compression[i]); } // Add schnorr constraints for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { const auto& constraint = constraint_system.schnorr_constraints[i]; create_schnorr_verify_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.schnorr_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.schnorr_constraints[i]); } // Add ECDSA k1 constraints for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ecdsa_k1_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.ecdsa_k1_constraints[i]); } // Add ECDSA r1 constraints for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ecdsa_r1_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.ecdsa_r1_constraints[i]); } // Add blake2s constraints for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { const auto& constraint = constraint_system.blake2s_constraints[i]; create_blake2s_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.blake2s_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.blake2s_constraints[i]); } // Add blake3 constraints for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { const auto& constraint = constraint_system.blake3_constraints[i]; create_blake3_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.blake3_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.blake3_constraints[i]); } // Add keccak constraints for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { const auto& constraint = constraint_system.keccak_constraints[i]; create_keccak_constraints(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.keccak_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.keccak_constraints[i]); } for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { const auto& constraint = constraint_system.keccak_permutations[i]; create_keccak_permutations(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.keccak_permutations[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.keccak_permutations[i]); } // Add pedersen constraints for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_constraints[i]; create_pedersen_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.pedersen_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.pedersen_constraints[i]); } for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { const auto& constraint = constraint_system.pedersen_hash_constraints[i]; create_pedersen_hash_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.pedersen_hash_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.pedersen_hash_constraints[i]); } for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { const auto& constraint = constraint_system.poseidon2_constraints[i]; create_poseidon2_permutations(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.poseidon2_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.poseidon2_constraints[i]); } // Add multi scalar mul constraints for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; create_multi_scalar_mul_constraint(builder, constraint); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.multi_scalar_mul_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.multi_scalar_mul_constraints[i]); } // Add ec add constraints for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { const auto& constraint = constraint_system.ec_add_constraints[i]; create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.ec_add_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.ec_add_constraints[i]); } // Add block constraints for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { const auto& constraint = constraint_system.block_constraints[i]; create_block_constraints(builder, constraint, has_valid_witness_assignments); - size_t avg_gates_per_opcode = - compute_gate_diff() / constraint_system.original_opcode_indices.block_constraints[i].size(); - for (size_t opcode_index : constraint_system.original_opcode_indices.block_constraints[i]) { - constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; + if (collect_gates_per_opcode) { + size_t avg_gates_per_opcode = + compute_gate_diff() / constraint_system.original_opcode_indices.block_constraints[i].size(); + for (size_t opcode_index : constraint_system.original_opcode_indices.block_constraints[i]) { + constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; + } } } @@ -198,24 +208,22 @@ void build_constraints(Builder& builder, for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); - constraint_system - .gates_per_opcode[constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints[i]); } for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { const auto& constraint = constraint_system.bigint_operations[i]; create_bigint_operations_constraint(constraint, dsl_bigints, has_valid_witness_assignments); - constraint_system.gates_per_opcode[constraint_system.original_opcode_indices.bigint_operations[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.bigint_operations[i]); } for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); - constraint_system - .gates_per_opcode[constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints[i]); } // RecursionConstraint @@ -290,9 +298,8 @@ void build_constraints(Builder& builder, nested_aggregation_object, has_valid_witness_assignments); current_input_aggregation_object = current_output_aggregation_object; - constraint_system - .gates_per_opcode[constraint_system.original_opcode_indices.recursion_constraints[constraint_idx]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.recursion_constraints[constraint_idx]); } // Now that the circuit has been completely built, we add the output aggregation as public @@ -358,9 +365,8 @@ void build_constraints(Builder& builder, current_aggregation_object, nested_aggregation_object, has_valid_witness_assignments); - constraint_system - .gates_per_opcode[constraint_system.original_opcode_indices.honk_recursion_constraints[i]] = - compute_gate_diff(); + track_gate_diff(constraint_system.gates_per_opcode, + constraint_system.original_opcode_indices.honk_recursion_constraints[i]); } // Now that the circuit has been completely built, we add the output aggregation as public From 6f1a07e7485ada472fa68b330d704036e2676adc Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 20:42:31 +0000 Subject: [PATCH 29/43] use imported line and column helper --- noir/noir-repo/Cargo.lock | 1 + .../compiler/noirc_errors/src/reporter.rs | 6 +-- noir/noir-repo/tooling/profiler/Cargo.toml | 1 + .../profiler/src/cli/gates_flamegraph_cmd.rs | 38 ++++++++----------- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index 641a99f41dd..bd1df426ffe 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2736,6 +2736,7 @@ dependencies = [ "im", "inferno", "nargo", + "noirc_errors", "noirc_frontend", "paste", "proptest", diff --git a/noir/noir-repo/compiler/noirc_errors/src/reporter.rs b/noir/noir-repo/compiler/noirc_errors/src/reporter.rs index cb5abbe2079..42cab72345d 100644 --- a/noir/noir-repo/compiler/noirc_errors/src/reporter.rs +++ b/noir/noir-repo/compiler/noirc_errors/src/reporter.rs @@ -202,14 +202,14 @@ fn stack_trace<'files>( let path = files.name(call_item.file).expect("should get file path"); let source = files.source(call_item.file).expect("should get file source"); - let (line, column) = location(source.as_ref(), call_item.span.start()); + let (line, column) = line_and_column_from_span(source.as_ref(), &call_item.span); result += &format!("{}. {}:{}:{}\n", i + 1, path, line, column); } result } -fn location(source: &str, span_start: u32) -> (u32, u32) { +pub fn line_and_column_from_span(source: &str, span: &Span) -> (u32, u32) { let mut line = 1; let mut column = 0; @@ -221,7 +221,7 @@ fn location(source: &str, span_start: u32) -> (u32, u32) { column = 0; } - if span_start <= i as u32 { + if span.start() <= i as u32 { break; } } diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 59645b50846..5af02be49be 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -29,6 +29,7 @@ inferno = "0.11.19" im.workspace = true noirc_frontend.workspace = true acir.workspace = true +noirc_errors.workspace = true # Logs tracing-subscriber.workspace = true diff --git a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index 88eb7d968a1..f9ba551dcb3 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize}; use acir::circuit::OpcodeLocation; use nargo::artifacts::program::ProgramArtifact; use nargo::errors::Location; +use noirc_errors::reporter::line_and_column_from_span; #[derive(Debug, Clone, Args)] pub(crate) struct GatesFlamegraphCommand { @@ -55,8 +56,6 @@ pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { serde_json::from_slice(&backend_gates_response.stdout)?; for (func_idx, func_gates) in backend_gates_response.functions.into_iter().enumerate() { - let mut folded_stack_items = HashMap::new(); - println!( "Opcode count: {}, Total gates by opcodes: {}, Circuit size: {}", func_gates.acir_opcodes, @@ -64,6 +63,9 @@ pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { func_gates.circuit_size ); + // Create a nested hashmap with the stack items, folding the gates for all the callsites that are equal + let mut folded_stack_items = HashMap::new(); + func_gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { let call_stack = &program.debug_symbols.debug_infos[func_idx] .locations @@ -135,31 +137,11 @@ fn location_to_callsite_label<'files>( .take(location.span.end() as usize - location.span.start() as usize) .collect::(); - let (line, column) = line_and_column_from_span(source.as_ref(), location.span.start()); + let (line, column) = line_and_column_from_span(source.as_ref(), &location.span); format!("{}:{}:{}::{}", filename, line, column, code_slice) } -fn line_and_column_from_span(source: &str, span_start: u32) -> (u32, u32) { - let mut line = 1; - let mut column = 0; - - for (i, char) in source.chars().enumerate() { - column += 1; - - if char == '\n' { - line += 1; - column = 0; - } - - if span_start <= i as u32 { - break; - } - } - - (line, column) -} - fn add_locations_to_folded_stack_items( stack_items: &mut HashMap, locations: Vec, @@ -179,6 +161,13 @@ fn add_locations_to_folded_stack_items( } } +/// Creates a vector of lines in the format that inferno expects from a nested hashmap of stack items +/// The lines have to be sorted in the following way, exploring the graph in a depth-first manner: +/// main 100 +/// main::foo 0 +/// main::foo::bar 200 +/// main::baz 27 +/// main::baz::qux 800 fn to_folded_sorted_lines( folded_stack_items: &HashMap, parent_stacks: im::Vector, @@ -201,3 +190,6 @@ fn to_folded_sorted_lines( }) .collect() } + +#[cfg(test)] +mod tests {} From 7e778b2450e65cb6613a433f524679ede7a1628a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 22:39:14 +0000 Subject: [PATCH 30/43] add test --- noir/noir-repo/Cargo.lock | 3 + noir/noir-repo/tooling/noirc_abi/src/lib.rs | 2 +- noir/noir-repo/tooling/profiler/Cargo.toml | 3 + .../profiler/src/cli/gates_flamegraph_cmd.rs | 341 ++++++++++++++++-- 4 files changed, 316 insertions(+), 33 deletions(-) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index bd1df426ffe..e7dfe9ff263 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2736,6 +2736,8 @@ dependencies = [ "im", "inferno", "nargo", + "noirc_abi", + "noirc_driver", "noirc_errors", "noirc_frontend", "paste", @@ -2743,6 +2745,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", + "tempfile", "tracing-appender", "tracing-subscriber", ] diff --git a/noir/noir-repo/tooling/noirc_abi/src/lib.rs b/noir/noir-repo/tooling/noirc_abi/src/lib.rs index b1ddba26f15..22767470daf 100644 --- a/noir/noir-repo/tooling/noirc_abi/src/lib.rs +++ b/noir/noir-repo/tooling/noirc_abi/src/lib.rs @@ -263,7 +263,7 @@ pub struct AbiReturnType { pub visibility: AbiVisibility, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct Abi { /// An ordered list of the arguments to the program's `main` function, specifying their types and visibility. pub parameters: Vec, diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 5af02be49be..17f4a6afbe6 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -30,6 +30,7 @@ im.workspace = true noirc_frontend.workspace = true acir.workspace = true noirc_errors.workspace = true +tempfile.workspace = true # Logs tracing-subscriber.workspace = true @@ -39,6 +40,8 @@ tracing-appender = "0.2.3" rand = "0.8.5" proptest = "1.2.0" paste = "1.0.14" +noirc_abi.workspace = true +noirc_driver.workspace = true [features] diff --git a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index f9ba551dcb3..828aedab0a0 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -1,6 +1,6 @@ -use std::collections::HashMap; +use std::collections::BTreeMap; use std::io::BufWriter; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::Command; use clap::Args; @@ -29,6 +29,63 @@ pub(crate) struct GatesFlamegraphCommand { output: String, } +trait GatesProvider { + fn get_gates(&self, artifact_path: &Path) -> eyre::Result; +} + +struct BackendGatesProvider { + backend_path: PathBuf, +} + +impl GatesProvider for BackendGatesProvider { + fn get_gates(&self, artifact_path: &Path) -> eyre::Result { + let backend_gates_response = + Command::new(&self.backend_path).arg("gates").arg("-b").arg(artifact_path).output()?; + + // Parse the backend gates command stdout as json + let backend_gates_response: BackendGatesResponse = + serde_json::from_slice(&backend_gates_response.stdout)?; + Ok(backend_gates_response) + } +} + +trait FlamegraphGenerator { + fn generate_flamegraph<'lines, I: IntoIterator>( + &self, + folded_lines: I, + artifact_name: &str, + function_name: &str, + output_path: &Path, + ) -> eyre::Result<()>; +} + +struct InfernoFlamegraphGenerator {} + +impl FlamegraphGenerator for InfernoFlamegraphGenerator { + fn generate_flamegraph<'lines, I: IntoIterator>( + &self, + folded_lines: I, + artifact_name: &str, + function_name: &str, + output_path: &Path, + ) -> eyre::Result<()> { + let flamegraph_file = std::fs::File::create(output_path)?; + let flamegraph_writer = BufWriter::new(flamegraph_file); + + let mut options = Options::default(); + options.hash = true; + options.deterministic = true; + options.title = format!("{}-{}", artifact_name, function_name); + options.subtitle = Some("Sample = Gate".to_string()); + options.frame_height = 24; + options.color_diffusion = true; + + from_lines(&mut options, folded_lines, flamegraph_writer)?; + + Ok(()) + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] struct BackendGatesReport { acir_opcodes: usize, @@ -43,17 +100,26 @@ struct BackendGatesResponse { struct FoldedStackItem { total_gates: usize, - nested_items: HashMap, + nested_items: BTreeMap, } pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { - let program = read_program_from_file(&args.artifact_path)?; - let backend_gates_response = - Command::new(args.backend_path).arg("gates").arg("-b").arg(&args.artifact_path).output()?; + run_with_provider( + &PathBuf::from(args.artifact_path), + &BackendGatesProvider { backend_path: PathBuf::from(args.backend_path) }, + &InfernoFlamegraphGenerator {}, + &PathBuf::from(args.output), + ) +} - // Parse the backend gates command stdout as json - let backend_gates_response: BackendGatesResponse = - serde_json::from_slice(&backend_gates_response.stdout)?; +fn run_with_provider( + artifact_path: &Path, + gates_provider: &Provider, + flamegraph_generator: &Generator, + output_path: &Path, +) -> eyre::Result<()> { + let program = read_program_from_file(artifact_path)?; + let backend_gates_response = gates_provider.get_gates(artifact_path)?; for (func_idx, func_gates) in backend_gates_response.functions.into_iter().enumerate() { println!( @@ -64,7 +130,7 @@ pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { ); // Create a nested hashmap with the stack items, folding the gates for all the callsites that are equal - let mut folded_stack_items = HashMap::new(); + let mut folded_stack_items = BTreeMap::new(); func_gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { let call_stack = &program.debug_symbols.debug_infos[func_idx] @@ -81,27 +147,13 @@ pub(crate) fn run(args: GatesFlamegraphCommand) -> eyre::Result<()> { add_locations_to_folded_stack_items(&mut folded_stack_items, location_names, gates); }); - - let output_path = - Path::new(&args.output).join(Path::new(&format!("{}.svg", &program.names[func_idx]))); - let flamegraph_file = std::fs::File::create(output_path)?; - - let flamegraph_writer = BufWriter::new(flamegraph_file); - let folded_lines = to_folded_sorted_lines(&folded_stack_items, Default::default()); - let mut options = Options::default(); - options.hash = true; - options.deterministic = true; - options.title = format!("{}-{}", &args.artifact_path, program.names[func_idx].clone()); - options.subtitle = Some("Sample = Gate".to_string()); - options.frame_height = 24; - options.color_diffusion = true; - - from_lines( - &mut options, + flamegraph_generator.generate_flamegraph( folded_lines.iter().map(|as_string| as_string.as_str()), - flamegraph_writer, + artifact_path.to_str().unwrap(), + &program.names[func_idx], + &Path::new(&output_path).join(Path::new(&format!("{}.svg", &program.names[func_idx]))), )?; } @@ -143,7 +195,7 @@ fn location_to_callsite_label<'files>( } fn add_locations_to_folded_stack_items( - stack_items: &mut HashMap, + stack_items: &mut BTreeMap, locations: Vec, gates: usize, ) { @@ -151,7 +203,7 @@ fn add_locations_to_folded_stack_items( for (index, location) in locations.iter().enumerate() { let current_item = child_map .entry(location.clone()) - .or_insert(FoldedStackItem { total_gates: 0, nested_items: HashMap::new() }); + .or_insert(FoldedStackItem { total_gates: 0, nested_items: BTreeMap::new() }); child_map = &mut current_item.nested_items; @@ -169,7 +221,7 @@ fn add_locations_to_folded_stack_items( /// main::baz 27 /// main::baz::qux 800 fn to_folded_sorted_lines( - folded_stack_items: &HashMap, + folded_stack_items: &BTreeMap, parent_stacks: im::Vector, ) -> Vec { folded_stack_items @@ -192,4 +244,229 @@ fn to_folded_sorted_lines( } #[cfg(test)] -mod tests {} +mod tests { + use acir::circuit::{OpcodeLocation, Program}; + use color_eyre::eyre::{self}; + use fm::{FileId, FileManager}; + use nargo::artifacts::program::ProgramArtifact; + use noirc_driver::DebugFile; + use noirc_errors::{ + debug_info::{DebugInfo, ProgramDebugInfo}, + Location, Span, + }; + use std::{ + cell::RefCell, + collections::{BTreeMap, HashMap}, + path::{Path, PathBuf}, + }; + use tempfile::TempDir; + + use super::{BackendGatesReport, BackendGatesResponse, GatesProvider}; + + struct TestGateProvider { + mock_responses: HashMap, + } + + impl GatesProvider for TestGateProvider { + fn get_gates(&self, artifact_path: &std::path::Path) -> eyre::Result { + let response = self + .mock_responses + .get(artifact_path) + .expect("should have a mock response for the artifact path"); + + Ok(response.clone()) + } + } + + #[derive(Default)] + struct TestFlamegraphGenerator { + lines_received: RefCell>>, + } + + impl super::FlamegraphGenerator for TestFlamegraphGenerator { + fn generate_flamegraph<'lines, I: IntoIterator>( + &self, + folded_lines: I, + _artifact_name: &str, + _function_name: &str, + _output_path: &std::path::Path, + ) -> eyre::Result<()> { + let lines = folded_lines.into_iter().map(|line| line.to_string()).collect(); + self.lines_received.borrow_mut().push(lines); + Ok(()) + } + } + + fn find_spans_for(source: &str, needle: &str) -> Vec { + let mut spans = Vec::new(); + let mut start = 0; + while let Some(start_idx) = source[start..].find(needle) { + let start_idx = start + start_idx; + let end_idx = start_idx + needle.len(); + spans.push(Span::inclusive(start_idx as u32, end_idx as u32 - 1)); + start = end_idx; + } + spans + } + + struct TestCase { + expected_folded_sorted_lines: Vec>, + debug_symbols: ProgramDebugInfo, + file_map: BTreeMap, + gates_report: BackendGatesResponse, + } + + fn simple_test_case(temp_dir: &TempDir) -> TestCase { + let source_code = r##" + fn main() { + foo(); + bar(); + whatever(); + } + fn foo() { + baz(); + } + fn bar () { + whatever() + } + fn baz () { + whatever() + } + "##; + + let source_file_name = Path::new("main.nr"); + let mut fm = FileManager::new(temp_dir.path()); + let file_id = fm.add_file_with_source(source_file_name, source_code.to_string()).unwrap(); + + let main_declaration_location = + Location::new(find_spans_for(source_code, "fn main()")[0], file_id); + let main_foo_call_location = + Location::new(find_spans_for(source_code, "foo()")[0], file_id); + let main_bar_call_location = + Location::new(find_spans_for(source_code, "bar()")[0], file_id); + let main_whatever_call_location = + Location::new(find_spans_for(source_code, "whatever()")[0], file_id); + let foo_baz_call_location = Location::new(find_spans_for(source_code, "baz()")[0], file_id); + let bar_whatever_call_location = + Location::new(find_spans_for(source_code, "whatever()")[1], file_id); + let baz_whatever_call_location = + Location::new(find_spans_for(source_code, "whatever()")[2], file_id); + + let mut opcode_locations = BTreeMap::>::new(); + // main::foo::baz::whatever + opcode_locations.insert( + OpcodeLocation::Acir(0), + vec![ + main_declaration_location, + main_foo_call_location, + foo_baz_call_location, + baz_whatever_call_location, + ], + ); + + // main::bar::whatever + opcode_locations.insert( + OpcodeLocation::Acir(1), + vec![main_declaration_location, main_bar_call_location, bar_whatever_call_location], + ); + // main::whatever + opcode_locations.insert( + OpcodeLocation::Acir(2), + vec![main_declaration_location, main_whatever_call_location], + ); + + let file_map = BTreeMap::from_iter(vec![( + file_id, + DebugFile { source: source_code.to_string(), path: source_file_name.to_path_buf() }, + )]); + + let debug_symbols = ProgramDebugInfo { + debug_infos: vec![DebugInfo::new( + opcode_locations, + BTreeMap::default(), + BTreeMap::default(), + BTreeMap::default(), + )], + }; + + let backend_gates_response = BackendGatesResponse { + functions: vec![BackendGatesReport { + acir_opcodes: 3, + circuit_size: 100, + gates_per_opcode: vec![10, 20, 30], + }], + }; + + let expected_folded_sorted_lines = vec![ + "main.nr:2:9::fn main() 0".to_string(), + "main.nr:2:9::fn main();main.nr:3:13::foo() 0".to_string(), + "main.nr:2:9::fn main();main.nr:3:13::foo();main.nr:8:13::baz() 0".to_string(), + "main.nr:2:9::fn main();main.nr:3:13::foo();main.nr:8:13::baz();main.nr:14:13::whatever() 10".to_string(), + "main.nr:2:9::fn main();main.nr:4:13::bar() 0".to_string(), + "main.nr:2:9::fn main();main.nr:4:13::bar();main.nr:11:13::whatever() 20".to_string(), + "main.nr:2:9::fn main();main.nr:5:13::whatever() 30".to_string(), + ]; + + TestCase { + expected_folded_sorted_lines: vec![expected_folded_sorted_lines], + debug_symbols, + file_map, + gates_report: backend_gates_response, + } + } + + #[test] + fn test_flamegraph() { + let temp_dir = tempfile::tempdir().unwrap(); + + let test_cases = vec![simple_test_case(&temp_dir)]; + let artifact_names: Vec<_> = + test_cases.iter().enumerate().map(|(idx, _)| format!("test{}.json", idx)).collect(); + + let test_cases_with_names: Vec<_> = test_cases.into_iter().zip(artifact_names).collect(); + + let mut mock_responses: HashMap = HashMap::new(); + // Collect mock responses + for (test_case, artifact_name) in test_cases_with_names.iter() { + mock_responses.insert( + temp_dir.path().join(artifact_name.clone()), + test_case.gates_report.clone(), + ); + } + + let provider = TestGateProvider { mock_responses }; + + for (test_case, artifact_name) in test_cases_with_names.iter() { + let artifact_path = temp_dir.path().join(artifact_name.clone()); + + let artifact = ProgramArtifact { + noir_version: "0.0.0".to_string(), + hash: 27, + abi: noirc_abi::Abi::default(), + bytecode: Program::default(), + debug_symbols: test_case.debug_symbols.clone(), + file_map: test_case.file_map.clone(), + names: vec!["main".to_string()], + }; + + // Write the artifact to a file + let artifact_file = std::fs::File::create(&artifact_path).unwrap(); + serde_json::to_writer(artifact_file, &artifact).unwrap(); + + let flamegraph_generator = TestFlamegraphGenerator::default(); + + super::run_with_provider( + &artifact_path, + &provider, + &flamegraph_generator, + temp_dir.path(), + ) + .expect("should run without errors"); + + // Check that the flamegraph generator was called with the correct folded sorted lines + let calls_received = flamegraph_generator.lines_received.borrow().clone(); + + assert_eq!(calls_received, test_case.expected_folded_sorted_lines); + } + } +} From ea459c768e680833b6f341d74dbadad6eef4a2cf Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 12 Jun 2024 22:55:55 +0000 Subject: [PATCH 31/43] fix: frame separator --- noir/noir-repo/tooling/profiler/Cargo.toml | 3 +-- .../tooling/profiler/src/cli/gates_flamegraph_cmd.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 17f4a6afbe6..0ca83967154 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -30,7 +30,6 @@ im.workspace = true noirc_frontend.workspace = true acir.workspace = true noirc_errors.workspace = true -tempfile.workspace = true # Logs tracing-subscriber.workspace = true @@ -42,7 +41,7 @@ proptest = "1.2.0" paste = "1.0.14" noirc_abi.workspace = true noirc_driver.workspace = true - +tempfile.workspace = true [features] default = ["bn254"] diff --git a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index 828aedab0a0..89fed68ea05 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -5,7 +5,7 @@ use std::process::Command; use clap::Args; use codespan_reporting::files::Files; -use color_eyre::eyre::{self}; +use color_eyre::eyre::{self, Context}; use inferno::flamegraph::{from_lines, Options}; use serde::{Deserialize, Serialize}; @@ -118,8 +118,10 @@ fn run_with_provider( flamegraph_generator: &Generator, output_path: &Path, ) -> eyre::Result<()> { - let program = read_program_from_file(artifact_path)?; - let backend_gates_response = gates_provider.get_gates(artifact_path)?; + let program = + read_program_from_file(artifact_path).context("Error reading program from file")?; + let backend_gates_response = + gates_provider.get_gates(artifact_path).context("Error querying backend for gates")?; for (func_idx, func_gates) in backend_gates_response.functions.into_iter().enumerate() { println!( @@ -189,6 +191,10 @@ fn location_to_callsite_label<'files>( .take(location.span.end() as usize - location.span.start() as usize) .collect::(); + // ";" is used for frame separation, and is not allowed by inferno + // Check code slice for ";" and replace it with 'GREEK QUESTION MARK' (U+037E) + let code_slice = code_slice.replace(';', "\u{037E}"); + let (line, column) = line_and_column_from_span(source.as_ref(), &location.span); format!("{}:{}:{}::{}", filename, line, column, code_slice) From 1137d5366487d2dbfc403da4ccff770a74c3744e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 09:09:31 +0000 Subject: [PATCH 32/43] rename binary --- noir/noir-repo/tooling/profiler/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 0ca83967154..4c2ccc5bf4d 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -13,7 +13,7 @@ repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[bin]] -name = "noir_profiler" +name = "noir-profiler" path = "src/main.rs" [dependencies] From 2c361db71843ad0fff6cc09ee0dc05ac19b814c7 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 09:13:17 +0000 Subject: [PATCH 33/43] fix extraneous naming --- noir/noir-repo/tooling/profiler/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/noir/noir-repo/tooling/profiler/src/main.rs b/noir/noir-repo/tooling/profiler/src/main.rs index 9ad3d694003..8e08644de23 100644 --- a/noir/noir-repo/tooling/profiler/src/main.rs +++ b/noir/noir-repo/tooling/profiler/src/main.rs @@ -12,8 +12,8 @@ use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; fn main() { // Setup tracing - if let Ok(log_dir) = env::var("ACVM_LOG_DIR") { - let debug_file = rolling::daily(log_dir, "acvm-log"); + if let Ok(log_dir) = env::var("PROFILER_LOG_DIR") { + let debug_file = rolling::daily(log_dir, "profiler-log"); tracing_subscriber::fmt() .with_span_events(FmtSpan::ACTIVE) .with_writer(debug_file) From b1a71e1eacf21a29e7ba863374dfa85d5f370e01 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 09:37:31 +0000 Subject: [PATCH 34/43] fmt --- .../noir-contracts/contracts/card_game_contract/src/cards.nr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr index e7d89b68f74..01d06077fb7 100644 --- a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr +++ b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr @@ -6,8 +6,8 @@ use dep::aztec::{ traits::{ToField, Serialize, FromField}, grumpkin_point::GrumpkinPoint, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL }, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_with_keys, note::note_getter::view_notes, - state_vars::PrivateSet, note::constants::MAX_NOTES_PER_PAGE + encrypted_logs::encrypted_note_emission::encode_and_encrypt_with_keys, + note::note_getter::view_notes, state_vars::PrivateSet, note::constants::MAX_NOTES_PER_PAGE }; use dep::std; use dep::std::{option::Option}; From 1f48133ebf0551e207528470e4378bedb80725ff Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 11:30:45 +0000 Subject: [PATCH 35/43] review changes --- .../dsl/acir_format/acir_format.cpp | 86 +++++++++---------- .../dsl/acir_format/acir_format.hpp | 2 +- .../dsl/acir_format/acir_format.test.cpp | 25 ------ .../dsl/acir_format/acir_format_mocks.cpp | 28 +----- .../acir_format/acir_to_constraint_buf.cpp | 60 ++++++------- 5 files changed, 75 insertions(+), 126 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 91617d4ebcb..bcfcce6776f 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -43,50 +43,50 @@ void build_constraints(Builder& builder, // Add arithmetic gates for (size_t i = 0; i < constraint_system.poly_triple_constraints.size(); ++i) { - const auto& constraint = constraint_system.poly_triple_constraints[i]; + const auto& constraint = constraint_system.poly_triple_constraints.at(i); builder.create_poly_gate(constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.poly_triple_constraints[i]); + constraint_system.original_opcode_indices.poly_triple_constraints.at(i)); } for (size_t i = 0; i < constraint_system.quad_constraints.size(); ++i) { - const auto& constraint = constraint_system.quad_constraints[i]; + const auto& constraint = constraint_system.quad_constraints.at(i); builder.create_big_mul_gate(constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.quad_constraints[i]); + constraint_system.original_opcode_indices.quad_constraints.at(i)); } // Add logic constraint for (size_t i = 0; i < constraint_system.logic_constraints.size(); ++i) { - const auto& constraint = constraint_system.logic_constraints[i]; + const auto& constraint = constraint_system.logic_constraints.at(i); create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.logic_constraints[i]); + constraint_system.original_opcode_indices.logic_constraints.at(i)); } // Add range constraint for (size_t i = 0; i < constraint_system.range_constraints.size(); ++i) { - const auto& constraint = constraint_system.range_constraints[i]; + const auto& constraint = constraint_system.range_constraints.at(i); builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.range_constraints[i]); + constraint_system.original_opcode_indices.range_constraints.at(i)); } // Add aes128 constraints for (size_t i = 0; i < constraint_system.aes128_constraints.size(); ++i) { - const auto& constraint = constraint_system.aes128_constraints[i]; + const auto& constraint = constraint_system.aes128_constraints.at(i); create_aes128_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.aes128_constraints[i]); + constraint_system.original_opcode_indices.aes128_constraints.at(i)); } // Add sha256 constraints for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { - const auto& constraint = constraint_system.sha256_constraints[i]; + const auto& constraint = constraint_system.sha256_constraints.at(i); create_sha256_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.sha256_constraints[i]); + constraint_system.original_opcode_indices.sha256_constraints.at(i)); } for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { @@ -98,50 +98,50 @@ void build_constraints(Builder& builder, // Add schnorr constraints for (size_t i = 0; i < constraint_system.schnorr_constraints.size(); ++i) { - const auto& constraint = constraint_system.schnorr_constraints[i]; + const auto& constraint = constraint_system.schnorr_constraints.at(i); create_schnorr_verify_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.schnorr_constraints[i]); + constraint_system.original_opcode_indices.schnorr_constraints.at(i)); } // Add ECDSA k1 constraints for (size_t i = 0; i < constraint_system.ecdsa_k1_constraints.size(); ++i) { - const auto& constraint = constraint_system.ecdsa_k1_constraints[i]; + const auto& constraint = constraint_system.ecdsa_k1_constraints.at(i); create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.ecdsa_k1_constraints[i]); + constraint_system.original_opcode_indices.ecdsa_k1_constraints.at(i)); } // Add ECDSA r1 constraints for (size_t i = 0; i < constraint_system.ecdsa_r1_constraints.size(); ++i) { - const auto& constraint = constraint_system.ecdsa_r1_constraints[i]; + const auto& constraint = constraint_system.ecdsa_r1_constraints.at(i); create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.ecdsa_r1_constraints[i]); + constraint_system.original_opcode_indices.ecdsa_r1_constraints.at(i)); } // Add blake2s constraints for (size_t i = 0; i < constraint_system.blake2s_constraints.size(); ++i) { - const auto& constraint = constraint_system.blake2s_constraints[i]; + const auto& constraint = constraint_system.blake2s_constraints.at(i); create_blake2s_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.blake2s_constraints[i]); + constraint_system.original_opcode_indices.blake2s_constraints.at(i)); } // Add blake3 constraints for (size_t i = 0; i < constraint_system.blake3_constraints.size(); ++i) { - const auto& constraint = constraint_system.blake3_constraints[i]; + const auto& constraint = constraint_system.blake3_constraints.at(i); create_blake3_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.blake3_constraints[i]); + constraint_system.original_opcode_indices.blake3_constraints.at(i)); } // Add keccak constraints for (size_t i = 0; i < constraint_system.keccak_constraints.size(); ++i) { - const auto& constraint = constraint_system.keccak_constraints[i]; + const auto& constraint = constraint_system.keccak_constraints.at(i); create_keccak_constraints(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.keccak_constraints[i]); + constraint_system.original_opcode_indices.keccak_constraints.at(i)); } for (size_t i = 0; i < constraint_system.keccak_permutations.size(); ++i) { @@ -153,50 +153,50 @@ void build_constraints(Builder& builder, // Add pedersen constraints for (size_t i = 0; i < constraint_system.pedersen_constraints.size(); ++i) { - const auto& constraint = constraint_system.pedersen_constraints[i]; + const auto& constraint = constraint_system.pedersen_constraints.at(i); create_pedersen_constraint(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.pedersen_constraints[i]); + constraint_system.original_opcode_indices.pedersen_constraints.at(i)); } for (size_t i = 0; i < constraint_system.pedersen_hash_constraints.size(); ++i) { - const auto& constraint = constraint_system.pedersen_hash_constraints[i]; + const auto& constraint = constraint_system.pedersen_hash_constraints.at(i); create_pedersen_hash_constraint(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.pedersen_hash_constraints[i]); + constraint_system.original_opcode_indices.pedersen_hash_constraints.at(i)); } for (size_t i = 0; i < constraint_system.poseidon2_constraints.size(); ++i) { - const auto& constraint = constraint_system.poseidon2_constraints[i]; + const auto& constraint = constraint_system.poseidon2_constraints.at(i); create_poseidon2_permutations(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.poseidon2_constraints[i]); + constraint_system.original_opcode_indices.poseidon2_constraints.at(i)); } // Add multi scalar mul constraints for (size_t i = 0; i < constraint_system.multi_scalar_mul_constraints.size(); ++i) { - const auto& constraint = constraint_system.multi_scalar_mul_constraints[i]; + const auto& constraint = constraint_system.multi_scalar_mul_constraints.at(i); create_multi_scalar_mul_constraint(builder, constraint); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.multi_scalar_mul_constraints[i]); + constraint_system.original_opcode_indices.multi_scalar_mul_constraints.at(i)); } // Add ec add constraints for (size_t i = 0; i < constraint_system.ec_add_constraints.size(); ++i) { - const auto& constraint = constraint_system.ec_add_constraints[i]; + const auto& constraint = constraint_system.ec_add_constraints.at(i); create_ec_add_constraint(builder, constraint, has_valid_witness_assignments); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.ec_add_constraints[i]); + constraint_system.original_opcode_indices.ec_add_constraints.at(i)); } // Add block constraints for (size_t i = 0; i < constraint_system.block_constraints.size(); ++i) { - const auto& constraint = constraint_system.block_constraints[i]; + const auto& constraint = constraint_system.block_constraints.at(i); create_block_constraints(builder, constraint, has_valid_witness_assignments); if (collect_gates_per_opcode) { size_t avg_gates_per_opcode = - compute_gate_diff() / constraint_system.original_opcode_indices.block_constraints[i].size(); - for (size_t opcode_index : constraint_system.original_opcode_indices.block_constraints[i]) { + compute_gate_diff() / constraint_system.original_opcode_indices.block_constraints.at(i).size(); + for (size_t opcode_index : constraint_system.original_opcode_indices.block_constraints.at(i)) { constraint_system.gates_per_opcode[opcode_index] = avg_gates_per_opcode; } } @@ -206,10 +206,10 @@ void build_constraints(Builder& builder, DSLBigInts dsl_bigints; dsl_bigints.set_builder(&builder); for (size_t i = 0; i < constraint_system.bigint_from_le_bytes_constraints.size(); ++i) { - const auto& constraint = constraint_system.bigint_from_le_bytes_constraints[i]; + const auto& constraint = constraint_system.bigint_from_le_bytes_constraints.at(i); create_bigint_from_le_bytes_constraint(builder, constraint, dsl_bigints); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints[i]); + constraint_system.original_opcode_indices.bigint_from_le_bytes_constraints.at(i)); } for (size_t i = 0; i < constraint_system.bigint_operations.size(); ++i) { @@ -220,10 +220,10 @@ void build_constraints(Builder& builder, } for (size_t i = 0; i < constraint_system.bigint_to_le_bytes_constraints.size(); ++i) { - const auto& constraint = constraint_system.bigint_to_le_bytes_constraints[i]; + const auto& constraint = constraint_system.bigint_to_le_bytes_constraints.at(i); create_bigint_to_le_bytes_constraint(builder, constraint, dsl_bigints); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints[i]); + constraint_system.original_opcode_indices.bigint_to_le_bytes_constraints.at(i)); } // RecursionConstraint @@ -341,7 +341,7 @@ void build_constraints(Builder& builder, // Add recursion constraints for (size_t i = 0; i < constraint_system.honk_recursion_constraints.size(); ++i) { - auto constraint = constraint_system.honk_recursion_constraints[i]; + auto constraint = constraint_system.honk_recursion_constraints.at(i); // A proof passed into the constraint should be stripped of its inner public inputs, but not the // nested aggregation object itself. The verifier circuit requires that the indices to a nested // proof aggregation state are a circuit constant. The user tells us they how they want these @@ -366,7 +366,7 @@ void build_constraints(Builder& builder, nested_aggregation_object, has_valid_witness_assignments); track_gate_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.honk_recursion_constraints[i]); + constraint_system.original_opcode_indices.honk_recursion_constraints.at(i)); } // Now that the circuit has been completely built, we add the output aggregation as public diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 80edf92270b..1e1faae10a5 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -112,7 +112,7 @@ struct AcirFormat { // Number of gates added to the circuit per original opcode. // Has length equal to num_acir_opcodes. - std::vector gates_per_opcode = {}; + std::vector gates_per_opcode; // Indices of the original opcode that originated each constraint in AcirFormat. AcirFormatOriginalOpcodeIndices original_opcode_indices; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 6085671fb0a..fc7c9603a6d 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -595,32 +595,7 @@ TEST_F(AcirFormatTests, TestCollectsGateCounts) .varnum = 4, .recursive = false, .num_acir_opcodes = 2, - .public_inputs = {}, - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, .poly_triple_constraints = { first_gate, second_gate }, - .quad_constraints = {}, - .block_constraints = {}, .original_opcode_indices = create_empty_original_opcode_indices(), }; mock_opcode_indices(constraint_system); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp index f53ff85d6e7..5290f93b507 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp @@ -2,33 +2,7 @@ acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices() { - return acir_format::AcirFormatOriginalOpcodeIndices{ - .logic_constraints = {}, - .range_constraints = {}, - .aes128_constraints = {}, - .sha256_constraints = {}, - .sha256_compression = {}, - .schnorr_constraints = {}, - .ecdsa_k1_constraints = {}, - .ecdsa_r1_constraints = {}, - .blake2s_constraints = {}, - .blake3_constraints = {}, - .keccak_constraints = {}, - .keccak_permutations = {}, - .pedersen_constraints = {}, - .pedersen_hash_constraints = {}, - .poseidon2_constraints = {}, - .multi_scalar_mul_constraints = {}, - .ec_add_constraints = {}, - .recursion_constraints = {}, - .honk_recursion_constraints = {}, - .bigint_from_le_bytes_constraints = {}, - .bigint_to_le_bytes_constraints = {}, - .bigint_operations = {}, - .poly_triple_constraints = {}, - .quad_constraints = {}, - .block_constraints = {}, - }; + return {}; } void mock_opcode_indices(acir_format::AcirFormat& constraint_system) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp index 3e375b79a8a..09029ada3a1 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp @@ -177,15 +177,15 @@ void handle_arithmetic(Program::Opcode::AssertZero const& arg, AcirFormat& af, s // gate instead. We could probably always use a width-4 gate in fact. if (pt == poly_triple{ 0, 0, 0, 0, 0, 0, 0, 0 }) { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); - af.original_opcode_indices.quad_constraints.push_back((opcode_index)); + af.original_opcode_indices.quad_constraints.push_back(opcode_index); } else { af.poly_triple_constraints.push_back(pt); - af.original_opcode_indices.poly_triple_constraints.push_back((opcode_index)); + af.original_opcode_indices.poly_triple_constraints.push_back(opcode_index); } } else { af.quad_constraints.push_back(serialize_mul_quad_gate(arg.value)); - af.original_opcode_indices.quad_constraints.push_back((opcode_index)); + af.original_opcode_indices.quad_constraints.push_back(opcode_index); } } @@ -205,7 +205,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .num_bits = arg.lhs.num_bits, .is_xor_gate = false, }); - af.original_opcode_indices.logic_constraints.push_back((opcode_index)); + af.original_opcode_indices.logic_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.logic_constraints.push_back(LogicConstraint{ .a = arg.lhs.witness.value, @@ -214,13 +214,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .num_bits = arg.lhs.num_bits, .is_xor_gate = true, }); - af.original_opcode_indices.logic_constraints.push_back((opcode_index)); + af.original_opcode_indices.logic_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.range_constraints.push_back(RangeConstraint{ .witness = arg.input.witness.value, .num_bits = arg.input.num_bits, }); - af.original_opcode_indices.range_constraints.push_back((opcode_index)); + af.original_opcode_indices.range_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.aes128_constraints.push_back(AES128Constraint{ @@ -247,7 +247,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .outputs = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.aes128_constraints.push_back((opcode_index)); + af.original_opcode_indices.aes128_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.sha256_constraints.push_back(Sha256Constraint{ @@ -260,7 +260,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.sha256_constraints.push_back((opcode_index)); + af.original_opcode_indices.sha256_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.sha256_compression.push_back(Sha256Compression{ @@ -280,7 +280,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.sha256_compression.push_back((opcode_index)); + af.original_opcode_indices.sha256_compression.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.blake2s_constraints.push_back(Blake2sConstraint{ .inputs = map(arg.inputs, @@ -292,7 +292,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.blake2s_constraints.push_back((opcode_index)); + af.original_opcode_indices.blake2s_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.blake3_constraints.push_back(Blake3Constraint{ .inputs = map(arg.inputs, @@ -304,7 +304,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.blake3_constraints.push_back((opcode_index)); + af.original_opcode_indices.blake3_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.schnorr_constraints.push_back(SchnorrConstraint{ .message = map(arg.message, [](auto& e) { return e.witness.value; }), @@ -313,7 +313,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); - af.original_opcode_indices.schnorr_constraints.push_back((opcode_index)); + af.original_opcode_indices.schnorr_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.pedersen_constraints.push_back(PedersenConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), @@ -321,14 +321,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result_x = arg.outputs[0].value, .result_y = arg.outputs[1].value, }); - af.original_opcode_indices.pedersen_constraints.push_back((opcode_index)); + af.original_opcode_indices.pedersen_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.pedersen_hash_constraints.push_back(PedersenHashConstraint{ .scalars = map(arg.inputs, [](auto& e) { return e.witness.value; }), .hash_index = arg.domain_separator, .result = arg.output.value, }); - af.original_opcode_indices.pedersen_hash_constraints.push_back((opcode_index)); + af.original_opcode_indices.pedersen_hash_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ecdsa_k1_constraints.push_back(EcdsaSecp256k1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -337,7 +337,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .pub_y_indices = map(arg.public_key_y, [](auto& e) { return e.witness.value; }), .result = arg.output.value, }); - af.original_opcode_indices.ecdsa_k1_constraints.push_back((opcode_index)); + af.original_opcode_indices.ecdsa_k1_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ecdsa_r1_constraints.push_back(EcdsaSecp256r1Constraint{ .hashed_message = map(arg.hashed_message, [](auto& e) { return e.witness.value; }), @@ -346,7 +346,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output.value, .signature = map(arg.signature, [](auto& e) { return e.witness.value; }), }); - af.original_opcode_indices.ecdsa_r1_constraints.push_back((opcode_index)); + af.original_opcode_indices.ecdsa_r1_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.multi_scalar_mul_constraints.push_back(MultiScalarMul{ .points = map(arg.points, [](auto& e) { return e.witness.value; }), @@ -355,7 +355,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .out_point_y = arg.outputs[1].value, .out_point_is_infinite = arg.outputs[2].value, }); - af.original_opcode_indices.multi_scalar_mul_constraints.push_back((opcode_index)); + af.original_opcode_indices.multi_scalar_mul_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.ec_add_constraints.push_back(EcAdd{ .input1_x = arg.input1[0].witness.value, @@ -368,7 +368,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result_y = arg.outputs[1].value, .result_infinite = arg.outputs[2].value, }); - af.original_opcode_indices.ec_add_constraints.push_back((opcode_index)); + af.original_opcode_indices.ec_add_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.keccak_constraints.push_back(KeccakConstraint{ .inputs = map(arg.inputs, @@ -381,13 +381,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = map(arg.outputs, [](auto& e) { return e.value; }), .var_message_size = arg.var_message_size.witness.value, }); - af.original_opcode_indices.keccak_constraints.push_back((opcode_index)); + af.original_opcode_indices.keccak_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.keccak_permutations.push_back(Keccakf1600{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.keccak_permutations.push_back((opcode_index)); + af.original_opcode_indices.keccak_permutations.push_back(opcode_index); } else if constexpr (std::is_same_v) { if (honk_recursion) { // if we're using the honk recursive verifier auto c = HonkRecursionConstraint{ @@ -396,7 +396,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .public_inputs = map(arg.public_inputs, [](auto& e) { return e.witness.value; }), }; af.honk_recursion_constraints.push_back(c); - af.original_opcode_indices.honk_recursion_constraints.push_back((opcode_index)); + af.original_opcode_indices.honk_recursion_constraints.push_back(opcode_index); } else { auto c = RecursionConstraint{ .key = map(arg.verification_key, [](auto& e) { return e.witness.value; }), @@ -405,7 +405,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .key_hash = arg.key_hash.witness.value, }; af.recursion_constraints.push_back(c); - af.original_opcode_indices.recursion_constraints.push_back((opcode_index)); + af.original_opcode_indices.recursion_constraints.push_back(opcode_index); } } else if constexpr (std::is_same_v) { af.bigint_from_le_bytes_constraints.push_back(BigIntFromLeBytes{ @@ -413,13 +413,13 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .modulus = map(arg.modulus, [](auto& e) -> uint32_t { return e; }), .result = arg.output, }); - af.original_opcode_indices.bigint_from_le_bytes_constraints.push_back((opcode_index)); + af.original_opcode_indices.bigint_from_le_bytes_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_to_le_bytes_constraints.push_back(BigIntToLeBytes{ .input = arg.input, .result = map(arg.outputs, [](auto& e) { return e.value; }), }); - af.original_opcode_indices.bigint_to_le_bytes_constraints.push_back((opcode_index)); + af.original_opcode_indices.bigint_to_le_bytes_constraints.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -427,7 +427,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Add, }); - af.original_opcode_indices.bigint_operations.push_back((opcode_index)); + af.original_opcode_indices.bigint_operations.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -435,7 +435,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Sub, }); - af.original_opcode_indices.bigint_operations.push_back((opcode_index)); + af.original_opcode_indices.bigint_operations.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -443,7 +443,7 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Mul, }); - af.original_opcode_indices.bigint_operations.push_back((opcode_index)); + af.original_opcode_indices.bigint_operations.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.bigint_operations.push_back(BigIntOperation{ .lhs = arg.lhs, @@ -451,14 +451,14 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, .result = arg.output, .opcode = BigIntOperationType::Div, }); - af.original_opcode_indices.bigint_operations.push_back((opcode_index)); + af.original_opcode_indices.bigint_operations.push_back(opcode_index); } else if constexpr (std::is_same_v) { af.poseidon2_constraints.push_back(Poseidon2Constraint{ .state = map(arg.inputs, [](auto& e) { return e.witness.value; }), .result = map(arg.outputs, [](auto& e) { return e.value; }), .len = arg.len, }); - af.original_opcode_indices.poseidon2_constraints.push_back((opcode_index)); + af.original_opcode_indices.poseidon2_constraints.push_back(opcode_index); } }, arg.value.value); @@ -531,7 +531,7 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit, bool ho af.public_inputs = join({ map(circuit.public_parameters.value, [](auto e) { return e.value; }), map(circuit.return_values.value, [](auto e) { return e.value; }) }); // Map to a pair of: BlockConstraint, and list of opcodes associated with that BlockConstraint - std::map>> block_id_to_block_constraint; + std::unordered_map>> block_id_to_block_constraint; for (size_t i = 0; i < circuit.opcodes.size(); ++i) { auto gate = circuit.opcodes[i]; std::visit( From 5093fddccec27e91f0bd95feafd15b65775f4fb4 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 11:56:15 +0000 Subject: [PATCH 36/43] fmt --- .../aztec-nr/aztec/src/encrypted_logs.nr | 2 +- noir-projects/aztec-nr/aztec/src/note.nr | 2 +- .../src/oracle/key_validation_request.nr | 5 +++- .../aztec-nr/aztec/src/oracle/logs.nr | 27 +++---------------- .../aztec-nr/aztec/src/oracle/notes.nr | 6 +---- 5 files changed, 11 insertions(+), 31 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr index a8d92e002f6..2f1b93d9aad 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr @@ -2,4 +2,4 @@ mod header; mod incoming_body; mod outgoing_body; mod payload; -mod encrypted_note_emission; \ No newline at end of file +mod encrypted_note_emission; diff --git a/noir-projects/aztec-nr/aztec/src/note.nr b/noir-projects/aztec-nr/aztec/src/note.nr index 644450efc4f..670b340735e 100644 --- a/noir-projects/aztec-nr/aztec/src/note.nr +++ b/noir-projects/aztec-nr/aztec/src/note.nr @@ -6,4 +6,4 @@ mod note_header; mod note_interface; mod note_viewer_options; mod utils; -mod note_emission; \ No newline at end of file +mod note_emission; diff --git a/noir-projects/aztec-nr/aztec/src/oracle/key_validation_request.nr b/noir-projects/aztec-nr/aztec/src/oracle/key_validation_request.nr index 8f9e0d428f6..77ab29f9e41 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/key_validation_request.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/key_validation_request.nr @@ -4,7 +4,10 @@ use dep::protocol_types::{ }; #[oracle(getKeyValidationRequest)] -unconstrained fn get_key_validation_request_oracle(_pk_m_hash: Field, _key_index: Field) -> [Field; KEY_VALIDATION_REQUEST_LENGTH] {} +unconstrained fn get_key_validation_request_oracle( + _pk_m_hash: Field, + _key_index: Field +) -> [Field; KEY_VALIDATION_REQUEST_LENGTH] {} unconstrained fn get_key_validation_request_internal(npk_m_hash: Field, key_index: Field) -> KeyValidationRequest { let result = get_key_validation_request_oracle(npk_m_hash, key_index); diff --git a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr index 34aea8558ac..02bbd75de4a 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr @@ -2,11 +2,7 @@ use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}; // = 480 + 32 * N bytes #[oracle(emitEncryptedNoteLog)] -unconstrained fn emit_encrypted_note_log_oracle( - _note_hash_counter: u32, - _encrypted_note: [u8; M], - _counter: u32 -) {} +unconstrained fn emit_encrypted_note_log_oracle(_note_hash_counter: u32, _encrypted_note: [u8; M], _counter: u32) {} unconstrained pub fn emit_encrypted_note_log( note_hash_counter: u32, @@ -17,12 +13,7 @@ unconstrained pub fn emit_encrypted_note_log( } #[oracle(emitEncryptedEventLog)] -unconstrained fn emit_encrypted_event_log_oracle( - _contract_address: AztecAddress, - _randomness: Field, - _encrypted_event: [u8; M], - _counter: u32 -) {} +unconstrained fn emit_encrypted_event_log_oracle(_contract_address: AztecAddress, _randomness: Field, _encrypted_event: [u8; M], _counter: u32) {} unconstrained pub fn emit_encrypted_event_log( contract_address: AztecAddress, @@ -98,12 +89,7 @@ unconstrained pub fn compute_encrypted_event_log( } #[oracle(emitUnencryptedLog)] -unconstrained fn emit_unencrypted_log_oracle_private( - _contract_address: AztecAddress, - _event_selector: Field, - _message: T, - _counter: u32 -) -> Field {} +unconstrained fn emit_unencrypted_log_oracle_private(_contract_address: AztecAddress, _event_selector: Field, _message: T, _counter: u32) -> Field {} unconstrained pub fn emit_unencrypted_log_private_internal( contract_address: AztecAddress, @@ -115,12 +101,7 @@ unconstrained pub fn emit_unencrypted_log_private_internal( } #[oracle(emitContractClassUnencryptedLog)] -unconstrained fn emit_contract_class_unencrypted_log_private( - contract_address: AztecAddress, - event_selector: Field, - message: [Field; N], - counter: u32 -) -> Field {} +unconstrained fn emit_contract_class_unencrypted_log_private(contract_address: AztecAddress, event_selector: Field, message: [Field; N], counter: u32) -> Field {} unconstrained pub fn emit_contract_class_unencrypted_log_private_internal( contract_address: AztecAddress, diff --git a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr index 6a2a311f3f9..42c6bcdb7ee 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr @@ -28,11 +28,7 @@ unconstrained pub fn notify_created_note( } #[oracle(notifyNullifiedNote)] -unconstrained fn notify_nullified_note_oracle( - _nullifier: Field, - _inner_note_hash: Field, - _counter: u32 -) -> Field {} +unconstrained fn notify_nullified_note_oracle(_nullifier: Field, _inner_note_hash: Field, _counter: u32) -> Field {} unconstrained pub fn notify_nullified_note( nullifier: Field, From ead7db90f836f7a0c392143fe174724d908734d2 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 12:12:15 +0000 Subject: [PATCH 37/43] restore initializers --- .../dsl/acir_format/acir_format_mocks.cpp | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp index 5290f93b507..f53ff85d6e7 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp @@ -2,7 +2,33 @@ acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices() { - return {}; + return acir_format::AcirFormatOriginalOpcodeIndices{ + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, + .poly_triple_constraints = {}, + .quad_constraints = {}, + .block_constraints = {}, + }; } void mock_opcode_indices(acir_format::AcirFormat& constraint_system) From 66026fd23e8706c3ce273e5cb5da1e586a9112ad Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 12:18:54 +0000 Subject: [PATCH 38/43] add initializer for gates per opcode --- .../cpp/src/barretenberg/dsl/acir_format/acir_format.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index 1e1faae10a5..80edf92270b 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -112,7 +112,7 @@ struct AcirFormat { // Number of gates added to the circuit per original opcode. // Has length equal to num_acir_opcodes. - std::vector gates_per_opcode; + std::vector gates_per_opcode = {}; // Indices of the original opcode that originated each constraint in AcirFormat. AcirFormatOriginalOpcodeIndices original_opcode_indices; From d3f231c87cb047c2effc68411b145020ac685301 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 12:29:48 +0000 Subject: [PATCH 39/43] address rust pr comments --- noir/noir-repo/Cargo.lock | 5 +---- noir/noir-repo/Cargo.toml | 2 +- noir/noir-repo/tooling/acvm_cli/Cargo.toml | 2 +- noir/noir-repo/tooling/nargo_cli/Cargo.toml | 9 +++++++-- noir/noir-repo/tooling/profiler/Cargo.toml | 9 ++------- noir/noir-repo/tooling/profiler/src/cli/mod.rs | 8 -------- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index e7dfe9ff263..ee688d59e59 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2725,7 +2725,7 @@ dependencies = [ [[package]] name = "noir_profiler" -version = "0.40.0" +version = "0.30.0" dependencies = [ "acir", "clap", @@ -2740,9 +2740,6 @@ dependencies = [ "noirc_driver", "noirc_errors", "noirc_frontend", - "paste", - "proptest", - "rand 0.8.5", "serde", "serde_json", "tempfile", diff --git a/noir/noir-repo/Cargo.toml b/noir/noir-repo/Cargo.toml index e99d13b6a60..eb55bd30795 100644 --- a/noir/noir-repo/Cargo.toml +++ b/noir/noir-repo/Cargo.toml @@ -132,7 +132,7 @@ similar-asserts = "1.5.0" tempfile = "3.6.0" jsonrpc = { version = "0.16.0", features = ["minreq_http"] } flate2 = "1.0.24" - +color-eyre = "0.6.2" im = { version = "15.1", features = ["serde"] } tracing = "0.1.40" tracing-web = "0.1.3" diff --git a/noir/noir-repo/tooling/acvm_cli/Cargo.toml b/noir/noir-repo/tooling/acvm_cli/Cargo.toml index 72424405d36..1939a25ce6a 100644 --- a/noir/noir-repo/tooling/acvm_cli/Cargo.toml +++ b/noir/noir-repo/tooling/acvm_cli/Cargo.toml @@ -20,7 +20,7 @@ path = "src/main.rs" [dependencies] thiserror.workspace = true toml.workspace = true -color-eyre = "0.6.2" +color-eyre.workspace = true clap.workspace = true acvm.workspace = true nargo.workspace = true diff --git a/noir/noir-repo/tooling/nargo_cli/Cargo.toml b/noir/noir-repo/tooling/nargo_cli/Cargo.toml index a9946c8700c..7c7e7b9d5ca 100644 --- a/noir/noir-repo/tooling/nargo_cli/Cargo.toml +++ b/noir/noir-repo/tooling/nargo_cli/Cargo.toml @@ -41,11 +41,16 @@ prettytable-rs = "0.10" rayon = "1.8.0" thiserror.workspace = true tower.workspace = true -async-lsp = { workspace = true, features = ["client-monitor", "stdio", "tracing", "tokio"] } +async-lsp = { workspace = true, features = [ + "client-monitor", + "stdio", + "tracing", + "tokio", +] } const_format.workspace = true similar-asserts.workspace = true termcolor = "1.1.2" -color-eyre = "0.6.2" +color-eyre.workspace = true tokio = { version = "1.0", features = ["io-std", "rt"] } dap.workspace = true clap-markdown = { git = "https://github.com/noir-lang/clap-markdown", rev = "450d759532c88f0dba70891ceecdbc9ff8f25d2b", optional = true } diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 4c2ccc5bf4d..60068fa8c39 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -1,9 +1,7 @@ [package] name = "noir_profiler" description = "Profiler for noir circuits" -# x-release-please-start-version -version = "0.40.0" -# x-release-please-end +version.workspace = true authors.workspace = true edition.workspace = true license.workspace = true @@ -17,7 +15,7 @@ name = "noir-profiler" path = "src/main.rs" [dependencies] -color-eyre = "0.6.2" +color-eyre.workspace = true clap.workspace = true nargo.workspace = true const_format.workspace = true @@ -36,9 +34,6 @@ tracing-subscriber.workspace = true tracing-appender = "0.2.3" [dev-dependencies] -rand = "0.8.5" -proptest = "1.2.0" -paste = "1.0.14" noirc_abi.workspace = true noirc_driver.workspace = true tempfile.workspace = true diff --git a/noir/noir-repo/tooling/profiler/src/cli/mod.rs b/noir/noir-repo/tooling/profiler/src/cli/mod.rs index f6a5637ed86..e94c247263b 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/mod.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/mod.rs @@ -22,7 +22,6 @@ enum GatesFlamegraphCommand { GatesFlamegraph(gates_flamegraph_cmd::GatesFlamegraphCommand), } -#[cfg(not(feature = "codegen-docs"))] pub(crate) fn start_cli() -> eyre::Result<()> { let ProfilerCli { command } = ProfilerCli::parse(); @@ -33,10 +32,3 @@ pub(crate) fn start_cli() -> eyre::Result<()> { Ok(()) } - -#[cfg(feature = "codegen-docs")] -pub(crate) fn start_cli() -> eyre::Result<()> { - let markdown: String = clap_markdown::help_markdown::(); - println!("{markdown}"); - Ok(()) -} From ba784f53e2947dd41f13b22b1601d4d500f88999 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 12:32:32 +0000 Subject: [PATCH 40/43] more pr comment addressing --- noir/noir-repo/Cargo.lock | 1 - noir/noir-repo/tooling/profiler/Cargo.toml | 3 +-- noir/noir-repo/tooling/profiler/src/cli/mod.rs | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/noir/noir-repo/Cargo.lock b/noir/noir-repo/Cargo.lock index ee688d59e59..08bc2589cae 100644 --- a/noir/noir-repo/Cargo.lock +++ b/noir/noir-repo/Cargo.lock @@ -2739,7 +2739,6 @@ dependencies = [ "noirc_abi", "noirc_driver", "noirc_errors", - "noirc_frontend", "serde", "serde_json", "tempfile", diff --git a/noir/noir-repo/tooling/profiler/Cargo.toml b/noir/noir-repo/tooling/profiler/Cargo.toml index 60068fa8c39..baebe9292e6 100644 --- a/noir/noir-repo/tooling/profiler/Cargo.toml +++ b/noir/noir-repo/tooling/profiler/Cargo.toml @@ -25,7 +25,6 @@ fm.workspace = true codespan-reporting.workspace = true inferno = "0.11.19" im.workspace = true -noirc_frontend.workspace = true acir.workspace = true noirc_errors.workspace = true @@ -40,4 +39,4 @@ tempfile.workspace = true [features] default = ["bn254"] -bn254 = ["noirc_frontend/bn254", "acir/bn254"] +bn254 = ["acir/bn254"] diff --git a/noir/noir-repo/tooling/profiler/src/cli/mod.rs b/noir/noir-repo/tooling/profiler/src/cli/mod.rs index e94c247263b..d54a3f6167c 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/mod.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/mod.rs @@ -1,7 +1,6 @@ use clap::{Parser, Subcommand}; use color_eyre::eyre; use const_format::formatcp; -use noirc_frontend as _; mod gates_flamegraph_cmd; From 9cc4691f22f4f2dbd4f44c47ae05ae47ad51f496 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 12:38:31 +0000 Subject: [PATCH 41/43] gcc constructors... --- .../dsl/acir_format/acir_format.test.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index fc7c9603a6d..6085671fb0a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -595,7 +595,32 @@ TEST_F(AcirFormatTests, TestCollectsGateCounts) .varnum = 4, .recursive = false, .num_acir_opcodes = 2, + .public_inputs = {}, + .logic_constraints = {}, + .range_constraints = {}, + .aes128_constraints = {}, + .sha256_constraints = {}, + .sha256_compression = {}, + .schnorr_constraints = {}, + .ecdsa_k1_constraints = {}, + .ecdsa_r1_constraints = {}, + .blake2s_constraints = {}, + .blake3_constraints = {}, + .keccak_constraints = {}, + .keccak_permutations = {}, + .pedersen_constraints = {}, + .pedersen_hash_constraints = {}, + .poseidon2_constraints = {}, + .multi_scalar_mul_constraints = {}, + .ec_add_constraints = {}, + .recursion_constraints = {}, + .honk_recursion_constraints = {}, + .bigint_from_le_bytes_constraints = {}, + .bigint_to_le_bytes_constraints = {}, + .bigint_operations = {}, .poly_triple_constraints = { first_gate, second_gate }, + .quad_constraints = {}, + .block_constraints = {}, .original_opcode_indices = create_empty_original_opcode_indices(), }; mock_opcode_indices(constraint_system); From 7ae3e3466f4171ce5cd577dc4753bce205ccf2d6 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 13:49:17 +0000 Subject: [PATCH 42/43] refactor: use debug artifact instead of implementing files --- .../tooling/nargo/src/artifacts/program.rs | 31 ------------------- .../profiler/src/cli/gates_flamegraph_cmd.rs | 18 ++++++++--- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/noir/noir-repo/tooling/nargo/src/artifacts/program.rs b/noir/noir-repo/tooling/nargo/src/artifacts/program.rs index fb723592d06..91f02157414 100644 --- a/noir/noir-repo/tooling/nargo/src/artifacts/program.rs +++ b/noir/noir-repo/tooling/nargo/src/artifacts/program.rs @@ -2,15 +2,12 @@ use std::collections::BTreeMap; use acvm::acir::circuit::Program; use acvm::FieldElement; -use codespan_reporting::files::{Error, Files, SimpleFile}; use fm::FileId; -use fm::PathString; use noirc_abi::Abi; use noirc_driver::CompiledProgram; use noirc_driver::DebugFile; use noirc_errors::debug_info::ProgramDebugInfo; use serde::{Deserialize, Serialize}; -use std::ops::Range; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct ProgramArtifact { @@ -70,31 +67,3 @@ impl From for CompiledProgram { } } } - -impl<'a> Files<'a> for ProgramArtifact { - type FileId = FileId; - type Name = PathString; - type Source = &'a str; - - fn name(&self, file_id: Self::FileId) -> Result { - self.file_map.get(&file_id).ok_or(Error::FileMissing).map(|file| file.path.clone().into()) - } - - fn source(&'a self, file_id: Self::FileId) -> Result { - self.file_map.get(&file_id).ok_or(Error::FileMissing).map(|file| file.source.as_ref()) - } - - fn line_index(&self, file_id: Self::FileId, byte_index: usize) -> Result { - self.file_map.get(&file_id).ok_or(Error::FileMissing).and_then(|file| { - SimpleFile::new(PathString::from(file.path.clone()), file.source.clone()) - .line_index((), byte_index) - }) - } - - fn line_range(&self, file_id: Self::FileId, line_index: usize) -> Result, Error> { - self.file_map.get(&file_id).ok_or(Error::FileMissing).and_then(|file| { - SimpleFile::new(PathString::from(file.path.clone()), file.source.clone()) - .line_range((), line_index) - }) - } -} diff --git a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs index 89fed68ea05..4f51eed4ba3 100644 --- a/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs +++ b/noir/noir-repo/tooling/profiler/src/cli/gates_flamegraph_cmd.rs @@ -7,6 +7,7 @@ use clap::Args; use codespan_reporting::files::Files; use color_eyre::eyre::{self, Context}; use inferno::flamegraph::{from_lines, Options}; +use nargo::artifacts::debug::DebugArtifact; use serde::{Deserialize, Serialize}; use acir::circuit::OpcodeLocation; @@ -120,10 +121,17 @@ fn run_with_provider( ) -> eyre::Result<()> { let program = read_program_from_file(artifact_path).context("Error reading program from file")?; + let backend_gates_response = gates_provider.get_gates(artifact_path).context("Error querying backend for gates")?; - for (func_idx, func_gates) in backend_gates_response.functions.into_iter().enumerate() { + let function_names = program.names.clone(); + + let debug_artifact: DebugArtifact = program.into(); + + for (func_idx, (func_gates, func_name)) in + backend_gates_response.functions.into_iter().zip(function_names).enumerate() + { println!( "Opcode count: {}, Total gates by opcodes: {}, Circuit size: {}", func_gates.acir_opcodes, @@ -135,13 +143,13 @@ fn run_with_provider( let mut folded_stack_items = BTreeMap::new(); func_gates.gates_per_opcode.into_iter().enumerate().for_each(|(opcode_index, gates)| { - let call_stack = &program.debug_symbols.debug_infos[func_idx] + let call_stack = &debug_artifact.debug_symbols[func_idx] .locations .get(&OpcodeLocation::Acir(opcode_index)); let location_names = if let Some(call_stack) = call_stack { call_stack .iter() - .map(|location| location_to_callsite_label(*location, &program)) + .map(|location| location_to_callsite_label(*location, &debug_artifact)) .collect::>() } else { vec!["unknown".to_string()] @@ -154,8 +162,8 @@ fn run_with_provider( flamegraph_generator.generate_flamegraph( folded_lines.iter().map(|as_string| as_string.as_str()), artifact_path.to_str().unwrap(), - &program.names[func_idx], - &Path::new(&output_path).join(Path::new(&format!("{}.svg", &program.names[func_idx]))), + &func_name, + &Path::new(&output_path).join(Path::new(&format!("{}.svg", &func_name))), )?; } From ebe49857ca1726ad081ba5100f3dab21b15e054d Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 13 Jun 2024 14:56:48 +0000 Subject: [PATCH 43/43] revert formatting change --- noir/noir-repo/Cargo.toml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/noir/noir-repo/Cargo.toml b/noir/noir-repo/Cargo.toml index 98cd414ed63..be4cd81ab58 100644 --- a/noir/noir-repo/Cargo.toml +++ b/noir/noir-repo/Cargo.toml @@ -79,12 +79,8 @@ bb_abstraction_leaks = { path = "tooling/bb_abstraction_leaks" } acvm_cli = { path = "tooling/acvm_cli" } # Arkworks -ark-bn254 = { version = "^0.4.0", default-features = false, features = [ - "curve", -] } -ark-bls12-381 = { version = "^0.4.0", default-features = false, features = [ - "curve", -] } +ark-bn254 = { version = "^0.4.0", default-features = false, features = ["curve"] } +ark-bls12-381 = { version = "^0.4.0", default-features = false, features = ["curve"] } grumpkin = { version = "0.1.0", package = "noir_grumpkin", features = ["std"] } ark-ec = { version = "^0.4.0", default-features = false } ark-ff = { version = "^0.4.0", default-features = false }