Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator_circuit_builder.test.cpp
Go to the documentation of this file.
5#include <array>
6#include <cstddef>
7#include <gtest/gtest.h>
8
9using namespace bb;
10namespace {
12}
14
19TEST(TranslatorCircuitBuilder, SeveralOperationCorrectness)
20{
21 using point = g1::affine_element;
22 using scalar = fr;
23 using Fq = fq;
24
25 auto P1 = point::random_element();
26 auto P2 = point::random_element();
27 auto z = scalar::random_element();
28
29 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
30 auto op_queue = std::make_shared<ECCOpQueue>();
31 op_queue->no_op_ultra_only();
32 op_queue->random_op_ultra_only();
33 op_queue->random_op_ultra_only();
34 op_queue->random_op_ultra_only();
35 op_queue->add_accumulate(P1);
36 op_queue->mul_accumulate(P2, z);
37 op_queue->eq_and_reset();
38 op_queue->merge();
39
40 op_queue->add_accumulate(P1);
41 op_queue->mul_accumulate(P2, z);
42 op_queue->add_accumulate(P1);
43 op_queue->mul_accumulate(P2, z);
44 op_queue->eq_and_reset();
45 // Placeholder for randomness
46 op_queue->random_op_ultra_only();
47 op_queue->random_op_ultra_only();
48 op_queue->merge(MergeSettings::APPEND, ECCOpQueue::OP_QUEUE_SIZE - op_queue->get_current_subtable_size());
49
50 Fq op_accumulator = 0;
51 Fq p_x_accumulator = 0;
52 Fq p_y_accumulator = 0;
53 Fq z_1_accumulator = 0;
54 Fq z_2_accumulator = 0;
55 Fq batching_challenge = fq::random_element();
56
57 // Sample the evaluation input x
59 // Compute x_pow (power given by the degree of the polynomial) to be number of real ultra ops - 1
60 Fq x_pow = Fq(1);
61 // Get an inverse
62 Fq x_inv = x.invert();
63 // Compute the batched evaluation of polynomials (multiplying by inverse to go from lower to higher)
64 const auto& ultra_ops = op_queue->get_ultra_ops();
65 for (const auto& ultra_op : ultra_ops) {
66
67 if (ultra_op.op_code.is_random_op || ultra_op.op_code.value() == 0) {
68 continue;
69 }
70 op_accumulator = op_accumulator * x_inv + ultra_op.op_code.value();
71 const auto [x_u256, y_u256] = ultra_op.get_base_point_standard_form();
72 p_x_accumulator = p_x_accumulator * x_inv + x_u256;
73 p_y_accumulator = p_y_accumulator * x_inv + y_u256;
74 z_1_accumulator = z_1_accumulator * x_inv + uint256_t(ultra_op.z_1);
75 z_2_accumulator = z_2_accumulator * x_inv + uint256_t(ultra_op.z_2);
76 x_pow *= x;
77 }
78 x_pow *= x_inv;
79 // Multiply by an appropriate power of x to get rid of the inverses
80 Fq result = ((((z_2_accumulator * batching_challenge + z_1_accumulator) * batching_challenge + p_y_accumulator) *
81 batching_challenge +
82 p_x_accumulator) *
83 batching_challenge +
84 op_accumulator) *
85 x_pow;
86
87 // Create circuit builder and feed the queue inside
88 auto circuit_builder = TranslatorCircuitBuilder(batching_challenge, x, op_queue);
89 // Check that the circuit passes
90 EXPECT_TRUE(CircuitChecker::check(circuit_builder));
91 // Check the accumulation result stored as 4 limbs in the circuit and then reconstructed is consistent with the
92 // value computed by hand.
93 EXPECT_EQ(result, CircuitChecker::get_computation_result(circuit_builder));
94}
The unified interface for check circuit functionality implemented in the specialized CircuitChecker c...
static const size_t OP_QUEUE_SIZE
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
static Fq get_computation_result(const Builder &circuit)
Get the result of accumulation, stored as 4 binary limbs in the first row of the circuit.
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
Entry point for Barretenberg command-line interface.
field< Bn254FqParams > fq
Definition fq.hpp:169
field< Bn254FrParams > fr
Definition fr.hpp:174
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
constexpr field invert() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
bb::fq Fq