barretenberg
Loading...
Searching...
No Matches
gen_perm_sort_relation.hpp
1#pragma once
2#include "barretenberg/relations/relation_types.hpp"
3
4namespace proof_system {
5
6template <typename FF_> class GenPermSortRelationImpl {
7 public:
8 using FF = FF_;
9
10 static constexpr std::array<size_t, 4> SUBRELATION_PARTIAL_LENGTHS{
11 6, // range constrain sub-relation 1
12 6, // range constrain sub-relation 2
13 6, // range constrain sub-relation 3
14 6 // range constrain sub-relation 4
15 };
16
32 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
33 inline static void accumulate(ContainerOverSubrelations& accumulators,
34 const AllEntities& in,
35 const Parameters&,
36 const FF& scaling_factor)
37 {
38 using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>;
39 using View = typename Accumulator::View;
40 auto w_1 = View(in.w_l);
41 auto w_2 = View(in.w_r);
42 auto w_3 = View(in.w_o);
43 auto w_4 = View(in.w_4);
44 auto w_1_shift = View(in.w_l_shift);
45 auto q_sort = View(in.q_sort);
46
47 static const FF minus_one = FF(-1);
48 static const FF minus_two = FF(-2);
49 static const FF minus_three = FF(-3);
50
51 // Compute wire differences
52 auto delta_1 = w_2 - w_1;
53 auto delta_2 = w_3 - w_2;
54 auto delta_3 = w_4 - w_3;
55 auto delta_4 = w_1_shift - w_4;
56
57 // Contribution (1)
58 auto tmp_1 = delta_1;
59 tmp_1 *= (delta_1 + minus_one);
60 tmp_1 *= (delta_1 + minus_two);
61 tmp_1 *= (delta_1 + minus_three);
62 tmp_1 *= q_sort;
63 tmp_1 *= scaling_factor;
64 std::get<0>(accumulators) += tmp_1;
65
66 // Contribution (2)
67 auto tmp_2 = delta_2;
68 tmp_2 *= (delta_2 + minus_one);
69 tmp_2 *= (delta_2 + minus_two);
70 tmp_2 *= (delta_2 + minus_three);
71 tmp_2 *= q_sort;
72 tmp_2 *= scaling_factor;
73 std::get<1>(accumulators) += tmp_2;
74
75 // Contribution (3)
76 auto tmp_3 = delta_3;
77 tmp_3 *= (delta_3 + minus_one);
78 tmp_3 *= (delta_3 + minus_two);
79 tmp_3 *= (delta_3 + minus_three);
80 tmp_3 *= q_sort;
81 tmp_3 *= scaling_factor;
82 std::get<2>(accumulators) += tmp_3;
83
84 // Contribution (4)
85 auto tmp_4 = delta_4;
86 tmp_4 *= (delta_4 + minus_one);
87 tmp_4 *= (delta_4 + minus_two);
88 tmp_4 *= (delta_4 + minus_three);
89 tmp_4 *= q_sort;
90 tmp_4 *= scaling_factor;
91 std::get<3>(accumulators) += tmp_4;
92 };
93};
94
95template <typename FF> using GenPermSortRelation = Relation<GenPermSortRelationImpl<FF>>;
96
97} // namespace proof_system
Definition: gen_perm_sort_relation.hpp:6
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &, const FF &scaling_factor)
Expression for the generalized permutation sort gate.
Definition: gen_perm_sort_relation.hpp:33