forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPZK_query.hpp
226 lines (184 loc) · 6.18 KB
/
PPZK_query.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
222
223
224
225
226
#ifndef _SNARKLIB_PPZK_QUERY_HPP_
#define _SNARKLIB_PPZK_QUERY_HPP_
#include <cstdint>
#include <istream>
#include <ostream>
#include <vector>
#include "AuxSTL.hpp"
#include "Group.hpp"
#include "MultiExp.hpp"
#include "Pairing.hpp"
#include "ProgressCallback.hpp"
#include "Rank1DSL.hpp"
#include "WindowExp.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// input consistency query vector
//
template <typename PAIRING>
class PPZK_QueryIC
{
typedef typename PAIRING::Fr Fr;
typedef typename PAIRING::G1 G1;
public:
PPZK_QueryIC() = default;
// only used for roundtrip marshalling tests
PPZK_QueryIC(const G1& base,
const std::vector<G1>& encoded_terms)
: m_base(base),
m_coeffs(),
m_encoded_terms(encoded_terms)
{}
PPZK_QueryIC(const std::vector<Fr>& qap_query) // QAP query IC
: m_base(qap_query[0] * G1::one()),
m_coeffs(qap_query.begin() + 1, qap_query.end()),
m_encoded_terms(qap_query.size() - 1, G1::zero())
{}
void accumTable(const WindowExp<G1>& g1_table,
ProgressCallback* callback = nullptr) {
g1_table.batchExp(m_encoded_terms,
m_coeffs,
callback);
}
PPZK_QueryIC accumWitness(const R1Witness<Fr>& witness) const {
G1 base = m_base;
std::vector<G1> encoded_terms;
const std::size_t
wsize = witness.size(),
tsize = input_size();
if (wsize < tsize) {
base = base + multiExp(
std::vector<G1>(m_encoded_terms.begin(),
m_encoded_terms.begin() + wsize),
*witness);
encoded_terms = std::vector<G1>(m_encoded_terms.begin() + wsize,
m_encoded_terms.end());
} else if (wsize > tsize) {
base = base + multiExp(m_encoded_terms,
*witness.truncate(tsize));
} else {
base = base + multiExp(m_encoded_terms,
*witness);
}
return PPZK_QueryIC(base, encoded_terms);
}
const G1& base() const {
return m_base;
}
std::size_t input_size() const {
return m_encoded_terms.size();
}
const std::vector<G1>& encoded_terms() const {
return m_encoded_terms;
}
// only used for roundtrip marshalling tests
bool operator== (const PPZK_QueryIC& other) const {
return
base() == other.base() &&
encoded_terms() == other.encoded_terms();
}
// only used for roundtrip marshalling tests
bool operator!= (const PPZK_QueryIC& other) const {
return ! (*this == other);
}
void marshal_out(std::ostream& os) const {
base().marshal_out(os);
snarklib::marshal_out(os, encoded_terms());
}
bool marshal_in(std::istream& is) {
return
m_base.marshal_in(is) &&
snarklib::marshal_in(is, m_encoded_terms);
}
void clear() {
m_base = G1::zero();
m_encoded_terms.clear();
}
bool empty() const {
return
base().isZero() ||
encoded_terms().empty();
}
private:
G1 m_base;
std::vector<Fr> m_coeffs;
std::vector<G1> m_encoded_terms;
};
////////////////////////////////////////////////////////////////////////////////
// query vectors A, B, C
//
template <typename GA, typename GB, typename FR>
class PPZK_QueryABC
{
public:
PPZK_QueryABC(const BlockVector<FR>& qap_query, // QAP query A, B, C
const FR& random_v,
const FR& random_alpha)
: m_qap_query(qap_query),
m_random_v(random_v),
m_random_prod(random_v * random_alpha)
{}
void accumTable(const WindowExp<GA>& ga_table,
const WindowExp<GB>& gb_table,
ProgressCallback* callback = nullptr) {
if (m_vec.empty()) {
m_vec = batchExp(ga_table,
gb_table,
m_random_v,
m_random_prod,
m_qap_query,
callback);
} else {
batchExp(m_vec,
ga_table,
gb_table,
m_random_v,
m_random_prod,
m_qap_query,
callback);
}
}
const SparseVector<Pairing<GA, GB>>& vec() const { return m_vec; }
private:
const BlockVector<FR>& m_qap_query;
const FR& m_random_v;
const FR m_random_prod;
SparseVector<Pairing<GA, GB>> m_vec;
};
template <typename PAIRING> using PPZK_QueryA =
PPZK_QueryABC<typename PAIRING::G1, typename PAIRING::G1, typename PAIRING::Fr>;
template <typename PAIRING> using PPZK_QueryB =
PPZK_QueryABC<typename PAIRING::G2, typename PAIRING::G1, typename PAIRING::Fr>;
template <typename PAIRING> using PPZK_QueryC =
PPZK_QueryABC<typename PAIRING::G1, typename PAIRING::G1, typename PAIRING::Fr>;
////////////////////////////////////////////////////////////////////////////////
// query vectors H, K
//
template <typename PAIRING>
class PPZK_QueryHK
{
typedef typename PAIRING::Fr Fr;
typedef typename PAIRING::G1 G1;
public:
PPZK_QueryHK(const BlockVector<Fr>& qap_query) // QAP query H, K
: m_qap_query(qap_query),
m_vec(qap_query.space(), qap_query.block())
{}
void accumTable(const WindowExp<G1>& g1_table,
ProgressCallback* callback = nullptr) {
g1_table.batchExp(m_vec,
m_qap_query,
callback);
}
const BlockVector<G1>& vec() const { return m_vec; }
const std::vector<G1>& vvec() const { return m_vec.vec(); }
BlockVector<G1>& lvec() { return m_vec; }
std::vector<G1>& llvec() { return m_vec.lvec(); }
private:
const BlockVector<Fr>& m_qap_query;
BlockVector<G1> m_vec;
};
template <typename PAIRING> using PPZK_QueryH = PPZK_QueryHK<PAIRING>;
template <typename PAIRING> using PPZK_QueryK = PPZK_QueryHK<PAIRING>;
} // namespace snarklib
#endif