Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator_non_native_field_relation_impl.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
9
10namespace bb {
73template <typename FF>
74template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
75void TranslatorNonNativeFieldRelationImpl<FF>::accumulate(ContainerOverSubrelations& accumulators,
76 const AllEntities& in,
77 const Parameters& params,
78 const FF& scaling_factor)
79{
80
82 using View = typename Accumulator::View;
83
84 static constexpr size_t NUM_LIMB_BITS = 68;
85 static FF shift = FF(uint256_t(1) << NUM_LIMB_BITS);
86 static FF shiftx2 = FF(uint256_t(1) << (NUM_LIMB_BITS * 2));
87 static FF shiftx3 = FF(uint256_t(1) << (NUM_LIMB_BITS * 3));
89 static uint512_t BINARY_BASIS_MODULUS = uint512_t(1) << (NUM_LIMB_BITS << 2);
90 static uint512_t NEGATIVE_PRIME_MODULUS = BINARY_BASIS_MODULUS - MODULUS_U512;
91 static const std::array<FF, 5> NEGATIVE_MODULUS_LIMBS = {
92 FF(NEGATIVE_PRIME_MODULUS.slice(0, NUM_LIMB_BITS).lo),
93 FF(NEGATIVE_PRIME_MODULUS.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2).lo),
94 FF(NEGATIVE_PRIME_MODULUS.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3).lo),
95 FF(NEGATIVE_PRIME_MODULUS.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4).lo),
97 };
98
99 const auto& evaluation_input_x_0 = params.evaluation_input_x[0];
100 const auto& evaluation_input_x_1 = params.evaluation_input_x[1];
101 const auto& evaluation_input_x_2 = params.evaluation_input_x[2];
102 const auto& evaluation_input_x_3 = params.evaluation_input_x[3];
103 const auto& evaluation_input_x_4 = params.evaluation_input_x[4];
104 // for j < 4, v_i_j is the j-th limb of v^{1+i}
105 // v_i_4 is v^{1+i} in the native field
106 const auto& v_0_0 = params.batching_challenge_v[0][0];
107 const auto& v_0_1 = params.batching_challenge_v[0][1];
108 const auto& v_0_2 = params.batching_challenge_v[0][2];
109 const auto& v_0_3 = params.batching_challenge_v[0][3];
110 const auto& v_0_4 = params.batching_challenge_v[0][4];
111 const auto& v_1_0 = params.batching_challenge_v[1][0];
112 const auto& v_1_1 = params.batching_challenge_v[1][1];
113 const auto& v_1_2 = params.batching_challenge_v[1][2];
114 const auto& v_1_3 = params.batching_challenge_v[1][3];
115 const auto& v_1_4 = params.batching_challenge_v[1][4];
116 const auto& v_2_0 = params.batching_challenge_v[2][0];
117 const auto& v_2_1 = params.batching_challenge_v[2][1];
118 const auto& v_2_2 = params.batching_challenge_v[2][2];
119 const auto& v_2_3 = params.batching_challenge_v[2][3];
120 const auto& v_2_4 = params.batching_challenge_v[2][4];
121 const auto& v_3_0 = params.batching_challenge_v[3][0];
122 const auto& v_3_1 = params.batching_challenge_v[3][1];
123 const auto& v_3_2 = params.batching_challenge_v[3][2];
124 const auto& v_3_3 = params.batching_challenge_v[3][3];
125 const auto& v_3_4 = params.batching_challenge_v[3][4];
126
127 const auto& op = View(in.op);
128 const auto& p_x_low_limbs = View(in.p_x_low_limbs);
129 const auto& p_y_low_limbs = View(in.p_y_low_limbs);
130 const auto& p_x_high_limbs = View(in.p_x_high_limbs);
131 const auto& p_y_high_limbs = View(in.p_y_high_limbs);
132 const auto& accumulators_binary_limbs_0 = View(in.accumulators_binary_limbs_0);
133 const auto& accumulators_binary_limbs_1 = View(in.accumulators_binary_limbs_1);
134 const auto& accumulators_binary_limbs_2 = View(in.accumulators_binary_limbs_2);
135 const auto& accumulators_binary_limbs_3 = View(in.accumulators_binary_limbs_3);
136 const auto& z_low_limbs = View(in.z_low_limbs);
137 const auto& z_high_limbs = View(in.z_high_limbs);
138 const auto& quotient_low_binary_limbs = View(in.quotient_low_binary_limbs);
139 const auto& quotient_high_binary_limbs = View(in.quotient_high_binary_limbs);
140 const auto& p_x_low_limbs_shift = View(in.p_x_low_limbs_shift);
141 const auto& p_y_low_limbs_shift = View(in.p_y_low_limbs_shift);
142 const auto& p_x_high_limbs_shift = View(in.p_x_high_limbs_shift);
143 const auto& p_y_high_limbs_shift = View(in.p_y_high_limbs_shift);
144 const auto& accumulators_binary_limbs_0_shift = View(in.accumulators_binary_limbs_0_shift);
145 const auto& accumulators_binary_limbs_1_shift = View(in.accumulators_binary_limbs_1_shift);
146 const auto& accumulators_binary_limbs_2_shift = View(in.accumulators_binary_limbs_2_shift);
147 const auto& accumulators_binary_limbs_3_shift = View(in.accumulators_binary_limbs_3_shift);
148 const auto& z_low_limbs_shift = View(in.z_low_limbs_shift);
149 const auto& z_high_limbs_shift = View(in.z_high_limbs_shift);
150 const auto& quotient_low_binary_limbs_shift = View(in.quotient_low_binary_limbs_shift);
151 const auto& quotient_high_binary_limbs_shift = View(in.quotient_high_binary_limbs_shift);
152 const auto& relation_wide_limbs = View(in.relation_wide_limbs);
153 const auto& relation_wide_limbs_shift = View(in.relation_wide_limbs_shift);
154 const auto& lagrange_even_in_minicircuit = View(in.lagrange_even_in_minicircuit);
155
156 // Contribution (1) Computing the mod 2²⁷² relation over lower 136 bits
157 // clang-format off
158 // the index-0 limb
159 auto tmp = accumulators_binary_limbs_0_shift * evaluation_input_x_0
160 + op
161 + p_x_low_limbs * v_0_0
162 + p_y_low_limbs * v_1_0
163 + z_low_limbs * v_2_0
164 + z_low_limbs_shift * v_3_0
165 + quotient_low_binary_limbs * NEGATIVE_MODULUS_LIMBS[0]
166 - accumulators_binary_limbs_0;
167
168 // the index-1 limb
169 tmp += (accumulators_binary_limbs_1_shift * evaluation_input_x_0
170 + accumulators_binary_limbs_0_shift * evaluation_input_x_1
171 + p_x_low_limbs * v_0_1
172 + p_x_low_limbs_shift * v_0_0
173 + p_y_low_limbs * v_1_1
174 + p_y_low_limbs_shift * v_1_0
175 + z_low_limbs * v_2_1
176 + z_high_limbs * v_2_0
177 + z_low_limbs_shift * v_3_1
178 + z_high_limbs_shift * v_3_0
179 + quotient_low_binary_limbs * NEGATIVE_MODULUS_LIMBS[1]
180 + quotient_low_binary_limbs_shift * NEGATIVE_MODULUS_LIMBS[0]
181 - accumulators_binary_limbs_1)
182 * shift ;
183 // clang-format on
184 // subtract large value; vanishing shows the desired relation holds on low 136-bit limb
185 tmp -= relation_wide_limbs * shiftx2;
186 tmp *= lagrange_even_in_minicircuit;
187 tmp *= scaling_factor;
188 std::get<0>(accumulators) += tmp;
189
190 // Contribution (2) Computing the 2²⁷² relation over higher 136 bits
191 // why declare another temporary?
192 // clang-format off
193 // the index-2 limb, with a carry from the previous calculation
194 tmp = relation_wide_limbs
195 + accumulators_binary_limbs_2_shift * evaluation_input_x_0
196 + accumulators_binary_limbs_1_shift * evaluation_input_x_1
197 + accumulators_binary_limbs_0_shift * evaluation_input_x_2
198 + p_x_high_limbs * v_0_0
199 + p_x_low_limbs_shift * v_0_1
200 + p_x_low_limbs * v_0_2
201 + p_y_high_limbs * v_1_0
202 + p_y_low_limbs_shift * v_1_1
203 + p_y_low_limbs * v_1_2
204 + z_high_limbs * v_2_1
205 + z_low_limbs * v_2_2
206 + z_high_limbs_shift * v_3_1
207 + z_low_limbs_shift * v_3_2
208 + quotient_high_binary_limbs * NEGATIVE_MODULUS_LIMBS[0]
209 + quotient_low_binary_limbs_shift * NEGATIVE_MODULUS_LIMBS[1]
210 + quotient_low_binary_limbs * NEGATIVE_MODULUS_LIMBS[2]
211 - accumulators_binary_limbs_2;
212
213 // the index-2 limb
214 tmp += (accumulators_binary_limbs_3_shift * evaluation_input_x_0
215 + accumulators_binary_limbs_2_shift * evaluation_input_x_1
216 + accumulators_binary_limbs_1_shift * evaluation_input_x_2
217 + accumulators_binary_limbs_0_shift * evaluation_input_x_3
218 + p_x_high_limbs_shift * v_0_0
219 + p_x_high_limbs * v_0_1
220 + p_x_low_limbs_shift * v_0_2
221 + p_x_low_limbs * v_0_3
222 + p_y_high_limbs_shift * v_1_0
223 + p_y_high_limbs * v_1_1
224 + p_y_low_limbs_shift * v_1_2
225 + p_y_low_limbs * v_1_3
226 + z_high_limbs * v_2_2
227 + z_low_limbs * v_2_3
228 + z_high_limbs_shift * v_3_2
229 + z_low_limbs_shift * v_3_3
230 + quotient_high_binary_limbs_shift * NEGATIVE_MODULUS_LIMBS[0]
231 + quotient_high_binary_limbs * NEGATIVE_MODULUS_LIMBS[1]
232 + quotient_low_binary_limbs_shift * NEGATIVE_MODULUS_LIMBS[2]
233 + quotient_low_binary_limbs * NEGATIVE_MODULUS_LIMBS[3]
234 - accumulators_binary_limbs_3)
235 * shift;
236 // clang-format on
237 // subtract large value; vanishing shows the desired relation holds on high 136-bit limb
238 tmp -= relation_wide_limbs_shift * shiftx2;
239 tmp *= lagrange_even_in_minicircuit;
240 tmp *= scaling_factor;
241 std::get<1>(accumulators) += tmp;
242
243 const auto reconstruct_from_two = [](const auto& l0, const auto& l1) { return l0 + l1 * shift; };
244
245 const auto reconstruct_from_four = [](const auto& l0, const auto& l1, const auto& l2, const auto& l3) {
246 return l0 + l1 * shift + l2 * shiftx2 + l3 * shiftx3;
247 };
248
249 // Reconstructing native versions of values
250 auto reconstructed_p_x =
251 reconstruct_from_four(p_x_low_limbs, p_x_low_limbs_shift, p_x_high_limbs, p_x_high_limbs_shift);
252 auto reconstructed_p_y =
253 reconstruct_from_four(p_y_low_limbs, p_y_low_limbs_shift, p_y_high_limbs, p_y_high_limbs_shift);
254 auto reconstructed_previous_accumulator = reconstruct_from_four(accumulators_binary_limbs_0_shift,
255 accumulators_binary_limbs_1_shift,
256 accumulators_binary_limbs_2_shift,
257 accumulators_binary_limbs_3_shift);
258 auto reconstructed_current_accumulator = reconstruct_from_four(accumulators_binary_limbs_0,
259 accumulators_binary_limbs_1,
260 accumulators_binary_limbs_2,
261 accumulators_binary_limbs_3);
262 auto reconstructed_z1 = reconstruct_from_two(z_low_limbs, z_high_limbs);
263 auto reconstructed_z2 = reconstruct_from_two(z_low_limbs_shift, z_high_limbs_shift);
264 auto reconstructed_quotient = reconstruct_from_four(quotient_low_binary_limbs,
265 quotient_low_binary_limbs_shift,
266 quotient_high_binary_limbs,
267 quotient_high_binary_limbs_shift);
268
269 // Contribution (3). Evaluating integer relation over native field
270 // clang-format off
271 // the native limb index is 4
272 tmp = reconstructed_previous_accumulator * evaluation_input_x_4
273 + op
274 + reconstructed_p_x * v_0_4
275 + reconstructed_p_y * v_1_4
276 + reconstructed_z1 * v_2_4
277 + reconstructed_z2 * v_3_4
278 + reconstructed_quotient * NEGATIVE_MODULUS_LIMBS[4]
279 - reconstructed_current_accumulator;
280 // clang-format on
281 tmp *= lagrange_even_in_minicircuit;
282 tmp *= scaling_factor;
283 std::get<2>(accumulators) += tmp;
284};
285} // namespace bb
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Expression for the computation of Translator accumulator in integers through 68-bit limbs and native ...
constexpr uintx slice(const uint64_t start, const uint64_t end) const
Definition uintx.hpp:82
uintx< uint256_t > uint512_t
Definition uintx.hpp:307
Entry point for Barretenberg command-line interface.
typename Flavor::FF FF
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr uint256_t modulus