forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTest_R1CS.hpp
356 lines (282 loc) · 9.83 KB
/
AutoTest_R1CS.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef _SNARKLIB_AUTOTEST_R1CS_HPP_
#define _SNARKLIB_AUTOTEST_R1CS_HPP_
#include <cstdint>
#include <ostream>
#include <sstream>
#include <string>
#ifdef USE_OLD_LIBSNARK
#include /*libsnark*/ "r1cs/r1cs.hpp"
#else
#include /*libsnark*/ "relations/constraint_satisfaction_problems/r1cs/r1cs.hpp"
#endif
#include "snarklib/ForeignLib.hpp"
#include "snarklib/HugeSystem.hpp"
#include "snarklib/Rank1DSL.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// base class for rank-1 constraint system with number of inputs
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS
{
public:
std::string r1csName() const {
std::stringstream ss;
ss << typeid(*this).name();
return ss.str();
}
const libsnark::r1cs_constraint_system<U>& systemA() const { return m_csA; }
#ifdef USE_OLD_LIBSNARK
const libsnark::r1cs_variable_assignment<U>& witnessA() const { return m_witnessA; }
const libsnark::r1cs_variable_assignment<U>& inputA() const { return m_inputA; }
#else
const libsnark::r1cs_auxiliary_input<U>& witnessA() const { return m_witnessA; }
const libsnark::r1cs_primary_input<U>& inputA() const { return m_inputA; }
#endif
const SYS<T>& systemB() const { return m_csB; }
const R1Witness<T>& witnessB() const { return m_witnessB; }
const R1Witness<T>& inputB() const { return m_inputB; }
const std::size_t numCircuitInputs() const {
return m_inputB.size();
}
protected:
AutoTestR1CS(const std::string& filePrefix)
: m_filePrefix(filePrefix)
{}
void init_A_from_B() {
copy_libsnark(
this->m_csB,
this->m_witnessB,
this->m_inputB,
this->m_csA,
this->m_witnessA,
this->m_inputA);
}
void addBooleanity_B(const R1Variable<T>& x) {
m_csB.addConstraint(x * (T::one() - x) == T::zero());
}
void clearAppend(R1System<T>& a) {}
void finalize(R1System<T>& a) {}
void clearAppend(HugeSystem<T>& a) { a.clearAppend(m_filePrefix, 1); }
void finalize(HugeSystem<T>& a) { a.finalize(numCircuitInputs()); }
libsnark::r1cs_constraint_system<U> m_csA;
#ifdef USE_OLD_LIBSNARK
libsnark::r1cs_variable_assignment<U> m_witnessA, m_inputA;
#else
libsnark::r1cs_auxiliary_input<U> m_witnessA;
libsnark::r1cs_primary_input<U> m_inputA;
#endif
SYS<T> m_csB;
R1Witness<T> m_witnessB, m_inputB;
private:
const std::string m_filePrefix;
};
template <template <typename> class SYS, typename T, typename U>
std::ostream& operator<< (std::ostream& out, const AutoTestR1CS<SYS, T, U>& a) {
return out << a.r1csName();
}
////////////////////////////////////////////////////////////////////////////////
// single AND gate
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS_AND : public AutoTestR1CS<SYS, T, U>
{
public:
AutoTestR1CS_AND(const bool x_IC, const bool y_IC, const std::string& filePrefix)
: AutoTestR1CS<SYS, T, U>(filePrefix),
m_booleanityX(x_IC),
m_booleanityY(y_IC)
{
initB();
this->init_A_from_B();
}
private:
void initB() {
this->clearAppend(this->m_csB);
R1Variable<T> x(1), y(2), z(3);
this->m_csB.addConstraint(x * y == z);
if (m_booleanityX) this->addBooleanity_B(x);
if (m_booleanityY) this->addBooleanity_B(y);
this->m_csB.swap_AB_if_beneficial();
this->m_witnessB.assignVar(x, T::one());
this->m_witnessB.assignVar(y, T::one());
this->m_witnessB.assignVar(z, T::one());
this->m_inputB.assignVar(x, T::one());
this->m_inputB.assignVar(y, T::one());
this->finalize(this->m_csB);
}
const bool m_booleanityX;
const bool m_booleanityY;
};
////////////////////////////////////////////////////////////////////////////////
// single OR gate
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS_OR : public AutoTestR1CS<SYS, T, U>
{
public:
AutoTestR1CS_OR(const bool x_IC, const bool y_IC, const std::string& filePrefix)
: AutoTestR1CS<SYS, T, U>(filePrefix),
m_booleanityX(x_IC),
m_booleanityY(y_IC)
{
initB();
this->init_A_from_B();
}
private:
void initB() {
this->clearAppend(this->m_csB);
R1Variable<T> x(1), y(2), z(3);
this->m_csB.addConstraint(x + y - z == x * y);
if (m_booleanityX) this->addBooleanity_B(x);
if (m_booleanityY) this->addBooleanity_B(y);
this->m_csB.swap_AB_if_beneficial();
this->m_witnessB.assignVar(x, T::one());
this->m_witnessB.assignVar(y, T::one());
this->m_witnessB.assignVar(z, T::one());
this->m_inputB.assignVar(x, T::one());
this->m_inputB.assignVar(y, T::one());
this->finalize(this->m_csB);
}
const bool m_booleanityX;
const bool m_booleanityY;
};
////////////////////////////////////////////////////////////////////////////////
// single XOR gate
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS_XOR : public AutoTestR1CS<SYS, T, U>
{
public:
AutoTestR1CS_XOR(const bool x_IC, const bool y_IC, const std::string& filePrefix)
: AutoTestR1CS<SYS, T, U>(filePrefix),
m_booleanityX(x_IC),
m_booleanityY(y_IC)
{
initB();
this->init_A_from_B();
}
private:
void initB() {
this->clearAppend(this->m_csB);
R1Variable<T> x(1), y(2), z(3);
const auto TWO = T::one() + T::one();
this->m_csB.addConstraint(x + y - z == (TWO * x) * y);
if (m_booleanityX) this->addBooleanity_B(x);
if (m_booleanityY) this->addBooleanity_B(y);
this->m_csB.swap_AB_if_beneficial();
this->m_witnessB.assignVar(x, T::one());
this->m_witnessB.assignVar(y, T::one());
this->m_witnessB.assignVar(z, T::zero());
this->m_inputB.assignVar(x, T::one());
this->m_inputB.assignVar(y, T::one());
this->finalize(this->m_csB);
}
const bool m_booleanityX;
const bool m_booleanityY;
};
////////////////////////////////////////////////////////////////////////////////
// single CMPLMNT gate
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS_CMPLMNT : public AutoTestR1CS<SYS, T, U>
{
public:
AutoTestR1CS_CMPLMNT(const bool x_IC, const std::string& filePrefix)
: AutoTestR1CS<SYS, T, U>(filePrefix),
m_booleanityX(x_IC)
{
initB();
this->init_A_from_B();
}
private:
void initB() {
this->clearAppend(this->m_csB);
R1Variable<T> x(1), y(2);
this->m_csB.addConstraint(x + y == T::one());
if (m_booleanityX) this->addBooleanity_B(x);
this->m_csB.swap_AB_if_beneficial();
this->m_witnessB.assignVar(x, T::zero());
this->m_witnessB.assignVar(y, T::one());
this->m_inputB.assignVar(x, T::zero());
this->finalize(this->m_csB);
}
const bool m_booleanityX;
};
////////////////////////////////////////////////////////////////////////////////
// unsoundness circuit from: A Note on the Unsoundness of vnTinyRAM's SNARK
//
// Six scalar wires, three multiplication gates:
//
// c1, c2, c3 are free
// c4 = c1 * c2
// c5 = c1 * c3
// c6 = c4 * c5
//
// To demonstrate unsoundness, the input wires are: c1, c2, c3, c6.
// As input variables must be first, it is convenient to relabel the
// wires so the circuit is:
//
// d1, d2, d3 are free
// d5 = d1 * d2
// d6 = d1 * d3
// d4 = d5 * d6
//
// Then the input wires are: d1, d2, d3, d4.
//
template <template <typename> class SYS, typename T, typename U>
class AutoTestR1CS_Soundness : public AutoTestR1CS<SYS, T, U>
{
public:
// may be unsound
AutoTestR1CS_Soundness(const unsigned long c1,
const unsigned long c2,
const unsigned long c3,
const unsigned long c4,
const unsigned long c5,
const unsigned long c6,
const std::string& filePrefix)
: AutoTestR1CS<SYS, T, U>(filePrefix),
m_d1(c1), // d1 is c1
m_d2(c2), // d2 is c2
m_d3(c3), // d3 is c3
m_d4(c6), // d4 is c6
m_d5(c4), // d5 is c4
m_d6(c5) // d6 is c5
{
initB();
this->init_A_from_B();
}
// will be sound as input is consistent with witness
AutoTestR1CS_Soundness(const unsigned long c1,
const unsigned long c2,
const unsigned long c3,
const std::string& filePrefix)
: AutoTestR1CS_Soundness{c1, c2, c3, c1*c2, c1*c3, c1*c1*c2*c3, filePrefix}
{}
private:
void initB() {
this->clearAppend(this->m_csB);
R1Variable<T> d1(1), d2(2), d3(3), d4(4), d5(5), d6(6);
this->m_csB.addConstraint(d1 * d2 == d5);
this->m_csB.addConstraint(d1 * d3 == d6);
this->m_csB.addConstraint(d5 * d6 == d4);
this->m_csB.swap_AB_if_beneficial();
// witness always consistent
this->m_witnessB.assignVar(d1, T(m_d1));
this->m_witnessB.assignVar(d2, T(m_d2));
this->m_witnessB.assignVar(d3, T(m_d3));
this->m_witnessB.assignVar(d4, T(m_d1 * m_d1 * m_d2 * m_d3));
this->m_witnessB.assignVar(d5, T(m_d1 * m_d2));
this->m_witnessB.assignVar(d6, T(m_d1 * m_d3));
// public inputs may be inconsistent
this->m_inputB.assignVar(d1, T(m_d1));
this->m_inputB.assignVar(d2, T(m_d2));
this->m_inputB.assignVar(d3, T(m_d3));
this->m_inputB.assignVar(d4, T(m_d4));
this->finalize(this->m_csB);
}
const unsigned long m_d1, m_d2, m_d3, m_d4, m_d5, m_d6;
};
} // namespace snarklib
#endif