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: Complete, auditors: [Luke, Raju], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
9namespace bb {
10
57template <typename FF_> class Poseidon2ExternalRelationImpl {
58 public:
59 using FF = FF_;
60
61 static constexpr std::array<size_t, 4> SUBRELATION_PARTIAL_LENGTHS{
62 7, // external poseidon2 round sub-relation for first value
63 7, // external poseidon2 round sub-relation for second value
64 7, // external poseidon2 round sub-relation for third value
65 7, // external poseidon2 round sub-relation for fourth value
66 };
67
72 template <typename AllEntities> inline static bool skip(const AllEntities& in)
73 {
75 }
76
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[AllEntities::EntityId::w_l]);
100 const auto w_2 = CoefficientAccumulator(in[AllEntities::EntityId::w_r]);
101 const auto w_3 = CoefficientAccumulator(in[AllEntities::EntityId::w_o]);
102 const auto w_4 = CoefficientAccumulator(in[AllEntities::EntityId::w_4]);
103 // Expected state, contained in the next row
104 const auto w_1_shift = CoefficientAccumulator(in[AllEntities::EntityId::w_l_shift]);
105 const auto w_2_shift = CoefficientAccumulator(in[AllEntities::EntityId::w_r_shift]);
106 const auto w_3_shift = CoefficientAccumulator(in[AllEntities::EntityId::w_o_shift]);
107 const auto w_4_shift = CoefficientAccumulator(in[AllEntities::EntityId::w_4_shift]);
108 // i-th external round constants
109 const auto c_1 = CoefficientAccumulator(in[AllEntities::EntityId::q_l]);
110 const auto c_2 = CoefficientAccumulator(in[AllEntities::EntityId::q_r]);
111 const auto c_3 = CoefficientAccumulator(in[AllEntities::EntityId::q_o]);
112 const auto c_4 = CoefficientAccumulator(in[AllEntities::EntityId::q_4]);
113 // Poseidon2 external relation selector
114 const auto q_poseidon2_external = CoefficientAccumulator(in[AllEntities::EntityId::q_poseidon2_external]);
115
116 // add round constants which are loaded in selectors
117
118 // Square the degree-1 sbox input in the coefficient basis before promoting -- see
119 // UnivariateCoefficientBasis::sqr.
120 auto sbox = [](const auto& x_m) {
121 const Accumulator x(x_m);
122 const Accumulator t2(x_m.sqr()); // x^2
123 return t2.sqr() * x; // x^4 * x = x^5
124 };
125 // apply s-box round
126 auto u1 = sbox(w_1 + c_1);
127 auto u2 = sbox(w_2 + c_2);
128 auto u3 = sbox(w_3 + c_3);
129 auto u4 = sbox(w_4 + c_4);
130 // Matrix mul v = M_E * u with 14 additions.
131 // Precompute common summands.
132 auto t0 = u1 + u2; // u_1 + u_2
133 auto t1 = u3 + u4; // u_3 + u_4
134 auto t2 = u2 + u2; // 2u_2
135 t2 += t1; // 2u_2 + u_3 + u_4
136 auto t3 = u4 + u4; // 2u_4
137 t3 += t0; // u_1 + u_2 + 2u_4
138
139 // Row 4: u_1 + u_2 + 4u_3 + 6u_4
140 auto v4 = t1 + t1;
141 v4 += v4;
142 v4 += t3;
143
144 // Row 2: 4u_1 + 6u_2 + u_3 + u_4
145 auto v2 = t0 + t0;
146 v2 += v2;
147 v2 += t2;
148 // Row 1: 5u_1 + 7u_2 + u_3 + 3u_4
149 auto v1 = t3 + v2;
150
151 // Row 3: u_1 + 3u_2 + 5u_3 + 7u_4
152 auto v3 = t2 + v4;
153
154 auto q_pos_by_scaling = Accumulator(q_poseidon2_external * scaling_factor);
155 std::get<0>(evals) += q_pos_by_scaling * (v1 - Accumulator(w_1_shift));
156
157 std::get<1>(evals) += q_pos_by_scaling * (v2 - Accumulator(w_2_shift));
158
159 std::get<2>(evals) += q_pos_by_scaling * (v3 - Accumulator(w_3_shift));
160
161 std::get<3>(evals) += q_pos_by_scaling * (v4 - Accumulator(w_4_shift));
162 };
163};
164
166} // namespace bb
Expression for the poseidon2 external round relation, based on in Section 6 of https://eprint....
static void accumulate(ContainerOverSubrelations &evals, const AllEntities &in, const Parameters &, const FF &scaling_factor)
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.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13