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
17
18namespace bb::avm2::tracegen {
19
29
31 public:
33
34 template <InteractionType type, typename... InteractionSettings> InteractionDefinition& add(auto&&... args)
35 {
36 std::string name = (std::string(InteractionSettings::NAME) + ...);
37 interactions[name] =
38 get_interaction_factory<type, InteractionSettings...>(std::forward<decltype(args)>(args)...);
39 return *this;
40 }
41
42 // Jobs for production (with shared index cache for efficient lookup index sharing).
44 // Stricter/more assertive jobs for testing.
46
47 std::unique_ptr<InteractionBuilderInterface> get_job(const std::string& interaction_name,
48 SharedIndexCache& cache) const;
49 std::unique_ptr<InteractionBuilderInterface> get_test_job(const std::string& interaction_name,
50 SharedIndexCache& cache) const;
51 template <typename InteractionSettings>
53 {
54 return get_test_job(std::string(InteractionSettings::NAME), cache);
55 }
56
57 private:
58 // Factory takes (strict, cache) and returns the interaction builder.
60 std::unordered_map<std::string, Factory> interactions;
61
62 template <InteractionType type, typename... InteractionSettings>
63 static Factory get_interaction_factory(auto&&... args)
64 {
65 if constexpr (type == InteractionType::LookupGeneric) {
66 return [args...](bool, SharedIndexCache& cache) {
67 // This class always checks.
68 return std::make_unique<LookupIntoDynamicTableGeneric<InteractionSettings...>>(cache, args...);
69 };
70 } else if constexpr (type == InteractionType::LookupIntoBitwise) {
71 return [args...](bool strict, SharedIndexCache&) {
72 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoBitwise<InteractionSettings...>>>(args...)
73 : std::make_unique<LookupIntoBitwise<InteractionSettings...>>(args...);
74 };
75 } else if constexpr (type == InteractionType::LookupIntoIndexedByRow) {
76 return [args...](bool strict, SharedIndexCache&) {
77 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoIndexedByRow<InteractionSettings...>>>(
78 args...)
79 : std::make_unique<LookupIntoIndexedByRow<InteractionSettings...>>(args...);
80 };
81 } else if constexpr (type == InteractionType::LookupIntoPDecomposition) {
82 return [args...](bool strict, SharedIndexCache&) {
83 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoPDecomposition<InteractionSettings...>>>(
84 args...)
85 : std::make_unique<LookupIntoPDecomposition<InteractionSettings...>>(args...);
86 };
87 } else if constexpr (type == InteractionType::LookupSequential) {
88 return [args...](bool, SharedIndexCache&) {
89 // This class always checks.
90 return std::make_unique<LookupIntoDynamicTableSequential<InteractionSettings...>>(args...);
91 };
92 } else if constexpr (type == InteractionType::Permutation) {
93 return [args...](bool strict, SharedIndexCache&) {
94 return strict ? std::make_unique<CheckingPermutationBuilder<InteractionSettings...>>(args...)
95 : std::make_unique<PermutationBuilder<InteractionSettings...>>(args...);
96 };
97 } else if constexpr (type == InteractionType::MultiPermutation) {
98 return [args...](bool, SharedIndexCache&) {
99 return std::make_unique<MultiPermutationBuilder<InteractionSettings...>>(args...);
100 };
101 } else {
102 throw std::runtime_error("Interaction type not supported: " + std::to_string(static_cast<int>(type)));
103 }
104 }
105
106 const Factory& get_job_internal(const std::string& interaction_name) const;
107};
108
109} // namespace bb::avm2::tracegen
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)