Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
evaluation_backend.h
1// Copyright 2025 Google LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_EVALUATION_BACKEND_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_EVALUATION_BACKEND_H_
17
18#include "util/panic.h"
19
20 namespace proofs {
21// backend that evaluates values directly
22 template <class Field>
23 class EvaluationBackend {
24 using Elt = typename Field::Elt;
25
26 public:
27 explicit EvaluationBackend(const Field& F,
28 bool panic_on_assertion_failure = true)
29 : f_(F),
30 panic_on_assertion_failure_(panic_on_assertion_failure),
31 assertion_failed_(false) {}
32
33 ~EvaluationBackend() {
34 // Crash if assertion_failed_, which indicates that a test
35 // has forgotten to read the value
36 check(!assertion_failed_, "assertion_failed_ true in ~EvaluationBackend()");
37 }
38
39 // Reading ASSERTION_FAILED_ returns the current ASSERTION_FAILED_
40 // state and resets the state.
41 bool assertion_failed() const {
42 bool b = assertion_failed_;
43 assertion_failed_ = false;
44 return b;
45 }
46
47 struct V {
48 Elt e;
49 V() = default;
50 explicit V(const Elt& x) : e(x) {}
51 Elt elt() const { return e; }
52
53 bool operator==(const V& y) const { return e == y.e; }
54 bool operator!=(const V& y) const { return e != y.e; }
55 };
56
57 V assert0(const V& a) const {
58 if (a.e == f_.zero()) {
59 return a;
60 } else {
61 if (panic_on_assertion_failure_) {
62 check(false, "a != F.zero()");
63 }
64 assertion_failed_ = true;
65 }
66 return a;
67 }
68
69 V add(const V& a, const V& b) const { return V{f_.addf(a.e, b.e)}; }
70 V sub(const V& a, const V& b) const { return V{f_.subf(a.e, b.e)}; }
71 V mul(const V& a, const V& b) const { return V{f_.mulf(a.e, b.e)}; }
72 V mul(const Elt& a, const V& b) const { return V{f_.mulf(a, b.e)}; }
73 V mul(const Elt& a, const V& b, const V& c) const {
74 return mul(a, mul(b, c));
75 }
76 V konst(const Elt& a) const { return V{a}; }
77
78 V ax(const Elt& a, const V& x) const { return V{f_.mulf(a, x.e)}; }
79 V axy(const Elt& a, const V& x, const V& y) const {
80 return V{f_.mulf(a, f_.mulf(x.e, y.e))};
81 }
82 V axpy(const V& y, const Elt& a, const V& x) const {
83 return V{f_.addf(y.e, f_.mulf(a, x.e))};
84 }
85 V apy(const V& y, const Elt& a) const { return V{f_.addf(y.e, a)}; }
86
87 private:
88 const Field& f_;
89 bool panic_on_assertion_failure_;
90 mutable bool assertion_failed_;
91};
92} // namespace proofs
93
94#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_LOGIC_EVALUATION_BACKEND_H_
proofs::EvaluationBackend::V
Definition evaluation_backend.h:47
proofs::GF2_128::Elt
Definition gf2_128.h:63