Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
zk_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_ZK_ZK_TESTING_H_
16#define PRIVACY_PROOFS_ZK_LIB_ZK_ZK_TESTING_H_
17
18#include <cstddef>
19#include <cstdint>
20#include <vector>
21
22#include "algebra/convolution.h"
23#include "algebra/fp2.h"
24#include "algebra/reed_solomon.h"
25#include "arrays/dense.h"
26#include "random/secure_random_engine.h"
27#include "random/transcript.h"
28#include "sumcheck/circuit.h"
29#include "util/log.h"
30#include "util/readbuffer.h"
31#include "zk/zk_proof.h"
32#include "zk/zk_prover.h"
33#include "zk/zk_verifier.h"
34#include "gtest/gtest.h"
35
36namespace proofs {
37
38constexpr size_t kLigeroRate = 4;
39constexpr size_t kLigeroNreq = 189;
40
41// Runs a zk prover and verifier for a field that requires a field extension
42// to perform the commitment.
43template <class Field>
44void run2_test_zk(const Circuit<Field>& circuit, Dense<Field>& W,
45 const Dense<Field>& pub, const Field& base,
46 const typename Field::Elt& root_x,
47 const typename Field::Elt& root_y, size_t root_order) {
48 // Build the relevant algebra objects.
49 using Field2 = Fp2<Field>;
50 using Elt2 = typename Field2::Elt;
51 using FftExtConvolutionFactory = FFTExtConvolutionFactory<Field, Field2>;
53
54 const Field2 base_2(base);
55 const Elt2 omega{root_x, root_y};
56 const FftExtConvolutionFactory fft(base, base_2, omega, root_order);
57 const RSFactory rsf(fft, base);
58
59 ZkProof<Field> zkpr(circuit, kLigeroRate, kLigeroNreq);
60
61 Transcript tp((uint8_t*)"zk_test", 7);
63 ZkProver<Field, RSFactory> prover(circuit, base, rsf);
64 prover.commit(zkpr, W, tp, rng);
65 EXPECT_TRUE(prover.prove(zkpr, W, tp));
66 log(INFO, "ZK Prover done");
67
68 std::vector<uint8_t> zbuf;
69 zkpr.write(zbuf, base);
70 log(INFO, "zkp len: %zu bytes", zbuf.size());
71
72 // ======= run verifier =============
73 // Re-parse the proof to simulate a different client.
74 ZkProof<Field> zkpv(circuit, kLigeroRate, kLigeroNreq);
75 ReadBuffer rb(zbuf);
76 EXPECT_TRUE(zkpv.read(rb, base));
77
78 ZkVerifier<Field, RSFactory> verifier(circuit, rsf, kLigeroRate, kLigeroNreq,
79 base);
80 Transcript tv((uint8_t*)"zk_test", 7);
81 verifier.recv_commitment(zkpv, tv);
82 EXPECT_TRUE(verifier.verify(zkpv, pub, tv));
83 log(INFO, "ZK Verify done");
84}
85
86template <class Field>
87void run_failing_test_zk2(const Circuit<Field>& circuit, Dense<Field>& W,
88 const Dense<Field>& pub, const Field& base,
89 const typename Field::Elt& root_x,
90 const typename Field::Elt& root_y,
91 size_t root_order) {
92 // Build the relevant algebra objects.
93 using Field2 = Fp2<Field>;
94 using Elt2 = typename Field2::Elt;
95 using FftExtConvolutionFactory = FFTExtConvolutionFactory<Field, Field2>;
97
98 const Field2 base_2(base);
99 const Elt2 omega{root_x, root_y};
100 const FftExtConvolutionFactory fft(base, base_2, omega, root_order);
101 const RSFactory rsf(fft, base);
102
103 ZkProof<Field> zkpr(circuit, kLigeroRate, kLigeroNreq);
104
105 Transcript tp((uint8_t*)"zk_test", 7);
107 ZkProver<Field, RSFactory> prover(circuit, base, rsf);
108 prover.commit(zkpr, W, tp, rng);
109 bool p_ok = prover.prove(zkpr, W, tp);
110 EXPECT_FALSE(p_ok);
111}
112
113// Runs a zk prover and verifier for a field that has a suitable root of unity.
114template <class Field>
115void run_test_zk(const Circuit<Field>& circuit, Dense<Field>& W,
116 const Dense<Field>& pub, const typename Field::Elt& omega,
117 uint64_t omega_order, const Field& F) {
118 using FftConvolutionFactory = FFTConvolutionFactory<Field>;
119
120 FftConvolutionFactory fft(F, omega, omega_order);
122 const RSFactory rsf(fft, F);
123
124 ZkProof<Field> zkpr(circuit, kLigeroRate, kLigeroNreq);
125
126 Transcript tp((uint8_t*)"zk_test", 7);
128 ZkProver<Field, RSFactory> prover(circuit, F, rsf);
129 prover.commit(zkpr, W, tp, rng);
130 EXPECT_TRUE(prover.prove(zkpr, W, tp));
131
132 log(INFO, "ZK Prover done");
133
134 std::vector<uint8_t> zbuf;
135 zkpr.write(zbuf, F);
136 log(INFO, "zkp len: %zu bytes", zbuf.size());
137
138 // ======= zk verifier =============
139 // Re-parse the proof to simulate a different client.
140 ZkProof<Field> zkpv(circuit, kLigeroRate, kLigeroNreq);
141 ReadBuffer rb(zbuf);
142 EXPECT_TRUE(zkpv.read(rb, F));
143
144 ZkVerifier<Field, RSFactory> verifier(circuit, rsf, kLigeroRate, kLigeroNreq,
145 F);
146 Transcript tv((uint8_t*)"zk_test", 7);
147 verifier.recv_commitment(zkpv, tv);
148 EXPECT_TRUE(verifier.verify(zkpv, pub, tv));
149}
150
151} // namespace proofs
152
153#endif // PRIVACY_PROOFS_ZK_LIB_ZK_ZK_TESTING_H_
Definition dense.h:37
Definition convolution.h:109
Definition convolution.h:195
Definition fp2.h:34
Definition readbuffer.h:26
Definition reed_solomon.h:133
Definition secure_random_engine.h:29
Definition transcript.h:65
Definition zk_prover.h:53
Definition zk_verifier.h:41
Definition circuit.h:45
Definition gf2_128.h:63
Definition zk_proof.h:47