Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
interactions_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
9
10namespace bb::avm2 {
11
13
20template <typename Settings_> struct lookup_settings : public Settings_ {
21 static constexpr size_t NUM_LOOKUP_TERMS = 1;
22 static constexpr size_t NUM_TABLE_TERMS = 1;
23 static constexpr std::array<uint8_t, NUM_LOOKUP_TERMS> LOOKUP_TYPES = { BASIC_LOOKUP };
24 static constexpr std::array<uint8_t, NUM_TABLE_TERMS> TABLE_TYPES = { BASIC_TABLE };
25 static constexpr std::array<size_t, NUM_LOOKUP_TERMS> LOOKUP_TERM_DEGREES = { 0 };
26 static constexpr std::array<size_t, NUM_TABLE_TERMS> TABLE_TERM_DEGREES = { 0 };
27 static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE = 2;
28
29 template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in)
30 {
31 return (in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)) == 1 ||
32 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)) == 1);
33 }
34
35 template <typename Accumulator, typename AllEntities>
36 static inline auto compute_inverse_exists(const AllEntities& in)
37 {
38 using View = typename Accumulator::View;
39 const auto is_operation = View(in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)));
40 const auto is_table_entry = View(in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)));
41 return (is_operation + is_table_entry - is_operation * is_table_entry);
42 }
43
44 template <typename AllEntities> static inline auto get_entities(AllEntities&& in)
45 {
46 return []<size_t... ISource, size_t... IDest>(
47 AllEntities&& in, std::index_sequence<ISource...>, std::index_sequence<IDest...>) {
48 return flat_tuple::forward_as_tuple(in.get(static_cast<ColumnAndShifts>(Settings_::INVERSES)),
49 in.get(static_cast<ColumnAndShifts>(Settings_::COUNTS)),
50 in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)),
51 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)),
52 in.get(Settings_::SRC_COLUMNS[ISource])...,
53 in.get(Settings_::DST_COLUMNS[IDest])...);
55 std::make_index_sequence<Settings_::SRC_COLUMNS.size()>{},
56 std::make_index_sequence<Settings_::DST_COLUMNS.size()>{});
57 }
58
59 template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in)
60 {
61 return get_entities(in);
62 }
63
64 template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in)
65 {
66 return get_entities(in);
67 }
68};
69
70template <typename FF_, typename Settings_> struct lookup_relation_base : public GenericLookupRelation<Settings_, FF_> {
71 using Settings = Settings_;
72 static constexpr std::string_view NAME = Settings::NAME;
73 static constexpr std::string_view RELATION_NAME = Settings::RELATION_NAME;
74
75 template <typename AllEntities> inline static bool skip(const AllEntities& in)
76 {
77 return (in.get(static_cast<ColumnAndShifts>(Settings::INVERSES))).is_zero();
78 }
79
80 // This obscures the definition from GenericLookupRelation, so that we can instantiate it somewhere else.
81 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
82 static void accumulate(ContainerOverSubrelations& accumulator,
83 const AllEntities& in,
84 const Parameters& params,
85 const FF_& scaling_factor);
86
87 static std::string get_subrelation_label(size_t index)
88 {
89 switch (index) {
90 case 0:
91 return "INVERSES_ARE_CORRECT";
92 case 1:
93 return "ACCUMULATION_IS_CORRECT";
94 default:
95 return std::to_string(index);
96 }
97 }
98};
99
101
102template <typename Settings_> struct permutation_settings : public Settings_ {
103 static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE = 2;
104
105 template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in)
106 {
107 return (in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)) == 1 ||
108 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)) == 1);
109 }
110
111 template <typename AllEntities> static inline auto get_entities(AllEntities&& in)
112 {
113 return []<size_t... ISource, size_t... IDest>(
114 AllEntities&& in, std::index_sequence<ISource...>, std::index_sequence<IDest...>) {
115 // 0. The polynomial containing the inverse products -> taken from the attributes
116 // 1. lhs selector
117 // 2. rhs selector
118 // 3.. + columns per set. lhs cols
119 // 3 + columns per set rhs cols
120 return flat_tuple::forward_as_tuple(in.get(static_cast<ColumnAndShifts>(Settings_::INVERSES)),
121 in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)),
122 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)),
123 in.get(Settings_::SRC_COLUMNS[ISource])...,
124 in.get(Settings_::DST_COLUMNS[IDest])...);
126 std::make_index_sequence<Settings_::SRC_COLUMNS.size()>{},
127 std::make_index_sequence<Settings_::DST_COLUMNS.size()>{});
128 }
129
130 template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in)
131 {
132 return get_entities(in);
133 }
134
135 template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in)
136 {
137 return get_entities(in);
138 }
139};
140
141template <typename FF_, typename Settings_>
143 using Settings = Settings_;
144 static constexpr std::string_view NAME = Settings::NAME;
145 static constexpr std::string_view RELATION_NAME = Settings::RELATION_NAME;
146
147 template <typename AllEntities> inline static bool skip(const AllEntities& in)
148 {
149 return (in.get(static_cast<ColumnAndShifts>(Settings::INVERSES))).is_zero();
150 }
151
152 static std::string get_subrelation_label(size_t index)
153 {
154 switch (index) {
155 case 0:
156 return "INVERSES_ARE_CORRECT";
157 case 1:
158 return "ACCUMULATION_IS_CORRECT";
159 default:
160 return std::to_string(index);
161 }
162 }
163};
164
165} // namespace bb::avm2
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
ColumnAndShifts
Definition columns.hpp:34
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
TUPLET_INLINE constexpr auto forward_as_tuple(T &&... a) noexcept
Definition tuplet.hpp:1067
static std::string get_subrelation_label(size_t index)
static bool skip(const AllEntities &in)
static constexpr std::string_view NAME
static void accumulate(ContainerOverSubrelations &accumulator, const AllEntities &in, const Parameters &params, const FF_ &scaling_factor)
static constexpr std::string_view RELATION_NAME
Settings to be passed ot GenericLookupRelationImpl.
static auto get_entities(AllEntities &&in)
static constexpr std::array< uint8_t, NUM_LOOKUP_TERMS > LOOKUP_TYPES
static constexpr size_t NUM_LOOKUP_TERMS
static auto compute_inverse_exists(const AllEntities &in)
static constexpr size_t NUM_TABLE_TERMS
static auto inverse_polynomial_is_computed_at_row(const AllEntities &in)
static auto get_nonconst_entities(AllEntities &in)
static constexpr std::array< uint8_t, NUM_TABLE_TERMS > TABLE_TYPES
static constexpr std::array< size_t, NUM_LOOKUP_TERMS > LOOKUP_TERM_DEGREES
static constexpr std::array< size_t, NUM_TABLE_TERMS > TABLE_TERM_DEGREES
static auto get_const_entities(const AllEntities &in)
static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE
static std::string get_subrelation_label(size_t index)
static constexpr std::string_view RELATION_NAME
static constexpr std::string_view NAME
static bool skip(const AllEntities &in)
static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE
static auto get_nonconst_entities(AllEntities &in)
static auto inverse_polynomial_is_computed_at_row(const AllEntities &in)
static auto get_const_entities(const AllEntities &in)
static auto get_entities(AllEntities &&in)