-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76_partition_verifier.py
More file actions
347 lines (287 loc) · 11.1 KB
/
Copy path76_partition_verifier.py
File metadata and controls
347 lines (287 loc) · 11.1 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
#!/usr/bin/env python3
"""
Script 76: Constant-folded partition verification
- Generates constant-folded sr=60 partitions by fixing MSBs of W1[57].
- Runs Kissat with proof output and verifies UNSAT via drat-trim.
- Cross-checks with CaDiCaL and CryptoMiniSat on the same CNFs.
Usage:
python3 76_partition_verifier.py [n_bits] [timeout] [n_workers] [limit]
"""
import csv
import os
import sys
import time
import tempfile
import subprocess
import multiprocessing
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from importlib import import_module
enc = import_module('13_custom_cnf_encoder')
DRAT_TRIM = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tools', 'drat-trim', 'drat-trim')
def encode_sr60_with_fixed_w57_bits(fixed_msb_value, n_fixed_bits=8):
M1 = [0x17149975] + [0xffffffff] * 15
M2 = M1.copy()
M2[0] ^= 0x80000000
M2[9] ^= 0x80000000
state1, W1_pre = enc.precompute_state(M1)
state2, W2_pre = enc.precompute_state(M2)
cnf = enc.CNFBuilder()
s1 = tuple(cnf.const_word(v) for v in state1)
s2 = tuple(cnf.const_word(v) for v in state2)
w1_57 = []
for bit in range(32):
if bit >= (32 - n_fixed_bits):
bit_val = bool((fixed_msb_value >> (bit - (32 - n_fixed_bits))) & 1)
w1_57.append(cnf._const(bit_val))
else:
v = cnf.new_var()
cnf.free_var_names[v] = f"W1_57[{bit}]"
w1_57.append(v)
w1_58 = cnf.free_word("W1_58")
w1_59 = cnf.free_word("W1_59")
w1_60 = cnf.free_word("W1_60")
w2_57 = cnf.free_word("W2_57")
w2_58 = cnf.free_word("W2_58")
w2_59 = cnf.free_word("W2_59")
w2_60 = cnf.free_word("W2_60")
W1_schedule = [w1_57, w1_58, w1_59, w1_60]
W2_schedule = [w2_57, w2_58, w2_59, w2_60]
w1_61 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[2]), cnf.const_word(W1_pre[54])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[46])), cnf.const_word(W1_pre[45])))
w2_61 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[2]), cnf.const_word(W2_pre[54])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[46])), cnf.const_word(W2_pre[45])))
W1_schedule.append(w1_61)
W2_schedule.append(w2_61)
w1_62 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[3]), cnf.const_word(W1_pre[55])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[47])), cnf.const_word(W1_pre[46])))
w2_62 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[3]), cnf.const_word(W2_pre[55])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[47])), cnf.const_word(W2_pre[46])))
w1_63 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[4]), cnf.const_word(W1_pre[56])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[48])), cnf.const_word(W1_pre[47])))
w2_63 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[4]), cnf.const_word(W2_pre[56])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[48])), cnf.const_word(W2_pre[47])))
W1_schedule.extend([w1_62, w1_63])
W2_schedule.extend([w2_62, w2_63])
st1 = s1
for i in range(7):
st1 = cnf.sha256_round_correct(st1, enc.K[57 + i], W1_schedule[i])
st2 = s2
for i in range(7):
st2 = cnf.sha256_round_correct(st2, enc.K[57 + i], W2_schedule[i])
for i in range(8):
cnf.eq_word(st1[i], st2[i])
return cnf
def encode_sr60_with_fixed_w57_bits_dual(w1_msb_value, w2_msb_value, n_fixed_bits=4):
M1 = [0x17149975] + [0xffffffff] * 15
M2 = M1.copy()
M2[0] ^= 0x80000000
M2[9] ^= 0x80000000
state1, W1_pre = enc.precompute_state(M1)
state2, W2_pre = enc.precompute_state(M2)
cnf = enc.CNFBuilder()
s1 = tuple(cnf.const_word(v) for v in state1)
s2 = tuple(cnf.const_word(v) for v in state2)
w1_57 = []
for bit in range(32):
if bit >= (32 - n_fixed_bits):
bit_val = bool((w1_msb_value >> (bit - (32 - n_fixed_bits))) & 1)
w1_57.append(cnf._const(bit_val))
else:
v = cnf.new_var()
cnf.free_var_names[v] = f"W1_57[{bit}]"
w1_57.append(v)
w2_57 = []
for bit in range(32):
if bit >= (32 - n_fixed_bits):
bit_val = bool((w2_msb_value >> (bit - (32 - n_fixed_bits))) & 1)
w2_57.append(cnf._const(bit_val))
else:
v = cnf.new_var()
cnf.free_var_names[v] = f"W2_57[{bit}]"
w2_57.append(v)
w1_58 = cnf.free_word("W1_58")
w1_59 = cnf.free_word("W1_59")
w1_60 = cnf.free_word("W1_60")
w2_58 = cnf.free_word("W2_58")
w2_59 = cnf.free_word("W2_59")
w2_60 = cnf.free_word("W2_60")
W1_schedule = [w1_57, w1_58, w1_59, w1_60]
W2_schedule = [w2_57, w2_58, w2_59, w2_60]
w1_61 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[2]), cnf.const_word(W1_pre[54])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[46])), cnf.const_word(W1_pre[45])))
w2_61 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[2]), cnf.const_word(W2_pre[54])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[46])), cnf.const_word(W2_pre[45])))
W1_schedule.append(w1_61)
W2_schedule.append(w2_61)
w1_62 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[3]), cnf.const_word(W1_pre[55])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[47])), cnf.const_word(W1_pre[46])))
w2_62 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[3]), cnf.const_word(W2_pre[55])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[47])), cnf.const_word(W2_pre[46])))
w1_63 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W1_schedule[4]), cnf.const_word(W1_pre[56])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W1_pre[48])), cnf.const_word(W1_pre[47])))
w2_63 = cnf.add_word(
cnf.add_word(cnf.sigma1_w(W2_schedule[4]), cnf.const_word(W2_pre[56])),
cnf.add_word(cnf.const_word(enc.sigma0_py(W2_pre[48])), cnf.const_word(W2_pre[47])))
W1_schedule.extend([w1_62, w1_63])
W2_schedule.extend([w2_62, w2_63])
st1 = s1
for i in range(7):
st1 = cnf.sha256_round_correct(st1, enc.K[57 + i], W1_schedule[i])
st2 = s2
for i in range(7):
st2 = cnf.sha256_round_correct(st2, enc.K[57 + i], W2_schedule[i])
for i in range(8):
cnf.eq_word(st1[i], st2[i])
return cnf
def run_cmd(cmd, timeout):
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
return r.returncode, r.stdout, r.stderr
except subprocess.TimeoutExpired:
return None, "", "TIMEOUT"
def run_kissat_with_proof(cnf_file, proof_file, timeout, seed=None):
cmd = ["kissat", "-q", "-f"]
if seed is not None:
cmd.append(f"--seed={seed}")
cmd.extend([cnf_file, proof_file])
rc, out, err = run_cmd(cmd, timeout)
if rc is None:
return "TIMEOUT"
if rc == 10:
return "SAT"
if rc == 20:
return "UNSAT"
return "ERROR"
def verify_drat(cnf_file, proof_file, timeout):
if not os.path.exists(DRAT_TRIM):
return "MISSING"
rc, out, err = run_cmd([DRAT_TRIM, cnf_file, proof_file], timeout)
if rc is None:
return "TIMEOUT"
return "OK" if rc == 0 else "FAIL"
def run_cadical(cnf_file, timeout, seed=None):
cmd = ["cadical", "-q"]
if seed is not None:
cmd.append(f"--seed={seed}")
cmd.append(cnf_file)
rc, out, err = run_cmd(cmd, timeout)
if rc is None:
return "TIMEOUT"
if rc == 10:
return "SAT"
if rc == 20:
return "UNSAT"
return "ERROR"
def run_cryptominisat(cnf_file, timeout, seed=None):
cmd = ["cryptominisat5"]
if seed is not None:
cmd.append(f"--random={seed}")
cmd.append(cnf_file)
rc, out, err = run_cmd(cmd, timeout)
if rc is None:
return "TIMEOUT"
if "s UNSATISFIABLE" in out:
return "UNSAT"
if "s SATISFIABLE" in out:
return "SAT"
return "ERROR"
def solve_partition(args):
msb_val, n_bits, timeout, work_dir, dual, seed = args
if dual:
mask = (1 << n_bits) - 1
w1_val = msb_val & mask
w2_val = msb_val >> n_bits
cnf = encode_sr60_with_fixed_w57_bits_dual(w1_val, w2_val, n_bits)
else:
cnf = encode_sr60_with_fixed_w57_bits(msb_val, n_bits)
cnf_file = os.path.join(work_dir, f"p_{msb_val:03d}.cnf")
proof_file = os.path.join(work_dir, f"p_{msb_val:03d}.drat")
cnf.write_dimacs(cnf_file)
kissat_status = run_kissat_with_proof(cnf_file, proof_file, timeout, seed=seed)
drat_status = "SKIP"
if kissat_status == "UNSAT":
drat_status = verify_drat(cnf_file, proof_file, timeout)
cadical_status = run_cadical(cnf_file, timeout, seed=seed)
cms_status = run_cryptominisat(cnf_file, timeout, seed=seed)
try:
os.unlink(cnf_file)
except OSError:
pass
try:
os.unlink(proof_file)
except OSError:
pass
return {
"partition": msb_val,
"kissat": kissat_status,
"drat": drat_status,
"cadical": cadical_status,
"cms": cms_status,
"seed": seed if seed is not None else "",
}
def main():
dual = "--dual" in sys.argv
partitions = None
seed = None
out_csv = None
args = []
for a in sys.argv[1:]:
if a == "--dual":
continue
if a.startswith("--partitions="):
parts = a.split("=", 1)[1].strip()
if parts:
partitions = [int(p.strip()) for p in parts.split(",") if p.strip()]
continue
if a.startswith("--seed="):
seed = int(a.split("=", 1)[1])
continue
if a.startswith("--out="):
out_csv = a.split("=", 1)[1]
continue
if a.startswith("--"):
continue
args.append(a)
n_bits = int(args[0]) if len(args) > 0 else 8
timeout = int(args[1]) if len(args) > 1 else 60
n_workers = int(args[2]) if len(args) > 2 else 4
limit = int(args[3]) if len(args) > 3 else 0
n_partitions = 2 ** (2 * n_bits) if dual else 2 ** n_bits
work_dir = tempfile.mkdtemp(prefix="verify_sr60_")
if partitions is not None:
tasks = [(msb, n_bits, timeout, work_dir, dual, seed) for msb in partitions]
else:
tasks = [(msb, n_bits, timeout, work_dir, dual, seed) for msb in range(n_partitions)]
if limit > 0:
tasks = tasks[:limit]
if out_csv is None:
out_csv = os.path.join(os.getcwd(), "verification_results.csv")
with open(out_csv, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["partition", "kissat", "drat", "cadical", "cms", "seed"])
writer.writeheader()
if n_workers <= 1:
for task in tasks:
result = solve_partition(task)
writer.writerow(result)
f.flush()
print(result, flush=True)
else:
with multiprocessing.Pool(n_workers) as pool:
for result in pool.imap_unordered(solve_partition, tasks):
writer.writerow(result)
f.flush()
print(result, flush=True)
print(f"Results written to {out_csv}", flush=True)
if __name__ == "__main__":
main()