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
14
15namespace bb::avm2::constraining {
16namespace detail {
17
18const RelationParameters<FF>& get_test_params();
19
20template <typename Relation> constexpr bool subrelation_is_linearly_independent(size_t subrelation_index)
21{
23 return Relation::SUBRELATION_LINEARLY_INDEPENDENT[subrelation_index];
24 } else {
25 return true;
26 }
27}
28
29template <typename Relation, typename Trace>
30void check_relation_internal(const Trace& trace, std::span<const size_t> subrelations, uint32_t num_rows)
31{
33
34 // Accumulate the trace over the subrelations and check the result
35 // if the subrelation is linearly independent.
36 for (uint32_t r = 0; r < num_rows; ++r) {
37 Relation::accumulate(result, trace.get_row(r), get_test_params(), 1);
38 for (size_t j : subrelations) {
39 if (subrelation_is_linearly_independent<Relation>(j) && !result[j].is_zero()) {
40 throw std::runtime_error(format("Relation ",
41 Relation::NAME,
42 ", subrelation ",
43 Relation::get_subrelation_label(j),
44 " failed at row ",
45 r));
46 }
47 }
48 }
49 // For all subrelations, the result should be zero at the end of the trace.
50 for (size_t j : subrelations) {
51 if (!result[j].is_zero()) {
52 throw std::runtime_error(format("Relation ",
53 Relation::NAME,
54 ", subrelation ",
55 Relation::get_subrelation_label(j),
56 " is non-zero at end of trace"));
57 }
58 }
59}
60
61} // namespace detail
62
63template <typename Relation, typename... Ts>
64void check_relation(const tracegen::TestTraceContainer& trace, Ts... subrelation)
65{
66 std::array<size_t, sizeof...(Ts)> subrelations = { subrelation... };
67 detail::check_relation_internal<Relation>(trace, subrelations, trace.get_num_rows());
68}
69
70template <typename Relation> void check_relation(const tracegen::TestTraceContainer& trace)
71{
72 auto subrelations = std::make_index_sequence<Relation::SUBRELATION_PARTIAL_LENGTHS.size()>();
73 [&]<size_t... Is>(std::index_sequence<Is...>) { check_relation<Relation>(trace, Is...); }(subrelations);
74}
75
76template <typename TraceBuilder, typename... Setting> inline void check_interaction(tracegen::TestTraceContainer& trace)
77{
78 (TraceBuilder::interactions.template get_test_job<Setting>()->process(trace), ...);
79}
80
81template <typename TraceBuilder> inline void check_all_interactions(tracegen::TestTraceContainer& trace)
82{
83 for (auto& job : TraceBuilder::interactions.get_all_test_jobs()) {
84 job->process(trace);
85 }
86}
87
88} // 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:20
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_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