Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
permutation_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Luke, Raju], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
9namespace bb {
39template <typename FF_> class UltraPermutationRelationImpl {
40 public:
41 using FF = FF_;
42
43 static constexpr std::array<size_t, 3> SUBRELATION_PARTIAL_LENGTHS{
44 6, // grand product construction sub-relation
45 3, // left-shiftable polynomial sub-relation
46 3 // z_perm initialization sub-relation
47 };
48
53 template <typename AllEntities> inline static bool skip(const AllEntities& in)
54 {
55 // If z_perm == z_perm_shift, this implies that none of the wire values for the present input are involved in
56 // non-trivial copy constraints.
57 return (in.z_perm - in.z_perm_shift).is_zero();
58 }
59
60 inline static auto& get_grand_product_polynomial(auto& in) { return in.z_perm; }
61 inline static auto& get_shifted_grand_product_polynomial(auto& in) { return in.z_perm_shift; }
62
63 template <typename Accumulator, typename AllEntities, typename Parameters>
64 inline static Accumulator compute_grand_product_numerator(const AllEntities& in, const Parameters& params)
65 {
66 using View = typename Accumulator::View;
67 using ParameterView = Parameters::DataType;
68
69 auto w_1 = View(in.w_l);
70 auto w_2 = View(in.w_r);
71 auto w_3 = View(in.w_o);
72 auto w_4 = View(in.w_4);
73 auto id_1 = View(in.id_1);
74 auto id_2 = View(in.id_2);
75 auto id_3 = View(in.id_3);
76 auto id_4 = View(in.id_4);
77
78 const auto& beta = ParameterView(params.beta);
79 const auto& gamma = ParameterView(params.gamma);
80
81 // witness degree 4
82 return (w_1 + id_1 * beta + gamma) * (w_2 + id_2 * beta + gamma) * (w_3 + id_3 * beta + gamma) *
83 (w_4 + id_4 * beta + gamma);
84 }
85
86 template <typename Accumulator, typename AllEntities, typename Parameters>
87 inline static Accumulator compute_grand_product_denominator(const AllEntities& in, const Parameters& params)
88 {
89 using View = typename Accumulator::View;
90 using ParameterView = Parameters::DataType;
91
92 auto w_1 = View(in.w_l);
93 auto w_2 = View(in.w_r);
94 auto w_3 = View(in.w_o);
95 auto w_4 = View(in.w_4);
96
97 auto sigma_1 = View(in.sigma_1);
98 auto sigma_2 = View(in.sigma_2);
99 auto sigma_3 = View(in.sigma_3);
100 auto sigma_4 = View(in.sigma_4);
101
102 const auto& beta = ParameterView(params.beta);
103 const auto& gamma = ParameterView(params.gamma);
104
105 // witness degree 4
106 return (w_1 + sigma_1 * beta + gamma) * (w_2 + sigma_2 * beta + gamma) * (w_3 + sigma_3 * beta + gamma) *
107 (w_4 + sigma_4 * beta + gamma);
108 }
109
126 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
127 inline static void accumulate(ContainerOverSubrelations& accumulators,
128 const AllEntities& in,
129 const Parameters& params,
130 const FF& scaling_factor)
131 {
132 // Contribution (1)
134 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
135 using ParameterView = Parameters::DataType;
136 using ParameterCoefficientAccumulator = typename ParameterView::CoefficientAccumulator;
137
138 const CoefficientAccumulator w_1_m(in.w_l);
139 const CoefficientAccumulator w_2_m(in.w_r);
140 const CoefficientAccumulator w_3_m(in.w_o);
141 const CoefficientAccumulator w_4_m(in.w_4);
142 const CoefficientAccumulator id_1_m(in.id_1);
143 const CoefficientAccumulator id_2_m(in.id_2);
144 const CoefficientAccumulator id_3_m(in.id_3);
145 const CoefficientAccumulator id_4_m(in.id_4);
146 const CoefficientAccumulator sigma_1_m(in.sigma_1);
147 const CoefficientAccumulator sigma_2_m(in.sigma_2);
148 const CoefficientAccumulator sigma_3_m(in.sigma_3);
149 const CoefficientAccumulator sigma_4_m(in.sigma_4);
150
151 const ParameterCoefficientAccumulator gamma_m(params.gamma);
152 const ParameterCoefficientAccumulator beta_m(params.beta);
153
154 const auto w_1_plus_gamma = w_1_m + gamma_m;
155 const auto w_2_plus_gamma = w_2_m + gamma_m;
156 const auto w_3_plus_gamma = w_3_m + gamma_m;
157 const auto w_4_plus_gamma = w_4_m + gamma_m;
158
159 auto t1 = (id_1_m * beta_m);
160 t1 += w_1_plus_gamma;
161 t1 *= scaling_factor;
162 auto t2 = id_2_m * beta_m;
163 t2 += w_2_plus_gamma;
164 auto t3 = id_3_m * beta_m;
165 t3 += w_3_plus_gamma;
166 auto t4 = id_4_m * beta_m;
167 t4 += w_4_plus_gamma;
168
169 auto t5 = sigma_1_m * beta_m;
170 t5 += w_1_plus_gamma;
171 t5 *= scaling_factor;
172 auto t6 = sigma_2_m * beta_m;
173 t6 += w_2_plus_gamma;
174 auto t7 = sigma_3_m * beta_m;
175 t7 += w_3_plus_gamma;
176 auto t8 = sigma_4_m * beta_m;
177 t8 += w_4_plus_gamma;
178
179 Accumulator numerator(t1);
180 numerator *= Accumulator(t2);
181 numerator *= Accumulator(t3);
182 numerator *= Accumulator(t4);
183
184 Accumulator denominator(t5);
185 denominator *= Accumulator(t6);
186 denominator *= Accumulator(t7);
187 denominator *= Accumulator(t8);
188
189 const ParameterCoefficientAccumulator public_input_delta_m(params.public_input_delta);
190 const auto z_perm_m = CoefficientAccumulator(in.z_perm);
191 const auto z_perm_shift_m = CoefficientAccumulator(in.z_perm_shift);
192 const auto lagrange_first_m = CoefficientAccumulator(in.lagrange_first);
193 const auto lagrange_last_m = CoefficientAccumulator(in.lagrange_last);
194
195 auto public_input_term_m = lagrange_last_m * public_input_delta_m;
196 public_input_term_m += z_perm_shift_m;
197 const Accumulator public_input_term(public_input_term_m);
198 // witness degree: deg 5 - deg 5 = deg 5
199 std::get<0>(accumulators) +=
200 ((Accumulator(z_perm_m + lagrange_first_m) * numerator) - (public_input_term * denominator));
201
202 // Contribution (2)
204
205 std::get<1>(accumulators) += ShortAccumulator((lagrange_last_m * z_perm_shift_m) * scaling_factor);
206
207 // Contribution (3): Enforce z_perm starts at 0. The grand product initialization relies on
208 // z_perm[0] = 0 so that (z_perm + L_first) evaluates to 1 at the first row.
209 // Without this constraint, a cheating prover could set z_perm[0] to a non-zero value.
211 std::get<2>(accumulators) += InitAccumulator((lagrange_first_m * z_perm_m) * scaling_factor);
212 };
213};
214
216
217} // namespace bb
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
static auto & get_grand_product_polynomial(auto &in)
static bool skip(const AllEntities &in)
Returns true if the contribution from all subrelations for the provided inputs is identically zero.
static auto & get_shifted_grand_product_polynomial(auto &in)
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Compute contribution of the permutation relation for a given edge (internal function)
static Accumulator compute_grand_product_numerator(const AllEntities &in, const Parameters &params)
static constexpr std::array< size_t, 3 > SUBRELATION_PARTIAL_LENGTHS
static Accumulator compute_grand_product_denominator(const AllEntities &in, const Parameters &params)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13