forked from meelgroup/bosphorus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplifybysat.cpp
More file actions
279 lines (237 loc) · 9.04 KB
/
Copy pathsimplifybysat.cpp
File metadata and controls
279 lines (237 loc) · 9.04 KB
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
/*****************************************************************************
Copyright (C) 2016 Security Research Labs
Copyright (C) 2018 Mate Soos, Davin Choo, Kian Ming A. Chai, DSO National Laboratories
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***********************************************/
#include "simplifybysat.hpp"
#include "cryptominisat5/cryptominisat.h"
#include "time_mem.h"
using std::cout;
using std::endl;
using namespace BLib;
inline bool testSolution(const ANF& anf, const vector<lbool>& solution)
{
bool goodSol = anf.evaluate(solution);
if (!goodSol) {
cout << "ERROR! Solution found is incorrect!" << endl;
exit(-1);
}
return goodSol;
}
SimplifyBySat::SimplifyBySat(const CNF& _cnf, const ConfigData& _config)
: config(_config), cnf(_cnf)
{
// Create SAT solver
solver = new CMSat::SATSolver();
solver->set_verbosity(config.verbosity >= 5 ? 1 : 0);
solver->set_num_threads(config.numThreads);
}
SimplifyBySat::~SimplifyBySat()
{
delete solver;
}
void SimplifyBySat::addClausesToSolver(size_t beg)
{
const auto& csets = cnf.getClauses();
for (auto it = csets.begin() + beg; it != csets.end(); ++it) {
for (const Clause& c : it->first) {
vector<Lit> lits = c.getClause();
vector<CMSat::Lit>* lits2 = (vector<CMSat::Lit>*)&lits;
solver->add_clause(*lits2);
}
}
}
int SimplifyBySat::extractUnitaries(vector<BoolePolynomial>& loop_learnt)
{
vector<CMSat::Lit> units = solver->get_zero_assigned_lits();
if (config.verbosity >= 3) {
cout << units.size(); // "c Number of unit learnt clauses: " << << endl;
}
uint64_t numVarLearnt = 0;
for (const CMSat::Lit& unit : units) {
//If var represents a partial XOR clause, skip it
if (!cnf.varRepresentsMonomial(unit.var())) {
continue;
}
BooleMonomial m = cnf.getMonomForVar(unit.var());
assert(m.deg() > 0);
// Monomial is high degree, and FALSE. That doesn't help much
if (m.deg() > 1 && unit.sign() == true) {
continue;
}
// If DEG is 1, then this will set the variable
// If DEG is >0 and setting is TRUE, the addBoolePolynomial() will
// automatically set all variables in the monomial to ONE
BoolePolynomial poly(!unit.sign(), cnf.getANFRing());
poly += m;
loop_learnt.push_back(poly);
++numVarLearnt;
}
if (config.verbosity >= 3) {
cout << '/'
<< numVarLearnt; // "c Num ANF assignments learnt: " << << endl;
}
return numVarLearnt;
}
int SimplifyBySat::extractBinaries(vector<BoolePolynomial>& loop_learnt)
{
auto binXors = solver->get_all_binary_xors();
if (config.verbosity >= 3) {
cout << '/'
<< binXors.size(); //"c Number of binary clauses:" << << endl;
}
uint64_t numVarReplaced = 0;
for (auto& pair : binXors) {
CMSat::Lit lit1 = pair.first;
CMSat::Lit lit2 = pair.second;
uint32_t v1 = lit1.var();
uint32_t v2 = lit2.var();
assert(v1 != v2);
//If any of the two vars represent a partial XOR clause, skip it
if (!cnf.varRepresentsMonomial(v1) || !cnf.varRepresentsMonomial(v2)) {
continue;
}
BooleMonomial m1 = cnf.getMonomForVar(v1);
BooleMonomial m2 = cnf.getMonomForVar(v2);
// Not anti/equivalence
if (m1.deg() > 1 || m2.deg() > 1) {
continue;
}
BoolePolynomial poly((lit1.sign() ^ lit2.sign()), cnf.getANFRing());
poly += m1;
poly += m2;
loop_learnt.push_back(poly);
++numVarReplaced;
}
if (config.verbosity >= 3) {
cout
<< '/'
<< numVarReplaced; //"c Num ANF anti/equivalence learnt: " << << endl;
}
return numVarReplaced;
}
bool SimplifyBySat::addPolynomial(vector<BoolePolynomial>& loop_learnt,
const pair<vector<uint32_t>, bool>& cnf_poly)
{
BoolePolynomial new_poly(cnf_poly.second, cnf.getANFRing());
for (const uint32_t& var_idx : cnf_poly.first) {
if (!cnf.varRepresentsMonomial(var_idx)) {
return false;
}
new_poly += cnf.getMonomForVar(var_idx);
}
if (new_poly.deg() == 1) {
loop_learnt.push_back(new_poly);
return true;
}
return false;
}
int SimplifyBySat::process(
vector<BoolePolynomial>& loop_learnt,
const vector<pair<vector<uint32_t>, bool> >& extracted)
{
int num_polys = 0;
for (const pair<vector<uint32_t>, bool>& cnf_poly : extracted) {
num_polys += addPolynomial(loop_learnt, cnf_poly);
}
return num_polys;
}
int SimplifyBySat::extractLinear(vector<BoolePolynomial>& loop_learnt)
{
int num_polys = 0;
num_polys += process(loop_learnt, solver->get_recovered_xors(false));
num_polys += process(loop_learnt, solver->get_recovered_xors(true));
if (config.verbosity >= 3) {
cout << '/' << num_polys; //"c Num ANF linear equations learnt: "
}
return num_polys;
}
lbool SimplifyBySat::simplify(const uint64_t numConfl_lim,
const uint64_t numConflinc, const double time_limit,
const size_t cbeg,
vector<BoolePolynomial>& loop_learnt, ANF& anf)
{
if (!anf.getOK()) {
cout << "c Nothing to simplify: UNSAT" << endl;
return l_False;
}
//Add problem to solver
solver->new_vars(cnf.getNumVars() - solver->nVars());
addClausesToSolver(cbeg);
double time_left = time_limit - cpuTime();
uint64_t numConfl_left = numConfl_lim;
// Solve system of CNF until conflict limit
if (config.verbosity >= 3) {
cout << "c Converted CNF has " << cnf.getNumVars() << " variables and "
<< cnf.getNumClauses() << " clause-sets to solve in increments of "
<< numConflinc << " conflicts until " << numConfl_lim
<< " conflicts within " << std::scientific << time_left
<< " seconds" << std::fixed << endl;
}
//Run the solver
Solution sol;
solver->set_max_time(time_left);
solver->set_timeout_all_calls(time_left);
solver->set_max_confl(std::min(numConflinc, numConfl_left));
sol.ret = solver->solve();
time_left = time_limit - cpuTime();
numConfl_left =
(numConfl_left > numConflinc) ? (numConfl_left - numConflinc) : 0;
if (config.verbosity >= 4)
cout << "c ... " << solver->get_sum_conflicts() << '/'
<< solver->get_sum_propagations() << '/'
<< solver->get_sum_decisions() << ' ' << std::scientific
<< time_left << std::fixed << " seconds" << endl;
//Extract data
if (config.verbosity >= 3) cout << "c Number of unit/assigns/binary/(anti-)equiv/linear ";
extractUnitaries(loop_learnt);
extractBinaries(loop_learnt);
extractLinear(loop_learnt);
if (config.verbosity >= 3) {
cout << endl;
}
//Deal with result
if (sol.ret == l_False) {
cout << "c UNSAT returned by solver" << endl;
anf.addBoolePolynomial(BoolePolynomial(true, anf.getRing()));
}
//Solution found
if(sol.ret == l_True) {
if (config.verbosity >= 1) {
cout << "c [SAT] has found a solution" << endl;
}
/*const size_t sz = solver->get_model().size();
vector<lbool> solutionFromSolver(sz, l_Undef);
for (uint32_t i = 0; i < sz; ++i) {
//solutionFromSolver[i] = solver->get_model()[i];
CMSat::lbool m = solver->get_model()[i];
if (m == CMSat::l_Undef) {
solutionFromSolver[i] = l_Undef;
} else if (m == CMSat::l_True) {
solutionFromSolver[i] = l_True;
} else if (m == CMSat::l_False) {
solutionFromSolver[i] = l_False;
}
assert(solutionFromSolver[i] != l_Undef);
}
vector<lbool> solution2 = cnf.mapSolToOrig(solutionFromSolver);
sol.sol = anf.extendSolution(solution2);
....-> we could use sol.sol here, but it's useless
*/
}
return sol.ret;
}