Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
check_relation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdlib>
5#include <span>
6#include <stdexcept>
7
15
16namespace bb::avm2::constraining {
17namespace detail {
18
19const RelationParameters<FF>& get_test_params();
20
21template <typename Relation> constexpr bool subrelation_is_linearly_independent(size_t subrelation_index)
22{
24 return Relation::SUBRELATION_LINEARLY_INDEPENDENT[subrelation_index];
25 } else {
26 return true;
27 }
28}
29
30template <typename Relation, typename Trace>
31void check_relation_internal(const Trace& trace, std::span<const size_t> subrelations, uint32_t num_rows)
32{
34
35 // Accumulate the trace over the subrelations and check the result
36 // if the subrelation is linearly independent.
37 for (uint32_t r = 0; r < num_rows; ++r) {
38 Relation::accumulate(result, trace.get_row(r), get_test_params(), 1);
39 for (size_t j : subrelations) {
40 if (subrelation_is_linearly_independent<Relation>(j) && !result[j].is_zero()) {
41 throw std::runtime_error(format("Relation ",
42 Relation::NAME,
43 ", subrelation ",
44 Relation::get_subrelation_label(j),
45 " failed at row ",
46 r));
47 }
48 }
49 }
50 // For all subrelations, the result should be zero at the end of the trace.
51 for (size_t j : subrelations) {
52 if (!result[j].is_zero()) {
53 throw std::runtime_error(format("Relation ",
54 Relation::NAME,
55 ", subrelation ",
56 Relation::get_subrelation_label(j),
57 " is non-zero at end of trace"));
58 }
59 }
60}
61
62} // namespace detail
63
64template <typename Relation, typename... Ts>
65void check_relation(const tracegen::TestTraceContainer& trace, Ts... subrelation)
66{
67 std::array<size_t, sizeof...(Ts)> subrelations = { subrelation... };
68 detail::check_relation_internal<Relation>(trace, subrelations, trace.get_num_rows());
69}
70
71template <typename Relation> void check_relation(const tracegen::TestTraceContainer& trace)
72{
73 auto subrelations = std::make_index_sequence<Relation::SUBRELATION_PARTIAL_LENGTHS.size()>();
74 [&]<size_t... Is>(std::index_sequence<Is...>) { check_relation<Relation>(trace, Is...); }(subrelations);
75}
76
77template <typename TraceBuilder, typename... Setting> inline void check_interaction(tracegen::TestTraceContainer& trace)
78{
80 (TraceBuilder::interactions.template get_test_job<Setting>(cache)->process(trace), ...);
81}
82
83// Warning: The below requires ALL permutation settings as defined in InteractionDefinition.add():
84template <typename TraceBuilder, typename... Setting>
86{
88 // Concatenates the names of given permutation interactions:
89 std::string name = (std::string(Setting::NAME) + ...);
90 TraceBuilder::interactions.get_test_job(name, cache)->process(trace);
91}
92
93template <typename TraceBuilder> inline void check_all_interactions(tracegen::TestTraceContainer& trace)
94{
96 for (auto& job : TraceBuilder::interactions.get_all_test_jobs(cache)) {
97 job->process(trace);
98 }
99}
100
101} // namespace bb::avm2::constraining
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
ArrayOfValues< FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS > SumcheckArrayOfValuesOverSubrelations
std::string format(Args... args)
Definition log.hpp:24
TestTraceContainer trace
const RelationParameters< FF > & get_test_params()
void check_relation_internal(const Trace &trace, std::span< const size_t > subrelations, uint32_t num_rows)
void check_all_interactions(tracegen::TestTraceContainer &trace)
void check_multipermutation_interaction(tracegen::TestTraceContainer &trace)
void check_interaction(tracegen::TestTraceContainer &trace)
void check_relation(const tracegen::TestTraceContainer &trace, Ts... subrelation)
constexpr bool subrelation_is_linearly_independent()
Check whether a given subrelation is linearly independent from the other subrelations.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13