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
9
#include "
barretenberg/common/assert.hpp
"
10
#include "
barretenberg/vm2/tracegen/lib/interaction_builder.hpp
"
11
#include "
barretenberg/vm2/tracegen/lib/lookup_builder.hpp
"
12
#include "
barretenberg/vm2/tracegen/lib/lookup_into_bitwise.hpp
"
13
#include "
barretenberg/vm2/tracegen/lib/lookup_into_indexed_by_row.hpp
"
14
#include "
barretenberg/vm2/tracegen/lib/lookup_into_p_decomposition.hpp
"
15
#include "
barretenberg/vm2/tracegen/lib/multi_permutation_builder.hpp
"
16
#include "
barretenberg/vm2/tracegen/lib/permutation_builder.hpp
"
17
#include "
barretenberg/vm2/tracegen/lib/shared_index_cache.hpp
"
18
#include "
barretenberg/vm2/tracegen/lib/test_interaction_builder.hpp
"
19
20
namespace
bb::avm2::tracegen
{
21
22
enum class
InteractionType
: uint8_t {
23
LookupGeneric
,
24
LookupSequential
,
25
LookupIntoBitwise
,
26
LookupIntoIndexedByRow
,
27
LookupIntoPDecomposition
,
28
Permutation
,
29
MultiPermutation
,
30
};
31
32
class
InteractionDefinition
{
33
public
:
34
InteractionDefinition
() =
default
;
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).
49
std::vector<std::unique_ptr<InteractionBuilderInterface>
>
get_all_jobs
(
SharedIndexCache
& cache)
const
;
50
// Stricter/more assertive jobs for testing.
51
std::vector<std::unique_ptr<InteractionBuilderInterface>
>
get_all_test_jobs
(
SharedIndexCache
& cache)
const
;
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>
58
std::unique_ptr<InteractionBuilderInterface>
get_test_job
(
SharedIndexCache
& cache)
const
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.
65
using
Factory
=
std::function<std::unique_ptr<InteractionBuilderInterface>
(
bool
strict,
SharedIndexCache
& cache)>;
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
assert.hpp
BB_ASSERT_DEBUG
#define BB_ASSERT_DEBUG(expression,...)
Definition
assert.hpp:55
bb::avm2::tracegen::AddChecksToBuilder
Definition
test_interaction_builder.hpp:17
bb::avm2::tracegen::CheckingPermutationBuilder
Definition
test_interaction_builder.hpp:56
bb::avm2::tracegen::InteractionDefinition
Definition
interaction_def.hpp:32
bb::avm2::tracegen::InteractionDefinition::add
InteractionDefinition & add(auto &&... args)
Definition
interaction_def.hpp:36
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:18
bb::avm2::tracegen::InteractionDefinition::get_test_job
std::unique_ptr< InteractionBuilderInterface > get_test_job(SharedIndexCache &cache) const
Definition
interaction_def.hpp:58
bb::avm2::tracegen::InteractionDefinition::get_all_jobs
std::vector< std::unique_ptr< InteractionBuilderInterface > > get_all_jobs(SharedIndexCache &cache) const
Definition
interaction_def.cpp:7
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:35
bb::avm2::tracegen::InteractionDefinition::get_job_internal
const Factory & get_job_internal(const std::string &interaction_name) const
Definition
interaction_def.cpp:41
bb::avm2::tracegen::InteractionDefinition::get_job
std::unique_ptr< InteractionBuilderInterface > get_job(const std::string &interaction_name, SharedIndexCache &cache) const
Definition
interaction_def.cpp:29
bb::avm2::tracegen::InteractionDefinition::Factory
std::function< std::unique_ptr< InteractionBuilderInterface >(bool strict, SharedIndexCache &cache)> Factory
Definition
interaction_def.hpp:65
bb::avm2::tracegen::InteractionDefinition::interactions
std::unordered_map< std::string, Factory > interactions
Definition
interaction_def.hpp:66
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:69
bb::avm2::tracegen::LookupIntoBitwise
Definition
lookup_into_bitwise.hpp:9
bb::avm2::tracegen::LookupIntoDynamicTableGeneric
Definition
lookup_builder.hpp:76
bb::avm2::tracegen::LookupIntoDynamicTableSequential
Definition
lookup_builder.hpp:165
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:34
bb::avm2::tracegen::PermutationBuilder
Definition
permutation_builder.hpp:8
bb::avm2::tracegen::SharedIndexCache
Definition
shared_index_cache.hpp:36
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:10
bb::avm2::tracegen::InteractionType
InteractionType
Definition
interaction_def.hpp:22
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
name
std::string name
Definition
opcode_spam.test.cpp:725
permutation_builder.hpp
shared_index_cache.hpp
test_interaction_builder.hpp
src
barretenberg
vm2
tracegen
lib
interaction_def.hpp
Generated by
1.9.8