forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPZK_witness.hpp
221 lines (182 loc) · 5.97 KB
/
PPZK_witness.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef _SNARKLIB_PPZK_WITNESS_HPP_
#define _SNARKLIB_PPZK_WITNESS_HPP_
#include <cassert>
#include <cstdint>
#include <memory>
#include <vector>
#include <snarklib/AuxSTL.hpp>
#include <snarklib/MultiExp.hpp>
#include <snarklib/Pairing.hpp>
#include <snarklib/ProgressCallback.hpp>
#include <snarklib/QAP_system.hpp>
#include <snarklib/Rank1DSL.hpp>
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// witnesses A, B, C
//
template <typename GA, typename GB, typename FR, std::size_t Z_INDEX>
class PPZK_WitnessABC
{
public:
typedef SparseVector<Pairing<GA, GB>> SparseVec;
typedef Pairing<GA, GB> Val;
PPZK_WitnessABC()
: m_witness(nullptr),
m_random_d(nullptr)
{}
PPZK_WitnessABC(const std::size_t qapNumVariables,
const R1Witness<FR>& witness,
const FR& random_d)
: m_numVariables(qapNumVariables),
m_witness(std::addressof(*witness)),
m_random_d(std::addressof(random_d))
{}
template <template <typename> class SYS>
PPZK_WitnessABC(const QAP<SYS, FR>& qap,
const R1Witness<FR>& witness,
const FR& random_d)
: PPZK_WitnessABC{qap.numVariables(), witness, random_d}
{}
void accumQuery(const SparseVector<Pairing<GA, GB>>& query,
const std::size_t reserveTune,
ProgressCallback* callback) {
m_val = m_val
+ (*m_random_d) * query.getElementForIndex(Z_INDEX)
+ query.getElementForIndex(3)
+ multiExp01(query,
*m_witness,
4,
4 + m_numVariables,
0 == reserveTune ? reserveTune : m_numVariables / reserveTune,
callback);
}
void accumQuery(const SparseVector<Pairing<GA, GB>>& query,
ProgressCallback* callback = nullptr) {
accumQuery(query, 0, callback);
}
const Pairing<GA, GB>& val() const { return m_val; }
private:
std::size_t m_numVariables;
const std::vector<FR>* m_witness;
const FR* m_random_d;
Pairing<GA, GB> m_val;
};
template <typename PAIRING> using PPZK_WitnessA =
PPZK_WitnessABC<typename PAIRING::G1, typename PAIRING::G1, typename PAIRING::Fr, 0>;
template <typename PAIRING> using PPZK_WitnessB =
PPZK_WitnessABC<typename PAIRING::G2, typename PAIRING::G1, typename PAIRING::Fr, 1>;
template <typename PAIRING> using PPZK_WitnessC =
PPZK_WitnessABC<typename PAIRING::G1, typename PAIRING::G1, typename PAIRING::Fr, 2>;
////////////////////////////////////////////////////////////////////////////////
// witness H
//
template <typename PAIRING>
class PPZK_WitnessH
{
typedef typename PAIRING::Fr Fr;
typedef typename PAIRING::G1 G1;
public:
PPZK_WitnessH() = default;
void accumQuery(const std::vector<G1>& query,
const std::vector<Fr>& scalar,
ProgressCallback* callback = nullptr)
{
m_val = m_val + multiExp(query,
scalar,
callback);
}
void accumQuery(const BlockVector<G1>& query,
const BlockVector<Fr>& scalar,
ProgressCallback* callback = nullptr)
{
#ifdef USE_ASSERT
assert(query.space() == scalar.space() &&
query.block() == scalar.block());
#endif
accumQuery(query.vec(),
scalar.vec(),
callback);
}
const G1& val() const { return m_val; }
private:
G1 m_val;
};
////////////////////////////////////////////////////////////////////////////////
// witness K
//
template <typename PAIRING>
class PPZK_WitnessK
{
typedef typename PAIRING::Fr Fr;
typedef typename PAIRING::G1 G1;
public:
PPZK_WitnessK()
: m_witness(nullptr),
m_random_d1(nullptr),
m_random_d2(nullptr),
m_random_d3(nullptr)
{}
PPZK_WitnessK(const R1Witness<Fr>& witness,
const Fr& random_d1,
const Fr& random_d2,
const Fr& random_d3)
: m_witness(std::addressof(*witness)),
m_random_d1(std::addressof(random_d1)),
m_random_d2(std::addressof(random_d2)),
m_random_d3(std::addressof(random_d3))
{}
void accumQuery(const std::vector<G1>& query,
const std::size_t reserveTune,
ProgressCallback* callback = nullptr)
{
const std::size_t startOffset = 4;
#ifdef USE_ASSERT
assert(query.size() >= 4);
#endif
m_val = m_val
+ (*m_random_d1) * query[0]
+ (*m_random_d2) * query[1]
+ (*m_random_d3) * query[2]
+ query[3]
+ multiExp01(
query,
startOffset,
4,
*m_witness,
0 == reserveTune ? reserveTune : (query.size() - startOffset) / reserveTune,
callback);
}
void accumQuery(const BlockVector<G1>& query,
const std::size_t reserveTune,
ProgressCallback* callback = nullptr)
{
std::size_t startOffset = 0;
if (0 == query.block()[0]) {
#ifdef USE_ASSERT
assert(query.size() >= 4);
#endif
m_val = m_val
+ (*m_random_d1) * query[0]
+ (*m_random_d2) * query[1]
+ (*m_random_d3) * query[2]
+ query[3];
startOffset = 4;
}
m_val = m_val + multiExp01(
query,
startOffset,
4,
*m_witness,
0 == reserveTune ? reserveTune : (query.size() - startOffset) / reserveTune,
callback);
}
const G1& val() const { return m_val; }
private:
const std::vector<Fr>* m_witness;
const Fr* m_random_d1;
const Fr* m_random_d2;
const Fr* m_random_d3;
G1 m_val;
};
} // namespace snarklib
#endif