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