barretenberg
Loading...
Searching...
No Matches
poseidon2_external_relation.hpp
1#pragma once
2#include "barretenberg/relations/relation_types.hpp"
3namespace proof_system {
4
5template <typename FF_> class Poseidon2ExternalRelationImpl {
6 public:
7 using FF = FF_;
8
9 static constexpr std::array<size_t, 4> SUBRELATION_PARTIAL_LENGTHS{
10 7, // external poseidon2 round sub-relation for first value
11 7, // external poseidon2 round sub-relation for second value
12 7, // external poseidon2 round sub-relation for third value
13 7, // external poseidon2 round sub-relation for fourth value
14 };
15
40 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
41 void static accumulate(ContainerOverSubrelations& evals,
42 const AllEntities& in,
43 const Parameters&,
44 const FF& scaling_factor)
45 {
46 using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>;
47 using View = typename Accumulator::View;
48 auto w_l = View(in.w_l);
49 auto w_r = View(in.w_r);
50 auto w_o = View(in.w_o);
51 auto w_4 = View(in.w_4);
52 auto w_l_shift = View(in.w_l_shift);
53 auto w_r_shift = View(in.w_r_shift);
54 auto w_o_shift = View(in.w_o_shift);
55 auto w_4_shift = View(in.w_4_shift);
56 auto q_l = View(in.q_l);
57 auto q_r = View(in.q_r);
58 auto q_o = View(in.q_o);
59 auto q_4 = View(in.q_4);
60 auto q_poseidon2_external = View(in.q_poseidon2_external);
61
62 // add round constants which are loaded in selectors
63 auto s1 = w_l + q_l;
64 auto s2 = w_r + q_r;
65 auto s3 = w_o + q_o;
66 auto s4 = w_4 + q_4;
67
68 // apply s-box round
69 auto u1 = s1 * s1;
70 u1 *= u1;
71 u1 *= s1;
72 auto u2 = s2 * s2;
73 u2 *= u2;
74 u2 *= s2;
75 auto u3 = s3 * s3;
76 u3 *= u3;
77 u3 *= s3;
78 auto u4 = s4 * s4;
79 u4 *= u4;
80 u4 *= s4;
81
82 // matrix mul v = M_E * u with 14 additions
83 auto t0 = u1 + u2; // u_1 + u_2
84 auto t1 = u3 + u4; // u_3 + u_4
85 auto t2 = u2 + u2; // 2u_2
86 t2 += t1; // 2u_2 + u_3 + u_4
87 auto t3 = u4 + u4; // 2u_4
88 t3 += t0; // u_1 + u_2 + 2u_4
89 auto v4 = t1 + t1;
90 v4 += v4;
91 v4 += t3; // u_1 + u_2 + 4u_3 + 6u_4
92 auto v2 = t0 + t0;
93 v2 += v2;
94 v2 += t2; // 4u_1 + 6u_2 + u_3 + u_4
95 auto v1 = t3 + v2; // 5u_1 + 7u_2 + u_3 + 3u_4
96 auto v3 = t2 + v4; // u_1 + 3u_2 + 5u_3 + 7u_4
97
98 auto tmp = q_poseidon2_external * (v1 - w_l_shift);
99 tmp *= scaling_factor;
100 std::get<0>(evals) += tmp;
101
102 tmp = q_poseidon2_external * (v2 - w_r_shift);
103 tmp *= scaling_factor;
104 std::get<1>(evals) += tmp;
105
106 tmp = q_poseidon2_external * (v3 - w_o_shift);
107 tmp *= scaling_factor;
108 std::get<2>(evals) += tmp;
109
110 tmp = q_poseidon2_external * (v4 - w_4_shift);
111 tmp *= scaling_factor;
112 std::get<3>(evals) += tmp;
113 };
114};
115
116template <typename FF> using Poseidon2ExternalRelation = Relation<Poseidon2ExternalRelationImpl<FF>>;
117} // namespace proof_system
Definition: poseidon2_external_relation.hpp:5
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....
Definition: poseidon2_external_relation.hpp:41