-
Notifications
You must be signed in to change notification settings - Fork 3
/
qkd_sim.py
389 lines (286 loc) · 11.8 KB
/
qkd_sim.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
import numpy as np
import random
import math
from optical_elements import LinearPolarizer, PolarizingBeamSplitter
class Alice:
def __init__(self, n):
self.n = n
self.alice = {} #{no. : [bit encoded, basis chosen to encode in ]} no. is some unique number
#{1:[0,0],2:[0,1],3:[0,0]} Example
def generate_and_encode(self):
"""
Will generate n bits randomly
For each bit generated, a basis is chosen in which it is encoded
Dependency for encoding: <class LinearPolarizer>
0-> horizontal/vertical polarization
1-> diagonal polarization
Should generate a dictionary of the form self.alice mentioned above
"""
LP = LinearPolarizer()
encode = []
count = self.n
while count!= 0:
self.alice[count] = [ random.randint(0,1), random.randint(0,1)]
if self.alice[count][1] == 0:
encode.append(LP.horizontal_vertical(self.alice[count][0]))
else:
encode.append(LP.diagonal_polarization(self.alice[count][0]))
count-=1
return encode
class Bob:
def __init__(self, n):
self.n = n
self.bob = {} #{no. : [bit after measurement, basis chosen to measure in]}
#{1:[1,0],2:[0,0],3:[1,0]} Example
def choose_basis_and_measure(self, received):
"""
received : the data received by bob
Dependency for measurement: <class PolarizingBeamSplitter>
0-> horizontal/vertical polarization
1-> diagonal polarization
Should generate a dictionary of the form self.bob mentioned above
"""
#self.bob[n][0] is the measured bit
PBS = PolarizingBeamSplitter()
count = self.n
i = 0
while count!= 0:
self.bob[count] = [0, random.randint(0,1)]
measure = PBS.measure(received[i], self.bob[count][1])
if measure[0] == measure[1]:
self.bob[count][0] = random.randint(0,1)
elif measure[0] > measure[1]:
self.bob[count][0] = 0
else:
self.bob[count][0] = 1
i += 1
count-=1
class Privacy_amplification:
def __init__(self, n):
self.n = n
def find_parity(self, bits):
count = 0
for i in bits:
count+=i
par = count%2
return par
def privacy_amplification(self, error_rate, s, alice_bit, bob_bit):
k = int(error_rate * 2)
subset_size = self.n - k - s
final_alice = []
final_bob = []
alice_b = []
bob_b = []
alice_subsets = []
bob_subsets = []
for i in alice_bit:
alice_b.append(i)
for i in bob_bit:
bob_b.append(i)
for i in range(0, self.n, subset_size):
alice_subsets.append(alice_b[i:i+subset_size])
bob_subsets.append(bob_b[i:i+subset_size])
bob_parity = 0
alice_parity = 0
#calculate parities of sets and compare and eliminate if parities dont match
for i in range(len(alice_subsets)):
alice = self.find_parity(alice_subsets[i])
bob = self.find_parity(bob_subsets[i])
if alice == bob:
final_alice.append(alice)
final_bob.append(bob)
return final_alice, final_bob
class BB84:
def __init__(self, n, delta, error_threshold):
"""
Alice generates (4+delta)n bits
delta: small fraction less than one
error_threshold: if error while announcing n bits from 2n bits is greater than this
key generation is aborted
"""
if delta > 1:
print("Value for delta should be lesser than 1")
return
self.n = n
self.total = math.ceil(4 + delta)*n
self.alice = Alice(self.total)
self.bob = Bob(self.total)
self.error_rate = 0
self.error = error_threshold
def eve_interfere(self, intercept, intensity):
"""
intercept: the encoeded bits alice sends to bob
intensity: number of bits to interfere with
"""
PBS = PolarizingBeamSplitter()
lp = LinearPolarizer()
indices = random.sample(list(range(self.total)), intensity)
for i in indices:
basis = random.randint(0, 1)
measure = PBS.measure(intercept[i], basis)
if measure[0] == measure[1]:
intercept[i] = lp.diagonal_polarization(0)
if measure[0] == -1 * measure[1]:
intercept[i] = lp.diagonal_polarization(1)
if measure[0] > measure[1]:
intercept[i] = lp.horizontal_vertical(0)
else:
intercept[i] = lp.horizontal_vertical(1)
return intercept
def distribute(self, eve, intensity, priv_amp):
"""
eve: if an evesdropper is present or not
"""
encoded = self.alice.generate_and_encode()
if eve==1:
encoded = self.eve_interfere(encoded, intensity)
self.bob.choose_basis_and_measure(encoded)
recon = Reconciliation(self.error, self.alice.alice, self.bob.bob, self.n)
recon_alice, recon_bob = recon.basis_reconciliation(self.alice.alice, self.bob.bob)
try:
final_alice, final_bob, error_rate = recon.error_correction(recon_alice, recon_bob)
self.error_rate = error_rate
if priv_amp:
priv = Privacy_amplification(self.n)
final_priv_alice, final_priv_bob = priv.privacy_amplification(error_rate, 2, final_alice, final_bob)
return final_priv_alice, final_priv_bob
else:
return final_alice, final_bob
except:
self.abort()
return [], []
def abort(self):
print("Protocol aborted")
return
def calcRedundantBits(m):
# Use the formula 2 ^ r >= m + r + 1
# to calculate the no of redundant bits.
# Iterate over 0 .. m and return the value
# that satisfies the equation
for i in range(m):
if(2**i >= m + i + 1):
return i
def posRedundantBits(data, r):
j = 0
k = 1
m = len(data)
res = ''
for i in range(1, m + r+1):
if(i == 2**j):
res = res + '0'
j += 1
else:
res = res + data[-1 * k]
k += 1
return res[::-1]
def calcParityBits(arr, r):
n = len(arr)
# For finding rth parity bit, iterate over
# 0 to r - 1
for i in range(r):
val = 0
for j in range(1, n + 1):
# If position has 1 in ith significant
# position then Bitwise OR the array value
# to find parity bit value.
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
# -1 * j is given since array is reversed
# String Concatenation
# (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
return arr
def detectError(arr, nr):
n = len(arr)
res = 0
# Calculate parity bits again
for i in range(nr):
val = 0
for j in range(1, n + 1):
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
# Create a binary no by appending
# parity bits together.
res = res + val*(10**i)
return int(str(res), 2)
class Reconciliation:
def __init__(self, error_threshold, alice, bob, n):
self.alice = alice
self.bob = bob
self.n = n
self.error_threshold = error_threshold
def basis_reconciliation(self, alice, bob):
"""
alice: {no. : [bit encoded, basis chosen to encode in ]}
bob : {no. : [bit after measurement, basis chosen to measure in]}
First check if the length of both lists are the same
-> if yes, keep only those bits for alice and bob for which
the basis encoded in and measured in is the same.
"""
basis_bit_alice = list(alice.values())
basis_bit_bob = list(bob.values())
if len(basis_bit_alice) == len(basis_bit_bob):
raw_key_alice = []
raw_key_bob = []
for i in range(len(basis_bit_alice)):
if basis_bit_alice[i][1] == basis_bit_bob[i][1]:
raw_key_alice.append(basis_bit_alice[i][0])
raw_key_bob.append(basis_bit_bob[i][0])
return raw_key_alice, raw_key_bob
else:
return None, None
def abort(self):
print("Protocol aborted here")
return
def sampling(self, raw_key_alice, raw_key_bob, n):
sampled_key_alice, sampled_key_bob, sampled_key_index = [], [], []
sampled_key_index = random.sample(list(enumerate(raw_key_alice)), n)
indices = []
for idx, val in sampled_key_index:
sampled_key_alice.append(val)
sampled_key_bob.append(raw_key_bob[idx])
indices.append(idx)
return sampled_key_alice, sampled_key_bob, indices
def error_correction(self, raw_key_alice, raw_key_bob):
if len(raw_key_alice)<2*self.n:
self.abort()
else:
sampled_key_alice, sampled_key_bob, sample_indices = self.sampling(raw_key_alice, raw_key_bob, 2*self.n)
check_alice, check_bob, indices = self.sampling(sampled_key_alice, sampled_key_bob, self.n)
error = 0
for i in range(len(check_alice)):
if check_alice[i] != check_bob[i]:
error+=1
error_rate = error/self.n
if error_rate >= self.error_threshold:
self.abort()
else:
req_alice = [sampled_key_alice[i] for i in range(len(sampled_key_alice)) if i not in indices]
req_bob = [sampled_key_bob[i] for i in range(len(sampled_key_bob)) if i not in indices]
if error_rate == 0.0:
return req_alice, req_bob, error_rate
string_alice = "".join(list(map(str, req_alice)))
string_bob = "".join(list(map(str, req_bob)))
m = len(string_bob)
r = calcRedundantBits(m)
arr = posRedundantBits(string_bob, r)
arr = calcParityBits(arr, r)
bob = []
k = 0
for i in range(self.n):
if i!=(2**k-1):
bob.append(req_bob[i])
else:
k += 1
correction = self.n - detectError(arr, r) - 1
req_bob[correction] = int(not req_bob[correction])
return req_alice, req_bob, error_rate
bb84 = BB84(10, 0.6, 0.3)
a, b = bb84.distribute(0, 3, 0)
count = 0
for i in range(len(a)):
if a[i] != b[i]:
count += 1
print("Weight of transmission: ", count)
print("\nDistributed Keys:\nAlice: %s \nBob: %s\n" % ("".join(list(map(str, a))), "".join(list(map(str, a)))))
print("Error rate: ", bb84.error_rate)