-
Notifications
You must be signed in to change notification settings - Fork 0
/
alice.py
60 lines (49 loc) · 1.82 KB
/
alice.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
from bb84.bb84 import *
from BitVector import BitVector
from Crypto.Cipher import AES
from Crypto import Random
key_size = 32
length = 3 * key_size
acceptable_error = 0.5
recipient = "Bob"
"""
with CQCConnection("Alice") as Alice:
print("Connection made")
# Send bob key length
Alice.sendClassical(recipient, length)
confirmation = int.from_bytes(Alice.recvClassical(), byteorder="big")
# Get key, encode key, send to bob
key, qubits, bases = create_master_key(Alice, length)
key_bit_vect = BitVector(intVal=key)
# print("Key: {}".format(bin(key)))
# print("Bases: {}".format(bin(int(bases))))
for q in qubits:
Alice.sendQubit(q, recipient)
# receive bases used by Bob
bobs_bases = BitVector(bitlist=Alice.recvClassical())
# print("Bobs bases: {}".format(bin(bobs_bases.int_val())))
correct_bases = ~bobs_bases ^ bases
correctness = correct_bases.count_bits() / length
# print("Correct: {}".format(bin(int(correct_bases))))
print(correctness)
# Send bob correct bases
Alice.sendClassical(recipient, correct_bases[:])
key = truncate_key(key, length, correct_bases)
if validate_generated_key(length, acceptable_error, key) != OK:
raise Exception("Poor error rate: {}".format(1 - (len(key) / length)))
exit(0)
expected_verify, key = break_into_parts(key, key_size)
verification_bits = BitVector(bitlist=Alice.recvClassical())
if expected_verify != verification_bits:
Alice.sendClassical(recipient, TAMPERED)
pass
else:
Alice.sendClassical(recipient, OK)
"""
key = initiate_keygen()
# Test AES encoding a message
with CQCConnection("Alice") as Alice:
message = "Hello bob!"
encrypted = encrypt(message, key)
print(encrypted)
Alice.sendClassical(recipient, encrypted)