Skip to content

Commit 656d794

Browse files
maramihalimaramihalicodygunton
authored
feat: Make the circuit constructors field agnostic so we can check circuits on grumpkin (#534)
Co-authored-by: maramihali <[email protected]> Co-authored-by: codygunton <[email protected]>
1 parent 96891de commit 656d794

14 files changed

+1660
-1574
lines changed

cpp/src/barretenberg/ecc/curves/bn254/bn254.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "../bn254/fq.hpp"
3+
#include "../bn254/fq12.hpp"
34
#include "../bn254/fq2.hpp"
45
#include "../bn254/fr.hpp"
56
#include "../bn254/g1.hpp"
@@ -15,5 +16,6 @@ class BN254 {
1516
using AffineElement = typename Group::affine_element;
1617
using G2AffineElement = typename barretenberg::g2::affine_element;
1718
using G2BaseField = typename barretenberg::fq2;
19+
using TargetField = barretenberg::fq12;
1820
};
1921
} // namespace curve

cpp/src/barretenberg/proof_system/arithmetization/arithmetization.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ template <typename FF, size_t num_selectors> struct SelectorsBase {
4848

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

82-
template <typename FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
83+
template <typename _FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
8384
public:
85+
using FF = _FF;
8486
struct Selectors : SelectorsBase<FF, num_selectors> {
8587
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_m = std::get<0>(this->_data);
8688
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_c = std::get<1>(this->_data);
@@ -122,8 +124,9 @@ template <typename FF> class Turbo : public Arithmetization</*NUM_WIRES =*/4, /*
122124
};
123125
};
124126

125-
template <typename FF> class Ultra : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
127+
template <typename _FF> class Ultra : public Arithmetization</*NUM_WIRES =*/4, /*num_selectors =*/11> {
126128
public:
129+
using FF = _FF;
127130
struct Selectors : SelectorsBase<FF, num_selectors> {
128131
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_m = std::get<0>(this->_data);
129132
std::vector<FF, barretenberg::ContainerSlabAllocator<FF>>& q_c = std::get<1>(this->_data);

cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,73 @@
33
#include "barretenberg/ecc/curves/bn254/fr.hpp"
44
#include <cstdint>
55

6+
// TODO(#557): The field-specific aliases for gates should be removed and the type could be explicit when this
7+
// structures are used to avoid having foo_gate and foo_gate_grumpkin (i.e. use foo_gate<field> instead). Moreover, we
8+
// need to ensure the read/write functions handle grumpkin gates as well.
69
namespace proof_system {
7-
struct add_triple {
10+
template <typename FF> struct add_triple_ {
811
uint32_t a;
912
uint32_t b;
1013
uint32_t c;
11-
barretenberg::fr a_scaling;
12-
barretenberg::fr b_scaling;
13-
barretenberg::fr c_scaling;
14-
barretenberg::fr const_scaling;
14+
FF a_scaling;
15+
FF b_scaling;
16+
FF c_scaling;
17+
FF const_scaling;
1518
};
19+
using add_triple = add_triple_<barretenberg::fr>;
1620

17-
struct add_quad {
21+
template <typename FF> struct add_quad_ {
1822
uint32_t a;
1923
uint32_t b;
2024
uint32_t c;
2125
uint32_t d;
22-
barretenberg::fr a_scaling;
23-
barretenberg::fr b_scaling;
24-
barretenberg::fr c_scaling;
25-
barretenberg::fr d_scaling;
26-
barretenberg::fr const_scaling;
26+
FF a_scaling;
27+
FF b_scaling;
28+
FF c_scaling;
29+
FF d_scaling;
30+
FF const_scaling;
2731
};
32+
using add_quad = add_quad_<barretenberg::fr>;
2833

29-
struct mul_quad {
34+
template <typename FF> struct mul_quad_ {
3035
uint32_t a;
3136
uint32_t b;
3237
uint32_t c;
3338
uint32_t d;
34-
barretenberg::fr mul_scaling;
35-
barretenberg::fr a_scaling;
36-
barretenberg::fr b_scaling;
37-
barretenberg::fr c_scaling;
38-
barretenberg::fr d_scaling;
39-
barretenberg::fr const_scaling;
39+
FF mul_scaling;
40+
FF a_scaling;
41+
FF b_scaling;
42+
FF c_scaling;
43+
FF d_scaling;
44+
FF const_scaling;
4045
};
46+
using mul_quad = mul_quad_<barretenberg::fr>;
4147

42-
struct mul_triple {
48+
template <typename FF> struct mul_triple_ {
4349
uint32_t a;
4450
uint32_t b;
4551
uint32_t c;
46-
barretenberg::fr mul_scaling;
47-
barretenberg::fr c_scaling;
48-
barretenberg::fr const_scaling;
52+
FF mul_scaling;
53+
FF c_scaling;
54+
FF const_scaling;
4955
};
56+
using mul_triple = mul_triple_<barretenberg::fr>;
5057

51-
struct poly_triple {
58+
template <typename FF> struct poly_triple_ {
5259
uint32_t a;
5360
uint32_t b;
5461
uint32_t c;
55-
barretenberg::fr q_m;
56-
barretenberg::fr q_l;
57-
barretenberg::fr q_r;
58-
barretenberg::fr q_o;
59-
barretenberg::fr q_c;
62+
FF q_m;
63+
FF q_l;
64+
FF q_r;
65+
FF q_o;
66+
FF q_c;
6067

61-
friend bool operator==(poly_triple const& lhs, poly_triple const& rhs) = default;
68+
friend bool operator==(poly_triple_<FF> const& lhs, poly_triple_<FF> const& rhs) = default;
6269
};
6370

71+
using poly_triple = poly_triple_<barretenberg::fr>;
72+
6473
template <typename B> inline void read(B& buf, poly_triple& constraint)
6574
{
6675
using serialize::read;
@@ -73,7 +82,6 @@ template <typename B> inline void read(B& buf, poly_triple& constraint)
7382
read(buf, constraint.q_o);
7483
read(buf, constraint.q_c);
7584
}
76-
7785
template <typename B> inline void write(B& buf, poly_triple const& constraint)
7886
{
7987
using serialize::write;
@@ -87,38 +95,42 @@ template <typename B> inline void write(B& buf, poly_triple const& constraint)
8795
write(buf, constraint.q_c);
8896
}
8997

90-
struct fixed_group_add_quad {
98+
template <typename FF> struct fixed_group_add_quad_ {
9199
uint32_t a;
92100
uint32_t b;
93101
uint32_t c;
94102
uint32_t d;
95-
barretenberg::fr q_x_1;
96-
barretenberg::fr q_x_2;
97-
barretenberg::fr q_y_1;
98-
barretenberg::fr q_y_2;
103+
FF q_x_1;
104+
FF q_x_2;
105+
FF q_y_1;
106+
FF q_y_2;
99107
};
108+
using fixed_group_add_quad = fixed_group_add_quad_<barretenberg::fr>;
100109

101-
struct fixed_group_init_quad {
102-
barretenberg::fr q_x_1;
103-
barretenberg::fr q_x_2;
104-
barretenberg::fr q_y_1;
105-
barretenberg::fr q_y_2;
110+
template <typename FF> struct fixed_group_init_quad_ {
111+
FF q_x_1;
112+
FF q_x_2;
113+
FF q_y_1;
114+
FF q_y_2;
106115
};
116+
using fixed_group_init_quad = fixed_group_init_quad_<barretenberg::fr>;
107117

108-
struct accumulator_triple {
118+
template <typename FF> struct accumulator_triple_ {
109119
std::vector<uint32_t> left;
110120
std::vector<uint32_t> right;
111121
std::vector<uint32_t> out;
112122
};
123+
using accumulator_triple = accumulator_triple_<barretenberg::fr>;
113124

114-
struct ecc_add_gate {
125+
template <typename FF> struct ecc_add_gate_ {
115126
uint32_t x1;
116127
uint32_t y1;
117128
uint32_t x2;
118129
uint32_t y2;
119130
uint32_t x3;
120131
uint32_t y3;
121-
barretenberg::fr endomorphism_coefficient;
122-
barretenberg::fr sign_coefficient;
132+
FF endomorphism_coefficient;
133+
FF sign_coefficient;
123134
};
135+
using ecc_add_gate = ecc_add_gate_<barretenberg::fr>;
124136
} // namespace proof_system

cpp/src/barretenberg/proof_system/circuit_builder/circuit_builder_base.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "circuit_builder_base.hpp"
2+
#include "barretenberg/ecc/curves/bn254/bn254.hpp"
3+
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
24

35
namespace proof_system {
46

@@ -42,6 +44,7 @@ void CircuitBuilderBase<Arithmetization>::assert_equal(const uint32_t a_variable
4244
}
4345
// Standard honk/ plonk instantiation
4446
template class CircuitBuilderBase<arithmetization::Standard<barretenberg::fr>>;
45-
template class CircuitBuilderBase<arithmetization::Turbo<barretenberg::fr>>;
47+
template class CircuitBuilderBase<arithmetization::Standard<grumpkin::fr>>;
4648
template class CircuitBuilderBase<arithmetization::Ultra<barretenberg::fr>>;
49+
template class CircuitBuilderBase<arithmetization::Turbo<barretenberg::fr>>;
4750
} // namespace proof_system

cpp/src/barretenberg/proof_system/circuit_builder/circuit_builder_base.hpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ static constexpr uint32_t DUMMY_TAG = 0;
1010

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

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

84-
virtual void create_add_gate(const add_triple& in) = 0;
85-
virtual void create_mul_gate(const mul_triple& in) = 0;
82+
virtual void create_add_gate(const add_triple_<FF>& in) = 0;
83+
virtual void create_mul_gate(const mul_triple_<FF>& in) = 0;
8684
virtual void create_bool_gate(const uint32_t a) = 0;
87-
virtual void create_poly_gate(const poly_triple& in) = 0;
85+
virtual void create_poly_gate(const poly_triple_<FF>& in) = 0;
8886
virtual size_t get_num_constant_gates() const = 0;
8987

9088
/**
@@ -123,7 +121,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
123121
* @param index The index of the variable.
124122
* @return The value of the variable.
125123
* */
126-
inline barretenberg::fr get_variable(const uint32_t index) const
124+
inline FF get_variable(const uint32_t index) const
127125
{
128126
ASSERT(variables.size() > index);
129127
return variables[real_variable_index[index]];
@@ -137,7 +135,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
137135
* @param index The index of the variable.
138136
* @return The value of the variable.
139137
* */
140-
inline const barretenberg::fr& get_variable_reference(const uint32_t index) const
138+
inline const FF& get_variable_reference(const uint32_t index) const
141139
{
142140
ASSERT(variables.size() > index);
143141
return variables[real_variable_index[index]];
@@ -156,11 +154,11 @@ template <typename Arithmetization> class CircuitBuilderBase {
156154
return result;
157155
}
158156

159-
barretenberg::fr get_public_input(const uint32_t index) const { return get_variable(public_inputs[index]); }
157+
FF get_public_input(const uint32_t index) const { return get_variable(public_inputs[index]); }
160158

161-
std::vector<barretenberg::fr> get_public_inputs() const
159+
std::vector<FF> get_public_inputs() const
162160
{
163-
std::vector<barretenberg::fr> result;
161+
std::vector<FF> result;
164162
for (uint32_t i = 0; i < get_num_public_inputs(); ++i) {
165163
result.push_back(get_public_input(i));
166164
}
@@ -173,7 +171,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
173171
* @param in The value of the variable
174172
* @return The index of the new variable in the variables vector
175173
*/
176-
virtual uint32_t add_variable(const barretenberg::fr& in)
174+
virtual uint32_t add_variable(const FF& in)
177175
{
178176
variables.emplace_back(in);
179177

@@ -195,7 +193,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
195193
* @param in The value of the variable
196194
* @return The index of the new variable in the variables vector
197195
*/
198-
virtual uint32_t add_public_variable(const barretenberg::fr& in)
196+
virtual uint32_t add_public_variable(const FF& in)
199197
{
200198
const uint32_t index = add_variable(in);
201199
public_inputs.emplace_back(index);

0 commit comments

Comments
 (0)