Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ecc_lookup_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Raju], commit: 2a49eb6 }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include <array>
9#include <tuple>
10
14
15namespace bb {
16
17template <typename FF_> class ECCVMLookupRelationImpl {
18 public:
19 using FF = FF_;
20 static constexpr size_t NUM_LOOKUP_TERMS = 4;
21 static constexpr size_t NUM_TABLE_TERMS = 2;
22 // 1 + polynomial degree of this relation
23 static constexpr size_t LENGTH = NUM_LOOKUP_TERMS + NUM_TABLE_TERMS + 3; // 9
24
25 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
26 LENGTH, // grand product construction sub-relation
27 LENGTH // left-shiftable polynomial sub-relation
28 };
29
30 static constexpr std::array<bool, 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false };
31
32 template <typename AllValues> static bool operation_exists_at_row(const AllValues& row)
33
34 {
35 return (row.msm_add == 1) || (row.msm_skew == 1) || (row.precompute_select == 1);
36 }
37
45 template <typename AllEntities> static auto& get_inverse_polynomial(AllEntities& in) { return in.lookup_inverses; }
46
47 template <typename Accumulator, typename AllEntities>
48 static Accumulator compute_inverse_exists(const AllEntities& in)
49 {
50 using View = typename Accumulator::View;
51
52 const auto row_has_write = View(in.precompute_select);
53 const auto row_has_read = View(in.msm_add) + View(in.msm_skew);
54 return row_has_write + row_has_read - (row_has_write * row_has_read);
55 }
56
57 template <typename Accumulator, size_t index, typename AllEntities>
58 static Accumulator lookup_read_counts(const AllEntities& in)
59 {
60 using View = typename Accumulator::View;
61
62 if constexpr (index == 0) {
63 return Accumulator(View(in.lookup_read_counts_0));
64 }
65 if constexpr (index == 1) {
66 return Accumulator(View(in.lookup_read_counts_1));
67 }
68 return Accumulator(1);
69 }
70
71 template <typename Accumulator, size_t lookup_index, typename AllEntities>
72 static Accumulator get_lookup_term_predicate(const AllEntities& in)
73
74 {
75 using View = typename Accumulator::View;
76
77 if constexpr (lookup_index == 0) {
78 return Accumulator(View(in.msm_add1));
79 }
80 if constexpr (lookup_index == 1) {
81 return Accumulator(View(in.msm_add2));
82 }
83 if constexpr (lookup_index == 2) {
84 return Accumulator(View(in.msm_add3));
85 }
86 if constexpr (lookup_index == 3) {
87 return Accumulator(View(in.msm_add4));
88 }
89 return Accumulator(1);
90 }
91
92 template <typename Accumulator, size_t table_index, typename AllEntities>
93 static Accumulator get_table_term_predicate(const AllEntities& in)
94 {
95 using View = typename Accumulator::View;
96 // anytime `precompute_select` is on, we "turn on" the table predicate. This concretely means that the sP, where
97 // s is a WNAF slice and P is the point being processed, are "written" to the lookup table, i.e., may be
98 // read/looked up later. `table_index == 0` corresponds to positive WNAF entries, `table_index == 1` corresponds
99 // to negative WNAF entries.
100 if constexpr (table_index == 0 || table_index == 1) {
101 return Accumulator(View(in.precompute_select));
102 }
103 return Accumulator(1);
104 }
109 template <typename Accumulator, size_t table_index, typename AllEntities, typename Parameters>
110 static Accumulator compute_table_term(const AllEntities& in, const Parameters& params)
111 {
112 using View = typename Accumulator::View;
113
114 static_assert(table_index < NUM_TABLE_TERMS);
115 // table_index == 0 means our wNAF digit is positive (i.e., ∈{1, 3..., 15}).
116 // table_index == 1 means our wNAF digit is negative (i.e., ∈{-15, -13..., -1})
117
118 // round starts at 0 and increments to 7
119 // point starts at 15[P] and decrements to [P]
120 // a slice value of 0 maps to -15[P]
121
122 // we have computed `(15 - 2 * round)[P] =: (precompute_tx, precompute_ty)`.
123 // `round`∈{0, 1..., 7}
124 // if table_index == 0, we want to write (pc, 15 - 2 * round, precompute_tx, precompute_ty)
125 // if table_index == 1, we want to write (pc, round, precompute_tx, -precompute_ty)
126 // to sum up, both:
127 // (pc, round, precompute_tx, -precompute_ty) _and_
128 // (pc, 15 - 2 * round, precompute_tx, precompute_ty)
129 // will be written to the lookup table.
130 //
131 // therefore, if `pc` corresponds to the elliptic curve point [P], we will write:
132 // | pc | 0 | -15[P].x | -15[P].y |
133 // | pc | 1 | -13[P].x | -13[P].y |
134 // | pc | 2 | -11[P].x | -11[P].y |
135 // | pc | 3 | -9[P].x | -9[P].y |
136 // | pc | 4 | -7[P].x | -7[P].y |
137 // | pc | 5 | -5[P].x | -5[P].y |
138 // | pc | 6 | -3[P].x | -3[P].y |
139 // | pc | 7 | -1[P].x | -1[P].y |
140 // | pc | 8 | [P].x | [P].y |
141 // | pc | 9 | 3[P].x | 3[P].y |
142 // | pc | 10 | 5[P].x | 5[P].y |
143 // | pc | 11 | 7[P].x | 7[P].y |
144 // | pc | 12 | 9[P].x | 9[P].y |
145 // | pc | 13 | 11[P].x | 11[P].y |
146 // | pc | 14 | 13[P].x | 13[P].y |
147 // | pc | 15 | 15[P].x | 15[P].y |
148
149 const auto& precompute_pc = View(in.precompute_pc);
150 const auto& tx = View(in.precompute_tx);
151 const auto& ty = View(in.precompute_ty);
152 const auto& precompute_round = View(in.precompute_round);
153 const auto& gamma = params.gamma;
154 const auto& beta = params.beta;
155 const auto& beta_sqr = params.beta_sqr;
156 const auto& beta_cube = params.beta_cube;
157
158 if constexpr (table_index == 0) {
159 const auto positive_slice_value = -(precompute_round) + 15;
160 const auto positive_term =
161 precompute_pc + gamma + positive_slice_value * beta + tx * beta_sqr + ty * beta_cube;
162 return positive_term; // degree 1
163 }
164 if constexpr (table_index == 1) {
165 const auto negative_term = precompute_pc + gamma + precompute_round * beta + tx * beta_sqr - ty * beta_cube;
166 return negative_term; // degree 1
167 }
168 return Accumulator(1);
169 }
170
171 template <typename Accumulator, size_t lookup_index, typename AllEntities, typename Parameters>
172 static Accumulator compute_lookup_term(const AllEntities& in, const Parameters& params)
173 {
174 using View = typename Accumulator::View;
175
176 // read term: (pc, compressed_slice, (2 * compressed_slice - 15)[P])
177 // (the latter term is of course represented via an x and y coordinate.)
178 static_assert(lookup_index < NUM_LOOKUP_TERMS);
179 const auto& gamma = params.gamma;
180 const auto& beta = params.beta;
181 const auto& beta_sqr = params.beta_sqr;
182 const auto& beta_cube = params.beta_cube;
183 const auto& msm_pc = View(in.msm_pc);
184 const auto& msm_count = View(in.msm_count);
185 const auto& msm_slice1 = View(in.msm_slice1);
186 const auto& msm_slice2 = View(in.msm_slice2);
187 const auto& msm_slice3 = View(in.msm_slice3);
188 const auto& msm_slice4 = View(in.msm_slice4);
189 const auto& msm_x1 = View(in.msm_x1);
190 const auto& msm_x2 = View(in.msm_x2);
191 const auto& msm_x3 = View(in.msm_x3);
192 const auto& msm_x4 = View(in.msm_x4);
193 const auto& msm_y1 = View(in.msm_y1);
194 const auto& msm_y2 = View(in.msm_y2);
195 const auto& msm_y3 = View(in.msm_y3);
196 const auto& msm_y4 = View(in.msm_y4);
197
198 // Recall that `pc` stands for point-counter. We recall how to compute the current pc.
199 //
200 // row pc = value of pc after msm
201 // msm_count = number of (128-bit) multiplications processed so far in current MSM round (NOT INCLUDING current
202 // row) current_pc = msm_pc - msm_count next_pc = current_pc - {0, 1, 2, 3}, depending on how many adds are
203 // performed in the current row.
204 const auto current_pc = msm_pc - msm_count;
205
206 if constexpr (lookup_index == 0) {
207 const auto lookup_term1 = (current_pc) + gamma + msm_slice1 * beta + msm_x1 * beta_sqr + msm_y1 * beta_cube;
208 return lookup_term1; // degree 1
209 }
210 if constexpr (lookup_index == 1) {
211 const auto lookup_term2 =
212 (current_pc - 1) + gamma + msm_slice2 * beta + msm_x2 * beta_sqr + msm_y2 * beta_cube;
213 return lookup_term2; // degree 1
214 }
215 if constexpr (lookup_index == 2) {
216 const auto lookup_term3 =
217 (current_pc - 2) + gamma + msm_slice3 * beta + msm_x3 * beta_sqr + msm_y3 * beta_cube;
218 return lookup_term3; // degree 1
219 }
220 if constexpr (lookup_index == 3) {
221 const auto lookup_term4 =
222 (current_pc - 3) + gamma + msm_slice4 * beta + msm_x4 * beta_sqr + msm_y4 * beta_cube;
223 return lookup_term4; // degree 1
224 }
225 return Accumulator(1);
226 }
227
241 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
242 static void accumulate(ContainerOverSubrelations& accumulator,
243 const AllEntities& in,
244 const Parameters& params,
245 const FF& scaling_factor);
246};
247
249
250} // namespace bb
static Accumulator compute_inverse_exists(const AllEntities &in)
static Accumulator get_table_term_predicate(const AllEntities &in)
static constexpr size_t NUM_LOOKUP_TERMS
static constexpr size_t NUM_TABLE_TERMS
static auto & get_inverse_polynomial(AllEntities &in)
Get the inverse lookup polynomial.
static Accumulator compute_table_term(const AllEntities &in, const Parameters &params)
Returns the fingerprint of (precompute_pc, compressed_slice, (2 * compressed_slice - 15)[P]),...
static constexpr size_t LENGTH
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
static Accumulator compute_lookup_term(const AllEntities &in, const Parameters &params)
static Accumulator get_lookup_term_predicate(const AllEntities &in)
static constexpr std::array< bool, 2 > SUBRELATION_LINEARLY_INDEPENDENT
static bool operation_exists_at_row(const AllValues &row)
static Accumulator lookup_read_counts(const AllEntities &in)
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Expression for ECCVM lookup tables.
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5