forked from siweisun/anf2cnf-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanf2cnf.py
executable file
·668 lines (576 loc) · 25.6 KB
/
anf2cnf.py
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/env python3
import itertools
import os
import re
import sys
"""
====================================================================================
Polynomial class
====================================================================================
"""
class Poly():
def __init__(self):
self._deg = 0
# The support of the polynomial sorted by degree.
self._support = dict()
# Return the degree of the polynomial.
# def _get_deg(self): return self._deg
# Return all terms of a certain degree.
def _get_terms_by_deg(self,deg):
if deg in self._support:
return self._support[deg]
else:
return []
# Add a term (in list format) to the polynomial. The operation respects the
# involutive property of the addition in F_2.
def _add_term(self,term):
# First term of a degree.
if len(term) not in self._support:
self._support[len(term)] = [term]
if self._deg < len(term):
self._deg = len(term)
# There are already other terms of a degree.
else:
terms = self._support[len(term)]
if term not in terms:
terms.append(term)
else:
terms.remove(term)
# Last term of that particular degree.
if terms == []:
del self._support[len(term)]
# Check if any elements are left in the polynomial.
if len(self._support.keys()) > 0:
# Update the degree.
self._deg = max(self._support.keys())
else: self._deg = -1
# Update terms of that degree.
else:
self._support[len(term)] = terms
# Return the polynomial in string format.
def to_string(self):
l = []
for deg in list(self._support.keys())[::-1]:
s = self._support[deg]
for i in range(len(s)):
l.append(str(s[i]))
return ' + '.join(l)
"""
====================================================================================
Data class
====================================================================================
"""
class _Data():
def __init__(self):
self._polys = list()
self._sub_polys = dict()
self._sub_poly_stack = list()
self._var_index = dict()
self._num_indets = 0
self._num_clauses = 0
"""
====================================================================================
Conversion functions
====================================================================================
"""
# Converts a quadratic (cubic) system of polynomials to (XOR-)CNF-SAT.
# Data: Object of the _Data class.
# cl: Cutting length, i.e. the maximal support length of a polynomial.
# qstrategy: Substitution strategy for quadratic terms.
# cstrategy: Substitution strategy for cubic terms.
# output: Format of the output.
# "XOR": XOR-CNF-SAT (for cryptominisat)
# "CNF": DIMACS CNF-SAT
def to_cnf(Data,cl,qstrategy,cstrategy,output,path):
strategies = {
"SS": ss,
"LPS": lps,
"DPS": dps,
"QPS": qps,
"CPS": cps
}
write_sub_poly = {
"SS": write_ss,
"LPS": write_lps,
"DPS": write_dps,
"QPS": write_qps,
"CPS": write_cps
}
ofile = open(path+".tmp",'a')
for i in range(len(Data._polys)):
#print("Polynomial:",Data._polys[i].to_string())
# Linearize the polynomial.
while len(Data._polys[i]._get_terms_by_deg(3)) != 0:
term = Data._polys[i]._get_terms_by_deg(3).pop(0)
strategies[cstrategy](Data,term,i)
while len(Data._polys[i]._get_terms_by_deg(2)) != 0:
term = Data._polys[i]._get_terms_by_deg(2).pop(0)
strategies[qstrategy](Data,term,i)
# Write new substitution polynomials to file.
while len(Data._sub_poly_stack) != 0:
sub_poly = Data._sub_poly_stack.pop(0)
c = write_sub_poly[sub_poly[0]](sub_poly,ofile)
Data._num_clauses += c
#print("Linearized:",Data._polys[i].to_string())
# Write linearized polynomial to file
if output == "XOR":
c = write_xor_poly(Data._polys[i],ofile)
Data._num_clauses += c
elif output == "CNF":
l = cut_poly(Data,i,cl)
for j in range(len(l)):
#print(j,":",l[j].to_string())
c = write_cnf_poly(l[j],ofile)
Data._num_clauses += c
ofile.close()
ofile = open(path+".tmp",'r')
contents = ofile.read()
ofile.close()
# Write the header and copy the clauses to the final CNF file.
out = open(path,'a')
out.write("p cnf {} {}\n".format(Data._num_indets,Data._num_clauses))
out.write(contents)
out.close()
# Delete temp file.
os.remove(path+".tmp")
"""
====================================================================================
Substitution strategies
====================================================================================
"""
# Perform the substitution of a term.
def substitute(Data,sub_poly,index):
# Construct the dicitionary key. (Note: sub_poly[0] contains the type, which is
# not needed for the key)
key = ' + '.join([str(sub_poly[i]) for i in range(1,len(sub_poly))])
if key not in Data._sub_polys:
Data._num_indets = Data._num_indets + 1
Data._sub_polys[key] = [Data._num_indets]
Data._polys[index]._add_term([Data._num_indets])
sub_poly.append([Data._num_indets])
Data._sub_poly_stack.append(sub_poly)
else:
subIndet = Data._sub_polys[key]
Data._polys[index]._add_term(subIndet)
# Standard substitution.
def ss(Data,term,index):
substitute(Data,["SS",term],index)
# Cut the poly according to the cutting length cl.
def cut_poly(Data,index,cl):
poly_list = []
lin_terms = Data._polys[index]._get_terms_by_deg(1)
f = Poly()
for i in range(len(lin_terms)):
if len(f._get_terms_by_deg(1)) == cl - 1:
Data._num_indets += 1
f._add_term([Data._num_indets])
poly_list.append(f)
f = Poly()
f._add_term([Data._num_indets])
f._add_term(lin_terms[i])
if (i == len(lin_terms) - 1):
if len(Data._polys[index]._get_terms_by_deg(0)) == 1:
f._add_term(Data._polys[index]._get_terms_by_deg(0)[0])
poly_list.append(f)
return poly_list
"""
====================================================================================
Strategies for quadratic terms
====================================================================================
"""
# Linear Partner Substitution.
def lps(Data,term,index):
i = -1
# Check for linear partners.
if [term[0]] in Data._polys[index]._get_terms_by_deg(1):
i = 0
elif [term[1]] in Data._polys[index]._get_terms_by_deg(1):
i = 1
# Linear partner found.
if (i == 0) or (i == 1):
# Remove linear partner from the polynomial.
Data._polys[index]._add_term([term[i]])
# Construct the term list for the substitution polynomial.
sub_poly = ["LPS",term,[term[i]]]
substitute(Data,sub_poly,index)
# No linear partner found.
else:
ss(Data,term,index)
# Double Partner Substitution.
def dps(Data,term,index):
if ([term[0]] in Data._polys[index]._get_terms_by_deg(1)) and \
([term[1]] in Data._polys[index]._get_terms_by_deg(1)) and \
([] in Data._polys[index]._get_terms_by_deg(0)):
# Remove linear partners from the polynomial.
Data._polys[index]._add_term([term[0]])
Data._polys[index]._add_term([term[1]])
Data._polys[index]._add_term([])
sub_poly = ["DPS",term,[term[0]],[term[1]],[]]
substitute(Data,sub_poly,index)
else:
lps(Data,term,index)
# Quadratic Partner Substitution.
def qps(Data,term,index):
x = False
terms = Data._polys[index]._get_terms_by_deg(2)
for t in terms:
u = set.intersection(set(term),set(t))
if len(u) == 1:
x = True
# Remove the quadratic partner from the polynomial.
Data._polys[index]._add_term(t)
sub_poly = ["QPS",term,t]
substitute(Data,sub_poly,index)
break
if x == False:
ss(Data,term,index)
"""
====================================================================================
Strategies for cubic terms
====================================================================================
"""
# Cubic Partner Substitution.
def cps(Data,term,index):
x = False
terms = Data._polys[index]._get_terms_by_deg(3)
for t in terms:
u = set.intersection(set(term),set(t))
if len(u) == 2:
x = True
# Remove the cubic partner from the polynomial.
Data._polys[index]._add_term(t)
sub_poly = ["CPS",term,t]
substitute(Data,sub_poly,index)
break
if x == False:
ss(Data,term,index)
"""
====================================================================================
CNF / XOR writing functions
====================================================================================
"""
def write_clauses(l,ofile):
for i in range(len(l)):
ofile.write("{}\n".format(l[i]))
def write_ss(p,ofile):
# List of clauses.
c = []
#print(p)
# Number of clauses.
if len(p[1]) == 2:
c.append(''.join([str(p[1][0])," -",str(p[2][0])," 0"]))
c.append(''.join([str(p[1][1])," -",str(p[2][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," -",str(p[1][1])," ",str(p[2][0])," 0"]))
elif len(p[1]) == 3:
c.append(''.join([str(p[1][0])," -",str(p[2][0])," 0"]))
c.append(''.join([str(p[1][1])," -",str(p[2][0])," 0"]))
c.append(''.join([str(p[1][2])," -",str(p[2][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," -",str(p[1][1])," -",str(p[1][2])," ",str(p[2][0])," 0"]))
write_clauses(c,ofile)
return len(c)
def write_lps(p,ofile):
c = []
#print(p)
# Move the partner indet to the first slot in the quadratic term.
if p[1][1] == p[2][0]:
p[1][0], p[1][1] = p[1][1], p[1][0]
c.append(''.join([str(p[1][0])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][1])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," ",str(p[1][1])," ",str(p[3][0])," 0"]))
write_clauses(c,ofile)
return len(c)
def write_dps(p,ofile):
c = []
#print(p)
c.append(''.join(["-",str(p[1][0])," -",str(p[5][0])," 0"]))
c.append(''.join(["-",str(p[1][1])," -",str(p[5][0])," 0"]))
c.append(''.join([str(p[1][0])," ",str(p[1][1])," ",str(p[5][0])," 0"]))
write_clauses(c,ofile)
return len(c)
def write_qps(p,ofile):
c = []
#print(p)
# Compute the common indet.
i = (set(p[1]) & set(p[2])).pop()
# Sort the quadratic terms s.t. the common indet is in the first slot.
if p[1][0] != i:
p[1][0], p[1][1] = p[1][1],p[1][0]
if p[2][0] != i:
p[2][0], p[2][1] = p[2][1],p[2][0]
c.append(''.join([str(p[1][0])," -",str(p[3][0])," 0"]))
c.append(''.join([str(p[1][1])," ",str(p[2][1])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][1])," -",str(p[2][1])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," -",str(p[1][1])," ",str(p[2][1])," ",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," ",str(p[1][1])," -",str(p[2][1])," ",str(p[3][0])," 0"]))
write_clauses(c,ofile)
return len(c)
def write_cps(p,ofile):
c = []
#print(p)
# Compute the common indets.
i = list(set(p[1]) & set(p[2]))
j = list(set(p[1]).difference(set(i)))
k = list(set(p[2]).difference(set(i)))
# Sort the cubic terms s.t. the common indets are in the first two slots.
p[1] = [i[0],i[1],j[0]]
p[2] = [i[0],i[1],k[0]]
c.append(''.join([str(p[1][0])," -",str(p[3][0])," 0"]))
c.append(''.join([str(p[1][1])," -",str(p[3][0])," 0"]))
c.append(''.join([str(p[1][2])," ",str(p[2][2])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][2])," -",str(p[2][2])," -",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," -",str(p[1][1])," -",str(p[1][2])," ",str(p[2][2])," ",str(p[3][0])," 0"]))
c.append(''.join(["-",str(p[1][0])," -",str(p[1][1])," ",str(p[1][2])," -",str(p[2][2])," ",str(p[3][0])," 0"]))
write_clauses(c,ofile)
return len(c)
def write_xor_poly(poly,ofile):
# Note: If the support does not include the constant factor 1, we invert the
# first variable. This way the whole XOR formula gets inverted.
if len(poly._get_terms_by_deg(0)) == 1:
s = "x"
else:
s = "x-"
ofile.write("{}\n".format(s + ' '.join([str(t[0]) for t in \
poly._get_terms_by_deg(1)[::-1]]) + " 0"))
return 1
def write_cnf_poly(poly,ofile):
# Flatten list of linear terms.
t = [str(u[0]) for u in poly._get_terms_by_deg(1)]
c = []
# Polynomial has no constant term.
if len(poly._get_terms_by_deg(0)) == 0 :
# x[1] + x[2]
if len(t) == 2:
c.append(''.join([ t[0]," -",t[1]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," 0"]))
# x[1] + x[2] + x[3]
elif len(t) == 3:
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," 0"]))
# x[1] + x[2] + x[3] + x[4]
elif len(t) == 4:
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3]," 0"]))
# x[1] + x[2] + x[3] + x[4] + x[5]
elif len(t) == 5:
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4]," 0"]))
# x[1] + x[2] + x[3] + x[4] + x[5] + x[6]
elif len(t) == 6:
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
# Polynomial has a constant term.
else:
# x[1] + x[2] + 1
if len(t) == 2:
c.append(''.join(["-",t[0]," -",t[1]," 0"]))
c.append(''.join([ t[0], " ",t[1]," 0"]))
# x[1] + x[2] + x[3] + 1
elif len(t) == 3:
c.append(''.join([ t[0], " ",t[1], " ",t[2]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," 0"]))
# x[1] + x[2] + x[3] + x[4] + 1
elif len(t) == 4:
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3]," 0"]))
# x[1] + x[2] + x[3] + x[4] + x[5] + 1
elif len(t) == 5:
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4]," 0"]))
# x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + 1
elif len(t) == 6:
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1], " ",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4], " ",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2], " ",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2]," -",t[3], " ",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1], " ",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1]," -",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1]," -",t[2], " ",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0], " ",t[1], " ",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0]," -",t[1], " ",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join([ t[0], " ",t[1]," -",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
c.append(''.join(["-",t[0]," -",t[1]," -",t[2]," -",t[3]," -",t[4]," -",t[5]," 0"]))
write_clauses(c,ofile)
return len(c)
"""
====================================================================================
Input processing functions
====================================================================================
"""
# Enumerate the variables and save the informations to a dictionary.
# Format: [indetname : indetindex]
def create_var_index(config,Data):
indets = config.rstrip().split(';')
counter = 1
for i in range(len(indets)):
varname = indets[i][0]
indices = indets[i][1:].strip('[]').split(',')
iranges = list() # index ranges
# Expand indices.
for j in range(len(indices)):
t = tuple(indices[j].split('..'))
iranges.append(list(range(int(t[0]),int(t[len(t)-1])+1)))
# Recombine the indices and save them to the var_index dictionary.
for ituple in itertools.product(*iranges):
Data._var_index[varname+str(list(ituple)).replace(" ","")] = counter
counter += 1
Data._num_indets = len(Data._var_index)
# Reads polynomials and writes them to a Data class object.
def read_polys(path,Data):
with open(path, encoding='utf-8') as infile:
# Read the first line, which contains the configuration of the indeterminates.
config = infile.readline()
create_var_index(config,Data)
for line in infile:
# Ignore empty lines in the input file.
if line != '\n':
f = Poly()
terms = line.rstrip().split('+')
for i in range(len(terms)):
term = list()
for indet in re.finditer('(([a-z]|[A-Z]){1}\[([0-9]*\,)*[0-9]+\])|(1)',terms[i]):
if indet.group(0) != '1':
term.append(Data._var_index[indet.group(0)])
f._add_term(term)
Data._polys.append(f)
# As the name suggests.
def help():
return "Usage: anf2cny.py CNF [3-6] [SS,LPS,DPS,QPS] [SS,CPS] source target\n"+ \
" or: anf2cny.py XOR [SS,LPS,DPS,QPS] [SS,CPS] source target"
"""
====================================================================================
Main
====================================================================================
"""
def main():
# Set default values.
output = "CNF"
cl = 4
qs = "SS"
cs = "SS"
# Process the input parameters.
num_args = len(sys.argv)
if ("--help") in sys.argv:
print(help())
return 0
if (sys.argv[1] == "CNF") and (num_args == 7):
output = "CNF"
cl = int(sys.argv[2])
qs = sys.argv[3]
cs = sys.argv[4]
elif (sys.argv[1] == "XOR") and (num_args == 6):
output = "XOR"
qs = sys.argv[2]
cs = sys.argv[3]
else:
print("Error! Wrong (number of) parameters.")
print(help())
return 2
ipath = sys.argv[num_args-2]
opath = sys.argv[num_args-1]
D = _Data()
# Reset output file.
if opath != "":
ofile = open(opath,'w')
ofile.close()
# Read the polynomials from the input file.
read_polys(ipath,D)
# Start conversion.
to_cnf(D,cl,qs,cs,output,opath)
if __name__ == '__main__':
status = main()
sys.exit(status)