Skip to content

Commit 8eabb89

Browse files
committed
chore(avm): suppport idiom with multipermutations
1 parent 7e0b229 commit 8eabb89

File tree

5 files changed

+80
-70
lines changed

5 files changed

+80
-70
lines changed

barretenberg/cpp/src/barretenberg/vm2/tracegen/lib/interaction_def.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ std::vector<std::unique_ptr<InteractionBuilderInterface>> InteractionDefinition:
66
{
77
std::vector<std::unique_ptr<InteractionBuilderInterface>> jobs;
88
jobs.reserve(interactions.size());
9-
for (const auto key : std::ranges::views::keys(interactions)) {
9+
for (const auto& key : std::ranges::views::keys(interactions)) {
1010
jobs.push_back(get_job(key));
1111
}
1212
return jobs;
@@ -16,24 +16,24 @@ std::vector<std::unique_ptr<InteractionBuilderInterface>> InteractionDefinition:
1616
{
1717
std::vector<std::unique_ptr<InteractionBuilderInterface>> jobs;
1818
jobs.reserve(interactions.size());
19-
for (const auto key : std::ranges::views::keys(interactions)) {
19+
for (const auto& key : std::ranges::views::keys(interactions)) {
2020
jobs.push_back(get_test_job(key));
2121
}
2222
return jobs;
2323
}
2424

25-
std::unique_ptr<InteractionBuilderInterface> InteractionDefinition::get_job(std::string_view interaction_name) const
25+
std::unique_ptr<InteractionBuilderInterface> InteractionDefinition::get_job(const std::string& interaction_name) const
2626
{
2727
return get_job_internal(interaction_name)(/*strict=*/false);
2828
}
2929

3030
std::unique_ptr<InteractionBuilderInterface> InteractionDefinition::get_test_job(
31-
std::string_view interaction_name) const
31+
const std::string& interaction_name) const
3232
{
3333
return get_job_internal(interaction_name)(/*strict=*/true);
3434
}
3535

36-
const InteractionDefinition::Factory& InteractionDefinition::get_job_internal(std::string_view interaction_name) const
36+
const InteractionDefinition::Factory& InteractionDefinition::get_job_internal(const std::string& interaction_name) const
3737
{
3838
auto it = interactions.find(interaction_name);
3939
if (it == interactions.end()) {
Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <memory>
4-
#include <string_view>
4+
#include <string>
55
#include <unordered_map>
66
#include <vector>
77

@@ -10,6 +10,7 @@
1010
#include "barretenberg/vm2/tracegen/lib/lookup_into_bitwise.hpp"
1111
#include "barretenberg/vm2/tracegen/lib/lookup_into_indexed_by_clk.hpp"
1212
#include "barretenberg/vm2/tracegen/lib/lookup_into_p_decomposition.hpp"
13+
#include "barretenberg/vm2/tracegen/lib/multi_permutation_builder.hpp"
1314
#include "barretenberg/vm2/tracegen/lib/permutation_builder.hpp"
1415
#include "barretenberg/vm2/tracegen/lib/test_interaction_builder.hpp"
1516

@@ -22,15 +23,26 @@ enum class InteractionType {
2223
LookupIntoIndexedByClk,
2324
LookupIntoPDecomposition,
2425
Permutation,
26+
MultiPermutation,
2527
};
2628

2729
class InteractionDefinition {
2830
public:
2931
InteractionDefinition() = default;
3032

31-
template <typename InteractionSettings, InteractionType type> InteractionDefinition& add()
33+
// Old format with InteractionSettings first. TODO: Migrate.
34+
template <typename InteractionSettings, InteractionType type> InteractionDefinition& add(auto&&... args)
3235
{
33-
interactions[InteractionSettings::NAME] = get_interaction_factory<InteractionSettings, type>();
36+
std::string name = std::string(InteractionSettings::NAME);
37+
interactions[name] = get_interaction_factory<type, InteractionSettings>(std::forward<decltype(args)>(args)...);
38+
return *this;
39+
}
40+
41+
template <InteractionType type, typename... InteractionSettings> InteractionDefinition& add(auto&&... args)
42+
{
43+
std::string name = (std::string(InteractionSettings::NAME) + ...);
44+
interactions[name] =
45+
get_interaction_factory<type, InteractionSettings...>(std::forward<decltype(args)>(args)...);
3446
return *this;
3547
}
3648

@@ -39,55 +51,61 @@ class InteractionDefinition {
3951
// Stricter/more assertive jobs for testing.
4052
std::vector<std::unique_ptr<InteractionBuilderInterface>> get_all_test_jobs() const;
4153

42-
std::unique_ptr<InteractionBuilderInterface> get_job(std::string_view interaction_name) const;
43-
std::unique_ptr<InteractionBuilderInterface> get_test_job(std::string_view interaction_name) const;
54+
std::unique_ptr<InteractionBuilderInterface> get_job(const std::string& interaction_name) const;
55+
std::unique_ptr<InteractionBuilderInterface> get_test_job(const std::string& interaction_name) const;
4456
template <typename InteractionSettings> std::unique_ptr<InteractionBuilderInterface> get_test_job() const
4557
{
46-
return get_test_job(InteractionSettings::NAME);
58+
return get_test_job(std::string(InteractionSettings::NAME));
4759
}
4860

4961
private:
5062
using Factory = std::function<std::unique_ptr<InteractionBuilderInterface>(bool strict)>;
51-
std::unordered_map<std::string_view, Factory> interactions;
63+
std::unordered_map<std::string, Factory> interactions;
5264

53-
template <typename InteractionSettings, InteractionType type> static Factory get_interaction_factory()
65+
template <InteractionType type, typename... InteractionSettings>
66+
static Factory get_interaction_factory(auto&&... args)
5467
{
5568
if constexpr (type == InteractionType::LookupGeneric) {
56-
return [](bool) {
69+
return [args...](bool) {
5770
// This class always checks.
58-
return std::make_unique<LookupIntoDynamicTableGeneric<InteractionSettings>>();
71+
return std::make_unique<LookupIntoDynamicTableGeneric<InteractionSettings...>>(args...);
5972
};
6073
} else if constexpr (type == InteractionType::LookupIntoBitwise) {
61-
return [](bool strict) {
62-
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoBitwise<InteractionSettings>>>()
63-
: std::make_unique<LookupIntoBitwise<InteractionSettings>>();
74+
return [args...](bool strict) {
75+
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoBitwise<InteractionSettings...>>>(args...)
76+
: std::make_unique<LookupIntoBitwise<InteractionSettings...>>(args...);
6477
};
6578
} else if constexpr (type == InteractionType::LookupIntoIndexedByClk) {
66-
return [](bool strict) {
67-
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoIndexedByClk<InteractionSettings>>>()
68-
: std::make_unique<LookupIntoIndexedByClk<InteractionSettings>>();
79+
return [args...](bool strict) {
80+
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoIndexedByClk<InteractionSettings...>>>(
81+
args...)
82+
: std::make_unique<LookupIntoIndexedByClk<InteractionSettings...>>(args...);
6983
};
7084
} else if constexpr (type == InteractionType::LookupIntoPDecomposition) {
71-
return [](bool strict) {
72-
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoPDecomposition<InteractionSettings>>>()
73-
: std::make_unique<LookupIntoPDecomposition<InteractionSettings>>();
85+
return [args...](bool strict) {
86+
return strict ? std::make_unique<AddChecksToBuilder<LookupIntoPDecomposition<InteractionSettings...>>>(
87+
args...)
88+
: std::make_unique<LookupIntoPDecomposition<InteractionSettings...>>(args...);
7489
};
7590
} else if constexpr (type == InteractionType::LookupSequential) {
76-
return [](bool) {
91+
return [args...](bool) {
7792
// This class always checks.
78-
return std::make_unique<LookupIntoDynamicTableSequential<InteractionSettings>>();
93+
return std::make_unique<LookupIntoDynamicTableSequential<InteractionSettings...>>(args...);
7994
};
8095
} else if constexpr (type == InteractionType::Permutation) {
81-
return [](bool strict) {
82-
return strict ? std::make_unique<CheckingPermutationBuilder<InteractionSettings>>()
83-
: std::make_unique<PermutationBuilder<InteractionSettings>>();
96+
return [args...](bool strict) {
97+
return strict ? std::make_unique<CheckingPermutationBuilder<InteractionSettings...>>(args...)
98+
: std::make_unique<PermutationBuilder<InteractionSettings...>>(args...);
8499
};
100+
} else if constexpr (type == InteractionType::MultiPermutation) {
101+
return
102+
[args...](bool) { return std::make_unique<MultiPermutationBuilder<InteractionSettings...>>(args...); };
85103
} else {
86104
throw std::runtime_error("Interaction type not supported: " + std::to_string(static_cast<int>(type)));
87105
}
88106
}
89107

90-
const Factory& get_job_internal(std::string_view interaction_name) const;
108+
const Factory& get_job_internal(const std::string& interaction_name) const;
91109
};
92110

93111
} // namespace bb::avm2::tracegen

barretenberg/cpp/src/barretenberg/vm2/tracegen/memory_trace.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include "barretenberg/vm2/common/field.hpp"
77
#include "barretenberg/vm2/tracegen/lib/interaction_def.hpp"
88

9+
// Permutations.
10+
#include "barretenberg/vm2/generated/relations/perms_addressing.hpp"
11+
#include "barretenberg/vm2/generated/relations/perms_keccak_memory.hpp"
12+
#include "barretenberg/vm2/generated/relations/perms_sha256_mem.hpp"
13+
914
namespace bb::avm2::tracegen {
1015

1116
void MemoryTraceBuilder::process(const simulation::EventEmitterInterface<simulation::MemoryEvent>::Container& events,
@@ -39,6 +44,31 @@ void MemoryTraceBuilder::process(const simulation::EventEmitterInterface<simulat
3944
}
4045
}
4146

42-
const InteractionDefinition MemoryTraceBuilder::interactions = InteractionDefinition();
47+
const InteractionDefinition MemoryTraceBuilder::interactions =
48+
InteractionDefinition()
49+
.add<InteractionType::MultiPermutation,
50+
// Addressing.
51+
perm_addressing_base_address_from_memory_settings,
52+
perm_addressing_indirect_from_memory_0_settings,
53+
perm_addressing_indirect_from_memory_1_settings,
54+
perm_addressing_indirect_from_memory_2_settings,
55+
perm_addressing_indirect_from_memory_3_settings,
56+
perm_addressing_indirect_from_memory_4_settings,
57+
perm_addressing_indirect_from_memory_5_settings,
58+
perm_addressing_indirect_from_memory_6_settings,
59+
// Keccak.
60+
perm_keccak_memory_slice_to_mem_settings,
61+
// Sha256.
62+
perm_sha256_mem_mem_op_0_settings,
63+
perm_sha256_mem_mem_op_1_settings,
64+
perm_sha256_mem_mem_op_2_settings,
65+
perm_sha256_mem_mem_op_3_settings,
66+
perm_sha256_mem_mem_op_4_settings,
67+
perm_sha256_mem_mem_op_5_settings,
68+
perm_sha256_mem_mem_op_6_settings,
69+
perm_sha256_mem_mem_op_7_settings,
70+
perm_sha256_mem_mem_input_read_settings
71+
// Others.
72+
>(Column::memory_sel);
4373

4474
} // namespace bb::avm2::tracegen

barretenberg/cpp/src/barretenberg/vm2/tracegen/memory_trace.hpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
#include "barretenberg/vm2/tracegen/lib/multi_permutation_builder.hpp"
1212
#include "barretenberg/vm2/tracegen/trace_container.hpp"
1313

14-
// Permutations.
15-
#include "barretenberg/vm2/generated/relations/perms_addressing.hpp"
16-
#include "barretenberg/vm2/generated/relations/perms_keccak_memory.hpp"
17-
#include "barretenberg/vm2/generated/relations/perms_sha256_mem.hpp"
18-
1914
namespace bb::avm2::tracegen {
2015

2116
class MemoryTraceBuilder final {
@@ -24,38 +19,6 @@ class MemoryTraceBuilder final {
2419
TraceContainer& trace);
2520

2621
static const InteractionDefinition interactions;
27-
28-
static std::vector<std::unique_ptr<InteractionBuilderInterface>> get_all_jobs()
29-
{
30-
auto definition_jobs = interactions.get_all_jobs();
31-
std::vector<std::unique_ptr<InteractionBuilderInterface>> multi_permutation_jobs;
32-
// Need to push back due to std::unique_ptr stuff.
33-
multi_permutation_jobs.push_back(std::make_unique<MultiPermutationBuilder<
34-
// Addressing.
35-
perm_addressing_base_address_from_memory_settings,
36-
perm_addressing_indirect_from_memory_0_settings,
37-
perm_addressing_indirect_from_memory_1_settings,
38-
perm_addressing_indirect_from_memory_2_settings,
39-
perm_addressing_indirect_from_memory_3_settings,
40-
perm_addressing_indirect_from_memory_4_settings,
41-
perm_addressing_indirect_from_memory_5_settings,
42-
perm_addressing_indirect_from_memory_6_settings,
43-
// Keccak.
44-
perm_keccak_memory_slice_to_mem_settings,
45-
// Sha256.
46-
perm_sha256_mem_mem_op_0_settings,
47-
perm_sha256_mem_mem_op_1_settings,
48-
perm_sha256_mem_mem_op_2_settings,
49-
perm_sha256_mem_mem_op_3_settings,
50-
perm_sha256_mem_mem_op_4_settings,
51-
perm_sha256_mem_mem_op_5_settings,
52-
perm_sha256_mem_mem_op_6_settings,
53-
perm_sha256_mem_mem_op_7_settings,
54-
perm_sha256_mem_mem_input_read_settings
55-
// Others.
56-
>>(Column::memory_sel));
57-
return concatenate_jobs(std::move(definition_jobs), std::move(multi_permutation_jobs));
58-
}
5922
};
6023

6124
} // namespace bb::avm2::tracegen

barretenberg/cpp/src/barretenberg/vm2/tracegen_helper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ void AvmTraceGenHelper::fill_trace_interactions(TraceContainer& trace)
441441
// Now we can compute lookups and permutations.
442442
{
443443
auto jobs_interactions =
444-
concatenate_jobs(MemoryTraceBuilder::get_all_jobs(),
444+
concatenate_jobs(MemoryTraceBuilder::interactions.get_all_jobs(),
445445
TxTraceBuilder::interactions.get_all_jobs(),
446446
ExecutionTraceBuilder::interactions.get_all_jobs(),
447447
AluTraceBuilder::interactions.get_all_jobs(),
@@ -460,7 +460,6 @@ void AvmTraceGenHelper::fill_trace_interactions(TraceContainer& trace)
460460
PublicDataTreeTraceBuilder::interactions.get_all_jobs(),
461461
UpdateCheckTraceBuilder::interactions.get_all_jobs(),
462462
NullifierTreeCheckTraceBuilder::interactions.get_all_jobs(),
463-
MemoryTraceBuilder::interactions.get_all_jobs(),
464463
DataCopyTraceBuilder::interactions.get_all_jobs(),
465464
CalldataTraceBuilder::interactions.get_all_jobs(),
466465
NoteHashTreeCheckTraceBuilder::interactions.get_all_jobs(),

0 commit comments

Comments
 (0)