-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
244 lines (202 loc) · 6.98 KB
/
client.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
# from socketIO_client import SocketIO, LoggingNamespace
import sys
import socketio
import numpy as np
import math
from random import randrange
sio = socketio.Client()
@sio.on('receive_chaos_output2')
def on_message(msg):
if msg['output'] == tpmclient.tpm.chaosmap():
tpmclient.IsSync = True
print('SUCCESS: synched with Bob')
sio.disconnect()
tpmclient.save_key()
# save key to KEYS
@sio.on('receive_chaos_output')
def on_message(msg):
sio.emit('confirm_chaos_output',
{
'msg':'sending chaos output',
'output': tpmclient.tpm.chaosmap(),
'sid': tpmclient.partner_sid
}
)
if msg['output'] == tpmclient.tpm.chaosmap():
tpmclient.IsSync = True
print('SUCCESS: synched with Alice')
sio.disconnect()
tpmclient.save_key()
# save key to KEYS
@sio.on('output_received')
def on_message(msg):
if tpmclient.tpm.out == msg['output']:
tpmclient.tpm.update_weights(msg['output'])
if tpmclient.n >= 200:
if tpmclient.n % 10 == 0:
tpmclient.send_chaos_output()
if not tpmclient.IsSync:
tpmclient.send_vector_and_output()
# print("output received", + msg['output'])
animation.update()
@sio.on('get_weights')
def on_message(msg):
try:
vector = msg['vector']
list_vec = [np.array(vector[x:x+16]) for x in range(0, len(vector), 16)]
tpmclient.receive_vector(list_vec)
tpmclient.send_output()
if tpmclient.tpm.out == msg['output']:
tpmclient.tpm.update_weights(msg['output'])
# print("output received", + msg['output'])
animation.update()
except socketio.exceptions.BadNamespaceError:
pass
@sio.on('status')
def on_message(msg):
if 'assign Alice' in msg:
tpmclient.user = msg['assign Alice']
if 'assign Bob' in msg and not tpmclient.user:
tpmclient.user = msg['assign Bob']
if 'start' in msg:
if tpmclient.user == 'A':
tpmclient.partner_sid = msg["BSid"]
# print(' B sid is ' + tpmclient.partner_sid)
tpmclient.send_vector_and_output()
else:
tpmclient.partner_sid = msg["ASid"]
# print(' A sid is ' + tpmclient.partner_sid)
print("Partner's sid is: " + tpmclient.partner_sid)
# print('message from server: ' + msg['message'])
@sio.event
def connect():
sio.emit('my message', " Client connected", namespace='/')
sio.emit('join', {'channel': CHANNEL}, namespace='/')
print("Connected to server")
class TPMClient:
def __init__(self):
self.user = None
self.tpm = TPM(16, 16, 100)
self.n = 0
self.IsSync = False
def send_vector_and_output(self):
vector = self.rand_vec()
list_vec = [np.array(vector[x:x+16]) for x in range(0, len(vector), 16)]
self.tpm.get_output(list_vec)
sio.emit('weights',
{
'msg':'sending random vector and output',
'vector': vector,
'output': self.tpm.out,
'sid': tpmclient.partner_sid
}
)
self.n += 1
def receive_vector(self, vector_):
vec = [np.array(x) for x in vector_]
self.tpm.get_output(vec)
def rand_vec(self):
l = []
for i in range(256):
l.append(randrange(-100, 100))
return l
def send_output(self):
sio.emit('send_output',
{
'msg':'sending output',
'output': self.tpm.out,
'sid': tpmclient.partner_sid
}
)
def send_chaos_output(self):
sio.emit('send_chaos_output',
{
'msg':'sending chaos output',
'output': self.tpm.chaosmap(),
'sid': tpmclient.partner_sid
}
)
def save_key(self):
re = [abs(item+155) for sublist in self.tpm.weights for item in sublist]
key = bytes(abs(x) for x in re).decode('cp437')
with open("KEYS/{}.txt".format(CHANNEL), "w") as text_file:
print(key, file=text_file)
class TPM:
def __init__(self, k_, n_, l_):
self.k = k_
self.n = n_
self.l = l_
self.weights = self.initialize_w()
self.inputs = None
self.H = np.zeros(self.k)
self.out = None
self.X = None
def get_output(self, input_):
self.X = input_
self.out = 1
for i in range(self.k):
self.H[i] = self.signum(np.dot(input_[i], self.weights[i]))
self.out *= self.signum(np.dot(input_[i], self.weights[i]))
def initialize_w(self):
p = []
for i in range(self.k):
p.append(np.random.randint(-self.l, self.l, size=self.n))
return p
def signum(self, x):
return math.copysign(1, x)
def update_weights(self, outputB):
for i in range(self.k):
for j in range(self.n):
self.weights[i][j] += self.X[i][j] * self.out * self.isequal(self.out, self.H[i]) * self.isequal(self.out, outputB)
self.weights[i][j] = self.g(self.weights[i][j])
def isequal(self, A, B):
if A==B:
return 1.0
else:
return 0.0
def g(self, w):
if w > self.l:
return self.l
if w < -self.l:
return -self.l
else:
return w
def chaosmap(self):
r = sum(list(np.hstack(self.weights)))
rr = sum([abs(x) for x in (list(np.hstack(self.weights)))])
t = float(abs(r)) / float(rr)
x = t
for i in range(rr):
x = (3.6 + t/2)* x *(1 - x)
return x
class Animation:
def __init__(self):
self.animation = [
"Synchronizing [ ]",
"Synchronizing [= ]",
"Synchronizing [=== ]",
"Synchronizing [==== ]",
"Synchronizing. [===== ]",
"Synchronizing. [====== ]",
"Synchronizing. [======= ]",
"Synchronizing. [========]",
"Synchronizing.. [ =======]",
"Synchronizing.. [ ======]",
"Synchronizing.. [ =====]",
"Synchronizing.. [ ====]",
"Synchronizing...[ ===]",
"Synchronizing...[ ==]",
"Synchronizing...[ =]",
"Synchronizing...[ ]",
"Synchronizing [ ]"]
self.i = 0
self.timer = None
def update(self):
print(self.animation[self.i % len(self.animation)], end='\r')
self.i += 1
if __name__ == "__main__":
CHANNEL = sys.argv[1]
tpmclient = TPMClient()
animation = Animation()
# sio.connect('https://tpmserver.herokuapp.com/')
sio.connect('http://localhost:5000')