Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
test_interaction_builder.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <unordered_set>
5
13
14namespace bb::avm2::tracegen {
15
16// Adds checks to a lookup builder. The BaseBuilder needs to be an IndexedLookupTraceBuilder.
17template <typename BaseBuilder> class AddChecksToBuilder : public BaseBuilder {
19 "BaseBuilder must be an IndexedLookupTraceBuilder");
20
21 public:
22 ~AddChecksToBuilder() override = default;
23
24 void init(TraceContainer& trace) override
25 {
26 BaseBuilder::init(trace);
27 this->trace = &trace;
28 }
29
30 uint32_t find_in_dst(
32 {
33 uint32_t dst_row = BaseBuilder::find_in_dst(src_values);
34
35 auto dst_values = trace->get_multiple(BaseBuilder::LookupSettings::DST_COLUMNS, dst_row);
36 if (src_values != dst_values) {
37 throw std::runtime_error("Failed computing counts for " + std::string(BaseBuilder::LookupSettings::NAME) +
38 ". Could not find tuple in destination. " + "SRC tuple: " +
39 column_values_to_string(src_values, BaseBuilder::LookupSettings::SRC_COLUMNS));
40 }
41
42 return dst_row;
43 }
44
45 private:
47};
48
49// Builds a permutation and performs additional checks.
50// Only use in tests and debugging. This is slow and memory intensive.
51template <typename PermutationSettings>
52class CheckingPermutationBuilder : public PermutationBuilder<PermutationSettings> {
53 public:
55
57 {
59
60 // Collect the source and destination tuples.
61 source_tuples.clear();
62 trace.visit_column(PermutationSettings::SRC_SELECTOR, [&](uint32_t row, const FF&) {
63 auto src_values = trace.get_multiple(PermutationSettings::SRC_COLUMNS, row);
64 source_tuples[src_values].insert(row);
65 });
66 destination_tuples.clear();
67 trace.visit_column(PermutationSettings::DST_SELECTOR, [&](uint32_t row, const FF&) {
68 auto dst_values = trace.get_multiple(PermutationSettings::DST_COLUMNS, row);
69 destination_tuples[dst_values].insert(row);
70 });
71
72 auto build_error_message =
73 [&](const ArrayTuple& tuple, const auto& columns, const auto& src_rows, const auto& dst_rows) {
74 std::string error = "Failure to build permutation " + std::string(PermutationSettings::NAME) + ".\n";
75 error += format("Tuple ",
76 column_values_to_string(tuple, columns),
77 " has multiplicity ",
78 src_rows.size(),
79 " in the source, but ",
80 dst_rows.size(),
81 " in the destination.\n");
82 error += format("Source rows: ");
83 for (auto row : src_rows) {
84 error += format(row, " ");
85 }
86 error += format("\n");
87 error += format("Destination rows: ");
88 for (auto row : dst_rows) {
89 error += format(row, " ");
90 }
91 return error;
92 };
93
94 // Check that every source tuple is found in the destination with the same multiplicity.
95 for (const auto& [src_tuple, src_rows] : source_tuples) {
96 auto dst_rows = destination_tuples.contains(src_tuple) ? destination_tuples.at(src_tuple)
97 : std::unordered_set<uint32_t>();
98 if (src_rows.size() != dst_rows.size()) {
99 throw std::runtime_error(
100 build_error_message(src_tuple, PermutationSettings::SRC_COLUMNS, src_rows, dst_rows));
101 }
102 }
103 // Check that every destination tuple is found in the source with the same multiplicity.
104 for (const auto& [dst_tuple, dst_rows] : destination_tuples) {
105 auto src_rows =
106 source_tuples.contains(dst_tuple) ? source_tuples.at(dst_tuple) : std::unordered_set<uint32_t>();
107 if (src_rows.size() != dst_rows.size()) {
108 throw std::runtime_error(
109 build_error_message(dst_tuple, PermutationSettings::DST_COLUMNS, src_rows, dst_rows));
110 }
111 }
112 }
113
114 private:
117};
118
119} // namespace bb::avm2::tracegen
uint32_t find_in_dst(const std::array< FF, BaseBuilder::LookupSettings::LOOKUP_TUPLE_SIZE > &src_values) const override
void init(TraceContainer &trace) override
unordered_flat_map< ArrayTuple, std::unordered_set< uint32_t > > destination_tuples
unordered_flat_map< ArrayTuple, std::unordered_set< uint32_t > > source_tuples
std::array< FF, PermutationSettings::COLUMNS_PER_SET > ArrayTuple
void process(TraceContainer &trace) override
std::array< FF, N > get_multiple(const std::array< ColumnAndShifts, N > &cols, uint32_t row) const
std::string format(Args... args)
Definition log.hpp:20
TestTraceContainer trace
std::string column_values_to_string(const std::array< FF, N > &arr, const std::array< ColumnAndShifts, N > &columns)
Definition stringify.hpp:44
AvmFlavorSettings::FF FF
Definition field.hpp:10
::ankerl::unordered_dense::map< Key, T > unordered_flat_map
Definition map.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13