Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
relation_checker.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
13
14namespace bb {
15
21template <typename Flavor> class RelationChecker {
22 public:
26 static void check_all([[maybe_unused]] const auto& polynomials, [[maybe_unused]] const auto& params)
27 {
28 // default; do nothing
29 }
30
34 template <typename Relation, bool has_linearly_dependent = false>
35 static void check(const auto& polynomials, const auto& params, std::string label = "Relation")
36 {
37
38 // Define the appropriate accumulator type for the relation and initialize to zero
40 for (auto& element : result) {
41 element = 0;
42 }
43
44 for (size_t i = 0; i < polynomials.get_polynomial_size(); i++) {
45
46 Relation::accumulate(result, polynomials.get_row(i), params, 1);
47 size_t subrelation_idx = 0;
48
49 // Iterate over all the subrelation results and report if a linearly independent one failed
50 for (auto& element : result) {
51 if constexpr (has_linearly_dependent) {
52 if (element != 0 && Relation::SUBRELATION_LINEARLY_INDEPENDENT[subrelation_idx]) {
53 std::ostringstream oss;
54 oss << "RelationChecker: " << label << " relation (subrelation idx: " << subrelation_idx
55 << ") failed at row idx: " << i << ".";
56 throw_or_abort(oss.str());
57 }
58 } else {
59 if (element != 0) {
60 std::ostringstream oss;
61 oss << "RelationChecker: " << label << " relation (subrelation idx: " << subrelation_idx
62 << ") failed at row idx: " << i << ".";
63 throw_or_abort(oss.str());
64 }
65 }
66 subrelation_idx++;
67 }
68 }
69
70 if constexpr (has_linearly_dependent) {
71 size_t subrelation_idx = 0;
72 for (auto& element : result) {
73 // Check that linearly dependent subrelation result is 0 over the entire execution trace
74 if (element != 0 && Relation::SUBRELATION_LINEARLY_INDEPENDENT[subrelation_idx]) {
75 std::ostringstream oss;
76 oss << "RelationChecker: " << label << " linearly dependent subrelation idx: " << subrelation_idx
77 << ") failed.";
78 throw_or_abort(oss.str());
79 }
80 subrelation_idx++;
81 }
82 }
83 };
84};
85
86// Specialization for Ultra
87template <> class RelationChecker<bb::UltraFlavor> : public RelationChecker<void> {
89
90 public:
91 static void check_all(const auto& polynomials, const auto& params)
92 {
93 using FF = UltraFlavor::FF;
94
95 // Linearly independent relations (must be satisfied at each row)
96 Base::check<UltraArithmeticRelation<FF>>(polynomials, params, "UltraArithmetic");
97 Base::check<UltraPermutationRelation<FF>>(polynomials, params, "UltraPermutation");
98 Base::check<DeltaRangeConstraintRelation<FF>>(polynomials, params, "DeltaRangeConstraint");
99 Base::check<EllipticRelation<FF>>(polynomials, params, "Elliptic");
100 Base::check<MemoryRelation<FF>>(polynomials, params, "Memory");
101 Base::check<NonNativeFieldRelation<FF>>(polynomials, params, "NonNativeField");
102 Base::check<Poseidon2ExternalRelation<FF>>(polynomials, params, "Poseidon2External");
103 Base::check<Poseidon2InternalRelation<FF>>(polynomials, params, "Poseidon2Internal");
104
105 // Linearly dependent relations (must be satisfied as a sum across all rows)
106 Base::check<LogDerivLookupRelation<FF>, true>(polynomials, params, "LogDerivLookup");
107 }
108};
109
110// Specialization for Mega
111template <> class RelationChecker<MegaFlavor> : public RelationChecker<void> {
113
114 public:
115 static void check_all(const auto& polynomials, const auto& params)
116 {
117 // Check relations that are shared with Ultra
118 RelationChecker<UltraFlavor>::check_all(polynomials, params);
119
120 using FF = MegaFlavor::FF;
121 Base::check<EccOpQueueRelation<FF>>(polynomials, params, "EccOpQueue");
122 Base::check<DatabusLookupRelation<FF>, true>(polynomials, params, "DatabusLookup");
123 }
124};
125} // namespace bb
126
127// namespace bb
Curve::ScalarField FF
static void check_all(const auto &polynomials, const auto &params)
static void check_all(const auto &polynomials, const auto &params)
A debugging utility for checking whether a set of polynomials satisfies the relations for a given Fla...
static void check(const auto &polynomials, const auto &params, std::string label="Relation")
Check that a single specified relation is satisfied for a set of polynomials.
static void check_all(const auto &polynomials, const auto &params)
Check that the provided polynomials satisfy all relations for a given Flavor.
ArrayOfValues< FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS > SumcheckArrayOfValuesOverSubrelations
Curve::ScalarField FF
Entry point for Barretenberg command-line interface.
typename Flavor::FF FF
void throw_or_abort(std::string const &err)