Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce compilation by breaking up acir types.hpp #6816

Merged
merged 10 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ jobs:
+bench-comment

bb-gcc:
needs: [build, changes]
needs: [setup, changes]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't running (luckily it passes so we're ok)

runs-on: ${{ github.event.pull_request.user.login || github.actor }}-x86
if: ${{ needs.changes.outputs.barretenberg-cpp == 'true' }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "barretenberg/bb/file_io.hpp"
#include "barretenberg/client_ivc/client_ivc.hpp"
#include "barretenberg/common/map.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/dsl/acir_format/acir_format.hpp"
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/honk/proof_system/types/proof.hpp"
#include "barretenberg/plonk/proof_system/proving_key/serialize.hpp"
#include "barretenberg/vm/avm_trace/avm_execution.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <benchmark/benchmark.h>

using namespace benchmark;
using namespace bb;
using namespace bb::crypto::merkle_tree;

using TreeType = MerkleTree<MemoryStore, PedersenHashPolicy>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace bb::crypto::merkle_tree {

using fr = bb::stdlib::fr;
using fr_hash_path = std::vector<std::pair<fr, fr>>;
using fr_sibling_path = std::vector<fr>;
template <typename Ctx> using hash_path = std::vector<std::pair<bb::stdlib::field_t<Ctx>, bb::stdlib::field_t<Ctx>>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "memory_store.hpp"
#include "memory_tree.hpp"

using namespace bb;
using namespace bb::stdlib;
using namespace bb::crypto::merkle_tree;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "acir_format.hpp"
#include "barretenberg/common/log.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"
#include <cstddef>

namespace acir_format {

using namespace bb;

template class DSLBigInts<UltraCircuitBuilder>;
template class DSLBigInts<MegaCircuitBuilder>;

Expand Down Expand Up @@ -284,11 +287,11 @@ void build_constraints(Builder& builder,
size_t agg_obj_indices_idx = 0;
for (fq val : aggregation_object_fq_values) {
const uint256_t x = val;
std::array<bb::fr, fq_ct::NUM_LIMBS> val_limbs = {
std::array<fr, fq_ct::NUM_LIMBS> val_limbs = {
x.slice(0, fq_ct::NUM_LIMB_BITS),
x.slice(fq_ct::NUM_LIMB_BITS, fq_ct::NUM_LIMB_BITS * 2),
x.slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 3),
x.slice(fq_ct::NUM_LIMB_BITS * 3, bb::stdlib::field_conversion::TOTAL_BITS)
x.slice(fq_ct::NUM_LIMB_BITS * 3, stdlib::field_conversion::TOTAL_BITS)
};
for (size_t i = 0; i < fq_ct::NUM_LIMBS; ++i) {
uint32_t idx = builder.add_variable(val_limbs[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "schnorr_verify.hpp"
#include "sha256_constraint.hpp"
#include <utility>
#include <vector>

namespace acir_format {

Expand Down Expand Up @@ -63,10 +64,11 @@ struct AcirFormat {
// 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
// This could be a large vector so use slab allocator, we don't expect the blackbox implementations to be so large.
std::vector<poly_triple_<curve::BN254::ScalarField>,
ContainerSlabAllocator<poly_triple_<curve::BN254::ScalarField>>>
std::vector<bb::poly_triple_<bb::curve::BN254::ScalarField>,
bb::ContainerSlabAllocator<bb::poly_triple_<bb::curve::BN254::ScalarField>>>
poly_triple_constraints;
std::vector<mul_quad_<curve::BN254::ScalarField>, ContainerSlabAllocator<mul_quad_<curve::BN254::ScalarField>>>
std::vector<bb::mul_quad_<bb::curve::BN254::ScalarField>,
bb::ContainerSlabAllocator<bb::mul_quad_<bb::curve::BN254::ScalarField>>>
quad_constraints;
std::vector<BlockConstraint> block_constraints;

Expand Down Expand Up @@ -101,7 +103,7 @@ struct AcirFormat {
friend bool operator==(AcirFormat const& lhs, AcirFormat const& rhs) = default;
};

using WitnessVector = std::vector<fr, ContainerSlabAllocator<fr>>;
using WitnessVector = std::vector<bb::fr, bb::ContainerSlabAllocator<bb::fr>>;
using WitnessVectorStack = std::vector<std::pair<uint32_t, WitnessVector>>;

struct AcirProgram {
Expand Down Expand Up @@ -139,12 +141,12 @@ struct AcirProgramStack {
void pop_back() { witness_stack.pop_back(); }
};

template <typename Builder = UltraCircuitBuilder>
template <typename Builder = bb::UltraCircuitBuilder>
Builder create_circuit(const AcirFormat& constraint_system,
size_t size_hint = 0,
WitnessVector const& witness = {},
bool honk_recursion = false,
std::shared_ptr<ECCOpQueue> op_queue = std::make_shared<ECCOpQueue>());
std::shared_ptr<bb::ECCOpQueue> op_queue = std::make_shared<bb::ECCOpQueue>());

template <typename Builder>
void build_constraints(Builder& builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include "acir_format.hpp"
#include "barretenberg/common/streams.hpp"
#include "barretenberg/crypto/schnorr/schnorr.hpp"
#include "barretenberg/plonk/composer/standard_composer.hpp"
#include "barretenberg/plonk/composer/ultra_composer.hpp"
#include "barretenberg/plonk/proof_system/types/proof.hpp"
#include "barretenberg/serialize/test_helper.hpp"
#include "ecdsa_secp256k1.hpp"
Expand All @@ -11,6 +14,8 @@ using namespace bb;
using namespace bb::crypto;
using namespace acir_format;

using Composer = plonk::UltraComposer;

class AcirFormatTests : public ::testing::Test {
protected:
static void SetUpTestSuite() { srs::init_crs_factory("../srs_db/ignition"); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "barretenberg/client_ivc/client_ivc.hpp"
#include "barretenberg/plonk/composer/ultra_composer.hpp"
#ifndef __wasm__
#include "barretenberg/bb/exec_pipe.hpp"
#include "barretenberg/circuit_checker/circuit_checker.hpp"
#include "barretenberg/common/streams.hpp"
#include "barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp"

Expand All @@ -9,6 +11,7 @@

// #define LOG_SIZES

using namespace bb;
class AcirIntegrationTest : public ::testing::Test {
public:
static std::vector<uint8_t> get_bytecode(const std::string& bytecodePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "acir_to_constraint_buf.hpp"
#include "barretenberg/common/container.hpp"
#ifndef __wasm__
#include "barretenberg/bb/get_bytecode.hpp"
#endif
#include "barretenberg/common/map.hpp"
namespace acir_format {

using namespace bb;

/**
* @brief Construct a poly_tuple for a standard width-3 arithmetic gate from its acir representation
*
Expand Down Expand Up @@ -50,7 +55,7 @@ poly_triple serialize_arithmetic_gate(Program::Expression const& arg)
// If necessary, set values for linears terms q_l * w_l, q_r * w_r and q_o * w_o
ASSERT(arg.linear_combinations.size() <= 3); // We can only accommodate 3 linear terms
for (const auto& linear_term : arg.linear_combinations) {
bb::fr selector_value(uint256_t(std::get<0>(linear_term)));
fr selector_value(uint256_t(std::get<0>(linear_term)));
uint32_t witness_idx = std::get<1>(linear_term).value;

// If the witness index has not yet been set or if the corresponding linear term is active, set the witness
Expand Down Expand Up @@ -88,24 +93,24 @@ poly_triple serialize_arithmetic_gate(Program::Expression const& arg)
pt.q_c = uint256_t(arg.q_c);
return pt;
}
mul_quad_<bb::fr> serialize_mul_quad_gate(Program::Expression const& arg)
mul_quad_<fr> serialize_mul_quad_gate(Program::Expression const& arg)
{
// TODO(https://github.com/AztecProtocol/barretenberg/issues/816): The initialization of the witness indices a,b,c
// to 0 is implicitly assuming that (builder.zero_idx == 0) which is no longer the case. Now, witness idx 0 in
// general will correspond to some non-zero value and some witnesses which are not explicitly set below will be
// erroneously populated with this value. This does not cause failures however because the corresponding selector
// will indeed be 0 so the gate will be satisfied. Still, its a bad idea to have erroneous wire values
// even if they dont break the relation. They'll still add cost in commitments, for example.
mul_quad_<bb::fr> quad{ .a = 0,
.b = 0,
.c = 0,
.d = 0,
.mul_scaling = 0,
.a_scaling = 0,
.b_scaling = 0,
.c_scaling = 0,
.d_scaling = 0,
.const_scaling = 0 };
mul_quad_<fr> quad{ .a = 0,
.b = 0,
.c = 0,
.d = 0,
.mul_scaling = 0,
.a_scaling = 0,
.b_scaling = 0,
.c_scaling = 0,
.d_scaling = 0,
.const_scaling = 0 };

// Flags indicating whether each witness index for the present mul_quad has been set
bool a_set = false;
Expand All @@ -125,7 +130,7 @@ mul_quad_<bb::fr> serialize_mul_quad_gate(Program::Expression const& arg)
// If necessary, set values for linears terms q_l * w_l, q_r * w_r and q_o * w_o
ASSERT(arg.linear_combinations.size() <= 4); // We can only accommodate 4 linear terms
for (const auto& linear_term : arg.linear_combinations) {
bb::fr selector_value(uint256_t(std::get<0>(linear_term)));
fr selector_value(uint256_t(std::get<0>(linear_term)));
uint32_t witness_idx = std::get<1>(linear_term).value;

// If the witness index has not yet been set or if the corresponding linear term is active, set the witness
Expand Down Expand Up @@ -543,10 +548,10 @@ WitnessVector witness_map_to_witness_vector(WitnessStack::WitnessMap const& witn
// To ensure that witnesses sit at the correct indices in the `WitnessVector`, we fill any indices
// which do not exist within the `WitnessMap` with the dummy value of zero.
while (index < e.first.value) {
wv.push_back(bb::fr(0));
wv.push_back(fr(0));
index++;
}
wv.push_back(bb::fr(uint256_t(e.second)));
wv.push_back(fr(uint256_t(e.second)));
index++;
}
return wv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <cstdio>
#include <span>

using namespace bb;

namespace acir_format {

template <typename Builder> void create_aes128_constraints(Builder& builder, const AES128Constraint& constraint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include <array>
#include <cstdint>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "bigint_constraint.hpp"
#include "barretenberg/common/assert.hpp"
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/numeric/uintx/uintx.hpp"
#include "barretenberg/stdlib/primitives/bigfield/bigfield.hpp"
Expand All @@ -9,6 +8,8 @@

namespace acir_format {

using namespace bb;

ModulusId modulus_param_to_id(ModulusParam param)
{
if (Bn254FqParams::modulus_0 == param.modulus_0 && Bn254FqParams::modulus_1 == param.modulus_1 &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/ecc/curves/secp256k1/secp256k1.hpp"
#include "barretenberg/ecc/curves/secp256r1/secp256r1.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include "barretenberg/stdlib/primitives/bigfield/bigfield.hpp"

#include <array>
#include <cstdint>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "acir_format.hpp"
#include "barretenberg/circuit_checker/circuit_checker.hpp"
#include "barretenberg/numeric/uint256/uint256.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"

Expand All @@ -11,6 +12,8 @@

namespace acir_format::tests {

using Composer = plonk::UltraComposer;

class BigIntTests : public ::testing::Test {
protected:
static void SetUpTestSuite() { bb::srs::init_crs_factory("../srs_db/ignition"); }
Expand Down Expand Up @@ -228,7 +231,7 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple)

BigIntFromLeBytes from_le_bytes_constraint_bigint1{
.inputs = { 1 },
.modulus = { 0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
.modulus = { 0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30, },
.result = 1,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "blake2s_constraint.hpp"
#include "barretenberg/stdlib/hash/blake2s/blake2s.hpp"
#include "barretenberg/stdlib/primitives/byte_array/byte_array.hpp"
#include "round.hpp"

namespace acir_format {

using namespace bb;

template <typename Builder> void create_blake2s_constraints(Builder& builder, const Blake2sConstraint& constraint)
{
using byte_array_ct = bb::stdlib::byte_array<Builder>;
using field_ct = bb::stdlib::field_t<Builder>;
using byte_array_ct = stdlib::byte_array<Builder>;
using field_ct = stdlib::field_t<Builder>;

// Create byte array struct
byte_array_ct arr(&builder);
Expand All @@ -26,7 +30,7 @@ template <typename Builder> void create_blake2s_constraints(Builder& builder, co
arr.write(element_bytes);
}

byte_array_ct output_bytes = bb::stdlib::blake2s<Builder>(arr);
byte_array_ct output_bytes = stdlib::blake2s<Builder>(arr);

// Convert byte array to vector of field_t
auto bytes = output_bytes.bytes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include <array>
#include <cstdint>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "blake3_constraint.hpp"
#include "barretenberg/stdlib/hash/blake3s/blake3s.hpp"
#include "barretenberg/stdlib/primitives/byte_array/byte_array.hpp"
#include "round.hpp"

namespace acir_format {
Expand Down Expand Up @@ -36,9 +38,9 @@ template <typename Builder> void create_blake3_constraints(Builder& builder, con
}
}

template void create_blake3_constraints<UltraCircuitBuilder>(UltraCircuitBuilder& builder,
const Blake3Constraint& constraint);
template void create_blake3_constraints<MegaCircuitBuilder>(MegaCircuitBuilder& builder,
const Blake3Constraint& constraint);
template void create_blake3_constraints<bb::UltraCircuitBuilder>(bb::UltraCircuitBuilder& builder,
const Blake3Constraint& constraint);
template void create_blake3_constraints<bb::MegaCircuitBuilder>(bb::MegaCircuitBuilder& builder,
const Blake3Constraint& constraint);

} // namespace acir_format
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include <array>
#include <cstdint>
#include <vector>

Expand Down
Loading
Loading