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
14template <typename Settings_> struct lookup_settings : public Settings_ {
15 static constexpr size_t READ_TERMS = 1;
16 static constexpr size_t WRITE_TERMS = 1;
17 static constexpr size_t READ_TERM_TYPES[READ_TERMS] = { 0 };
18 static constexpr size_t WRITE_TERM_TYPES[WRITE_TERMS] = { 0 };
19 static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE = 4;
20 static constexpr size_t READ_TERM_DEGREE = 0;
21 static constexpr size_t WRITE_TERM_DEGREE = 0;
22
23 template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in)
24 {
25 return (in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)) == 1 ||
26 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)) == 1);
27 }
28
29 template <typename Accumulator, typename AllEntities>
30 static inline auto compute_inverse_exists(const AllEntities& in)
31 {
32 using View = typename Accumulator::View;
33 const auto is_operation = View(in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)));
34 const auto is_table_entry = View(in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)));
35 return (is_operation + is_table_entry - is_operation * is_table_entry);
36 }
37
38 template <typename AllEntities> static inline auto get_entities(AllEntities&& in)
39 {
40 return []<size_t... ISource, size_t... IDest>(
41 AllEntities&& in, std::index_sequence<ISource...>, std::index_sequence<IDest...>) {
42 return flat_tuple::forward_as_tuple(in.get(static_cast<ColumnAndShifts>(Settings_::INVERSES)),
43 in.get(static_cast<ColumnAndShifts>(Settings_::COUNTS)),
44 in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)),
45 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)),
46 in.get(Settings_::SRC_COLUMNS[ISource])...,
47 in.get(Settings_::DST_COLUMNS[IDest])...);
49 std::make_index_sequence<Settings_::SRC_COLUMNS.size()>{},
50 std::make_index_sequence<Settings_::DST_COLUMNS.size()>{});
51 }
52
53 template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in)
54 {
55 return get_entities(in);
56 }
57
58 template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in)
59 {
60 return get_entities(in);
61 }
62};
63
64template <typename FF_, typename Settings_> struct lookup_relation_base : public GenericLookupRelation<Settings_, FF_> {
65 using Settings = Settings_;
66 static constexpr std::string_view NAME = Settings::NAME;
67 static constexpr std::string_view RELATION_NAME = Settings::RELATION_NAME;
68
69 template <typename AllEntities> inline static bool skip(const AllEntities& in)
70 {
71 return (in.get(static_cast<ColumnAndShifts>(Settings::INVERSES))).is_zero();
72 }
73
74 // This obscures the definition from GenericLookupRelation, so that we can instantiate it somewhere else.
75 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
76 static void accumulate(ContainerOverSubrelations& accumulator,
77 const AllEntities& in,
78 const Parameters& params,
79 const FF_& scaling_factor);
80
81 static std::string get_subrelation_label(size_t index)
82 {
83 switch (index) {
84 case 0:
85 return "INVERSES_ARE_CORRECT";
86 case 1:
87 return "ACCUMULATION_IS_CORRECT";
88 default:
89 return std::to_string(index);
90 }
91 }
92};
93
95
96template <typename Settings_> struct permutation_settings : public Settings_ {
97 template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in)
98 {
99 return (in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)) == 1 ||
100 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)) == 1);
101 }
102
103 template <typename AllEntities> static inline auto get_entities(AllEntities&& in)
104 {
105 return []<size_t... ISource, size_t... IDest>(
106 AllEntities&& in, std::index_sequence<ISource...>, std::index_sequence<IDest...>) {
107 // 0. The polynomial containing the inverse products -> taken from the attributes
108 // 1. The polynomial enabling the relation (the selector)
109 // 2. lhs selector
110 // 3. rhs selector
111 // 4.. + columns per set. lhs cols
112 // 4 + columns per set rhs cols
113 return flat_tuple::forward_as_tuple(in.get(static_cast<ColumnAndShifts>(Settings_::INVERSES)),
114 in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)),
115 in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)),
116 in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)),
117 in.get(Settings_::SRC_COLUMNS[ISource])...,
118 in.get(Settings_::DST_COLUMNS[IDest])...);
120 std::make_index_sequence<Settings_::SRC_COLUMNS.size()>{},
121 std::make_index_sequence<Settings_::DST_COLUMNS.size()>{});
122 }
123
124 template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in)
125 {
126 return get_entities(in);
127 }
128
129 template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in)
130 {
131 return get_entities(in);
132 }
133};
134
135template <typename FF_, typename Settings_>
137 using Settings = Settings_;
138 static constexpr std::string_view NAME = Settings::NAME;
139 static constexpr std::string_view RELATION_NAME = Settings::RELATION_NAME;
140
141 template <typename AllEntities> inline static bool skip(const AllEntities& in)
142 {
143 return (in.get(static_cast<ColumnAndShifts>(Settings::INVERSES))).is_zero();
144 }
145
146 static std::string get_subrelation_label(size_t index)
147 {
148 switch (index) {
149 case 0:
150 return "INVERSES_ARE_CORRECT";
151 case 1:
152 return "ACCUMULATION_IS_CORRECT";
153 default:
154 return std::to_string(index);
155 }
156 }
157};
158
159} // namespace bb::avm2
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
This file contains the template for the generic lookup that can be specialized to enforce various loo...
This file contains the template for the generic permutation that can be specialized to enforce variou...
ColumnAndShifts
Definition columns.hpp:35
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
static constexpr size_t WRITE_TERM_DEGREE
static auto get_entities(AllEntities &&in)
static auto compute_inverse_exists(const AllEntities &in)
static constexpr size_t WRITE_TERMS
static auto inverse_polynomial_is_computed_at_row(const AllEntities &in)
static constexpr size_t READ_TERM_TYPES[READ_TERMS]
static auto get_nonconst_entities(AllEntities &in)
static constexpr size_t WRITE_TERM_TYPES[WRITE_TERMS]
static constexpr size_t READ_TERM_DEGREE
static auto get_const_entities(const AllEntities &in)
static constexpr size_t READ_TERMS
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 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)