Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
poseidon2_external_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
9namespace bb {
10
11template <typename FF_> class Poseidon2ExternalRelationImpl {
12 public:
13 using FF = FF_;
14
15 static constexpr std::array<size_t, 4> SUBRELATION_PARTIAL_LENGTHS{
16 7, // external poseidon2 round sub-relation for first value
17 7, // external poseidon2 round sub-relation for second value
18 7, // external poseidon2 round sub-relation for third value
19 7, // external poseidon2 round sub-relation for fourth value
20 };
21
26 template <typename AllEntities> inline static bool skip(const AllEntities& in)
27 {
28 return in.q_poseidon2_external.is_zero();
29 }
30
87 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
88 void static accumulate(ContainerOverSubrelations& evals,
89 const AllEntities& in,
90 const Parameters&,
91 const FF& scaling_factor)
92 {
93 // Univariates of degree 6 represented in Lagrange basis
95 // Low-degree univariates represented in monomial basis
96 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
97
98 // Current state
99 const auto w_1 = CoefficientAccumulator(in.w_l);
100 const auto w_2 = CoefficientAccumulator(in.w_r);
101 const auto w_3 = CoefficientAccumulator(in.w_o);
102 const auto w_4 = CoefficientAccumulator(in.w_4);
103 // Expected state, contained in the next row
104 const auto w_1_shift = CoefficientAccumulator(in.w_l_shift);
105 const auto w_2_shift = CoefficientAccumulator(in.w_r_shift);
106 const auto w_3_shift = CoefficientAccumulator(in.w_o_shift);
107 const auto w_4_shift = CoefficientAccumulator(in.w_4_shift);
108 // i-th external round constants
109 const auto c_1 = CoefficientAccumulator(in.q_l);
110 const auto c_2 = CoefficientAccumulator(in.q_r);
111 const auto c_3 = CoefficientAccumulator(in.q_o);
112 const auto c_4 = CoefficientAccumulator(in.q_4);
113 // Poseidon2 external relation selector
114 const auto q_poseidon2_external = CoefficientAccumulator(in.q_poseidon2_external);
115
116 // add round constants which are loaded in selectors
117
118 auto sbox = [](const Accumulator& x) {
119 auto t2 = x.sqr(); // x^2
120 auto t4 = t2.sqr(); // x^4
121 return t4 * x; // x^5
122 };
123 // apply s-box round
124 auto u1 = sbox(Accumulator(w_1 + c_1));
125 auto u2 = sbox(Accumulator(w_2 + c_2));
126 auto u3 = sbox(Accumulator(w_3 + c_3));
127 auto u4 = sbox(Accumulator(w_4 + c_4));
128 // Matrix mul v = M_E * u with 14 additions.
129 // Precompute common summands.
130 auto t0 = u1 + u2; // u_1 + u_2
131 auto t1 = u3 + u4; // u_3 + u_4
132 auto t2 = u2 + u2; // 2u_2
133 t2 += t1; // 2u_2 + u_3 + u_4
134 auto t3 = u4 + u4; // 2u_4
135 t3 += t0; // u_1 + u_2 + 2u_4
136
137 // Row 4: u_1 + u_2 + 4u_3 + 6u_4
138 auto v4 = t1 + t1;
139 v4 += v4;
140 v4 += t3;
141
142 // Row 2: 4u_1 + 6u_2 + u_3 + u_4
143 auto v2 = t0 + t0;
144 v2 += v2;
145 v2 += t2;
146 // Row 1: 5u_1 + 7u_2 + u_3 + 3u_4
147 auto v1 = t3 + v2;
148
149 // Row 3: u_1 + 3u_2 + 5u_3 + 7u_4
150 auto v3 = t2 + v4;
151
152 auto q_pos_by_scaling = Accumulator(q_poseidon2_external * scaling_factor);
153 std::get<0>(evals) += q_pos_by_scaling * (v1 - Accumulator(w_1_shift));
154
155 std::get<1>(evals) += q_pos_by_scaling * (v2 - Accumulator(w_2_shift));
156
157 std::get<2>(evals) += q_pos_by_scaling * (v3 - Accumulator(w_3_shift));
158
159 std::get<3>(evals) += q_pos_by_scaling * (v4 - Accumulator(w_4_shift));
160 };
161};
162
164} // namespace bb
static void accumulate(ContainerOverSubrelations &evals, const AllEntities &in, const Parameters &, const FF &scaling_factor)
Expression for the poseidon2 external round relation, based on in Section 6 of https://eprint....
static bool skip(const AllEntities &in)
Returns true if the contribution from all subrelations for the provided inputs is identically zero.
static constexpr std::array< size_t, 4 > 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.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13