Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
poseidon2_internal_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
9#include "relation_types.hpp"
10
11namespace bb {
12
65template <typename FF_> class Poseidon2InternalRelationImpl {
66 public:
67 using FF = FF_;
68
69 static constexpr std::array<size_t, 4> SUBRELATION_PARTIAL_LENGTHS{
70 7, // internal poseidon2 round sub-relation for first value
71 7, // internal poseidon2 round sub-relation for second value
72 7, // internal poseidon2 round sub-relation for third value
73 7, // internal poseidon2 round sub-relation for fourth value
74 };
75
76 // Internal matrix diagonal values minus one: these are D_i - 1 where D_i are the actual diagonal entries of M_I.
77 // The internal round computes: v[i] = (D_i - 1) * u[i] + sum = D_i * u[i] + (sum of other elements)
82 static constexpr fr D1 = fr{ 1 } + D1_minus_1;
87 template <typename AllEntities> inline static bool skip(const AllEntities& in)
88 {
89 return (in.q_poseidon2_internal.is_zero());
90 }
91
101 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
102 void static accumulate(ContainerOverSubrelations& evals,
103 const AllEntities& in,
104 const Parameters&,
105 const FF& scaling_factor)
106 {
107 // Univariates of degree 6 represented in Lagrange basis
109 // Low-degree univariates represented in monomial basis
110 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
111
112 // Current state
113 const auto w_1 = CoefficientAccumulator(in.w_l);
114 const auto w_2 = CoefficientAccumulator(in.w_r);
115 const auto w_3 = CoefficientAccumulator(in.w_o);
116 const auto w_4 = CoefficientAccumulator(in.w_4);
117 // Expected state, contained in the next row
118 const auto w_1_shift = CoefficientAccumulator(in.w_l_shift);
119 const auto w_2_shift = CoefficientAccumulator(in.w_r_shift);
120 const auto w_3_shift = CoefficientAccumulator(in.w_o_shift);
121 const auto w_4_shift = CoefficientAccumulator(in.w_4_shift);
122 // Poseidon2 internal relation selector
123 const auto q_poseidon2_internal_m = CoefficientAccumulator(in.q_poseidon2_internal);
124 // ĉ₀⁽ⁱ⁾ - the round constant in `i`-th internal round
125 const auto c_0_int = CoefficientAccumulator(in.q_l);
126
127 Accumulator barycentric_term;
128
129 // Add ĉ₀⁽ⁱ⁾ stored in the selector and convert to Lagrange basis
130 auto s1 = Accumulator(w_1 + c_0_int);
131
132 // Apply S-box. Note that the multiplication is performed point-wise
133 auto u1 = s1.sqr();
134 u1 = u1.sqr();
135 u1 *= s1;
136
137 const auto q_pos_by_scaling_m = (q_poseidon2_internal_m * scaling_factor);
138 const auto q_pos_by_scaling = Accumulator(q_pos_by_scaling_m);
139 // Common terms
140 const auto partial_sum = w_2 + w_3 + w_4;
141 const auto scaled_u1 = u1 * q_pos_by_scaling;
142
143 // Row 1: v_1 = D_1 * u_1 + u_2 + u_3 + u_4
144 barycentric_term = scaled_u1 * D1;
145 auto monomial_term = partial_sum - w_1_shift;
146 barycentric_term += Accumulator(monomial_term * q_pos_by_scaling_m);
147 std::get<0>(evals) += barycentric_term;
148
149 // Row 2: v_2 = u_1 + D_2 * u_2 + u_3 + u_4 = u_1 + (D_2 - 1) * u_2 + u_2 + u_3 + u_4
150 auto v2_m = w_2 * D2_minus_1 + partial_sum - w_2_shift;
151 barycentric_term = Accumulator(v2_m * q_pos_by_scaling_m);
152 barycentric_term += scaled_u1;
153 std::get<1>(evals) += barycentric_term;
154
155 // Row 3: v_3 = u_1 + u_2 + D_3 * u_3 + u_4 = u_1 + u_2 + (D_3 - 1) * u_3 + u_3 + u_4
156 auto v3_m = w_3 * D3_minus_1 + partial_sum - w_3_shift;
157 barycentric_term = Accumulator(v3_m * q_pos_by_scaling_m);
158 barycentric_term += scaled_u1;
159 std::get<2>(evals) += barycentric_term;
160
161 // Row 4: v_4 = u_1 + u_2 + u_3 + D_4 * u_4 = u_1 + u_2 + u_3 + (D_4 - 1) * u_4 + u_4
162 auto v4_m = w_4 * D4_minus_1 + partial_sum - w_4_shift;
163 barycentric_term = Accumulator(v4_m * q_pos_by_scaling_m);
164 barycentric_term += scaled_u1;
165 std::get<3>(evals) += barycentric_term;
166 };
167}; // namespace bb
168
170} // namespace bb
Expression for the Poseidon2 internal round relation, based on I_i 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
static void accumulate(ContainerOverSubrelations &evals, const AllEntities &in, const Parameters &, const FF &scaling_factor)
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
static constexpr std::array< FF, t > internal_matrix_diagonal_minus_one