|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | + |
| 5 | +#include "barretenberg/relations/generic_lookup/generic_lookup_relation.hpp" |
| 6 | +#include "barretenberg/vm2/generated/columns.hpp" |
| 7 | + |
| 8 | +namespace bb::avm2 { |
| 9 | + |
| 10 | +template <typename Settings_> struct lookup_settings : public Settings_ { |
| 11 | + static constexpr size_t READ_TERMS = 1; |
| 12 | + static constexpr size_t WRITE_TERMS = 1; |
| 13 | + static constexpr size_t READ_TERM_TYPES[READ_TERMS] = { 0 }; |
| 14 | + static constexpr size_t WRITE_TERM_TYPES[WRITE_TERMS] = { 0 }; |
| 15 | + static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE = 4; |
| 16 | + static constexpr size_t READ_TERM_DEGREE = 0; |
| 17 | + static constexpr size_t WRITE_TERM_DEGREE = 0; |
| 18 | + |
| 19 | + template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in) |
| 20 | + { |
| 21 | + return (in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)) == 1 || |
| 22 | + in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)) == 1); |
| 23 | + } |
| 24 | + |
| 25 | + template <typename Accumulator, typename AllEntities> |
| 26 | + static inline auto compute_inverse_exists(const AllEntities& in) |
| 27 | + { |
| 28 | + using View = typename Accumulator::View; |
| 29 | + const auto is_operation = View(in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR))); |
| 30 | + const auto is_table_entry = View(in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR))); |
| 31 | + return (is_operation + is_table_entry - is_operation * is_table_entry); |
| 32 | + } |
| 33 | + |
| 34 | + template <typename AllEntities> static inline auto get_entities(AllEntities&& in) |
| 35 | + { |
| 36 | + return []<size_t... ISource, size_t... IDest>( |
| 37 | + AllEntities&& in, std::index_sequence<ISource...>, std::index_sequence<IDest...>) { |
| 38 | + return std::forward_as_tuple(in.get(static_cast<ColumnAndShifts>(Settings_::INVERSES)), |
| 39 | + in.get(static_cast<ColumnAndShifts>(Settings_::COUNTS)), |
| 40 | + in.get(static_cast<ColumnAndShifts>(Settings_::SRC_SELECTOR)), |
| 41 | + in.get(static_cast<ColumnAndShifts>(Settings_::DST_SELECTOR)), |
| 42 | + in.get(Settings_::SRC_COLUMNS[ISource])..., |
| 43 | + in.get(Settings_::DST_COLUMNS[IDest])...); |
| 44 | + }(std::forward<AllEntities>(in), |
| 45 | + std::make_index_sequence<Settings_::SRC_COLUMNS.size()>{}, |
| 46 | + std::make_index_sequence<Settings_::DST_COLUMNS.size()>{}); |
| 47 | + } |
| 48 | + |
| 49 | + template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in) |
| 50 | + { |
| 51 | + return get_entities(in); |
| 52 | + } |
| 53 | + |
| 54 | + template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in) |
| 55 | + { |
| 56 | + return get_entities(in); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +template <typename FF_, typename Settings_> struct lookup_relation_base : public GenericLookupRelation<Settings_, FF_> { |
| 61 | + using Settings = Settings_; |
| 62 | + static constexpr std::string_view NAME = Settings::NAME; |
| 63 | + static constexpr std::string_view RELATION_NAME = Settings::RELATION_NAME; |
| 64 | + |
| 65 | + template <typename AllEntities> inline static bool skip(const AllEntities& in) |
| 66 | + { |
| 67 | + auto inverse = std::get<0>(Settings::get_entities(in)); |
| 68 | + return inverse.is_zero(); |
| 69 | + } |
| 70 | + |
| 71 | + static std::string get_subrelation_label(size_t index) |
| 72 | + { |
| 73 | + switch (index) { |
| 74 | + case 0: |
| 75 | + return "INVERSES_ARE_CORRECT"; |
| 76 | + case 1: |
| 77 | + return "ACCUMULATION_IS_CORRECT"; |
| 78 | + default: |
| 79 | + return std::to_string(index); |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +} // namespace bb::avm2 |
0 commit comments