Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph_description_bigfield.test.cpp
Go to the documentation of this file.
4
7
16#include <memory>
17#include <utility>
18
19using namespace bb;
20using namespace cdg;
21
22namespace {
24}
25
32
46
58TEST(boomerang_bigfield, test_graph_description_bigfield_constructors)
59{
61 [[maybe_unused]] fq_ct constant = fq_ct(1);
62 [[maybe_unused]] fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
63 [[maybe_unused]] fr_ct small_var = witness_ct(&builder, fr(1));
64 [[maybe_unused]] fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
65 [[maybe_unused]] fq_ct r;
66
67 auto graph = StaticAnalyzer(builder);
68 auto connected_components = graph.find_connected_components();
69 EXPECT_EQ(connected_components.size(), 1);
70 auto variables_in_one_gate = graph.get_variables_in_one_gate();
71 EXPECT_EQ(variables_in_one_gate.size(), 1);
72}
73
81TEST(boomerang_bigfield, test_graph_description_bigfield_addition)
82{
84 [[maybe_unused]] fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
85 [[maybe_unused]] fr_ct small_var = witness_ct(&builder, fr(1));
86 [[maybe_unused]] fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
87 [[maybe_unused]] fq_ct r;
88 [[maybe_unused]] fq_ct r1;
89 [[maybe_unused]] fq_ct r2;
90
91 r = mixed + var;
93 r1 = r + mixed;
95 r2 = r + var;
97
98 auto graph = StaticAnalyzer(builder);
99 auto connected_components = graph.find_connected_components();
100 EXPECT_EQ(connected_components.size(), 1);
101 auto variables_in_one_gate = graph.get_variables_in_one_gate();
102 EXPECT_EQ(variables_in_one_gate.size(), 0);
103}
104
112TEST(boomerang_bigfield, test_graph_description_bigfield_substraction)
113{
115 [[maybe_unused]] fq_ct constant = fq_ct(1);
116 [[maybe_unused]] fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
117 [[maybe_unused]] fr_ct small_var = witness_ct(&builder, fr(1));
118 [[maybe_unused]] fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
119 [[maybe_unused]] fq_ct r;
120
121 r = mixed - mixed;
123 r = mixed - constant;
125 r = mixed - var;
127 r = var - mixed;
129
130 auto graph = StaticAnalyzer(builder);
131 auto connected_components = graph.find_connected_components();
132 EXPECT_EQ(connected_components.size(), 1);
133 auto variables_in_one_gate = graph.get_variables_in_one_gate();
134 EXPECT_EQ(variables_in_one_gate.size(), 0);
135 for (const auto& elem : variables_in_one_gate) {
136 info("elem == ", elem);
137 }
138}
139
147TEST(boomerang_bigfield, test_graph_description_bigfield_multiplication)
148{
150 [[maybe_unused]] fq_ct constant = fq_ct(1);
151 [[maybe_unused]] fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
152 [[maybe_unused]] fr_ct small_var = witness_ct(&builder, fr(1));
153 [[maybe_unused]] fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
154 [[maybe_unused]] fq_ct r;
155
156 r = var * constant;
157 r = constant * constant;
158 r = mixed * var;
159 r = mixed * constant;
160 r = mixed * mixed;
161 auto graph = StaticAnalyzer(builder);
162 auto connected_components = graph.find_connected_components();
163 EXPECT_EQ(connected_components.size(), 1);
164 auto variables_in_one_gate = graph.get_variables_in_one_gate();
165 EXPECT_EQ(variables_in_one_gate.size(), 0);
166}
167
177TEST(boomerang_bigfield, test_graph_description_bigfield_division)
178{
180 [[maybe_unused]] fq_ct constant = fq_ct(1);
181 [[maybe_unused]] fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
182 [[maybe_unused]] fr_ct small_var = witness_ct(&builder, fr(1));
183 [[maybe_unused]] fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
184 [[maybe_unused]] fq_ct r;
185
186 r = constant / var;
188 r = constant / constant;
189 r = mixed / mixed;
191 r = mixed / var;
193
195 auto graph = StaticAnalyzer(builder);
196 auto connected_components = graph.find_connected_components();
197 EXPECT_EQ(connected_components.size(), 1);
198 auto variables_in_one_gate = graph.get_variables_in_one_gate();
199 EXPECT_EQ(variables_in_one_gate.size(), 0);
200}
201
209TEST(boomerang_bigfield, test_graph_description_bigfield_mix_operations)
210{
211 auto builder = Builder();
212 fq_ct constant = fq_ct(1);
213 fq_ct var = fq_ct::create_from_u512_as_witness(&builder, 1);
214 fr_ct small_var = witness_ct(&builder, fr(1));
215 fq_ct mixed = fq_ct(1).add_to_lower_limb(small_var, 1);
216 fq_ct r;
217
218 r = mixed + mixed;
220 r = mixed - mixed;
222 r = mixed + var;
224 r = mixed + constant;
226 r = mixed - var;
228 r = mixed - constant;
230 r = var - mixed;
232
233 r = var * constant;
235 r = constant / var;
237 r = constant * constant;
238 r = constant / constant;
239
240 r = mixed * var;
242 r = mixed / var;
244 r = mixed * mixed;
246 r = mixed * constant;
248 auto graph = StaticAnalyzer(builder);
249 auto connected_components = graph.find_connected_components();
250 EXPECT_EQ(connected_components.size(), 1);
251 auto variables_in_one_gate = graph.get_variables_in_one_gate();
252 EXPECT_EQ(variables_in_one_gate.size(), 0);
253}
254
262TEST(boomerang_bigfield, test_graph_description_constructor_high_low_bits_and_operations)
263{
264 auto builder = Builder();
266 fq_ct a(witness_ct(&builder, fr(uint256_t(inputs[0]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
267 witness_ct(&builder, fr(uint256_t(inputs[0]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
268 fq_ct b(witness_ct(&builder, fr(uint256_t(inputs[1]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
269 witness_ct(&builder, fr(uint256_t(inputs[1]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
270 fq_ct c = a * b;
272 fq_ct d1(witness_ct(&builder, fr(uint256_t(d).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
273 witness_ct(&builder, fr(uint256_t(d).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
274 c = c + d1;
276 auto graph = StaticAnalyzer(builder);
277 auto connected_components = graph.find_connected_components();
278 EXPECT_EQ(connected_components.size(), 1);
279 auto variables_in_one_gate = graph.get_variables_in_one_gate();
280 EXPECT_EQ(variables_in_one_gate.size(), 0);
281}
282
290TEST(boomerang_bigfield, test_graph_description_mul_function)
291{
292 auto builder = Builder();
294 fq_ct a(witness_ct(&builder, fr(uint256_t(inputs[0]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
295 witness_ct(&builder, fr(uint256_t(inputs[0]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
296 fq_ct b(witness_ct(&builder, fr(uint256_t(inputs[1]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
297 witness_ct(&builder, fr(uint256_t(inputs[1]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
298 fq_ct c = a * b;
300 auto graph = StaticAnalyzer(builder);
301 auto connected_components = graph.find_connected_components();
302 EXPECT_EQ(connected_components.size(), 1);
303 auto variables_in_one_gate = graph.get_variables_in_one_gate();
304 EXPECT_EQ(variables_in_one_gate.size(), 0);
305}
306
314TEST(boomerang_bigfield, test_graph_description_sqr_function)
315{
316 auto builder = Builder();
317 fq input = fq::random_element();
318 fq_ct a(witness_ct(&builder, fr(uint256_t(input).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
319 witness_ct(&builder, fr(uint256_t(input).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
320 fq_ct c = a.sqr();
322 auto graph = StaticAnalyzer(builder);
323 auto connected_components = graph.find_connected_components();
324 EXPECT_EQ(connected_components.size(), 1);
325 auto variables_in_one_gate = graph.get_variables_in_one_gate();
326 EXPECT_EQ(variables_in_one_gate.size(), 0);
327}
328
336TEST(boomerang_bigfield, test_graph_description_madd_function)
337{
338 auto builder = Builder();
340 fq_ct a(witness_ct(&builder, fr(uint256_t(inputs[0]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
341 witness_ct(&builder, fr(uint256_t(inputs[0]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
342 fq_ct b(witness_ct(&builder, fr(uint256_t(inputs[1]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
343 witness_ct(&builder, fr(uint256_t(inputs[1]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
344 fq_ct c(witness_ct(&builder, fr(uint256_t(inputs[2]).slice(0, fq_ct::NUM_LIMB_BITS * 2))),
345 witness_ct(&builder, fr(uint256_t(inputs[2]).slice(fq_ct::NUM_LIMB_BITS * 2, fq_ct::NUM_LIMB_BITS * 4))));
346 fq_ct d = a.madd(b, { c });
348 auto graph = StaticAnalyzer(builder);
349 auto connected_components = graph.find_connected_components();
350 EXPECT_EQ(connected_components.size(), 1);
351 auto variables_in_one_gate = graph.get_variables_in_one_gate();
352 EXPECT_EQ(variables_in_one_gate.size(), 0);
353}
354
363TEST(boomerang_bigfield, test_graph_description_mult_madd_function)
364{
365 auto builder = Builder();
366 const size_t number_of_madds = 16;
367 fq mul_left_values[number_of_madds];
368 fq mul_right_values[number_of_madds];
369 fq to_add_values[number_of_madds];
370
371 std::vector<fq_ct> mul_left;
372 std::vector<fq_ct> mul_right;
373 std::vector<fq_ct> to_add;
374 mul_left.reserve(number_of_madds);
375 mul_right.reserve(number_of_madds);
376 to_add.reserve(number_of_madds);
377 for (size_t j = 0; j < number_of_madds; j++) {
378 mul_left_values[j] = fq::random_element();
379 mul_right_values[j] = fq::random_element();
380 mul_left.emplace_back(fq_ct::create_from_u512_as_witness(&builder, uint512_t(uint256_t(mul_left_values[j]))));
381 mul_right.emplace_back(fq_ct::create_from_u512_as_witness(&builder, uint512_t(uint256_t(mul_right_values[j]))));
382 to_add_values[j] = fq::random_element();
383 to_add.emplace_back(fq_ct::create_from_u512_as_witness(&builder, uint512_t(uint256_t(to_add_values[j]))));
384 }
385 fq_ct f = fq_ct::mult_madd(mul_left, mul_right, to_add);
387 builder.finalize_circuit();
388 auto graph = StaticAnalyzer(builder);
389 auto variables_in_one_gate = graph.get_variables_in_one_gate();
390 EXPECT_EQ(variables_in_one_gate.size(), 0);
391}
392
400TEST(boomerang_bigfield, test_graph_description_constructor_high_low_bits)
401{
402 auto builder = Builder();
403 fq mul_left_value = fq::random_element();
404 fq mul_right_value = fq::random_element();
405 // fq mul_right_value = fq::random_element();
406 [[maybe_unused]] fq_ct mul_left =
407 fq_ct::create_from_u512_as_witness(&builder, uint512_t(uint256_t(mul_left_value)));
408 [[maybe_unused]] fq_ct mul_right =
409 fq_ct::create_from_u512_as_witness(&builder, uint512_t(uint256_t(mul_right_value)));
410 fq_ct product = mul_left * mul_right;
411 fix_bigfield_element(product);
412 builder.finalize_circuit();
413 auto graph = StaticAnalyzer(builder);
414 auto connected_components = graph.find_connected_components();
415 auto variables_in_one_gate = graph.get_variables_in_one_gate();
416 EXPECT_EQ(variables_in_one_gate.size(), 0);
417}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
static void fix_witness_in_place(const bigfield &bf)
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
FF a
FF b
numeric::RNG & engine
bn254::BaseField fq_ct
TEST(boomerang_bigfield, test_graph_description_bigfield_constructors)
Test graph description for bigfield constructors.
stdlib::witness_t< Builder > witness_ct
UltraCircuitBuilder Builder
void fix_bigfield_element(const fq_ct &element)
Fix a bigfield element to prevent it from being identified as a variable in one gate.
AvmProvingInputs inputs
uintx< uint256_t > uint512_t
Definition uintx.hpp:309
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:245
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:155
C slice(C const &container, size_t start)
Definition container.hpp:9
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
Definition graph.cpp:21
UltraStaticAnalyzer StaticAnalyzer
Definition graph.hpp:190
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
field_t< CircuitBuilder > ScalarField
Definition bn254.hpp:30
Group::BaseField BaseField
Definition bn254.hpp:32