forked from akosba/libsnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.cpp
217 lines (188 loc) · 8.41 KB
/
constraint.cpp
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
/** @file
*****************************************************************************
Implementation of the Constraint class.
*****************************************************************************
* @author This file is part of libsnark, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <set>
#include <libsnark/gadgetlib2/constraint.hpp>
#include <libsnark/gadgetlib2/variable.hpp>
using ::std::string;
using ::std::vector;
using ::std::set;
using ::std::cout;
using ::std::cerr;
using ::std::endl;
using ::std::shared_ptr;
namespace gadgetlib2 {
/*************************************************************************************************/
/*************************************************************************************************/
/******************* ******************/
/******************* class Constraint ******************/
/******************* ******************/
/*************************************************************************************************/
/*************************************************************************************************/
#ifdef DEBUG
Constraint::Constraint(const string& name) : name_(name) {}
#else
Constraint::Constraint(const string& name) { libff::UNUSED(name); }
#endif
string Constraint::name() const {
# ifdef DEBUG
return name_;
# else
return "";
# endif
}
/***********************************/
/*** END OF CLASS DEFINITION ***/
/***********************************/
/*************************************************************************************************/
/*************************************************************************************************/
/******************* ******************/
/******************* class Rank1Constraint ******************/
/******************* ******************/
/*************************************************************************************************/
/*************************************************************************************************/
Rank1Constraint::Rank1Constraint(const LinearCombination &a,
const LinearCombination &b,
const LinearCombination &c,
const string& name)
: Constraint(name), a_(a), b_(b), c_(c) {}
LinearCombination Rank1Constraint::a() const {return a_;}
LinearCombination Rank1Constraint::b() const {return b_;}
LinearCombination Rank1Constraint::c() const {return c_;}
bool Rank1Constraint::isSatisfied(const VariableAssignment& assignment,
const PrintOptions& printOnFail) const {
const FElem ares = a_.eval(assignment);
const FElem bres = b_.eval(assignment);
const FElem cres = c_.eval(assignment);
if (ares*bres != cres) {
# ifdef DEBUG
if (printOnFail == PrintOptions::DBG_PRINT_IF_NOT_SATISFIED) {
cerr << GADGETLIB2_FMT("Constraint named \"%s\" not satisfied. Constraint is:",
name().c_str()) << endl;
cerr << annotation() << endl;
cerr << "Variable assignments are:" << endl;
const Variable::set varSet = getUsedVariables();
for(const Variable& var : varSet) {
cerr << var.name() << ": " << assignment.at(var).asString() << endl;
}
cerr << "a: " << ares.asString() << endl;
cerr << "b: " << bres.asString() << endl;
cerr << "a*b: " << (ares*bres).asString() << endl;
cerr << "c: " << cres.asString() << endl;
}
# else
libff::UNUSED(printOnFail);
# endif
return false;
}
return true;
}
string Rank1Constraint::annotation() const {
# ifndef DEBUG
return "";
# endif
return string("( ") + a_.asString() + " ) * ( " + b_.asString() + " ) = "+ c_.asString();
}
const Variable::set Rank1Constraint::getUsedVariables() const {
Variable::set retSet;
const Variable::set aSet = a_.getUsedVariables();
retSet.insert(aSet.begin(), aSet.end());
const Variable::set bSet = b_.getUsedVariables();
retSet.insert(bSet.begin(), bSet.end());
const Variable::set cSet = c_.getUsedVariables();
retSet.insert(cSet.begin(), cSet.end());
return retSet;
}
/***********************************/
/*** END OF CLASS DEFINITION ***/
/***********************************/
/*************************************************************************************************/
/*************************************************************************************************/
/******************* ******************/
/******************* class PolynomialConstraint ******************/
/******************* ******************/
/*************************************************************************************************/
/*************************************************************************************************/
PolynomialConstraint::PolynomialConstraint(const Polynomial& a, const Polynomial& b,
const string& name) : Constraint(name), a_(a), b_(b) {}
bool PolynomialConstraint::isSatisfied(const VariableAssignment& assignment,
const PrintOptions& printOnFail) const {
const FElem aEval = a_.eval(assignment);
const FElem bEval = b_.eval(assignment);
if (aEval != bEval) {
# ifdef DEBUG
if(printOnFail == PrintOptions::DBG_PRINT_IF_NOT_SATISFIED) {
cerr << GADGETLIB2_FMT("Constraint named \"%s\" not satisfied. Constraint is:",
name().c_str()) << endl;
cerr << annotation() << endl;
cerr << "Expecting: " << aEval << " == " << bEval << endl;
cerr << "Variable assignments are:" << endl;
const Variable::set varSet = getUsedVariables();
for(const Variable& var : varSet) {
cerr << var.name() << ": " << assignment.at(var).asString() << endl;
}
}
# else
libff::UNUSED(printOnFail);
# endif
return false;
}
return true;
}
string PolynomialConstraint::annotation() const {
# ifndef DEBUG
return "";
# endif
return a_.asString() + " == " + b_.asString();
}
const Variable::set PolynomialConstraint::getUsedVariables() const {
Variable::set retSet;
const Variable::set aSet = a_.getUsedVariables();
retSet.insert(aSet.begin(), aSet.end());
const Variable::set bSet = b_.getUsedVariables();
retSet.insert(bSet.begin(), bSet.end());
return retSet;
}
/***********************************/
/*** END OF CLASS DEFINITION ***/
/***********************************/
void ConstraintSystem::addConstraint(const Rank1Constraint& c) {
constraintsPtrs_.emplace_back(::std::shared_ptr<Constraint>(new Rank1Constraint(c)));
}
void ConstraintSystem::addConstraint(const PolynomialConstraint& c) {
constraintsPtrs_.emplace_back(::std::shared_ptr<Constraint>(new PolynomialConstraint(c)));
}
bool ConstraintSystem::isSatisfied(const VariableAssignment& assignment,
const PrintOptions& printOnFail) const {
for(size_t i = 0; i < constraintsPtrs_.size(); ++i) {
if (!constraintsPtrs_[i]->isSatisfied(assignment, printOnFail)){
return false;
}
}
return true;
}
string ConstraintSystem::annotation() const {
string retVal("\n");
for(auto i = constraintsPtrs_.begin(); i != constraintsPtrs_.end(); ++i) {
retVal += (*i)->annotation() + '\n';
}
return retVal;
}
Variable::set ConstraintSystem::getUsedVariables() const {
Variable::set retSet;
for(auto& pConstraint : constraintsPtrs_) {
const Variable::set curSet = pConstraint->getUsedVariables();
retSet.insert(curSet.begin(), curSet.end());
}
return retSet;
}
} // namespace gadgetlib2