forked from meelgroup/bosphorus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplacer.cpp
More file actions
384 lines (327 loc) · 11.4 KB
/
Copy pathreplacer.cpp
File metadata and controls
384 lines (327 loc) · 11.4 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*****************************************************************************
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 "replacer.hpp"
#include <iostream>
#include <fstream>
#include "anf.hpp"
using std::cout;
using std::endl;
using namespace BLib;
bool Replacer::evaluate(const vector<lbool>& vals) const
{
bool ret = true;
size_t num = 0;
for (vector<lbool>::const_iterator it = value.begin(), end = value.end();
it != end; it++, num++) {
if (*it == l_Undef)
continue;
assert(vals.size() >= num);
ret &= (vals[num] == *it);
}
num = 0;
for (vector<Lit>::const_iterator it = replaceTable.begin(),
end = replaceTable.end();
it != end; it++, num++) {
if (num == it->var())
continue;
assert(vals.size() >= it->var());
assert(vals.size() >= num);
lbool one = vals[it->var()] ^ it->sign();
lbool other = vals[num];
ret &= (one == other);
}
return ret;
}
/*
* Suppose monomial is a*b*c and we want to replace:
* a with 1+d
* b with e
* c with 1+f
* (Note: e may be b itself)
*
* init : ret = 1
* after a: ret = (d) + (1)
* = d + 1
* after b: ret = e*d + e
* after c: ret = (f*e*d + f*e) + (e*d + e)
* = f*e*d + f*e + e*d + e
* = (1+d)*(e)*(1+f)
* = a*b*c
*/
BoolePolynomial Replacer::update(const BooleMonomial& m) const
{
BoolePolynomial ret(true, m.ring());
for (const uint32_t v : m) {
if (value[v] != l_Undef) {
if (value[v] == l_True)
continue;
else
return BoolePolynomial(m.ring());
}
assert(replaceTable.size() > v); //Variable must exist
const Lit lit = replaceTable[v];
BoolePolynomial alsoAdd(m.ring());
if (lit.sign())
alsoAdd = ret;
ret *= BooleVariable(lit.var(), m.ring());
ret += alsoAdd;
}
return ret;
}
BoolePolynomial Replacer::update(const BoolePolynomial& eq) const
{
BoolePolynomial ret = BoolePolynomial(eq.ring());
for (const BooleMonomial& mono : eq) {
ret += update(mono);
}
return ret;
}
bool Replacer::willUpdate(const BoolePolynomial& eq) const
{
for (const uint32_t v : eq.usedVariables()) {
if (value[v] != l_Undef)
return true;
if (replaceTable[v] != Lit(v, false))
return true;
}
return false;
}
vector<uint32_t> Replacer::setValue(uint32_t var, bool val)
{
vector<uint32_t> alsoUpdated;
alsoUpdated.push_back(var);
//update to representative
if (replaceTable[var] != Lit(var, false)) {
val ^= replaceTable[var].sign();
var = replaceTable[var].var();
}
alsoUpdated.push_back(var);
//set value
if (value[var] != l_Undef) {
if (value[var] != boolToLBool(val)) {
ok = false;
}
return alsoUpdated;
}
value[var] = boolToLBool(val);
//update anti/equivalent variables
map<uint32_t, vector<uint32_t> >::iterator it = revReplaceTable.find(var);
if (it == revReplaceTable.end()) {
return alsoUpdated;
}
vector<uint32_t>& vars = it->second;
for (vector<uint32_t>::iterator it2 = vars.begin(), end2 = vars.end();
it2 != end2; it2++) {
assert(replaceTable[*it2].var() == var);
value[*it2] = boolToLBool(replaceTable[*it2].sign() ^ val);
alsoUpdated.push_back(*it2);
}
return alsoUpdated;
}
vector<uint32_t> Replacer::setReplace(uint32_t var, Lit lit)
{
assert(var < replaceTable.size());
assert(lit.var() < replaceTable.size());
vector<uint32_t> ret;
//if value is set somewhere, set it
if (value[var] != l_Undef) {
return setValue(lit.var(), (value[var] ^ lit.sign()) == l_True);
}
if (value[lit.var()] != l_Undef) {
return setValue(var, (value[lit.var()] ^ lit.sign()) == l_True);
}
//move forward
lit ^= replaceTable[var].sign();
var = replaceTable[var].var();
lit = replaceTable[lit.var()] ^ lit.sign();
ret.push_back(lit.var());
ret.push_back(var);
//they are already replaced with each other
if (lit.var() == var) {
//..but invertedly, so UNSAT
if (lit.sign())
ok = false;
return ret;
}
//lit.var() is not the head of a replace group
if (revReplaceTable.find(lit.var()) == revReplaceTable.end()) {
replaceTable[lit.var()] = Lit(var, lit.sign());
revReplaceTable[var].push_back(lit.var());
//These may have been updated
for (vector<uint32_t>::const_iterator it = revReplaceTable[var].begin(),
end = revReplaceTable[var].end();
it != end; it++) {
ret.push_back(*it);
}
return ret;
}
//var is not the head of a replace group
if (revReplaceTable.find(var) == revReplaceTable.end()) {
replaceTable[var] = lit;
revReplaceTable[lit.var()].push_back(var);
//These may have been updated
for (vector<uint32_t>::const_iterator
it = revReplaceTable[lit.var()].begin(),
end = revReplaceTable[lit.var()].end();
it != end; it++) {
ret.push_back(*it);
}
return ret;
}
//both are dependent, move lit's dependencies under var
assert(revReplaceTable.find(lit.var()) != revReplaceTable.end());
assert(revReplaceTable.find(var) != revReplaceTable.end());
vector<uint32_t>& vars = revReplaceTable[lit.var()];
for (vector<uint32_t>::iterator it = vars.begin(), end = vars.end();
it != end; it++) {
revReplaceTable[var].push_back(*it);
replaceTable[*it] = Lit(var, replaceTable[*it].sign() ^ lit.sign());
ret.push_back(*it);
}
revReplaceTable.erase(revReplaceTable.find(lit.var()));
replaceTable[lit.var()] = Lit(var, lit.sign());
revReplaceTable[var].push_back(lit.var());
//These may have been updated
for (vector<uint32_t>::const_iterator it = revReplaceTable[var].begin(),
end = revReplaceTable[var].end();
it != end; it++) {
ret.push_back(*it);
}
return ret;
}
void Replacer::print_solution_map(std::ofstream* ofs)
{
uint32_t num = 0;
for (vector<lbool>::const_iterator it = value.begin(), end = value.end();
it != end; it++, num++) {
if (*it != l_Undef) {
(*ofs) << "ANF-var-val " << num << " = " << *it << endl;
}
}
num = 0;
for (vector<Lit>::const_iterator it = replaceTable.begin(), end = replaceTable.end();
it != end; it++, num++
) {
//it is not replaced
if (it->var() == num)
continue;
//maybe never solved for, because equation is "a = b", and neither "a", nor "b" appear anywhere else
//so, just set the value randomly... to true :)
(*ofs) << "must-set-ANF-var-to-any " << it->var() << endl;
(*ofs) << "ANF-var " << num << " = "
<< "ANF-var " << it->var() << " ^ " << it->sign() << endl;
}
}
set<size_t> Replacer::get_proj_map(const set<size_t>& vars) const {
set<size_t> ret;
for(const auto& v: vars) {
if (value[v] != l_Undef) continue;
Lit l;
l = replaceTable[v];
if (value[l.var()] != l_Undef) continue;
ret.insert(l.var());
}
return ret;
}
void Replacer::get_solution_map(map<uint32_t, VarMap>& ret) const
{
uint32_t num = 0;
for (vector<lbool>::const_iterator it = value.begin(), end = value.end();
it != end; it++, num++) {
if (*it != l_Undef) {
VarMap m;
m.type = Bosph::VarMap::fixed;
m.value = (*it == l_True);
ret[num] = m;
}
}
num = 0;
for (vector<Lit>::const_iterator it = replaceTable.begin(), end = replaceTable.end();
it != end; it++, num++
) {
//it is not replaced
if (it->var() == num)
continue;
//Don't overwrite existing value with this
if (ret.find(it->var()) == ret.end()) {
VarMap m;
m.type = Bosph::VarMap::must_set;
ret[it->var()] = m;
}
VarMap m2;
m2.type = Bosph::VarMap::anf_repl;
m2.other_var = it->var();
m2.inv = it->sign();
ret[num] = m2;
}
}
vector<lbool> Replacer::extendSolution(const vector<lbool>& solution) const
{
assert(solution.size() <= value.size());
vector<lbool> sol2(solution);
sol2.resize(value.size(), l_Undef);
//add stored solutions
assert(sol2.size() == value.size());
size_t num = 0;
for (vector<lbool>::const_iterator it = value.begin(), end = value.end();
it != end; it++, num++) {
if (sol2[num] != l_Undef && *it != l_Undef) {
if (sol2[num] != *it) {
cout << "Solved solution and solution stored by replacer "
"differ!\n";
exit(-1);
}
continue;
}
if (*it != l_Undef) {
sol2[num] = *it;
}
}
//Add replaced variables
assert(sol2.size() == replaceTable.size());
num = 0;
for (vector<Lit>::const_iterator it = replaceTable.begin(),
end = replaceTable.end();
it != end; it++, num++) {
//uint32_t is not replaced
if (it->var() == num)
continue;
//maybe never solved for, because equation is "a = b", and neither "a", nor "b" appear anywhere else
//so, just set the value randomly... to true :)
if (sol2[it->var()] == l_Undef)
sol2[it->var()] = l_True;
const lbool val = sol2[it->var()] ^ it->sign();
if (sol2[num] != l_Undef && sol2[num] != val) {
cout << "num:" << num << " var:" << it->var()
<< "sign: " << (int)it->sign() << endl;
cout << "sol2[num]: " << sol2[num]
<< " sol2[it->var()]:" << sol2[it->var()] << endl;
cout << "Solved replaced solution and stored solution differ!"
<< endl;
exit(-1);
}
sol2[num] = val;
}
return sol2;
}
bool Replacer::isReplaced(const uint32_t var) const
{
return getReplaced(var).var() != var;
}