forked from akosba/libsnark
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpb_variable.tcc
331 lines (287 loc) · 8.57 KB
/
pb_variable.tcc
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/** @file
*****************************************************************************
* @author This file is part of libsnark, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#ifndef PB_VARIABLE_TCC_
#define PB_VARIABLE_TCC_
#include <cassert>
#include <libff/common/utils.hpp>
#include <libsnark/gadgetlib1/protoboard.hpp>
namespace libsnark {
template<typename FieldT>
void pb_variable<FieldT>::allocate(protoboard<FieldT> &pb, const std::string &annotation)
{
this->index = pb.allocate_var_index(annotation);
}
/* allocates pb_variable<FieldT> array in MSB->LSB order */
template<typename FieldT>
void pb_variable_array<FieldT>::allocate(protoboard<FieldT> &pb, const size_t n, const std::string &annotation_prefix)
{
#ifdef DEBUG
assert(annotation_prefix != "");
#endif
(*this).resize(n);
for (size_t i = 0; i < n; ++i)
{
(*this)[i].allocate(pb, FMT(annotation_prefix, "_%zu", i));
}
}
template<typename FieldT>
void pb_variable_array<FieldT>::fill_with_field_elements(protoboard<FieldT> &pb, const std::vector<FieldT>& vals) const
{
assert(this->size() == vals.size());
for (size_t i = 0; i < vals.size(); ++i)
{
pb.val((*this)[i]) = vals[i];
}
}
template<typename FieldT>
void pb_variable_array<FieldT>::fill_with_bits(protoboard<FieldT> &pb, const libff::bit_vector& bits) const
{
assert(this->size() == bits.size());
for (size_t i = 0; i < bits.size(); ++i)
{
pb.val((*this)[i]) = (bits[i] ? FieldT::one() : FieldT::zero());
}
}
template<typename FieldT>
void pb_variable_array<FieldT>::fill_with_bits_of_field_element(protoboard<FieldT> &pb, const FieldT &r) const
{
const libff::bigint<FieldT::num_limbs> rint = r.as_bigint();
for (size_t i = 0; i < this->size(); ++i)
{
pb.val((*this)[i]) = rint.test_bit(i) ? FieldT::one() : FieldT::zero();
}
}
template<typename FieldT>
void pb_variable_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const
{
this->fill_with_bits_of_field_element(pb, FieldT(i, true));
}
template<typename FieldT>
std::vector<FieldT> pb_variable_array<FieldT>::get_vals(const protoboard<FieldT> &pb) const
{
std::vector<FieldT> result(this->size());
for (size_t i = 0; i < this->size(); ++i)
{
result[i] = pb.val((*this)[i]);
}
return result;
}
template<typename FieldT>
libff::bit_vector pb_variable_array<FieldT>::get_bits(const protoboard<FieldT> &pb) const
{
libff::bit_vector result;
for (size_t i = 0; i < this->size(); ++i)
{
const FieldT v = pb.val((*this)[i]);
assert(v == FieldT::zero() || v == FieldT::one());
result.push_back(v == FieldT::one());
}
return result;
}
template<typename FieldT>
FieldT pb_variable_array<FieldT>::get_field_element_from_bits(const protoboard<FieldT> &pb) const
{
FieldT result = FieldT::zero();
for (size_t i = 0; i < this->size(); ++i)
{
/* push in the new bit */
const FieldT v = pb.val((*this)[this->size()-1-i]);
assert(v == FieldT::zero() || v == FieldT::one());
result += result + v;
}
return result;
}
template<typename FieldT>
pb_linear_combination<FieldT>::pb_linear_combination()
{
this->is_variable = false;
}
template<typename FieldT>
pb_linear_combination<FieldT>::pb_linear_combination(const pb_variable<FieldT> &var)
{
this->is_variable = true;
this->index = var.index;
this->terms.emplace_back(linear_term<FieldT>(var));
}
template<typename FieldT>
void pb_linear_combination<FieldT>::assign(protoboard<FieldT> &pb, const linear_combination<FieldT> &lc)
{
assert(this->is_variable == false);
this->index = pb.allocate_lc_index();
this->terms = lc.terms;
}
template<typename FieldT>
void pb_linear_combination<FieldT>::evaluate(protoboard<FieldT> &pb) const
{
if (this->is_variable)
{
return; // do nothing
}
FieldT sum = 0;
for (auto term : this->terms)
{
sum += term.coeff * pb.val(pb_variable<FieldT>(term.index));
}
pb.lc_val(*this) = sum;
}
template<typename FieldT>
bool pb_linear_combination<FieldT>::is_constant() const
{
if (is_variable)
{
return (index == 0);
}
else
{
for (auto term : this->terms)
{
if (term.index != 0)
{
return false;
}
}
return true;
}
}
template<typename FieldT>
FieldT pb_linear_combination<FieldT>::constant_term() const
{
if (is_variable)
{
return (index == 0 ? FieldT::one() : FieldT::zero());
}
else
{
FieldT result = FieldT::zero();
for (auto term : this->terms)
{
if (term.index == 0)
{
result += term.coeff;
}
}
return result;
}
}
template<typename FieldT>
void pb_linear_combination_array<FieldT>::evaluate(protoboard<FieldT> &pb) const
{
for (size_t i = 0; i < this->size(); ++i)
{
(*this)[i].evaluate(pb);
}
}
template<typename FieldT>
void pb_linear_combination_array<FieldT>::fill_with_field_elements(protoboard<FieldT> &pb, const std::vector<FieldT>& vals) const
{
assert(this->size() == vals.size());
for (size_t i = 0; i < vals.size(); ++i)
{
pb.lc_val((*this)[i]) = vals[i];
}
}
template<typename FieldT>
void pb_linear_combination_array<FieldT>::fill_with_bits(protoboard<FieldT> &pb, const libff::bit_vector& bits) const
{
assert(this->size() == bits.size());
for (size_t i = 0; i < bits.size(); ++i)
{
pb.lc_val((*this)[i]) = (bits[i] ? FieldT::one() : FieldT::zero());
}
}
template<typename FieldT>
void pb_linear_combination_array<FieldT>::fill_with_bits_of_field_element(protoboard<FieldT> &pb, const FieldT &r) const
{
const libff::bigint<FieldT::num_limbs> rint = r.as_bigint();
for (size_t i = 0; i < this->size(); ++i)
{
pb.lc_val((*this)[i]) = rint.test_bit(i) ? FieldT::one() : FieldT::zero();
}
}
template<typename FieldT>
void pb_linear_combination_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const
{
this->fill_with_bits_of_field_element(pb, FieldT(i));
}
template<typename FieldT>
std::vector<FieldT> pb_linear_combination_array<FieldT>::get_vals(const protoboard<FieldT> &pb) const
{
std::vector<FieldT> result(this->size());
for (size_t i = 0; i < this->size(); ++i)
{
result[i] = pb.lc_val((*this)[i]);
}
return result;
}
template<typename FieldT>
libff::bit_vector pb_linear_combination_array<FieldT>::get_bits(const protoboard<FieldT> &pb) const
{
libff::bit_vector result;
for (size_t i = 0; i < this->size(); ++i)
{
const FieldT v = pb.lc_val((*this)[i]);
assert(v == FieldT::zero() || v == FieldT::one());
result.push_back(v == FieldT::one());
}
return result;
}
template<typename FieldT>
FieldT pb_linear_combination_array<FieldT>::get_field_element_from_bits(const protoboard<FieldT> &pb) const
{
FieldT result = FieldT::zero();
for (size_t i = 0; i < this->size(); ++i)
{
/* push in the new bit */
const FieldT v = pb.lc_val((*this)[this->size()-1-i]);
assert(v == FieldT::zero() || v == FieldT::one());
result += result + v;
}
return result;
}
template<typename FieldT>
linear_combination<FieldT> pb_sum(const pb_linear_combination_array<FieldT> &v)
{
linear_combination<FieldT> result;
for (auto &term : v)
{
result = result + term;
}
return result;
}
template<typename FieldT>
linear_combination<FieldT> pb_packing_sum(const pb_linear_combination_array<FieldT> &v)
{
FieldT twoi = FieldT::one(); // will hold 2^i entering each iteration
std::vector<linear_term<FieldT> > all_terms;
for (auto &lc : v)
{
for (auto &term : lc.terms)
{
all_terms.emplace_back(twoi * term);
}
twoi += twoi;
}
return linear_combination<FieldT>(all_terms);
}
template<typename FieldT>
linear_combination<FieldT> pb_coeff_sum(const pb_linear_combination_array<FieldT> &v, const std::vector<FieldT> &coeffs)
{
assert(v.size() == coeffs.size());
std::vector<linear_term<FieldT> > all_terms;
auto coeff_it = coeffs.begin();
for (auto &lc : v)
{
for (auto &term : lc.terms)
{
all_terms.emplace_back((*coeff_it) * term);
}
++coeff_it;
}
return linear_combination<FieldT>(all_terms);
}
} // libsnark
#endif // PB_VARIABLE_TCC