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