-rwxr-xr-x 2250 cryptattacktester-20231020/aes128.py raw
#!/usr/bin/env python3
import sys
import subprocess
import multiprocessing
def I_select(K,C):
L = 0
R = 1<<K
while L < R:
I = (L+R)//2
KC = f'K={K},C={C}'
KCI = f'K={K},C={C},I={I}'
searchparams = subprocess.run(['./searchparams','problem=aes128',KCI],capture_output=True,universal_newlines=True,check=True)
searchparams = searchparams.stdout.splitlines()[-1]
searchparams = searchparams.split()
assert searchparams[0] == 'searchparams'
assert searchparams[1] == 'problem=aes128'
assert searchparams[2] == KC
assert searchparams[3] == 'attack=aes128_enum'
attackparams = searchparams[4]
predictedprob = subprocess.run(['./predictedprob','problem=aes128',KC,attackparams],capture_output=True,universal_newlines=True,check=True)
predictedprob = predictedprob.stdout.splitlines()[-1]
predictedprob = predictedprob.split()
assert predictedprob[0] == 'predictedprob'
assert predictedprob[1] == 'problem=aes128'
assert predictedprob[2] == KC
assert predictedprob[3] == 'attack=aes128_enum'
assert predictedprob[9] == 'prob2'
prob2 = predictedprob[10]
prob2 = prob2.split('[')[1]
prob2 = prob2.split(',')[0]
prob2 = float(prob2)
if prob2 >= 0.5000001:
R = I
else:
L = I+1
I = R
if I >= (1<<K): I = (1<<K)-1
return I
for K in range(1,129):
Crange = [K-1,K] if K in [2,3,4] else [K]
for C in Crange:
I = I_select(K,C)
KC = f'K={K},C={C}'
KCI = f'K={K},C={C},I={I}'
searchparams = subprocess.run(['./searchparams','problem=aes128',KCI],capture_output=True,universal_newlines=True,check=True)
searchparams = searchparams.stdout.splitlines()[-1]
print(searchparams)
sys.stdout.flush()
searchparams = searchparams.split()
assert searchparams[0] == 'searchparams'
assert searchparams[1] == 'problem=aes128'
assert searchparams[2] == KC
assert searchparams[3] == 'attack=aes128_enum'
attackparams = searchparams[4]
subprocess.run(['./predictedcp','problem=aes128',KC,attackparams])
if K <= 10:
subprocess.run(['./circuitcost','problem=aes128',KC,attackparams])
subprocess.run(['./circuitprob','problem=aes128',KC,attackparams,'trialfactor=100000'])