barretenberg
Loading...
Searching...
No Matches
databus_lookup_relation.hpp
1#pragma once
2#include <array>
3#include <tuple>
4
5#include "barretenberg/common/constexpr_utils.hpp"
6#include "barretenberg/honk/proof_system/logderivative_library.hpp"
7#include "barretenberg/polynomials/polynomial.hpp"
8#include "barretenberg/polynomials/univariate.hpp"
9#include "barretenberg/relations/relation_types.hpp"
10
11namespace proof_system {
12
13template <typename FF_> class DatabusLookupRelationImpl {
14 public:
15 using FF = FF_;
16 static constexpr size_t READ_TERMS = 1;
17 static constexpr size_t WRITE_TERMS = 1;
18 // 1 + polynomial degree of this relation
19 static constexpr size_t LENGTH = READ_TERMS + WRITE_TERMS + 3;
20
21 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
22 LENGTH, // inverse polynomial correctness subrelation
23 LENGTH // log-derivative lookup argument subrelation
24 };
25
26 // The second subrelation is "linearly dependant" in the sense that it establishes the value of a sum across the
27 // entire execution trace rather than a per-row identity.
28 static constexpr std::array<bool, 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false };
29
40 template <typename AllValues> static bool operation_exists_at_row(const AllValues& row)
41 {
42 return (row.q_busread == 1 || row.calldata_read_counts > 0);
43 }
44
52 template <typename AllEntities> static auto& get_inverse_polynomial(AllEntities& in) { return in.lookup_inverses; }
59 template <typename Accumulator, typename AllEntities>
60 static Accumulator compute_inverse_exists(const AllEntities& in)
61 {
62 using View = typename Accumulator::View;
63 // TODO(luke): row_has_read should really be a boolean object thats equal to 1 when counts > 0 and 0 otherwise.
64 // This current structure will lead to failure if call_data_read_counts > 1.
65 const auto row_has_write = View(in.q_busread);
66 const auto row_has_read = View(in.calldata_read_counts);
67
68 return row_has_write + row_has_read - (row_has_write * row_has_read);
69
70 return Accumulator(View(in.q_busread) + View(in.calldata_read_counts));
71 }
72
73 template <typename Accumulator, size_t index, typename AllEntities>
74 static Accumulator lookup_read_counts(const AllEntities& in)
75 {
76 using View = typename Accumulator::View;
77
78 if constexpr (index == 0) {
79 return Accumulator(View(in.calldata_read_counts));
80 }
81 return Accumulator(1);
82 }
83
88 template <typename Accumulator, size_t read_index, typename AllEntities>
89 static Accumulator compute_read_term_predicate([[maybe_unused]] const AllEntities& in)
90
91 {
92 using View = typename Accumulator::View;
93
94 if constexpr (read_index == 0) {
95 return Accumulator(View(in.q_busread));
96 }
97 return Accumulator(1);
98 }
99
104 template <typename Accumulator, size_t write_index, typename AllEntities>
105 static Accumulator compute_write_term_predicate(const AllEntities& /*unused*/)
106 {
107 return Accumulator(1);
108 }
109
114 template <typename Accumulator, size_t write_index, typename AllEntities, typename Parameters>
115 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
116 {
117 using View = typename Accumulator::View;
118 using ParameterView = GetParameterView<Parameters, View>;
119
120 static_assert(write_index < WRITE_TERMS);
121
122 const auto& calldata = View(in.calldata);
123 const auto& id = View(in.databus_id);
124
125 const auto& gamma = ParameterView(params.gamma);
126 const auto& beta = ParameterView(params.beta);
127
128 // Construct b_i + idx_i*\beta + \gamma
129 if constexpr (write_index == 0) {
130 return calldata + gamma + id * beta; // degree 1
131 }
132
133 return Accumulator(1);
134 }
135
140 template <typename Accumulator, size_t read_index, typename AllEntities, typename Parameters>
141 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
142 {
143 using View = typename Accumulator::View;
144 using ParameterView = GetParameterView<Parameters, View>;
145
146 static_assert(read_index < READ_TERMS);
147
148 // Bus value stored in w_1, index into bus column stored in w_2
149 const auto& w_1 = View(in.w_l);
150 const auto& w_2 = View(in.w_r);
151
152 const auto& gamma = ParameterView(params.gamma);
153 const auto& beta = ParameterView(params.beta);
154
155 // Construct value + index*\beta + \gamma
156 if constexpr (read_index == 0) {
157 return w_1 + gamma + w_2 * beta;
158 }
159
160 return Accumulator(1);
161 }
162
172 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
173 static void accumulate(ContainerOverSubrelations& accumulator,
174 const AllEntities& in,
175 const Parameters& params,
176 const FF& scaling_factor)
177 {
178 honk::logderivative_library::
179 accumulate_logderivative_lookup_subrelation_contributions<FF, DatabusLookupRelationImpl<FF>>(
180 accumulator, in, params, scaling_factor);
181 }
182};
183
184template <typename FF> using DatabusLookupRelation = Relation<DatabusLookupRelationImpl<FF>>;
185
186} // namespace proof_system
Definition: databus_lookup_relation.hpp:13
static Accumulator compute_write_term_predicate(const AllEntities &)
Compute scalar for write term in log derivative lookup argument.
Definition: databus_lookup_relation.hpp:105
static auto & get_inverse_polynomial(AllEntities &in)
Get the lookup inverse polynomial.
Definition: databus_lookup_relation.hpp:52
static Accumulator compute_inverse_exists(const AllEntities &in)
Compute the Accumulator whose values indicate whether the inverse is computed or not.
Definition: databus_lookup_relation.hpp:60
static Accumulator compute_write_term(const AllEntities &in, const Parameters &params)
Compute write term denominator in log derivative lookup argument.
Definition: databus_lookup_relation.hpp:115
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Accumulate the contribution from two surelations for the log derivative databus lookup argument.
Definition: databus_lookup_relation.hpp:173
static Accumulator compute_read_term_predicate(const AllEntities &in)
Compute scalar for read term in log derivative lookup argument.
Definition: databus_lookup_relation.hpp:89
static bool operation_exists_at_row(const AllValues &row)
Determine whether the inverse I needs to be computed at a given row.
Definition: databus_lookup_relation.hpp:40
static Accumulator compute_read_term(const AllEntities &in, const Parameters &params)
Compute read term denominator in log derivative lookup argument.
Definition: databus_lookup_relation.hpp:141