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
8
#include "
barretenberg/common/assert.hpp
"
9
#include "
barretenberg/vm2/tracegen/lib/interaction_builder.hpp
"
10
#include "
barretenberg/vm2/tracegen/lib/lookup_builder.hpp
"
11
#include "
barretenberg/vm2/tracegen/lib/lookup_into_bitwise.hpp
"
12
#include "
barretenberg/vm2/tracegen/lib/lookup_into_indexed_by_row.hpp
"
13
#include "
barretenberg/vm2/tracegen/lib/lookup_into_p_decomposition.hpp
"
14
#include "
barretenberg/vm2/tracegen/lib/multi_permutation_builder.hpp
"
15
#include "
barretenberg/vm2/tracegen/lib/permutation_builder.hpp
"
16
#include "
barretenberg/vm2/tracegen/lib/shared_index_cache.hpp
"
17
#include "
barretenberg/vm2/tracegen/lib/test_interaction_builder.hpp
"
18
19
namespace
bb::avm2::tracegen
{
20
21
enum class
InteractionType
{
22
LookupGeneric
,
23
LookupSequential
,
24
LookupIntoBitwise
,
25
LookupIntoIndexedByRow
,
26
LookupIntoPDecomposition
,
27
Permutation
,
28
MultiPermutation
,
29
};
30
31
class
InteractionDefinition
{
32
public
:
33
InteractionDefinition
() =
default
;
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).
48
std::vector<std::unique_ptr<InteractionBuilderInterface>
>
get_all_jobs
(
SharedIndexCache
& cache)
const
;
49
// Stricter/more assertive jobs for testing.
50
std::vector<std::unique_ptr<InteractionBuilderInterface>
>
get_all_test_jobs
(
SharedIndexCache
& cache)
const
;
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>
57
std::unique_ptr<InteractionBuilderInterface>
get_test_job
(
SharedIndexCache
& cache)
const
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.
64
using
Factory
=
std::function<std::unique_ptr<InteractionBuilderInterface>
(
bool
strict,
SharedIndexCache
& cache)>;
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
assert.hpp
BB_ASSERT_DEBUG
#define BB_ASSERT_DEBUG(expression,...)
Definition
assert.hpp:55
bb::avm2::tracegen::AddChecksToBuilder
Definition
test_interaction_builder.hpp:19
bb::avm2::tracegen::CheckingPermutationBuilder
Definition
test_interaction_builder.hpp:54
bb::avm2::tracegen::InteractionDefinition
Definition
interaction_def.hpp:31
bb::avm2::tracegen::InteractionDefinition::add
InteractionDefinition & add(auto &&... args)
Definition
interaction_def.hpp:35
bb::avm2::tracegen::InteractionDefinition::get_all_test_jobs
std::vector< std::unique_ptr< InteractionBuilderInterface > > get_all_test_jobs(SharedIndexCache &cache) const
Definition
interaction_def.cpp:16
bb::avm2::tracegen::InteractionDefinition::get_test_job
std::unique_ptr< InteractionBuilderInterface > get_test_job(SharedIndexCache &cache) const
Definition
interaction_def.hpp:57
bb::avm2::tracegen::InteractionDefinition::get_all_jobs
std::vector< std::unique_ptr< InteractionBuilderInterface > > get_all_jobs(SharedIndexCache &cache) const
Definition
interaction_def.cpp:5
bb::avm2::tracegen::InteractionDefinition::get_test_job
std::unique_ptr< InteractionBuilderInterface > get_test_job(const std::string &interaction_name, SharedIndexCache &cache) const
Definition
interaction_def.cpp:33
bb::avm2::tracegen::InteractionDefinition::get_job_internal
const Factory & get_job_internal(const std::string &interaction_name) const
Definition
interaction_def.cpp:39
bb::avm2::tracegen::InteractionDefinition::get_job
std::unique_ptr< InteractionBuilderInterface > get_job(const std::string &interaction_name, SharedIndexCache &cache) const
Definition
interaction_def.cpp:27
bb::avm2::tracegen::InteractionDefinition::Factory
std::function< std::unique_ptr< InteractionBuilderInterface >(bool strict, SharedIndexCache &cache)> Factory
Definition
interaction_def.hpp:64
bb::avm2::tracegen::InteractionDefinition::interactions
std::unordered_map< std::string, Factory > interactions
Definition
interaction_def.hpp:65
bb::avm2::tracegen::InteractionDefinition::InteractionDefinition
InteractionDefinition()=default
bb::avm2::tracegen::InteractionDefinition::get_interaction_factory
static Factory get_interaction_factory(auto &&... args)
Definition
interaction_def.hpp:68
bb::avm2::tracegen::LookupIntoBitwise
Definition
lookup_into_bitwise.hpp:9
bb::avm2::tracegen::LookupIntoDynamicTableGeneric
Definition
lookup_builder.hpp:77
bb::avm2::tracegen::LookupIntoDynamicTableSequential
Definition
lookup_builder.hpp:166
bb::avm2::tracegen::LookupIntoIndexedByRow
Definition
lookup_into_indexed_by_row.hpp:18
bb::avm2::tracegen::LookupIntoPDecomposition
Definition
lookup_into_p_decomposition.hpp:12
bb::avm2::tracegen::MultiPermutationBuilder
Definition
multi_permutation_builder.hpp:35
bb::avm2::tracegen::PermutationBuilder
Definition
permutation_builder.hpp:8
bb::avm2::tracegen::SharedIndexCache
Definition
shared_index_cache.hpp:35
GetEnvVarMutationOptions::type
@ type
interaction_builder.hpp
lookup_builder.hpp
lookup_into_bitwise.hpp
lookup_into_indexed_by_row.hpp
lookup_into_p_decomposition.hpp
multi_permutation_builder.hpp
bb::avm2::tracegen
Definition
full_row.hpp:9
bb::avm2::tracegen::InteractionType
InteractionType
Definition
interaction_def.hpp:21
bb::avm2::tracegen::InteractionType::LookupIntoIndexedByRow
@ LookupIntoIndexedByRow
bb::avm2::tracegen::InteractionType::LookupGeneric
@ LookupGeneric
bb::avm2::tracegen::InteractionType::MultiPermutation
@ MultiPermutation
bb::avm2::tracegen::InteractionType::LookupSequential
@ LookupSequential
bb::avm2::tracegen::InteractionType::LookupIntoPDecomposition
@ LookupIntoPDecomposition
bb::avm2::tracegen::InteractionType::LookupIntoBitwise
@ LookupIntoBitwise
bb::avm2::tracegen::InteractionType::Permutation
@ Permutation
std::get
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition
tuple.hpp:13
std::to_string
std::string to_string(bb::avm2::ValueTag tag)
Definition
tagged_value.cpp:402
permutation_builder.hpp
shared_index_cache.hpp
test_interaction_builder.hpp
src
barretenberg
vm2
tracegen
lib
interaction_def.hpp
Generated by
1.9.8