barretenberg
Loading...
Searching...
No Matches
lookup_relation.hpp
1#pragma once
2#include "barretenberg/relations/relation_types.hpp"
3
4namespace proof_system {
5
21template <typename FF_> class LookupRelationImpl {
22 public:
23 using FF = FF_;
24
25 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
26 6, // grand product construction sub-relation
27 3 // left-shiftable polynomial sub-relation
28 };
29
30 static constexpr std::array<size_t, 2> TOTAL_LENGTH_ADJUSTMENTS{
31 6, // grand product construction sub-relation
32 0 // left-shiftable polynomial sub-relation
33 };
40 inline static auto& get_grand_product_polynomial(auto& input) { return input.z_lookup; }
41
49 inline static auto& get_shifted_grand_product_polynomial(auto& input) { return input.z_lookup_shift; }
50
61 template <typename Accumulator, typename AllEntities, typename Parameters>
62 inline static Accumulator compute_grand_product_numerator(const AllEntities& in, const Parameters& params)
63 {
64 using View = typename Accumulator::View;
65 using ParameterView = GetParameterView<Parameters, View>;
66
67 const auto& beta = ParameterView(params.beta);
68 const auto& gamma = ParameterView(params.gamma);
69 const auto& eta = ParameterView(params.eta);
70 const auto eta_sqr = eta * eta;
71 const auto eta_cube = eta_sqr * eta;
72
73 const auto one_plus_beta = beta + FF(1);
74 const auto gamma_by_one_plus_beta = gamma * one_plus_beta;
75
76 auto w_1 = View(in.w_l);
77 auto w_2 = View(in.w_r);
78 auto w_3 = View(in.w_o);
79
80 auto w_1_shift = View(in.w_l_shift);
81 auto w_2_shift = View(in.w_r_shift);
82 auto w_3_shift = View(in.w_o_shift);
83
84 auto table_1 = View(in.table_1);
85 auto table_2 = View(in.table_2);
86 auto table_3 = View(in.table_3);
87 auto table_4 = View(in.table_4);
88
89 auto table_1_shift = View(in.table_1_shift);
90 auto table_2_shift = View(in.table_2_shift);
91 auto table_3_shift = View(in.table_3_shift);
92 auto table_4_shift = View(in.table_4_shift);
93
94 auto table_index = View(in.q_o);
95 auto column_1_step_size = View(in.q_r);
96 auto column_2_step_size = View(in.q_m);
97 auto column_3_step_size = View(in.q_c);
98 auto q_lookup = View(in.q_lookup);
99
100 // (w_1 + q_2*w_1_shift) + η(w_2 + q_m*w_2_shift) + η²(w_3 + q_c*w_3_shift) + η³q_index.
101 // deg 2 or 4
102 auto wire_accum = (w_1 + column_1_step_size * w_1_shift) + (w_2 + column_2_step_size * w_2_shift) * eta +
103 (w_3 + column_3_step_size * w_3_shift) * eta_sqr + table_index * eta_cube;
104
105 // t_1 + ηt_2 + η²t_3 + η³t_4
106 // deg 1 or 4
107 auto table_accum = table_1 + table_2 * eta + table_3 * eta_sqr + table_4 * eta_cube;
108
109 // t_1_shift + ηt_2_shift + η²t_3_shift + η³t_4_shift
110 // deg 4
111 auto table_accum_shift =
112 table_1_shift + table_2_shift * eta + table_3_shift * eta_sqr + table_4_shift * eta_cube;
113
114 auto tmp = (q_lookup * wire_accum + gamma); // deg 2 or 4
115 tmp *= (table_accum + table_accum_shift * beta + gamma_by_one_plus_beta); // 1 or 5
116 tmp *= one_plus_beta; // deg 1
117 return tmp; // deg 4 or 10
118 }
119
130 template <typename Accumulator, typename AllEntities, typename Parameters>
131 inline static Accumulator compute_grand_product_denominator(const AllEntities& in, const Parameters& params)
132 {
133
134 using View = typename Accumulator::View;
135 using ParameterView = GetParameterView<Parameters, View>;
136
137 const auto& beta = ParameterView(params.beta);
138 const auto& gamma = ParameterView(params.gamma);
139
140 const auto one_plus_beta = beta + FF(1);
141 const auto gamma_by_one_plus_beta = gamma * one_plus_beta; // deg 0 or 2
142
143 // Contribution (1)
144 auto s_accum = View(in.sorted_accum);
145 auto s_accum_shift = View(in.sorted_accum_shift);
146
147 auto tmp = (s_accum + s_accum_shift * beta + gamma_by_one_plus_beta); // 1 or 2
148 return tmp;
149 }
150
169 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
170 inline static void accumulate(ContainerOverSubrelations& accumulators,
171 const AllEntities& in,
172 const Parameters& params,
173 const FF& scaling_factor)
174 {
175
176 {
177 using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>;
178 using View = typename Accumulator::View;
179 using ParameterView = GetParameterView<Parameters, View>;
180
181 const auto& grand_product_delta = ParameterView(params.lookup_grand_product_delta);
182
183 auto z_lookup = View(in.z_lookup);
184 auto z_lookup_shift = View(in.z_lookup_shift);
185
186 auto lagrange_first = View(in.lagrange_first);
187 auto lagrange_last = View(in.lagrange_last);
188
189 const auto lhs = compute_grand_product_numerator<Accumulator>(in, params); // deg 4 or 10
190 const auto rhs = compute_grand_product_denominator<Accumulator>(in, params); // deg 1 or 2
191
192 // (deg 5 or 11) - (deg 3 or 5)
193 const auto tmp =
194 lhs * (z_lookup + lagrange_first) - rhs * (z_lookup_shift + lagrange_last * grand_product_delta);
195 std::get<0>(accumulators) += tmp * scaling_factor;
196 };
197
198 {
199 using Accumulator = std::tuple_element_t<1, ContainerOverSubrelations>;
200 using View = typename Accumulator::View;
201 auto z_lookup_shift = View(in.z_lookup_shift);
202 auto lagrange_last = View(in.lagrange_last);
203
204 // Contribution (2)
205 std::get<1>(accumulators) += (lagrange_last * z_lookup_shift) * scaling_factor;
206 };
207 };
208};
209
210template <typename FF> using LookupRelation = Relation<LookupRelationImpl<FF>>;
211
212} // namespace proof_system
LookupRelationImpl defines the algebra for the lookup polynomial:
Definition: lookup_relation.hpp:21
static auto & get_shifted_grand_product_polynomial(auto &input)
Get the shifted grand product polynomial object (either from the proving key or AllEntities depending...
Definition: lookup_relation.hpp:49
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Compute contribution of the lookup grand prod relation for a given edge (internal function)
Definition: lookup_relation.hpp:170
static auto & get_grand_product_polynomial(auto &input)
Get the grand product polynomial object (either from the proving key or AllEntities depending on cont...
Definition: lookup_relation.hpp:40
static Accumulator compute_grand_product_numerator(const AllEntities &in, const Parameters &params)
Compute numerator term of the lookup relation:
Definition: lookup_relation.hpp:62
static Accumulator compute_grand_product_denominator(const AllEntities &in, const Parameters &params)
Compute denominator term of the lookup relation:
Definition: lookup_relation.hpp:131