Skip to content

Commit

Permalink
feat: Make the circuit constructors field agnostic so we can check ci…
Browse files Browse the repository at this point in the history
…rcuits on grumpkin (#534)

Co-authored-by: maramihali <mara@aztecprotocol.com>
Co-authored-by: codygunton <codygunton@gmail.com>
  • Loading branch information
3 people authored Jul 5, 2023
1 parent 96891de commit 656d794
Show file tree
Hide file tree
Showing 14 changed files with 1,660 additions and 1,574 deletions.
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/ecc/curves/bn254/bn254.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "../bn254/fq.hpp"
#include "../bn254/fq12.hpp"
#include "../bn254/fq2.hpp"
#include "../bn254/fr.hpp"
#include "../bn254/g1.hpp"
Expand All @@ -15,5 +16,6 @@ class BN254 {
using AffineElement = typename Group::affine_element;
using G2AffineElement = typename barretenberg::g2::affine_element;
using G2BaseField = typename barretenberg::fq2;
using TargetField = barretenberg::fq12;
};
} // namespace curve
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ template <typename FF, size_t num_selectors> struct SelectorsBase {

// These are not magic numbers and they should not be written with global constants. These parameters are not accessible
// through clearly named static class members.
template <typename FF> class Standard : public Arithmetization</*NUM_WIRES =*/3, /*num_selectors =*/5> {
template <typename _FF> class Standard : public Arithmetization</*NUM_WIRES =*/3, /*num_selectors =*/5> {
public:
using FF = _FF;
struct Selectors : SelectorsBase<FF, num_selectors> {
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_m = std::get<0>(this->_data);
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_1 = std::get<1>(this->_data);
Expand Down Expand Up @@ -79,8 +80,9 @@ template <typename FF> class Standard : public Arithmetization</*NUM_WIRES =*/3,
};
};

template <typename FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
template <typename _FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
public:
using FF = _FF;
struct Selectors : SelectorsBase<FF, num_selectors> {
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_m = std::get<0>(this->_data);
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_c = std::get<1>(this->_data);
Expand Down Expand Up @@ -122,8 +124,9 @@ template <typename FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*
};
};

template <typename FF> class Ultra : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
template <typename _FF> class Ultra : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
public:
using FF = _FF;
struct Selectors : SelectorsBase<FF, num_selectors> {
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_m = std::get<0>(this->_data);
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_c = std::get<1>(this->_data);
Expand Down
100 changes: 56 additions & 44 deletions cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,73 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include <cstdint>

// TODO(#557): The field-specific aliases for gates should be removed and the type could be explicit when this
// structures are used to avoid having foo_gate and foo_gate_grumpkin (i.e. use foo_gate<field> instead). Moreover, we
// need to ensure the read/write functions handle grumpkin gates as well.
namespace proof_system {
struct add_triple {
template <typename FF> struct add_triple_ {
uint32_t a;
uint32_t b;
uint32_t c;
barretenberg::fr a_scaling;
barretenberg::fr b_scaling;
barretenberg::fr c_scaling;
barretenberg::fr const_scaling;
FF a_scaling;
FF b_scaling;
FF c_scaling;
FF const_scaling;
};
using add_triple = add_triple_<barretenberg::fr>;

struct add_quad {
template <typename FF> struct add_quad_ {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
barretenberg::fr a_scaling;
barretenberg::fr b_scaling;
barretenberg::fr c_scaling;
barretenberg::fr d_scaling;
barretenberg::fr const_scaling;
FF a_scaling;
FF b_scaling;
FF c_scaling;
FF d_scaling;
FF const_scaling;
};
using add_quad = add_quad_<barretenberg::fr>;

struct mul_quad {
template <typename FF> struct mul_quad_ {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
barretenberg::fr mul_scaling;
barretenberg::fr a_scaling;
barretenberg::fr b_scaling;
barretenberg::fr c_scaling;
barretenberg::fr d_scaling;
barretenberg::fr const_scaling;
FF mul_scaling;
FF a_scaling;
FF b_scaling;
FF c_scaling;
FF d_scaling;
FF const_scaling;
};
using mul_quad = mul_quad_<barretenberg::fr>;

struct mul_triple {
template <typename FF> struct mul_triple_ {
uint32_t a;
uint32_t b;
uint32_t c;
barretenberg::fr mul_scaling;
barretenberg::fr c_scaling;
barretenberg::fr const_scaling;
FF mul_scaling;
FF c_scaling;
FF const_scaling;
};
using mul_triple = mul_triple_<barretenberg::fr>;

struct poly_triple {
template <typename FF> struct poly_triple_ {
uint32_t a;
uint32_t b;
uint32_t c;
barretenberg::fr q_m;
barretenberg::fr q_l;
barretenberg::fr q_r;
barretenberg::fr q_o;
barretenberg::fr q_c;
FF q_m;
FF q_l;
FF q_r;
FF q_o;
FF q_c;

friend bool operator==(poly_triple const& lhs, poly_triple const& rhs) = default;
friend bool operator==(poly_triple_<FF> const& lhs, poly_triple_<FF> const& rhs) = default;
};

using poly_triple = poly_triple_<barretenberg::fr>;

template <typename B> inline void read(B& buf, poly_triple& constraint)
{
using serialize::read;
Expand All @@ -73,7 +82,6 @@ template <typename B> inline void read(B& buf, poly_triple& constraint)
read(buf, constraint.q_o);
read(buf, constraint.q_c);
}

template <typename B> inline void write(B& buf, poly_triple const& constraint)
{
using serialize::write;
Expand All @@ -87,38 +95,42 @@ template <typename B> inline void write(B& buf, poly_triple const& constraint)
write(buf, constraint.q_c);
}

struct fixed_group_add_quad {
template <typename FF> struct fixed_group_add_quad_ {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
barretenberg::fr q_x_1;
barretenberg::fr q_x_2;
barretenberg::fr q_y_1;
barretenberg::fr q_y_2;
FF q_x_1;
FF q_x_2;
FF q_y_1;
FF q_y_2;
};
using fixed_group_add_quad = fixed_group_add_quad_<barretenberg::fr>;

struct fixed_group_init_quad {
barretenberg::fr q_x_1;
barretenberg::fr q_x_2;
barretenberg::fr q_y_1;
barretenberg::fr q_y_2;
template <typename FF> struct fixed_group_init_quad_ {
FF q_x_1;
FF q_x_2;
FF q_y_1;
FF q_y_2;
};
using fixed_group_init_quad = fixed_group_init_quad_<barretenberg::fr>;

struct accumulator_triple {
template <typename FF> struct accumulator_triple_ {
std::vector<uint32_t> left;
std::vector<uint32_t> right;
std::vector<uint32_t> out;
};
using accumulator_triple = accumulator_triple_<barretenberg::fr>;

struct ecc_add_gate {
template <typename FF> struct ecc_add_gate_ {
uint32_t x1;
uint32_t y1;
uint32_t x2;
uint32_t y2;
uint32_t x3;
uint32_t y3;
barretenberg::fr endomorphism_coefficient;
barretenberg::fr sign_coefficient;
FF endomorphism_coefficient;
FF sign_coefficient;
};
using ecc_add_gate = ecc_add_gate_<barretenberg::fr>;
} // namespace proof_system
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "circuit_builder_base.hpp"
#include "barretenberg/ecc/curves/bn254/bn254.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"

namespace proof_system {

Expand Down Expand Up @@ -42,6 +44,7 @@ void CircuitBuilderBase<Arithmetization>::assert_equal(const uint32_t a_variable
}
// Standard honk/ plonk instantiation
template class CircuitBuilderBase<arithmetization::Standard<barretenberg::fr>>;
template class CircuitBuilderBase<arithmetization::Turbo<barretenberg::fr>>;
template class CircuitBuilderBase<arithmetization::Standard<grumpkin::fr>>;
template class CircuitBuilderBase<arithmetization::Ultra<barretenberg::fr>>;
template class CircuitBuilderBase<arithmetization::Turbo<barretenberg::fr>>;
} // namespace proof_system
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ static constexpr uint32_t DUMMY_TAG = 0;

template <typename Arithmetization> class CircuitBuilderBase {
public:
// TODO(Cody): This needs to be templated to allow constructing circuits over Grumpkin. For now, adding FF here
// since the flavor can extract it.
using FF = barretenberg::fr;
using FF = typename Arithmetization::FF;
static constexpr size_t NUM_WIRES = Arithmetization::NUM_WIRES;
// Keeping NUM_WIRES, at least temporarily, for backward compatibility
static constexpr size_t program_width = Arithmetization::NUM_WIRES;
Expand All @@ -27,7 +25,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
typename Arithmetization::Selectors selectors;

std::vector<uint32_t> public_inputs;
std::vector<barretenberg::fr> variables;
std::vector<FF> variables;
// index of next variable in equivalence class (=REAL_VARIABLE if you're last)
std::vector<uint32_t> next_var_index;
// index of previous variable in equivalence class (=FIRST if you're in a cycle alone)
Expand Down Expand Up @@ -81,10 +79,10 @@ template <typename Arithmetization> class CircuitBuilderBase {
uint32_t zero_idx = 0;
uint32_t one_idx = 1;

virtual void create_add_gate(const add_triple& in) = 0;
virtual void create_mul_gate(const mul_triple& in) = 0;
virtual void create_add_gate(const add_triple_<FF>& in) = 0;
virtual void create_mul_gate(const mul_triple_<FF>& in) = 0;
virtual void create_bool_gate(const uint32_t a) = 0;
virtual void create_poly_gate(const poly_triple& in) = 0;
virtual void create_poly_gate(const poly_triple_<FF>& in) = 0;
virtual size_t get_num_constant_gates() const = 0;

/**
Expand Down Expand Up @@ -123,7 +121,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
* @param index The index of the variable.
* @return The value of the variable.
* */
inline barretenberg::fr get_variable(const uint32_t index) const
inline FF get_variable(const uint32_t index) const
{
ASSERT(variables.size() > index);
return variables[real_variable_index[index]];
Expand All @@ -137,7 +135,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
* @param index The index of the variable.
* @return The value of the variable.
* */
inline const barretenberg::fr& get_variable_reference(const uint32_t index) const
inline const FF& get_variable_reference(const uint32_t index) const
{
ASSERT(variables.size() > index);
return variables[real_variable_index[index]];
Expand All @@ -156,11 +154,11 @@ template <typename Arithmetization> class CircuitBuilderBase {
return result;
}

barretenberg::fr get_public_input(const uint32_t index) const { return get_variable(public_inputs[index]); }
FF get_public_input(const uint32_t index) const { return get_variable(public_inputs[index]); }

std::vector<barretenberg::fr> get_public_inputs() const
std::vector<FF> get_public_inputs() const
{
std::vector<barretenberg::fr> result;
std::vector<FF> result;
for (uint32_t i = 0; i < get_num_public_inputs(); ++i) {
result.push_back(get_public_input(i));
}
Expand All @@ -173,7 +171,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
* @param in The value of the variable
* @return The index of the new variable in the variables vector
*/
virtual uint32_t add_variable(const barretenberg::fr& in)
virtual uint32_t add_variable(const FF& in)
{
variables.emplace_back(in);

Expand All @@ -195,7 +193,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
* @param in The value of the variable
* @return The index of the new variable in the variables vector
*/
virtual uint32_t add_public_variable(const barretenberg::fr& in)
virtual uint32_t add_public_variable(const FF& in)
{
const uint32_t index = add_variable(in);
public_inputs.emplace_back(index);
Expand Down
Loading

0 comments on commit 656d794

Please sign in to comment.