Skip to content

Commit

Permalink
Merge pull request #895 from AztecProtocol/cg/circuit-builders
Browse files Browse the repository at this point in the history
refactor!: Use circuit builders
  • Loading branch information
codygunton authored Jun 28, 2023
2 parents 4bd5469 + 5d20a8d commit 1df243e
Show file tree
Hide file tree
Showing 145 changed files with 1,798 additions and 1,829 deletions.
6 changes: 3 additions & 3 deletions circuits/CODING_STANDARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ Here are the types of automation that should help stick to the standard:
// ...
} // namespace aztec3::circuits::abis::private_kernel
```
* use`init.hpp` *only* for core/critical renames like `NT/CT` and for toggling core types like `Composer`
* use`init.hpp` *only* for core/critical renames like `NT/CT` and for toggling core types like `CircuitBuilder`
* use unnamed/anonymous namespaces to import and shorten external names into *just this one file*
* all of a file's external imports belong in a single anonymous namespace `namespace { ...\n } // namespace` at the very top of the file directly after `#include`s
* use `using Rename = old::namespace::prefix::Name;` to import and shorten names from external namespaces
* avoid using renames to obscure template params (`using A = A<NT>;`)
* never use renames to remove the `std::` prefix
* never use renames to remove a `NT::` or `CT::` prefix
* `test.cpp` tests must always explicitly import every single name they intend to use
* they might want to test over multiple namespaces, native and circuit types, and composer types
* they might want to test over multiple namespaces, native and circuit types, and builder types
* avoid calling barretenberg's functions directly and instead go through interface files like `circuit_types` and
`native_types`
* `using` statements should be sorted case according to the `LexicographicNumeric` rules
Expand All @@ -155,7 +155,7 @@ Here are the types of automation that should help stick to the standard:
1. **includes**
* start every header with `#pragma once`
* `index.hpp` should include common headers that will be referenced by most cpp/hpp files in the current directory
* `init.hpp` should inject ONLY critical renames (like `NT`/`CT`) and type toggles (like Composer)
* `init.hpp` should inject ONLY critical renames (like `NT`/`CT`) and type toggles (like CircuitBuilder)
* example `using NT = aztec3::utils::types::NativeTypes;`
* avoid including headers via relative paths (`../../other_dir`) unless they are a subdir (`subdir/header.hpp`)
* use full path like `aztec3/circuits/hash.hpp`
Expand Down
2 changes: 1 addition & 1 deletion circuits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The core Aztec 3 C++ code lives in `cpp/src/aztec3/`, and is split into the foll
- `rollup`: rollup circuits, their interfaces, and thier tests
- `recursion`: types and examples for aggregation of recursive proof objects
- `mock`: mock circuits
- `oracle`: used to fetch external information (like private data notes) and inject them as inputs into the circuit "Composer" during execution of circuit logic ([more here](https://github.com/AztecProtocol/aztec3-packages/tree/master/circuits/cpp/src/aztec3/oracle))
- `oracle`: used to fetch external information (like private data notes) and inject them as inputs into the circuit during execution of circuit logic ([more here](https://github.com/AztecProtocol/aztec3-packages/tree/master/circuits/cpp/src/aztec3/oracle))
- `dbs`: database infrastructure (_e.g._ PrivateStateDb)

### Typescript
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/barretenberg
Submodule barretenberg updated 316 files
14 changes: 7 additions & 7 deletions circuits/cpp/src/aztec3/circuits/abis/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <gtest/gtest.h>

namespace {
// Composer
using Composer = plonk::UltraPlonkComposer;
// Builder
using Builder = UltraCircuitBuilder;

// Types
using CT = aztec3::utils::types::CircuitTypes<Composer>;
using CT = aztec3::utils::types::CircuitTypes<Builder>;
using NT = aztec3::utils::types::NativeTypes;
} // namespace

Expand Down Expand Up @@ -74,8 +74,8 @@ TEST(abi_tests, native_to_circuit_function_data)

info("function data: ", native_function_data);

Composer composer = Composer("../barretenberg/cpp/srs_db/ignition");
FunctionData<CT> const circuit_function_data = native_function_data.to_circuit_type(composer);
Builder builder = Builder();
FunctionData<CT> const circuit_function_data = native_function_data.to_circuit_type(builder);

info("function data: ", circuit_function_data);
}
Expand Down Expand Up @@ -105,8 +105,8 @@ TEST(abi_tests, native_to_circuit_call_context)

info("call context: ", native_call_context);

Composer composer = Composer("../barretenberg/cpp/srs_db/ignition");
CallContext<CT> const circuit_call_context = native_call_context.to_circuit_type(composer);
Builder builder = Builder();
CallContext<CT> const circuit_call_context = native_call_context.to_circuit_type(builder);

info("call context: ", circuit_call_context);
}
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Contains all ABIs for use by:
- Kernel circuits
- Rollup circuits

All ABIs are generalised by an `NCT` template parameter (meaning `NativeOrCircuitTypes`). `NCT` can be either `aztec3::utils::types::NativeTypes` or `aztec3::utils::types::CircuitTypes<Composer>` for some `Composer`. The idea being, there's a single implementation of every struct/class for native and circuit types. NativeType structs can be switched to CircuitType with the `to_circuit_type()` method.
All ABIs are generalised by an `NCT` template parameter (meaning `NativeOrCircuitTypes`). `NCT` can be either `aztec3::utils::types::NativeTypes` or `aztec3::utils::types::CircuitTypes<Builder>` for some `Builder`. The idea being, there's a single implementation of every struct/class for native and circuit types. NativeType structs can be switched to CircuitType with the `to_circuit_type()` method.
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ WASM_EXPORT const char* abis__test_roundtrip_serialize_combined_accumulated_data

WASM_EXPORT const char* abis__test_roundtrip_serialize_signature(uint8_t const* input, uint32_t* size)
{
return as_string_output<NT::ecdsa_signature>(input, size);
return as_string_output<NT::schnorr_signature>(input, size);
}

WASM_EXPORT const char* abis__test_roundtrip_serialize_private_kernel_inputs_inner(uint8_t const* input, uint32_t* size)
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ TEST(abi_tests, hash_vk)
{
// Initialize some random VK data
NT::VKData vk_data;
vk_data.composer_type = engine.get_random_uint32();
vk_data.circuit_type = engine.get_random_uint32();
vk_data.circuit_size = static_cast<uint32_t>(1) << (engine.get_random_uint8() >> 3); // must be a power of two
vk_data.num_public_inputs = engine.get_random_uint32();
vk_data.commitments["test1"] = g1::element::random_element();
Expand Down
18 changes: 9 additions & 9 deletions circuits/cpp/src/aztec3/circuits/abis/call_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ template <typename NCT> struct CallContext {
return utils::msgpack_derived_equals<boolean>(*this, other);
};

template <typename Composer> CallContext<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> CallContext<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

CallContext<CircuitTypes<Composer>> call_context = {
CallContext<CircuitTypes<Builder>> call_context = {
to_ct(msg_sender), to_ct(storage_contract_address), to_ct(portal_contract_address),
to_ct(is_delegate_call), to_ct(is_static_call), to_ct(is_contract_deployment),

Expand All @@ -56,10 +56,10 @@ template <typename NCT> struct CallContext {
return call_context;
};

template <typename Composer> CallContext<NativeTypes> to_native_type() const
template <typename Builder> CallContext<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };

CallContext<NativeTypes> call_context = {
to_nt(msg_sender), to_nt(storage_contract_address), to_nt(portal_contract_address),
Expand All @@ -79,9 +79,9 @@ template <typename NCT> struct CallContext {
return NCT::compress(inputs, GeneratorIndex::CALL_CONTEXT);
}

template <typename Composer> void assert_is_zero()
template <typename Builder> void assert_is_zero()
{
static_assert((std::is_same<CircuitTypes<Composer>, NCT>::value));
static_assert((std::is_same<CircuitTypes<Builder>, NCT>::value));

msg_sender.to_field().assert_is_zero();
storage_contract_address.to_field().assert_is_zero();
Expand Down
14 changes: 7 additions & 7 deletions circuits/cpp/src/aztec3/circuits/abis/call_stack_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ template <typename NCT, template <class> typename PrivatePublic> struct CallStac
return utils::msgpack_derived_equals<boolean>(*this, other);
};

template <typename Composer>
CallStackItem<CircuitTypes<Composer>, PrivatePublic> to_circuit_type(Composer& composer) const
template <typename Builder>
CallStackItem<CircuitTypes<Builder>, PrivatePublic> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

CallStackItem<CircuitTypes<Composer>, PrivatePublic> call_stack_item = {
CallStackItem<CircuitTypes<Builder>, PrivatePublic> call_stack_item = {
to_ct(contract_address),
function_data.to_circuit_type(composer),
public_inputs.to_circuit_type(composer),
function_data.to_circuit_type(builder),
public_inputs.to_circuit_type(builder),
to_ct(is_execution_request),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ template <typename NCT> struct CombinedAccumulatedData {
public_data_reads == other.public_data_reads;
};

template <typename Composer>
CombinedAccumulatedData<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> CombinedAccumulatedData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
typedef CircuitTypes<Composer> CT;
typedef CircuitTypes<Builder> CT;
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(composer); };
// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(builder); };

CombinedAccumulatedData<CT> acc_data = {
typename CT::AggregationObject{
Expand Down Expand Up @@ -122,11 +121,11 @@ template <typename NCT> struct CombinedAccumulatedData {
return acc_data;
};

template <typename Composer> CombinedAccumulatedData<NativeTypes> to_native_type() const
template <typename Builder> CombinedAccumulatedData<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Composer>(); };
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };

CombinedAccumulatedData<NativeTypes> acc_data = {
typename NativeTypes::AggregationObject{
Expand Down
14 changes: 7 additions & 7 deletions circuits/cpp/src/aztec3/circuits/abis/combined_constant_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ template <typename NCT> struct CombinedConstantData {
return historic_tree_roots == other.historic_tree_roots && tx_context == other.tx_context;
}

template <typename Composer> CombinedConstantData<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> CombinedConstantData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

CombinedConstantData<CircuitTypes<Composer>> constant_data = {
historic_tree_roots.to_circuit_type(composer),
tx_context.to_circuit_type(composer),
CombinedConstantData<CircuitTypes<Builder>> constant_data = {
historic_tree_roots.to_circuit_type(builder),
tx_context.to_circuit_type(builder),
};

return constant_data;
};

template <typename Composer> CombinedConstantData<NativeTypes> to_native_type() const
template <typename Builder> CombinedConstantData<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);

auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Composer>(); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };

CombinedConstantData<NativeTypes> constant_data = {
to_native_type(historic_tree_roots),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@ template <typename NCT> struct CombinedHistoricTreeRoots {
return private_historic_tree_roots == other.private_historic_tree_roots;
};

template <typename Composer>
CombinedHistoricTreeRoots<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> CombinedHistoricTreeRoots<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(composer); };
// Capture the circuit builder:
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(builder); };

CombinedHistoricTreeRoots<CircuitTypes<Composer>> data = {
CombinedHistoricTreeRoots<CircuitTypes<Builder>> data = {
to_circuit_type(private_historic_tree_roots),
};

return data;
};

template <typename Composer> CombinedHistoricTreeRoots<NativeTypes> to_native_type() const
template <typename Builder> CombinedHistoricTreeRoots<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
auto to_native_type = [&]<typename T>(T& e) { return e.template to_native_type<Composer>(); };
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_native_type = [&]<typename T>(T& e) { return e.template to_native_type<Builder>(); };

CombinedHistoricTreeRoots<NativeTypes> data = {
to_native_type(private_historic_tree_roots),
Expand Down
23 changes: 11 additions & 12 deletions circuits/cpp/src/aztec3/circuits/abis/contract_deployment_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ template <typename NCT> struct ContractDeploymentData {
portal_contract_address == other.portal_contract_address;
};

template <typename Composer>
ContractDeploymentData<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> ContractDeploymentData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

ContractDeploymentData<CircuitTypes<Composer>> data = {
deployer_public_key.to_circuit_type(composer),
ContractDeploymentData<CircuitTypes<Builder>> data = {
deployer_public_key.to_circuit_type(builder),
to_ct(constructor_vk_hash),
to_ct(function_tree_root),
to_ct(contract_address_salt),
Expand All @@ -56,11 +55,11 @@ template <typename NCT> struct ContractDeploymentData {
return data;
};

template <typename Composer> ContractDeploymentData<NativeTypes> to_native_type() const
template <typename Builder> ContractDeploymentData<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Composer>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Composer>(); };
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };

ContractDeploymentData<NativeTypes> call_context = {
to_native_type(deployer_public_key), to_nt(constructor_vk_hash), to_nt(function_tree_root),
Expand All @@ -70,9 +69,9 @@ template <typename NCT> struct ContractDeploymentData {
return call_context;
};

template <typename Composer> void assert_is_zero()
template <typename Builder> void assert_is_zero()
{
static_assert((std::is_same<CircuitTypes<Composer>, NCT>::value));
static_assert((std::is_same<CircuitTypes<Builder>, NCT>::value));

deployer_public_key.assert_is_zero();
constructor_vk_hash.assert_is_zero();
Expand Down
14 changes: 7 additions & 7 deletions circuits/cpp/src/aztec3/circuits/abis/contract_storage_read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ template <typename NCT> struct ContractStorageRead {
MSGPACK_FIELDS(storage_slot, current_value);
bool operator==(ContractStorageRead<NCT> const&) const = default;

template <typename Composer> ContractStorageRead<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
template <typename Builder> ContractStorageRead<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

ContractStorageRead<CircuitTypes<Composer>> contract_storage_read = {
ContractStorageRead<CircuitTypes<Builder>> contract_storage_read = {
to_ct(storage_slot),
to_ct(current_value),
};

return contract_storage_read;
};

template <typename Composer> ContractStorageRead<NativeTypes> to_native_type() const
template <typename Builder> ContractStorageRead<NativeTypes> to_native_type() const
{
static_assert((std::is_same<CircuitTypes<Composer>, NCT>::value));
static_assert((std::is_same<CircuitTypes<Builder>, NCT>::value));

auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Composer>(e); };
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };

ContractStorageRead<NativeTypes> contract_storage_read = {
to_nt(storage_slot),
Expand Down
Loading

0 comments on commit 1df243e

Please sign in to comment.