Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
interaction_def.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <unordered_map>
6#include <vector>
7
18
19namespace bb::avm2::tracegen {
20
30
32 public:
34
35 template <InteractionType type, typename... InteractionSettings> InteractionDefinition& add(auto&&... args)
36 {
37 std::string name = (std::string(InteractionSettings::NAME) + ...);
38 auto [_, inserted] = interactions.emplace(
39 name, get_interaction_factory<type, InteractionSettings...>(std::forward<decltype(args)>(args)...));
40
41 // Safeguard detecting a collision in the interaction names (we do not use separators to have an injective
42 // serialization).
43 BB_ASSERT_DEBUG(inserted, "InteractionDefinition::add: collision in interaction name: " + name);
44 return *this;
45 }
46
47 // Jobs for production (with shared index cache for efficient lookup index sharing).
49 // Stricter/more assertive jobs for testing.
51
52 std::unique_ptr<InteractionBuilderInterface> get_job(const std::string& interaction_name,
53 SharedIndexCache& cache) const;
54 std::unique_ptr<InteractionBuilderInterface> get_test_job(const std::string& interaction_name,
55 SharedIndexCache& cache) const;
56 template <typename InteractionSettings>
58 {
59 return get_test_job(std::string(InteractionSettings::NAME), cache);
60 }
61
62 private:
63 // Factory takes (strict, cache) and returns the interaction builder.
65 std::unordered_map<std::string, Factory> interactions;
66
67 template <InteractionType type, typename... InteractionSettings>
68 static Factory get_interaction_factory(auto&&... args)
69 {
70 if constexpr (type == InteractionType::LookupGeneric) {
71 return [args...](bool, SharedIndexCache& cache) {
72 // This class always checks.
73 return std::make_unique<LookupIntoDynamicTableGeneric<InteractionSettings...>>(cache, args...);
74 };
75 } else if constexpr (type == InteractionType::LookupIntoBitwise) {
76 return [args...](bool strict, SharedIndexCache&) {
77 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoBitwise<InteractionSettings...>>>(args...)
78 : std::make_unique<LookupIntoBitwise<InteractionSettings...>>(args...);
79 };
80 } else if constexpr (type == InteractionType::LookupIntoIndexedByRow) {
81 return [args...](bool strict, SharedIndexCache&) {
82 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoIndexedByRow<InteractionSettings...>>>(
83 args...)
84 : std::make_unique<LookupIntoIndexedByRow<InteractionSettings...>>(args...);
85 };
86 } else if constexpr (type == InteractionType::LookupIntoPDecomposition) {
87 return [args...](bool strict, SharedIndexCache&) {
88 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoPDecomposition<InteractionSettings...>>>(
89 args...)
90 : std::make_unique<LookupIntoPDecomposition<InteractionSettings...>>(args...);
91 };
92 } else if constexpr (type == InteractionType::LookupSequential) {
93 return [args...](bool, SharedIndexCache&) {
94 // This class always checks.
95 return std::make_unique<LookupIntoDynamicTableSequential<InteractionSettings...>>(args...);
96 };
97 } else if constexpr (type == InteractionType::Permutation) {
98 return [args...](bool strict, SharedIndexCache&) {
99 return strict ? std::make_unique<CheckingPermutationBuilder<InteractionSettings...>>(args...)
100 : std::make_unique<PermutationBuilder<InteractionSettings...>>(args...);
101 };
102 } else if constexpr (type == InteractionType::MultiPermutation) {
103 return [args...](bool, SharedIndexCache&) {
104 return std::make_unique<MultiPermutationBuilder<InteractionSettings...>>(args...);
105 };
106 } else {
107 throw std::runtime_error("Interaction type not supported: " + std::to_string(static_cast<int>(type)));
108 }
109 }
110
111 const Factory& get_job_internal(const std::string& interaction_name) const;
112};
113
114} // namespace bb::avm2::tracegen
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
InteractionDefinition & add(auto &&... args)
std::vector< std::unique_ptr< InteractionBuilderInterface > > get_all_test_jobs(SharedIndexCache &cache) const
std::unique_ptr< InteractionBuilderInterface > get_test_job(SharedIndexCache &cache) const
std::vector< std::unique_ptr< InteractionBuilderInterface > > get_all_jobs(SharedIndexCache &cache) const
std::unique_ptr< InteractionBuilderInterface > get_test_job(const std::string &interaction_name, SharedIndexCache &cache) const
const Factory & get_job_internal(const std::string &interaction_name) const
std::unique_ptr< InteractionBuilderInterface > get_job(const std::string &interaction_name, SharedIndexCache &cache) const
std::function< std::unique_ptr< InteractionBuilderInterface >(bool strict, SharedIndexCache &cache)> Factory
std::unordered_map< std::string, Factory > interactions
static Factory get_interaction_factory(auto &&... args)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)