Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
cbor_testing.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_CBOR_PARSER_CBOR_TESTING_H_
16#define PRIVACY_PROOFS_ZK_LIB_CIRCUITS_CBOR_PARSER_CBOR_TESTING_H_
17
18#include <stddef.h>
19
20#include "circuits/cbor_parser/cbor.h"
21#include "circuits/cbor_parser/cbor_constants.h"
22#include "circuits/cbor_parser/cbor_witness.h"
23#include "circuits/logic/counter.h"
24#include "circuits/logic/evaluation_backend.h"
25#include "circuits/logic/logic.h"
26
27// The purpose of this class is to convert the witnesses from Elt to
28// EltW.
29//
30// Why?
31//
32// We want EltW in the evaluation backend to be a distinct type from
33// Elt. They are really the same thing, but we want to be able to
34// instantiate circuits in the compiler backend as well, and thus
35// circuits ought not to rely on the fact that EvaluationBackend::EltW
36// is really an Elt in disguise.
37// Consequently, tests in the evaluation backend must accept EltW.
38//
39// The witness generator must produce Elt, otherwise this forces the
40// inclusion of Logic in the app. We don't like that because Logic
41// is just a set of helpers to generate circuits, and the final app
42// is not supposed to generate circuits (since circuits are part of the
43// prover<->verifier API and so they must be set in stone in advance.)
44//
45// So this class is the price to be paid to maintain this typing
46// hygiene. Time will tell whether it was worth it.
47
48 namespace proofs {
49
50 template <class Field>
51 class CborTesting {
52 using EvalBackend = EvaluationBackend<Field>;
53 using LogicF = Logic<Field, EvalBackend>;
54 using EltW = typename LogicF::EltW;
55 using BitW = typename LogicF::BitW;
56 using CborL = Cbor<LogicF>;
57 using CborWitnessF = CborWitness<Field>;
58
59 public:
60 explicit CborTesting(const Field &F) : f_(F) {}
61
62 void convert_witnesses(
63 size_t n, typename CborL::v8 in[/*n*/],
64 typename CborL::position_witness pw[/*n*/],
65 typename CborL::global_witness &gw,
66 const typename CborWitnessF::v8 inS[/*n*/],
67 const typename CborWitnessF::position_witness pwS[/*n*/],
68 const typename CborWitnessF::global_witness &gwS) const {
69 const EvalBackend ebk(f_);
70 const LogicF L(&ebk, f_);
71 const Counter<LogicF> CTR(L);
72
73 for (size_t i = 0; i < n; ++i) {
74 for (size_t j = 0; j < 8; ++j) {
75 in[i][j] = BitW(L.konst(inS[i][j]), f_);
76 }
77 pw[i].encoded_sel_header = L.konst(pwS[i].encoded_sel_header);
78 }
79
80 gw.invprod_decode = L.konst(gwS.invprod_decode);
81 gw.cc0_counter = CTR.as_counter(gwS.cc0_counter);
82 gw.invprod_parse = L.konst(gwS.invprod_parse);
83 }
84
85 // Return an index that can be fed to a circuit in the
86 // evaluation backend (i.e., a bit vector).
87 typename CborL::vindex index(size_t j) const {
88 const EvalBackend ebk(f_);
89 const LogicF L(&ebk, f_);
90 return L.template vbit<CborConstants::kIndexBits>(j);
91 }
92
93 private:
94 const Field &f_;
95};
96} // namespace proofs
97
98#endif // PRIVACY_PROOFS_ZK_LIB_CIRCUITS_CBOR_PARSER_CBOR_TESTING_H_
proofs::Cbor
Definition cbor.h:35
proofs::CborWitness
Definition cbor_witness.h:29
proofs::EvaluationBackend
Definition evaluation_backend.h:23
proofs::Logic
Definition logic.h:38
proofs::CborWitness::global_witness
Definition cbor_witness.h:48
proofs::CborWitness::position_witness
Definition cbor_witness.h:38