-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPPZK_proof.hpp
197 lines (167 loc) · 5.41 KB
/
PPZK_proof.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef _SNARKLIB_PPZK_PROOF_HPP_
#define _SNARKLIB_PPZK_PROOF_HPP_
#include <cstdint>
#include <istream>
#include <memory>
#include <ostream>
#include <snarklib/AuxSTL.hpp>
#include <snarklib/Pairing.hpp>
#include <snarklib/PPZK_keystruct.hpp>
#include <snarklib/PPZK_randomness.hpp>
#include <snarklib/PPZK_witness.hpp>
#include <snarklib/ProgressCallback.hpp>
#include <snarklib/QAP_witness.hpp>
#include <snarklib/Rank1DSL.hpp>
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// Proof generation
//
template <typename PAIRING>
class PPZK_Proof
{
typedef typename PAIRING::Fr Fr;
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
public:
PPZK_Proof() = default;
// used for libsnark proof tests and marshalling
PPZK_Proof(const Pairing<G1, G1>& A,
const Pairing<G2, G1>& B,
const Pairing<G1, G1>& C,
const G1& H,
const G1& K)
: m_A(A),
m_B(B),
m_C(C),
m_H(H),
m_K(K)
{}
template <template <typename> class SYS>
PPZK_Proof(const SYS<Fr>& constraintSystem,
const std::size_t numCircuitInputs,
const PPZK_ProvingKey<PAIRING>& pk,
const R1Witness<Fr>& witness,
const PPZK_ProofRandomness<Fr>& proofRand,
const std::size_t reserveTune,
ProgressCallback* callback)
{
ProgressCallback_NOP<PAIRING> dummyNOP;
ProgressCallback* dummy = callback ? callback : std::addressof(dummyNOP);
dummy->majorSteps(6);
// randomness
const auto
&d1 = proofRand.d1(),
&d2 = proofRand.d2(),
&d3 = proofRand.d3();
const QAP_SystemPoint<SYS, Fr> qap(constraintSystem, numCircuitInputs);
// step 6 - A
dummy->major(true);
PPZK_WitnessA<PAIRING> Aw(qap, witness, d1);
Aw.accumQuery(pk.A_query(), reserveTune, callback);
m_A = Aw.val();
// step 5 - B
dummy->major(true);
PPZK_WitnessB<PAIRING> Bw(qap, witness, d2);
Bw.accumQuery(pk.B_query(), reserveTune, callback);
m_B = Bw.val();
// step 4 - C
dummy->major(true);
PPZK_WitnessC<PAIRING> Cw(qap, witness, d3);
Cw.accumQuery(pk.C_query(), reserveTune, callback);
m_C = Cw.val();
// step 3 - ABCH
dummy->major(true);
const QAP_WitnessABCH<SYS, Fr> ABCH(qap, witness, d1, d2, d3, callback);
// step 2 - H
dummy->major(true);
PPZK_WitnessH<PAIRING> Hw;
Hw.accumQuery(pk.H_query(), ABCH.vec(), callback);
m_H = Hw.val();
// step 1 - K
dummy->major(true);
PPZK_WitnessK<PAIRING> Kw(witness, d1, d2, d3);
Kw.accumQuery(pk.K_query(), reserveTune, callback);
m_K = Kw.val();
}
template <template <typename> class SYS>
PPZK_Proof(const SYS<Fr>& constraintSystem,
const std::size_t numCircuitInputs,
const PPZK_ProvingKey<PAIRING>& pk,
const R1Witness<Fr>& witness,
const PPZK_ProofRandomness<Fr>& proofRand,
ProgressCallback* callback = nullptr)
: PPZK_Proof{constraintSystem, numCircuitInputs, pk, witness, proofRand, 0, callback}
{}
const Pairing<G1, G1>& A() const { return m_A; }
const Pairing<G2, G1>& B() const { return m_B; }
const Pairing<G1, G1>& C() const { return m_C; }
const G1& H() const { return m_H; }
const G1& K() const { return m_K; }
bool wellFormed() const {
return
m_A.G().wellFormed() && m_A.H().wellFormed() &&
m_B.G().wellFormed() && m_B.H().wellFormed() &&
m_C.G().wellFormed() && m_C.H().wellFormed() &&
m_H.wellFormed() &&
m_K.wellFormed();
}
bool operator== (const PPZK_Proof& other) const {
return
A() == other.A() &&
B() == other.B() &&
C() == other.C() &&
H() == other.H() &&
K() == other.K();
}
bool operator!= (const PPZK_Proof& other) const {
return ! (*this == other);
}
void marshal_out(std::ostream& os) const {
A().marshal_out_raw(os);
B().marshal_out_raw(os);
C().marshal_out_raw(os);
H().marshal_out_raw(os);
K().marshal_out_raw(os);
}
bool marshal_in(std::istream& is) {
return
m_A.marshal_in_raw(is) &&
m_B.marshal_in_raw(is) &&
m_C.marshal_in_raw(is) &&
m_H.marshal_in_raw(is) &&
m_K.marshal_in_raw(is);
}
void clear() {
m_A = Pairing<G1, G1>::zero();
m_B = Pairing<G2, G1>::zero();
m_C = Pairing<G1, G1>::zero();
m_H = G1::zero();
m_K = G1::zero();
}
bool empty() const {
return
m_A.isZero() ||
m_B.isZero() ||
m_C.isZero() ||
m_H.isZero() ||
m_K.isZero();
}
private:
Pairing<G1, G1> m_A;
Pairing<G2, G1> m_B;
Pairing<G1, G1> m_C;
G1 m_H;
G1 m_K;
};
template <typename PAIRING>
std::ostream& operator<< (std::ostream& os, const PPZK_Proof<PAIRING>& a) {
a.marshal_out(os);
return os;
}
template <typename PAIRING>
std::istream& operator>> (std::istream& is, PPZK_Proof<PAIRING>& a) {
if (! a.marshal_in(is)) a.clear();
return is;
}
} // namespace snarklib
#endif