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
55 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
56 void static accumulate(ContainerOverSubrelations& evals,
57 const AllEntities& in,
58 const Parameters&,
59 const FF& scaling_factor)
60 {
62 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
63 auto w_l = CoefficientAccumulator(in.w_l);
64 auto w_r = CoefficientAccumulator(in.w_r);
65 auto w_o = CoefficientAccumulator(in.w_o);
66 auto w_4 = CoefficientAccumulator(in.w_4);
67 auto w_l_shift = CoefficientAccumulator(in.w_l_shift);
68 auto w_r_shift = CoefficientAccumulator(in.w_r_shift);
69 auto w_o_shift = CoefficientAccumulator(in.w_o_shift);
70 auto w_4_shift = CoefficientAccumulator(in.w_4_shift);
71 auto q_l = CoefficientAccumulator(in.q_l);
72 auto q_r = CoefficientAccumulator(in.q_r);
73 auto q_o = CoefficientAccumulator(in.q_o);
74 auto q_4 = CoefficientAccumulator(in.q_4);
75 auto q_poseidon2_external = CoefficientAccumulator(in.q_poseidon2_external);
76
77 // add round constants which are loaded in selectors
78 auto s1 = Accumulator(w_l + q_l);
79 auto s2 = Accumulator(w_r + q_r);
80 auto s3 = Accumulator(w_o + q_o);
81 auto s4 = Accumulator(w_4 + q_4);
82
83 // apply s-box round
84 auto u1 = s1.sqr();
85 u1 = u1.sqr();
86 u1 *= s1;
87 auto u2 = s2.sqr();
88 u2 = u2.sqr();
89 u2 *= s2;
90 auto u3 = s3.sqr();
91 u3 = u3.sqr();
92 u3 *= s3;
93 auto u4 = s4.sqr();
94 u4 = u4.sqr();
95 u4 *= s4;
96
97 // matrix mul v = M_E * u with 14 additions
98 auto t0 = u1 + u2; // u_1 + u_2
99 auto t1 = u3 + u4; // u_3 + u_4
100 auto t2 = u2 + u2; // 2u_2
101 t2 += t1; // 2u_2 + u_3 + u_4
102 auto t3 = u4 + u4; // 2u_4
103 t3 += t0; // u_1 + u_2 + 2u_4
104 auto v4 = t1 + t1;
105 v4 += v4;
106 v4 += t3; // u_1 + u_2 + 4u_3 + 6u_4
107 auto v2 = t0 + t0;
108 v2 += v2;
109 v2 += t2; // 4u_1 + 6u_2 + u_3 + u_4
110 auto v1 = t3 + v2; // 5u_1 + 7u_2 + u_3 + 3u_4
111 auto v3 = t2 + v4; // u_1 + 3u_2 + 5u_3 + 7u_4
112
113 auto q_pos_by_scaling = Accumulator(q_poseidon2_external * scaling_factor);
114 auto tmp = q_pos_by_scaling * (v1 - Accumulator(w_l_shift));
115 std::get<0>(evals) += tmp;
116
117 tmp = q_pos_by_scaling * (v2 - Accumulator(w_r_shift));
118 std::get<1>(evals) += tmp;
119
120 tmp = q_pos_by_scaling * (v3 - Accumulator(w_o_shift));
121 std::get<2>(evals) += tmp;
122
123 tmp = q_pos_by_scaling * (v4 - Accumulator(w_4_shift));
124 std::get<3>(evals) += tmp;
125 };
126};
127
129} // 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 E_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
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