Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
memory_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Luke, Raju], commit: 05a381f8b31ae4648e480f1369e911b148216e8b}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
10
11namespace bb {
12
34template <typename FF_> class MemoryRelationImpl {
35 public:
36 using FF = FF_;
37
38 static constexpr std::array<size_t, 6> SUBRELATION_PARTIAL_LENGTHS{
39 6, // memory sub-relation;
40 6, // ROM consistency sub-relation 1
41 6, // ROM consistency sub-relation 2
42 6, // RAM consistency sub-relation 1
43 6, // RAM consistency sub-relation 2
44 6 // RAM consistency sub-relation 3
45 };
46
51 template <typename AllEntities> inline static bool skip(const AllEntities& in) { return in.q_memory.is_zero(); }
52
59 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
60 inline static void accumulate(ContainerOverSubrelations& accumulators,
61 const AllEntities& in,
62 const Parameters& params,
63 const FF& scaling_factor)
64 {
65 // all accumulators are of the same length, so we set our accumulator type to (arbitrarily) be the first one.
66 // if there were one that were shorter, we could also profitably use a `ShortAccumulator` type. however,
67 // that is not the case here.
68 using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
69 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
70
71 using ParameterCoefficientAccumulator = typename Parameters::DataType::CoefficientAccumulator;
72
73 const auto& eta_m = ParameterCoefficientAccumulator(params.eta);
74 const auto& eta_two_m = ParameterCoefficientAccumulator(params.eta_two);
75 const auto& eta_three_m = ParameterCoefficientAccumulator(params.eta_three);
76
77 auto w_1_m = CoefficientAccumulator(in.w_l);
78 auto w_2_m = CoefficientAccumulator(in.w_r);
79 auto w_3_m = CoefficientAccumulator(in.w_o);
80 auto w_4_m = CoefficientAccumulator(in.w_4);
81 auto w_1_shift_m = CoefficientAccumulator(in.w_l_shift);
82 auto w_2_shift_m = CoefficientAccumulator(in.w_r_shift);
83 auto w_3_shift_m = CoefficientAccumulator(in.w_o_shift);
84 auto w_4_shift_m = CoefficientAccumulator(in.w_4_shift);
85
86 auto q_1_m = CoefficientAccumulator(in.q_l);
87 auto q_2_m = CoefficientAccumulator(in.q_r);
88 auto q_3_m = CoefficientAccumulator(in.q_o);
89 auto q_4_m = CoefficientAccumulator(in.q_4);
90 auto q_m_m = CoefficientAccumulator(in.q_m);
91 auto q_c_m = CoefficientAccumulator(in.q_c);
92
93 auto q_memory_m = CoefficientAccumulator(in.q_memory);
94
136 auto memory_record_check_m = w_3_m * eta_three_m; // degree 1
137 memory_record_check_m += w_2_m * eta_two_m;
138 memory_record_check_m += w_1_m * eta_m;
139 memory_record_check_m += q_c_m;
140 auto partial_record_check_m = memory_record_check_m; // degree 1. used later in RAM consistency check
141 memory_record_check_m = memory_record_check_m - w_4_m;
142 auto memory_record_check = Accumulator(memory_record_check_m);
160 auto neg_index_delta_m = w_1_m - w_1_shift_m;
161 auto index_delta_is_zero_m = neg_index_delta_m + FF(1); // deg 1
162 auto record_delta_m = w_4_shift_m - w_4_m;
163
164 Accumulator index_increases_by_zero_or_one(neg_index_delta_m.sqr() +
165 neg_index_delta_m); // check if next index minus current index is
166 // boolean. applies to both ROM and RAM. deg 2
167
168 auto adjacent_values_match_if_adjacent_indices_match =
169 Accumulator(index_delta_is_zero_m * record_delta_m); // deg 2
170
171 auto q_memory_by_scaling_m = q_memory_m * scaling_factor; // deg 1
172 auto q_memory_by_scaling = Accumulator(q_memory_by_scaling_m);
173 auto q_one_by_two_m = q_1_m * q_2_m; // deg 2
174 auto q_one_by_two = Accumulator(q_one_by_two_m);
175 auto q_one_by_two_by_memory_by_scaling = q_one_by_two * q_memory_by_scaling; // deg 3
176 // witnesses that for consecutive ROM gates, values match if indices match.
177 std::get<1>(accumulators) +=
178 adjacent_values_match_if_adjacent_indices_match * q_one_by_two_by_memory_by_scaling; // deg 5
179 // witnesses that index increases by {0, 1} for sorted ROM gates.
180 std::get<2>(accumulators) += index_increases_by_zero_or_one * q_one_by_two_by_memory_by_scaling; // deg 5
181
182 auto ROM_consistency_check_identity = memory_record_check * q_one_by_two; // deg 3
183
208 auto neg_access_type_m = (partial_record_check_m - w_4_m); // will be boolean for honest Prover; degree 1.
209 Accumulator neg_access_type(neg_access_type_m);
210 auto access_check = neg_access_type.sqr() + neg_access_type; // check value is boolean; degree 2.
211
212 auto neg_next_gate_access_type_m = w_3_shift_m * eta_three_m; // degree 1
213 neg_next_gate_access_type_m += w_2_shift_m * eta_two_m;
214 neg_next_gate_access_type_m += w_1_shift_m * eta_m;
215 neg_next_gate_access_type_m = neg_next_gate_access_type_m - w_4_shift_m;
216 Accumulator neg_next_gate_access_type(neg_next_gate_access_type_m); // degree 1
217 auto value_delta_m = w_3_shift_m - w_3_m; // degree 1
218 auto adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation =
219 Accumulator(index_delta_is_zero_m * value_delta_m) *
220 Accumulator(neg_next_gate_access_type_m + FF(1)); // deg 3
221
222 // We can't apply the RAM consistency check identity on the final entry in the sorted list: the wires in the
223 // next gate would make the identity fail. We need to validate that its 'access type' bool is correct. Can't
224 // do with an arithmetic gate because of the `eta` factors.
225 // Our solution is that we have the final sorted RAM record be unconstrained (i.e., none of the
226 // `MEMORY_SELECTORS` are turned on); then, we may _uniformly_ check that the *next* gate's access type is
227 // correct, to cover this edge case.
228 auto next_gate_access_type_is_boolean = neg_next_gate_access_type.sqr() + neg_next_gate_access_type; // deg 2
229
230 auto q_3_by_memory_and_scaling = Accumulator(q_3_m * q_memory_by_scaling_m);
231 // For RAM entries, if adjacent indices match and the next access is a read, then
232 // values must be equal.
233 std::get<3>(accumulators) +=
234 adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation *
235 q_3_by_memory_and_scaling; // deg 5
236 std::get<4>(accumulators) += index_increases_by_zero_or_one * q_3_by_memory_and_scaling; // deg 4
237 std::get<5>(accumulators) += next_gate_access_type_is_boolean * q_3_by_memory_and_scaling; // deg 4
238
239 auto RAM_consistency_check_identity = access_check * q_3_by_memory_and_scaling; // deg 4
240
260 auto timestamp_delta_m = w_2_shift_m - w_2_m; // deg 1
261 auto RAM_timestamp_check_identity_m = index_delta_is_zero_m * timestamp_delta_m - w_3_m; // deg 2
262 Accumulator RAM_timestamp_check_identity(RAM_timestamp_check_identity_m);
268 // degree 5
269 auto memory_identity = ROM_consistency_check_identity;
270 memory_identity += RAM_timestamp_check_identity * Accumulator(q_4_m * q_1_m); // deg 4
271 memory_identity += memory_record_check * Accumulator(q_m_m * q_1_m); // deg 4
272
273 memory_identity *= q_memory_by_scaling; // deg 5
274 memory_identity += RAM_consistency_check_identity; // deg 5
275 std::get<0>(accumulators) += memory_identity; // deg 5
276 };
277};
278
279template <typename FF> using MemoryRelation = Relation<MemoryRelationImpl<FF>>;
280} // namespace bb
RAM/ROM memory relation.
static bool skip(const AllEntities &in)
Returns true if the contribution from all subrelations for the provided inputs is identically zero.
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
static constexpr std::array< size_t, 6 > SUBRELATION_PARTIAL_LENGTHS
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
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13