barretenberg
Loading...
Searching...
No Matches
toy_avm_circuit_builder.hpp
1
7#pragma once
8
9#include "barretenberg/common/constexpr_utils.hpp"
10#include "barretenberg/ecc/curves/bn254/fr.hpp"
11#include "barretenberg/flavor/toy_avm.hpp"
12#include "barretenberg/honk/proof_system/logderivative_library.hpp"
13#include "barretenberg/relations/relation_parameters.hpp"
15
16namespace proof_system {
17
23template <typename Flavor> class ToyAVMCircuitBuilder {
24 public:
25 using FF = typename Flavor::FF;
26 using Polynomial = typename Flavor::Polynomial;
27
28 static constexpr size_t NUM_POLYNOMIALS = Flavor::NUM_ALL_ENTITIES;
29 static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES;
30
31 using ProverPolynomials = typename Flavor::ProverPolynomials;
32 size_t num_gates = 0;
33 std::array<std::vector<FF>, NUM_WIRES> wires;
34 ToyAVMCircuitBuilder() = default;
35
36 void add_row(const std::array<FF, NUM_WIRES> row)
37 {
38 for (size_t i = 0; i < NUM_WIRES; i++) {
39 wires[i].emplace_back(row[i]);
40 }
41 num_gates = wires[0].size();
42 }
43
49 ProverPolynomials compute_polynomials()
50 {
51
52 const auto num_gates_log2 = static_cast<size_t>(numeric::get_msb64(num_gates));
53 size_t num_gates_pow2 = 1UL << (num_gates_log2 + (1UL << num_gates_log2 == num_gates ? 0 : 1));
54
55 ProverPolynomials polys;
56 for (Polynomial& poly : polys.get_all()) {
57 poly = Polynomial(num_gates_pow2);
58 }
59
60 polys.lagrange_first[0] = 1;
61
62 for (size_t i = 0; i < num_gates; ++i) {
63 // Fill out the witness polynomials
64 polys.permutation_set_column_1[i] = wires[0][i];
65 polys.permutation_set_column_2[i] = wires[1][i];
66 polys.permutation_set_column_3[i] = wires[2][i];
67 polys.permutation_set_column_4[i] = wires[3][i];
68 polys.self_permutation_column[i] = wires[4][i];
69 // By default the permutation is over all rows where we place data
70 polys.enable_tuple_set_permutation[i] = 1;
71 // The same column permutation alternates between even and odd values
72 polys.enable_single_column_permutation[i] = 1;
73 polys.enable_first_set_permutation[i] = i & 1;
74 polys.enable_second_set_permutation[i] = 1 - (i & 1);
75 }
76 return polys;
77 }
78
84 {
85 // using FirstPermutationRelation = typename std::tuple_element_t<0, Flavor::Relations>;
86 // For now only gamma and beta are used
87 const FF gamma = FF::random_element();
88 const FF beta = FF::random_element();
90 .eta = 0,
91 .beta = beta,
92 .gamma = gamma,
93 .public_input_delta = 0,
94 .lookup_grand_product_delta = 0,
95 .beta_sqr = 0,
96 .beta_cube = 0,
97 .eccvm_set_permutation_delta = 0,
98 };
99
100 // Compute polynomial values
101 auto polynomials = compute_polynomials();
102 const size_t num_rows = polynomials.get_polynomial_size();
103
104 // Check the tuple permutation relation
105 proof_system::honk::logderivative_library::compute_logderivative_inverse<
106 Flavor,
108 polynomials, params, num_rows);
109
110 using PermutationRelation =
113 typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations
114 permutation_result;
115 for (auto& r : permutation_result) {
116 r = 0;
117 }
118 for (size_t i = 0; i < num_rows; ++i) {
119 PermutationRelation::accumulate(permutation_result, polynomials.get_row(i), params, 1);
120 }
121 for (auto r : permutation_result) {
122 if (r != 0) {
123 info("Tuple GenericPermutationRelation failed.");
124 return false;
125 }
126 }
127 // Check the single permutation relation
128 proof_system::honk::logderivative_library::compute_logderivative_inverse<
129 Flavor,
131 polynomials, params, num_rows);
132
133 using SameWirePermutationRelation =
136 typename Flavor::FF>::SumcheckArrayOfValuesOverSubrelations
137 second_permutation_result;
138 for (auto& r : second_permutation_result) {
139 r = 0;
140 }
141 for (size_t i = 0; i < num_rows; ++i) {
142 SameWirePermutationRelation::accumulate(second_permutation_result, polynomials.get_row(i), params, 1);
143 }
144 for (auto r : second_permutation_result) {
145 if (r != 0) {
146 info("Same wire GenericPermutationRelation failed.");
147 return false;
148 }
149 }
150 return true;
151 }
152
153 [[nodiscard]] size_t get_num_gates() const { return num_gates; }
154
155 [[nodiscard]] size_t get_circuit_subgroup_size(const size_t num_rows) const
156 {
157
158 const auto num_rows_log2 = static_cast<size_t>(numeric::get_msb64(num_rows));
159 size_t num_rows_pow2 = 1UL << (num_rows_log2 + (1UL << num_rows_log2 == num_rows ? 0 : 1));
160 return num_rows_pow2;
161 }
162};
163} // namespace proof_system
Definition: polynomial.hpp:12
The templates defined herein facilitate sharing the relation arithmetic between the prover and the ve...
Definition: relation_types.hpp:121
Circuit builder for the ToyAVM that is used to explain generic permutation settings.
Definition: toy_avm_circuit_builder.hpp:23
ProverPolynomials compute_polynomials()
Compute the AVM Template flavor polynomial data required to generate a proof.
Definition: toy_avm_circuit_builder.hpp:49
bool check_circuit()
Check that the circuit is correct (proof should work)
Definition: toy_avm_circuit_builder.hpp:83
A container for the prover polynomials handles.
Definition: AvmMini_flavor.hpp:263
Definition: AvmMini_flavor.hpp:21
This class contains an example of how to set PermutationSettings classes used by the GenericPermutati...
Definition: relation_definer.hpp:118
This class contains an example of how to set PermutationSettings classes used by the GenericPermutati...
Definition: relation_definer.hpp:26
This file contains the template for the generic permutation that can be specialized to enforce variou...
Container for parameters used by the grand product (permutation, lookup) Honk relations.
Definition: relation_parameters.hpp:12