Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bilinear_or_batched_eq_check_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
9
10namespace bb {
11
57template <typename FF_> class BilinearOrBatchedEqCheckRelationImpl {
58 public:
59 using FF = FF_;
60
61 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{ 6, 5 };
62
63 template <typename AllEntities> inline static bool skip(const AllEntities& in)
64 {
66 }
67
74 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
75 inline static void accumulate(ContainerOverSubrelations& evals,
76 const AllEntities& in,
77 BB_UNUSED const Parameters& params,
78 const FF& scaling_factor)
79 {
81 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
82
83 auto q_cp_m = CoefficientAccumulator(in[AllEntities::EntityId::q_bilinear_batched_eq]);
84 auto w_l_m = CoefficientAccumulator(in[AllEntities::EntityId::w_l]);
85 auto w_r_m = CoefficientAccumulator(in[AllEntities::EntityId::w_r]);
86 auto w_o_m = CoefficientAccumulator(in[AllEntities::EntityId::w_o]);
87 auto w_4_m = CoefficientAccumulator(in[AllEntities::EntityId::w_4]);
88
89 // Mode gates:
90 // bilinear_gate = q_cp · (2 − q_cp) → 0 / 1 / 0 for q_cp = 0 / 1 / 2
91 // batched_eq_gate = q_cp · (q_cp − 1) · 1/2 → 0 / 0 / 1 for q_cp = 0 / 1 / 2
92 // The 1/2 in batched_eq_gate absorbs the factor of 2 that q_cp·(q_cp−1) evaluates to in BATCHED_EQ mode,
93 // so q_l..q_5, q_c, q_m are written by the builder without halving.
94 static const FF half = FF(2).invert();
95 auto two_minus_q_cp = -q_cp_m + FF(2);
96 auto q_cp_minus_1 = q_cp_m - FF(1);
97 auto bilinear_gate = q_cp_m * two_minus_q_cp;
98 auto batched_eq_gate = q_cp_m * q_cp_minus_1 * half;
99
100 // Pre-scale the two mode gates by the scaling factor so it threads through both
101 // contributions without an extra Accumulator-level multiply at the end.
102 auto scaled_bilinear_gate = bilinear_gate * scaling_factor;
103 auto scaled_batched_eq_gate = batched_eq_gate * scaling_factor;
104
105 auto q_m_m = CoefficientAccumulator(in[AllEntities::EntityId::q_m]);
106 auto q_l_m = CoefficientAccumulator(in[AllEntities::EntityId::q_l]);
107 auto q_r_m = CoefficientAccumulator(in[AllEntities::EntityId::q_r]);
108 auto q_o_m = CoefficientAccumulator(in[AllEntities::EntityId::q_o]);
109 auto q_4_m = CoefficientAccumulator(in[AllEntities::EntityId::q_4]);
110 auto q_c_m = CoefficientAccumulator(in[AllEntities::EntityId::q_c]);
111 auto q_5_m = CoefficientAccumulator(in[AllEntities::EntityId::q_5]);
112
113 // Subrelation 1
114 {
115 // Two products sharing the wire w_l, i.e. q_m·w_l·w_r + q_5·w_l·w_o (= w_l·(q_m·w_r + q_5·w_o)).
116 // The q·w coefficient products are formed at the CoefficientAccumulator level so each product
117 // needs a single Accumulator multiply.
118 auto products =
119 Accumulator(q_m_m * w_l_m) * Accumulator(w_r_m) + Accumulator(q_5_m * w_l_m) * Accumulator(w_o_m);
120 auto linears = (q_l_m * w_l_m) + (q_r_m * w_r_m) + (q_o_m * w_o_m) + (q_4_m * w_4_m) + q_c_m;
121 auto bilinear = products + Accumulator(linears);
122
123 auto batched_eq_half_1 = (q_l_m * w_l_m) + (q_r_m * w_r_m) + q_c_m;
124
125 std::get<0>(evals) += Accumulator(scaled_bilinear_gate) * bilinear +
126 Accumulator(scaled_batched_eq_gate) * Accumulator(batched_eq_half_1);
127 }
128
129 // Subrelation 2
130 {
132
133 auto batched_eq_half_2 = (q_o_m * w_o_m) + (q_4_m * w_4_m) + q_m_m;
134 std::get<1>(evals) += ShortAccumulator(scaled_batched_eq_gate) * ShortAccumulator(batched_eq_half_2);
135 }
136 };
137};
138
140} // namespace bb
Bilinear / batched-eq custom gate (Mega flavors only).
static void accumulate(ContainerOverSubrelations &evals, const AllEntities &in, BB_UNUSED const Parameters &params, const FF &scaling_factor)
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
#define BB_UNUSED
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13