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#include <utility>
6
15
16namespace bb::avm2::tracegen {
17
18// Adds checks to a lookup builder. The BaseBuilder needs to be an IndexedLookupTraceBuilder.
19template <typename BaseBuilder> class AddChecksToBuilder : public BaseBuilder {
21 "BaseBuilder must be an IndexedLookupTraceBuilder");
22
23 public:
24 using TupleType = typename BaseBuilder::TupleType;
25 ~AddChecksToBuilder() override = default;
26
27 void init(TraceContainer& trace) override
28 {
29 BaseBuilder::init(trace);
30 this->trace = &trace;
31 }
32
33 uint32_t find_in_dst(const TupleType& src_values) const override
34 {
35 uint32_t dst_row = BaseBuilder::find_in_dst(src_values);
36
37 auto dst_values = trace->get_multiple(BaseBuilder::LookupSettings::DST_COLUMNS, dst_row);
38 if (src_values != dst_values) {
39 throw std::runtime_error("Failed computing counts for " + std::string(BaseBuilder::LookupSettings::NAME) +
40 ". Could not find tuple in destination. " + "SRC tuple: " +
41 column_values_to_string(src_values, BaseBuilder::LookupSettings::SRC_COLUMNS));
42 }
43
44 return dst_row;
45 }
46
47 private:
49};
50
51// Builds a permutation and performs additional checks.
52// Only use in tests and debugging. This is slow and memory intensive.
53template <typename PermutationSettings>
54class CheckingPermutationBuilder : public PermutationBuilder<PermutationSettings> {
55 public:
56 // Owning value array; see get_multiple_as_array in interaction_builder.hpp for why we can't key on RefTuple.
58
59 void process(TraceContainer& trace) override
60 {
62
63 // Collect the source and destination tuples.
64 source_tuples.clear();
65 trace.visit_column(PermutationSettings::SRC_SELECTOR, [&](uint32_t row, const FF&) {
66 source_tuples[get_multiple_as_array(trace, PermutationSettings::SRC_COLUMNS, row)].insert(row);
67 });
68 destination_tuples.clear();
69 trace.visit_column(PermutationSettings::DST_SELECTOR, [&](uint32_t row, const FF&) {
70 destination_tuples[get_multiple_as_array(trace, PermutationSettings::DST_COLUMNS, row)].insert(row);
71 });
72
73 auto build_error_message =
74 [&](const ArrayTuple& tuple, const auto& columns, const auto& src_rows, const auto& dst_rows) {
75 std::string error = "Failure to build permutation " + std::string(PermutationSettings::NAME) + ".\n";
76 error += format("Tuple ",
77 column_values_to_string(tuple, columns),
78 " has multiplicity ",
79 src_rows.size(),
80 " in the source, but ",
81 dst_rows.size(),
82 " in the destination.\n");
83 error += format("Source rows: ");
84 for (auto row : src_rows) {
85 error += format(row, " ");
86 }
87 error += format("\n");
88 error += format("Destination rows: ");
89 for (auto row : dst_rows) {
90 error += format(row, " ");
91 }
92 return error;
93 };
94
95 // Check that every source tuple is found in the destination with the same multiplicity.
96 for (const auto& [src_tuple, src_rows] : source_tuples) {
97 auto dst_rows = destination_tuples.contains(src_tuple) ? destination_tuples.at(src_tuple)
98 : std::unordered_set<uint32_t>();
99 if (src_rows.size() != dst_rows.size()) {
100 throw std::runtime_error(
101 build_error_message(src_tuple, PermutationSettings::SRC_COLUMNS, src_rows, dst_rows));
102 }
103 }
104 // Check that every destination tuple is found in the source with the same multiplicity.
105 for (const auto& [dst_tuple, dst_rows] : destination_tuples) {
106 auto src_rows =
107 source_tuples.contains(dst_tuple) ? source_tuples.at(dst_tuple) : std::unordered_set<uint32_t>();
108 if (src_rows.size() != dst_rows.size()) {
109 throw std::runtime_error(
110 build_error_message(dst_tuple, PermutationSettings::DST_COLUMNS, src_rows, dst_rows));
111 }
112 }
113 }
114
115 private:
118};
119
120} // namespace bb::avm2::tracegen
uint32_t find_in_dst(const TupleType &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 &) override
auto get_multiple(const std::array< ColumnAndShifts, N > &cols, uint32_t row) const
std::string format(Args... args)
Definition log.hpp:23
TestTraceContainer trace
std::array< FF, N > get_multiple_as_array(const TraceContainer &trace, const std::array< ColumnAndShifts, N > &cols, uint32_t row)
AvmFlavorSettings::FF FF
Definition field.hpp:10
std::string column_values_to_string(const std::array< FF, N > &arr, const std::array< ColumnAndShifts, N > &columns)
Definition stringify.hpp:46
::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