Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
arithmetic_constraints.test.cpp
Go to the documentation of this file.
2#include "acir_format.hpp"
3
7
8#include <cstdint>
9#include <gtest/gtest.h>
10#include <vector>
11
12using namespace acir_format;
13
14template <typename Builder_,
15 typename AcirConstraint_,
16 size_t num_multiplication_terms,
17 size_t num_linear_terms,
18 bool overlap_mul_and_linear,
19 bool overlap_linear>
21 public:
22 using Builder = Builder_;
23 using AcirConstraint = AcirConstraint_;
24 static constexpr size_t NUM_MULTIPLICATION_TERMS = num_multiplication_terms;
25 static constexpr size_t NUM_LINEAR_TERMS = num_linear_terms;
26 static constexpr bool OVERLAP_MUL_AND_LINEAR = overlap_mul_and_linear;
27 static constexpr bool OVERLAP_LINEAR = overlap_linear;
28};
29
30template <typename Builder_,
31 typename AcirConstraint_,
32 size_t num_multiplication_terms,
33 size_t num_linear_terms,
34 bool overlap_mul_and_linear,
35 bool overlap_linear>
37 public:
38 using Builder = Builder_;
39 using AcirConstraint = AcirConstraint_;
40
42
46 static constexpr size_t num_overlap_mul_and_linear()
47 {
48 size_t result = 0;
49
50 if constexpr (overlap_mul_and_linear) {
51 result++;
52 }
53
54 if constexpr (overlap_mul_and_linear && num_multiplication_terms > 1) {
55 result++;
56 }
57
58 if constexpr (overlap_mul_and_linear && num_multiplication_terms > 2) {
59 result++;
60 }
61
62 return result;
63 }
64
66 static constexpr size_t NUM_OVERLAP_LINEAR = 1;
67 static constexpr size_t LINEAR_OFFSET = overlap_mul_and_linear ? NUM_OVERLAP_MUL_AND_LINEAR : 0U;
68
69 static size_t expected_num_gates()
70 {
71 size_t num_gates = 0;
72
73 size_t num_multiplication_gates = num_multiplication_terms;
74 num_gates += num_multiplication_gates;
75
76 // Compute the number of witnesses that have to be put into wires on top of the multiplication terms
77 size_t num_witnesses_into_wires = num_linear_terms;
78 num_witnesses_into_wires -= overlap_mul_and_linear ? NUM_OVERLAP_MUL_AND_LINEAR : 0U;
79 num_witnesses_into_wires -= overlap_linear ? NUM_OVERLAP_LINEAR : 0U;
80
81 // Update the number of required gates
82
83 // The first gate uses all wires, so we fit two new witnesses when there are multiplication terms, 4 otherwise
84 size_t num_witnesses_first_wire = num_multiplication_gates != 0 ? 2U : 4U;
85 if (num_witnesses_into_wires <= num_witnesses_first_wire) {
86 return num_multiplication_gates != 0 ? num_multiplication_gates : 1U;
87 }
88 num_witnesses_into_wires -= num_witnesses_first_wire;
89 num_gates += num_multiplication_gates != 0 ? 0U : 1U;
90
91 // All other gates don't use the last wire, so we fit one new witness per gate
92 if (num_witnesses_into_wires + 1 <= num_gates) {
93 return num_gates;
94 }
95 num_witnesses_into_wires -= (num_multiplication_gates - 1);
96
97 // Now we add the remaining witnesses
98 size_t num_additional_gates = num_witnesses_into_wires / (Builder::NUM_WIRES - 1);
99 size_t diff = num_witnesses_into_wires - num_additional_gates * (Builder::NUM_WIRES - 1);
100 num_additional_gates += diff == 0 ? 0U : 1U;
101
102 return num_gates + num_additional_gates;
103 }
104
106 public:
112 static std::vector<std::string> get_labels() { return { "None", "InvalidateConstant", "InvalidateWitness" }; }
113 };
114
116 const std::vector<std::tuple<bb::fr, std::pair<uint32_t, bb::fr>, std::pair<uint32_t, bb::fr>>>& mul_terms,
117 const std::vector<std::pair<bb::fr, std::pair<uint32_t, bb::fr>>>& linear_terms,
118 const std::vector<bb::fr>& witness_values)
119 {
120 bb::fr result = 0;
121
122 for (const auto& mul_term : mul_terms) {
123 bb::fr scalar = std::get<0>(mul_term);
124 bb::fr lhs_value = witness_values[std::get<1>(mul_term).first];
125 bb::fr rhs_value = witness_values[std::get<2>(mul_term).first];
126 result += scalar * lhs_value * rhs_value;
127 }
128
129 for (const auto& linear_term : linear_terms) {
130 bb::fr scalar = linear_term.first;
131 bb::fr value = witness_values[linear_term.second.first];
132 result += scalar * value;
133 }
134
135 return result;
136 }
137
139
140 static void generate_constraints(AcirConstraint& arithmetic_constraint, WitnessVector& witness_values)
141 {
142 // (scalar, (lhs_index, lhs_value), (rhs_index, rhs_value))
144 // (scalar, (index, value)
146
147 mul_terms.reserve(num_multiplication_terms);
148 for (size_t idx = 0; idx < num_multiplication_terms; ++idx) {
149 bb::fr lhs_value = bb::fr::random_element();
150 bb::fr rhs_value = bb::fr::random_element();
152
153 uint32_t lhs_index = add_to_witness_and_track_indices(witness_values, lhs_value);
154 uint32_t rhs_index = add_to_witness_and_track_indices(witness_values, rhs_value);
155 mul_terms.push_back(
156 std::make_tuple(scalar, std::make_pair(lhs_index, lhs_value), std::make_pair(rhs_index, rhs_value)));
157 }
158
159 linear_terms.reserve(num_linear_terms);
160 for (size_t idx = 0; idx < num_linear_terms; ++idx) {
163
164 uint32_t index = add_to_witness_and_track_indices(witness_values, value);
165 linear_terms.push_back(std::make_pair(scalar, std::make_pair(index, value)));
166 }
167
168 // Expressions that would lead to these cases are:
169 // 1. w1 * w2 + w1
170 // 2. w1 * w2 + w3 * w4 + w1 + w4
171 // 3. w1 * w1 + w3 * w4 + w5 * w5 + w1 + w4 + w5
172 if constexpr (overlap_mul_and_linear) {
173 BB_ASSERT_GTE(num_linear_terms, 1U, "We need at least 1 linear terms when overlapping is turned on.");
175 num_multiplication_terms, 1U, "We need at least 1 multiplication terms when overlapping is turned on.");
176
177 // Overlap lhs of multiplication term with linear term
178 std::get<1>(mul_terms[0]).first = linear_terms[0].second.first;
179
180 if constexpr (num_multiplication_terms > 1 && num_linear_terms > 1) {
181 // Overlap rhs of multiplication term with linear term
182 std::get<2>(mul_terms[1]).first = linear_terms[1].second.first;
183 }
184
185 if constexpr (num_multiplication_terms > 2 && num_linear_terms > 2) {
186 // Overlap both terms in the multiplication term with linear term
187 std::get<1>(mul_terms[2]).first = linear_terms[2].second.first;
188 std::get<2>(mul_terms[2]).first = linear_terms[2].second.first;
189 }
190 }
191
192 // Expression that would lead to this case is:
193 // w1 + w1
194 if constexpr (overlap_linear) {
195 BB_ASSERT_GT(num_linear_terms,
197 "We need at least " << NUM_OVERLAP_LINEAR + LINEAR_OFFSET + 1
198 << " linear term when overlapping is turned on.");
199
200 // Overlap two linear terms
201 linear_terms[LINEAR_OFFSET].second.first = linear_terms[LINEAR_OFFSET + 1].second.first;
202 }
203
204 bb::fr result = -evaluate_expression_result(mul_terms, linear_terms, witness_values);
205
206 // Build the Acir::Expression
207 Acir::Expression expression;
208 for (const auto& mul_term : mul_terms) {
209 expression.mul_terms.push_back(std::make_tuple(std::get<0>(mul_term).to_buffer(),
210 Acir::Witness(std::get<1>(mul_term).first),
211 Acir::Witness(std::get<2>(mul_term).first)));
212 }
213 for (const auto& linear_term : linear_terms) {
214 expression.linear_combinations.push_back(
215 std::make_tuple(linear_term.first.to_buffer(), Acir::Witness(linear_term.second.first)));
216 }
217 expression.q_c = result.to_buffer();
218
219 // Construct the big quad constraint
220 Acir::Opcode::AssertZero acir_assert_zero{ .value = expression };
221 AcirFormat dummy_acir_format;
222 assert_zero_to_quad_constraints(acir_assert_zero, dummy_acir_format, 0);
223
224 // Check that the construction worked as expected
225 size_t EXPECTED_NUM_GATES = expected_num_gates();
226 if (EXPECTED_NUM_GATES > 1) {
227 BB_ASSERT(dummy_acir_format.quad_constraints.empty());
228 BB_ASSERT_EQ(dummy_acir_format.big_quad_constraints.size(), 1U);
229 BB_ASSERT_EQ(dummy_acir_format.big_quad_constraints[0].size(), EXPECTED_NUM_GATES);
230 } else {
231 BB_ASSERT(dummy_acir_format.big_quad_constraints.empty());
232 BB_ASSERT_EQ(dummy_acir_format.quad_constraints.size(), 1U);
233 }
234
235 if constexpr (IS_BIG_QUAD) {
236 arithmetic_constraint = dummy_acir_format.big_quad_constraints[0];
237 } else {
238 arithmetic_constraint = dummy_acir_format.quad_constraints[0];
239 }
240 }
241
243 AcirConstraint constraint,
244 WitnessVector witness_values,
245 const typename InvalidWitness::Target& invalid_witness_target)
246 {
247 switch (invalid_witness_target) {
249 break;
251 // Invalidate the equation by changing the constant term
252 if constexpr (IS_BIG_QUAD) {
253 constraint[0].const_scaling += bb::fr::one();
254 } else {
255 constraint.const_scaling += bb::fr::one();
256 }
257 break;
258 }
260 // Invalidate the equation by changing one of the witness values
261 if constexpr (IS_BIG_QUAD) {
262 witness_values[constraint[0].a] += bb::fr::one();
263 } else {
264 witness_values[constraint.a] += bb::fr::one();
265 }
266 break;
267 }
268 };
269
270 return { constraint, witness_values };
271 };
272};
273
274template <typename ArithmeticConstraintParams_>
276 : public ::testing::Test,
277 public TestClass<ArithmeticConstraintsTestingFunctions<typename ArithmeticConstraintParams_::Builder,
278 typename ArithmeticConstraintParams_::AcirConstraint,
279 ArithmeticConstraintParams_::NUM_MULTIPLICATION_TERMS,
280 ArithmeticConstraintParams_::NUM_LINEAR_TERMS,
281 ArithmeticConstraintParams_::OVERLAP_MUL_AND_LINEAR,
282 ArithmeticConstraintParams_::OVERLAP_LINEAR>> {
283 protected:
285};
286
287using BigQuadConstraintConfigs = testing::Types<
289 // requiring 2 gates
294 // requiring 2 gates
297 // requiring 2 gates
299 // requiring 2 gates
304 // requiring 2 gates
307 // requiring 2 gates
308
310
311TYPED_TEST(BigQuadConstraintTest, GenerateVKFromConstraints)
312{
313 using Flavor =
315 TestFixture::template test_vk_independence<Flavor>();
316}
317
319{
320 TestFixture::test_tampering();
321}
322
323template <typename ArithmeticConstraintParams_>
325 : public ::testing::Test,
326 public TestClass<ArithmeticConstraintsTestingFunctions<typename ArithmeticConstraintParams_::Builder,
327 typename ArithmeticConstraintParams_::AcirConstraint,
328 ArithmeticConstraintParams_::NUM_MULTIPLICATION_TERMS,
329 ArithmeticConstraintParams_::NUM_LINEAR_TERMS,
330 ArithmeticConstraintParams_::OVERLAP_MUL_AND_LINEAR,
331 ArithmeticConstraintParams_::OVERLAP_LINEAR>> {
332 protected:
334};
335
336using QuadConstraintConfigs = testing::Types<
351
353
354TYPED_TEST(QuadConstraintTest, GenerateVKFromConstraints)
355{
356 using Flavor =
358 TestFixture::template test_vk_independence<Flavor>();
359}
360
362{
363 TestFixture::test_tampering();
364}
testing::Types< ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 1, 0, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 1, 1, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 1, 2, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 1, 3, false, true >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 1, 4, true, true >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 0, 4, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, QuadConstraint, 0, 4, false, true >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 1, 0, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 1, 1, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 1, 2, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 1, 3, false, true >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 1, 4, true, true >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 0, 4, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, QuadConstraint, 0, 5, false, true > > QuadConstraintConfigs
testing::Types< ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 1, 3, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 0, 5, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 2, 0, false, false >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 3, 3, true, false >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 1, 4, false, true >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 5, 5, true, true >, ArithmeticConstraintParams< UltraCircuitBuilder, BigQuadConstraint, 0, 6, false, true >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 1, 3, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 0, 5, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 2, 0, false, false >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 3, 3, true, false >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 1, 4, false, true >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 5, 5, true, true >, ArithmeticConstraintParams< MegaCircuitBuilder, BigQuadConstraint, 0, 6, false, true > > BigQuadConstraintConfigs
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:138
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:123
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
static constexpr size_t NUM_MULTIPLICATION_TERMS
static void generate_constraints(AcirConstraint &arithmetic_constraint, WitnessVector &witness_values)
static std::pair< AcirConstraint, WitnessVector > invalidate_witness(AcirConstraint constraint, WitnessVector witness_values, const typename InvalidWitness::Target &invalid_witness_target)
static bb::fr evaluate_expression_result(const std::vector< std::tuple< bb::fr, std::pair< uint32_t, bb::fr >, std::pair< uint32_t, bb::fr > > > &mul_terms, const std::vector< std::pair< bb::fr, std::pair< uint32_t, bb::fr > > > &linear_terms, const std::vector< bb::fr > &witness_values)
static constexpr size_t num_overlap_mul_and_linear()
Compute the number of elements to overlap between multiplication and linear terms.
std::vector< bb::fr > WitnessVector
std::vector< uint32_t > add_to_witness_and_track_indices(std::vector< bb::fr > &witness, const T &input)
Append values to a witness vector and track their indices.
Definition utils.hpp:90
void assert_zero_to_quad_constraints(Acir::Opcode::AssertZero const &arg, AcirFormat &af, size_t opcode_index)
Single entrypoint for processing arithmetic (AssertZero) opcodes.
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TYPED_TEST_SUITE(BoomerangRecursiveVerifierTest, Flavors)
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< uint8_t > to_buffer(T const &value)
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness > > linear_combinations
Definition acir.hpp:4000
std::vector< uint8_t > q_c
Definition acir.hpp:4001
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness, Acir::Witness > > mul_terms
Definition acir.hpp:3999
Acir::Expression value
Definition acir.hpp:4359
Barretenberg's representation of ACIR constraints.
std::vector< QuadConstraint > quad_constraints
std::vector< BigQuadConstraint > big_quad_constraints
Metadata required to create a circuit.
static constexpr field one()
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE std::vector< uint8_t > to_buffer() const