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 <cstdint>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
19
20namespace bb::avm2::tracegen {
21
31
33 public:
35
36 template <InteractionType type, typename... InteractionSettings> InteractionDefinition& add(auto&&... args)
37 {
38 std::string name = (std::string(InteractionSettings::NAME) + ...);
39 auto [_, inserted] = interactions.emplace(
40 name, get_interaction_factory<type, InteractionSettings...>(std::forward<decltype(args)>(args)...));
41
42 // Safeguard detecting a collision in the interaction names (we do not use separators to have an injective
43 // serialization).
44 BB_ASSERT_DEBUG(inserted, "InteractionDefinition::add: collision in interaction name: " + name);
45 return *this;
46 }
47
48 // Jobs for production (with shared index cache for efficient lookup index sharing).
50 // Stricter/more assertive jobs for testing.
52
53 std::unique_ptr<InteractionBuilderInterface> get_job(const std::string& interaction_name,
54 SharedIndexCache& cache) const;
55 std::unique_ptr<InteractionBuilderInterface> get_test_job(const std::string& interaction_name,
56 SharedIndexCache& cache) const;
57 template <typename InteractionSettings>
59 {
60 return get_test_job(std::string(InteractionSettings::NAME), cache);
61 }
62
63 private:
64 // Factory takes (strict, cache) and returns the interaction builder.
66 std::unordered_map<std::string, Factory> interactions;
67
68 template <InteractionType type, typename... InteractionSettings>
69 static Factory get_interaction_factory(auto&&... args)
70 {
71 if constexpr (type == InteractionType::LookupGeneric) {
72 return [args...](bool, SharedIndexCache& cache) {
73 // This class always checks.
74 return std::make_unique<LookupIntoDynamicTableGeneric<InteractionSettings...>>(cache, args...);
75 };
76 } else if constexpr (type == InteractionType::LookupIntoBitwise) {
77 return [args...](bool strict, SharedIndexCache&) {
78 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoBitwise<InteractionSettings...>>>(args...)
79 : std::make_unique<LookupIntoBitwise<InteractionSettings...>>(args...);
80 };
81 } else if constexpr (type == InteractionType::LookupIntoIndexedByRow) {
82 return [args...](bool strict, SharedIndexCache&) {
83 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoIndexedByRow<InteractionSettings...>>>(
84 args...)
85 : std::make_unique<LookupIntoIndexedByRow<InteractionSettings...>>(args...);
86 };
87 } else if constexpr (type == InteractionType::LookupIntoPDecomposition) {
88 return [args...](bool strict, SharedIndexCache&) {
89 return strict ? std::make_unique<AddChecksToBuilder<LookupIntoPDecomposition<InteractionSettings...>>>(
90 args...)
91 : std::make_unique<LookupIntoPDecomposition<InteractionSettings...>>(args...);
92 };
93 } else if constexpr (type == InteractionType::LookupSequential) {
94 return [args...](bool, SharedIndexCache&) {
95 // This class always checks.
96 return std::make_unique<LookupIntoDynamicTableSequential<InteractionSettings...>>(args...);
97 };
98 } else if constexpr (type == InteractionType::Permutation) {
99 return [args...](bool strict, SharedIndexCache&) {
100 return strict ? std::make_unique<CheckingPermutationBuilder<InteractionSettings...>>(args...)
101 : std::make_unique<PermutationBuilder<InteractionSettings...>>(args...);
102 };
103 } else if constexpr (type == InteractionType::MultiPermutation) {
104 return [args...](bool, SharedIndexCache&) {
105 return std::make_unique<MultiPermutationBuilder<InteractionSettings...>>(args...);
106 };
107 } else {
108 throw std::runtime_error("Interaction type not supported: " + std::to_string(static_cast<int>(type)));
109 }
110 }
111
112 const Factory& get_job_internal(const std::string& interaction_name) const;
113};
114
115} // 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)
std::string name