|
|
@ -2,29 +2,41 @@ from alice import Alice |
|
|
|
from bob import Bob |
|
|
|
from bob import Bob |
|
|
|
from channel import QChannelImpl |
|
|
|
from channel import QChannelImpl |
|
|
|
from threading import Thread |
|
|
|
from threading import Thread |
|
|
|
|
|
|
|
import typing |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
n = 1000 |
|
|
|
def run_qkd(alice, bob, n=1000): |
|
|
|
channel = QChannelImpl() |
|
|
|
|
|
|
|
alice, bob = Alice(channel), Bob(channel) |
|
|
|
|
|
|
|
alice_thread = Thread(target=lambda: alice.generate_key(n)) |
|
|
|
alice_thread = Thread(target=lambda: alice.generate_key(n)) |
|
|
|
bob_thread = Thread(target=lambda: bob.generate_key(n)) |
|
|
|
bob_thread = Thread(target=lambda: bob.generate_key(n)) |
|
|
|
alice_thread.start() |
|
|
|
alice_thread.start() |
|
|
|
bob_thread.start() |
|
|
|
bob_thread.start() |
|
|
|
while alice_thread.is_alive() or bob_thread.is_alive(): |
|
|
|
while alice_thread.is_alive() or bob_thread.is_alive(): |
|
|
|
pass |
|
|
|
pass |
|
|
|
print(list(map(int, alice.key))) |
|
|
|
|
|
|
|
print(list(map(int, bob.key))) |
|
|
|
|
|
|
|
print('Alice bits: ', *list(map(int, alice.sent))) |
|
|
|
def get_e_and_r(alice, bob, n=1000) -> typing.Tuple[float, float]: |
|
|
|
print('Bob bits: ', |
|
|
|
return 1 - sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key), len(alice.key) / n |
|
|
|
*list(map(lambda t: {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(t[0]), int(t[1]))], bob.my_results))) |
|
|
|
|
|
|
|
print('Alice basises:', *list(map(int, alice.my_basises))) |
|
|
|
|
|
|
|
print('Bob basises: ', *list(map(int, bob.my_basises))) |
|
|
|
if __name__ == '__main__': |
|
|
|
bob_correctness = [left + right == 1 for left, right in bob.my_results] |
|
|
|
N = 1000 |
|
|
|
print(' ', *[ |
|
|
|
channel = QChannelImpl() |
|
|
|
int(k) if c and b1 == b2 else ' ' |
|
|
|
alice, bob = Alice(channel), Bob(channel) |
|
|
|
for c, k, b1, b2 in zip(bob_correctness, alice.sent, bob.my_basises, alice.my_basises)]) |
|
|
|
run_qkd(alice, bob) |
|
|
|
print(' ', *[ |
|
|
|
# print(list(map(int, alice.key))) |
|
|
|
{(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(k[0]), int(k[1]))] if c and b1 == b2 else ' ' |
|
|
|
# print(list(map(int, bob.key))) |
|
|
|
for c, k, b1, b2 in zip(bob_correctness, bob.my_results, bob.my_basises, alice.my_basises)]) |
|
|
|
# print('Alice bits: ', *list(map(int, alice.sent))) |
|
|
|
print(f'{100 * sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key):.2f}%, key length: {len(alice.key)}') |
|
|
|
# print('Bob bits: ', |
|
|
|
|
|
|
|
# *list(map(lambda t: {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(t[0]), int(t[1]))], bob.my_results))) |
|
|
|
|
|
|
|
# print('Alice basises:', *list(map(int, alice.my_basises))) |
|
|
|
|
|
|
|
# print('Bob basises: ', *list(map(int, bob.my_basises))) |
|
|
|
|
|
|
|
# bob_correctness = [left + right == 1 for left, right in bob.my_results] |
|
|
|
|
|
|
|
# print(' ', *[ |
|
|
|
|
|
|
|
# int(k) if c and b1 == b2 else ' ' |
|
|
|
|
|
|
|
# for c, k, b1, b2 in zip(bob_correctness, alice.sent, bob.my_basises, alice.my_basises)]) |
|
|
|
|
|
|
|
# print(' ', *[ |
|
|
|
|
|
|
|
# {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(k[0]), int(k[1]))] if c and b1 == b2 else ' ' |
|
|
|
|
|
|
|
# for c, k, b1, b2 in zip(bob_correctness, bob.my_results, bob.my_basises, alice.my_basises)]) |
|
|
|
|
|
|
|
# print(f'{100 * sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key):.2f}%, key length: {len(alice.key)}') |
|
|
|
|
|
|
|
e, r = get_e_and_r(alice, bob, N) |
|
|
|
|
|
|
|
print(f'E: {e * 100:.1f}%, R: {r}') |
|
|
|