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: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
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 READ_TERMS = 4;
21 static constexpr size_t WRITE_TERMS = 2;
22 // 1 + polynomial degree of this relation
23 static constexpr size_t LENGTH = READ_TERMS + WRITE_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 read_index, typename AllEntities>
72 static Accumulator compute_read_term_predicate(const AllEntities& in)
73
74 {
75 using View = typename Accumulator::View;
76
77 if constexpr (read_index == 0) {
78 return Accumulator(View(in.msm_add1));
79 }
80 if constexpr (read_index == 1) {
81 return Accumulator(View(in.msm_add2));
82 }
83 if constexpr (read_index == 2) {
84 return Accumulator(View(in.msm_add3));
85 }
86 if constexpr (read_index == 3) {
87 return Accumulator(View(in.msm_add4));
88 }
89 return Accumulator(1);
90 }
91
92 template <typename Accumulator, size_t write_index, typename AllEntities>
93 static Accumulator compute_write_term_predicate(const AllEntities& in)
94 {
95 using View = typename Accumulator::View;
96
97 if constexpr (write_index == 0) {
98 return Accumulator(View(in.precompute_select));
99 }
100 if constexpr (write_index == 1) {
101 // TODO(https://github.com/AztecProtocol/barretenberg/issues/750) Is this a bug?
102 return Accumulator(View(in.precompute_select));
103 }
104 return Accumulator(1);
105 }
110 template <typename Accumulator, size_t write_index, typename AllEntities, typename Parameters>
111 static Accumulator compute_write_term(const AllEntities& in, const Parameters& params)
112 {
113 using View = typename Accumulator::View;
114
115 static_assert(write_index < WRITE_TERMS);
116 // write_index == 0 means our wNAF digit is positive (i.e., ∈{1, 3..., 15}).
117 // write_index == 1 means our wNAF digit is negative (i.e., ∈{-15, -13..., -1})
118
119 // round starts at 0 and increments to 7
120 // point starts at 15[P] and decrements to [P]
121 // a slice value of 0 maps to -15[P]
122
123 // we have computed `(15 - 2 * round)[P] =: (precompute_tx, precompute_ty)`.
124 // `round`∈{0, 1..., 7}
125 // if write_index == 0, we want to write (pc, 15 - 2 * round, precompute_tx, precompute_ty)
126 // if write_index == 1, we want to write (pc, round, precompute_tx, -precompute_ty)
127 // to sum up, both:
128 // (pc, round, precompute_tx, -precompute_ty) _and_
129 // (pc, 15 - 2 * round, precompute_tx, precompute_ty)
130 // will be written to the lookup table.
131 //
132 // therefore, if `pc` corresponds to the elliptic curve point [P], we will write:
133 // | pc | 0 | -15[P].x | -15[P].y |
134 // | pc | 1 | -13[P].x | -13[P].y |
135 // | pc | 2 | -11[P].x | -11[P].y |
136 // | pc | 3 | -9[P].x | -9[P].y |
137 // | pc | 4 | -7[P].x | -7[P].y |
138 // | pc | 5 | -5[P].x | -5[P].y |
139 // | pc | 6 | -3[P].x | -3[P].y |
140 // | pc | 7 | -1[P].x | -1[P].y |
141 // | pc | 8 | [P].x | [P].y |
142 // | pc | 9 | 3[P].x | 3[P].y |
143 // | pc | 10 | 5[P].x | 5[P].y |
144 // | pc | 11 | 7[P].x | 7[P].y |
145 // | pc | 12 | 9[P].x | 9[P].y |
146 // | pc | 13 | 11[P].x | 11[P].y |
147 // | pc | 14 | 13[P].x | 13[P].y |
148 // | pc | 15 | 15[P].x | 15[P].y |
149
150 const auto& precompute_pc = View(in.precompute_pc);
151 const auto& tx = View(in.precompute_tx);
152 const auto& ty = View(in.precompute_ty);
153 const auto& precompute_round = View(in.precompute_round);
154 const auto& gamma = params.gamma;
155 const auto& beta = params.beta;
156 const auto& beta_sqr = params.beta_sqr;
157 const auto& beta_cube = params.beta_cube;
158
159 if constexpr (write_index == 0) {
160 const auto positive_slice_value = -(precompute_round) + 15;
161 const auto positive_term =
162 precompute_pc + gamma + positive_slice_value * beta + tx * beta_sqr + ty * beta_cube;
163 return positive_term; // degree 1
164 }
165 if constexpr (write_index == 1) {
166 const auto negative_term = precompute_pc + gamma + precompute_round * beta + tx * beta_sqr - ty * beta_cube;
167 return negative_term; // degree 1
168 }
169 return Accumulator(1);
170 }
171
172 template <typename Accumulator, size_t read_index, typename AllEntities, typename Parameters>
173 static Accumulator compute_read_term(const AllEntities& in, const Parameters& params)
174 {
175 using View = typename Accumulator::View;
176
177 // read term: (pc, compressed_slice, (2 * compressed_slice - 15)[P])
178 // (the latter term is of course represented via an x and y coordinate.)
179 static_assert(read_index < READ_TERMS);
180 const auto& gamma = params.gamma;
181 const auto& beta = params.beta;
182 const auto& beta_sqr = params.beta_sqr;
183 const auto& beta_cube = params.beta_cube;
184 const auto& msm_pc = View(in.msm_pc);
185 const auto& msm_count = View(in.msm_count);
186 const auto& msm_slice1 = View(in.msm_slice1);
187 const auto& msm_slice2 = View(in.msm_slice2);
188 const auto& msm_slice3 = View(in.msm_slice3);
189 const auto& msm_slice4 = View(in.msm_slice4);
190 const auto& msm_x1 = View(in.msm_x1);
191 const auto& msm_x2 = View(in.msm_x2);
192 const auto& msm_x3 = View(in.msm_x3);
193 const auto& msm_x4 = View(in.msm_x4);
194 const auto& msm_y1 = View(in.msm_y1);
195 const auto& msm_y2 = View(in.msm_y2);
196 const auto& msm_y3 = View(in.msm_y3);
197 const auto& msm_y4 = View(in.msm_y4);
198
199 // Recall that `pc` stands for point-counter. We recall how to compute the current pc.
200 //
201 // row pc = value of pc after msm
202 // msm_count = number of (128-bit) multiplications processed so far in current MSM round (NOT INCLUDING current
203 // row) current_pc = msm_pc - msm_count next_pc = current_pc - {0, 1, 2, 3}, depending on how many adds are
204 // performed in the current row.
205 const auto current_pc = msm_pc - msm_count;
206
207 if constexpr (read_index == 0) {
208 const auto read_term1 = (current_pc) + gamma + msm_slice1 * beta + msm_x1 * beta_sqr + msm_y1 * beta_cube;
209 return read_term1; // degree 1
210 }
211 if constexpr (read_index == 1) {
212 const auto read_term2 =
213 (current_pc - 1) + gamma + msm_slice2 * beta + msm_x2 * beta_sqr + msm_y2 * beta_cube;
214 return read_term2; // degree 1
215 }
216 if constexpr (read_index == 2) {
217 const auto read_term3 =
218 (current_pc - 2) + gamma + msm_slice3 * beta + msm_x3 * beta_sqr + msm_y3 * beta_cube;
219 return read_term3; // degree 1
220 }
221 if constexpr (read_index == 3) {
222 const auto read_term4 =
223 (current_pc - 3) + gamma + msm_slice4 * beta + msm_x4 * beta_sqr + msm_y4 * beta_cube;
224 return read_term4; // degree 1
225 }
226 return Accumulator(1);
227 }
228
242 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
243 static void accumulate(ContainerOverSubrelations& accumulator,
244 const AllEntities& in,
245 const Parameters& params,
246 const FF& scaling_factor);
247};
248
250
251} // namespace bb
static Accumulator compute_inverse_exists(const AllEntities &in)
static constexpr size_t READ_TERMS
static Accumulator compute_write_term_predicate(const AllEntities &in)
static Accumulator compute_read_term(const AllEntities &in, const Parameters &params)
static Accumulator compute_read_term_predicate(const AllEntities &in)
static auto & get_inverse_polynomial(AllEntities &in)
Get the inverse lookup polynomial.
static constexpr size_t LENGTH
static Accumulator compute_write_term(const AllEntities &in, const Parameters &params)
Returns the fingerprint of (precompute_pc, compressed_slice, (2 * compressed_slice - 15)[P]),...
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
static constexpr std::array< bool, 2 > SUBRELATION_LINEARLY_INDEPENDENT
static constexpr size_t WRITE_TERMS
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.
typename Flavor::FF FF