-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.py
More file actions
72 lines (54 loc) · 2.1 KB
/
Copy pathcontrol.py
File metadata and controls
72 lines (54 loc) · 2.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
import numpy as np
import qiskit
import my_secrets
BLOCK_BITS = 4
BLOCK_SIZE = 2**BLOCK_BITS
BLOCK_EL = BLOCK_SIZE**2
NBITS = 20
STATE_SIZE = 2**NBITS
encoding_dict = { c:'{:06b}'.format(v) for v,c in enumerate('ABCDEFGHIJKLMNOPQRSTUVXWYZabcdefghijklmnopqrstuvxwyz01234567{}-_')}
def encode_string(s):
return ''.join(encoding_dict[c] for c in s )
def gen_keys(enc_s):
if len(enc_s)<BLOCK_EL: enc_s +='0'*(BLOCK_EL-len(enc_s))
return [enc_s[i:i+BLOCK_SIZE] for i in range(0,BLOCK_EL,BLOCK_SIZE)]
def get_states(keys,otp):
return [[otp[int(i)][j][kn] for j,i in enumerate(k)] for kn,k in enumerate(keys)]
def initial_state(s):
res = np.array(s)
return res/np.linalg.norm(res)
def setup_state(vec):
out = ( 'initialize('
+ ','.join('{:.9f}'.format(v) for v in vec)
+ ')'
+ ','.join('b[{}]'.format(j) for j in range(NBITS))
+ ';\n'
)
return out
def get_otp():
otp0 = list( list(v) for v in np.loadtxt('./otp/otp0.csv',delimiter=','))
otp1 = list( list(v) for v in np.loadtxt('./otp/otp1.csv',delimiter=','))
return [otp0,otp1]
def main():
pwd = input("Password: ")
otp = get_otp()
count=0
keys = gen_keys(encode_string(pwd))
states = get_states(gen_keys(encode_string(pwd)), otp)
for kn,padding,state in zip(range(len(states)),my_secrets.padding, states):
with open("converted_input.inc","w") as f:
print(setup_state(initial_state(state + padding)))
f.write(setup_state(initial_state(state + padding)))
circ = qiskit.QuantumCircuit.from_qasm_file('chall.qasm')
backend = qiskit.Aer.get_backend('harpa_secret_quantum_computer')
target = []
while len(target)==0:
results = qiskit.execute(circ, backend, shots=30).results()
counts = results.get_counts(circ)
target = list(filter(lambda s: s[-1]=='1', counts.keys()))
if len(target)==1 and int(target[0][-NBITS-2:-1], 2) == int(kn):
count+=1
if count==BLOCK_SIZE:
print("Login successful!")
if __name__=='__main__':
main()