def main(): network = Network.get_instance() nodes = ['Alice', 'Eve', 'Bob'] network.start(nodes) network.delay = 0.1 host_alice = Host('Alice') host_alice.add_connection('Eve') host_alice.start() host_eve = Host('Eve') host_eve.add_connections(['Alice', 'Bob']) host_eve.start() host_bob = Host('Bob') host_bob.add_connection('Eve') host_bob.delay = 0.2 host_bob.start() network.add_host(host_alice) network.add_host(host_eve) network.add_host(host_bob) # network.draw_classical_network() # network.draw_quantum_network() network.start() alice_basis, bob_basis, alice_bits, alice_encoded = preparation() print("Alice bases: {}".format(alice_basis)) print("Bob bases: {}".format(bob_basis)) print("Alice bits: {}".format(alice_bits)) print("Alice encoded: {}".format(alice_encoded)) if INTERCEPTION: host_eve.q_relay_sniffing = True host_eve.q_relay_sniffing_fn = eve_sniffing_quantum t1 = host_alice.run_protocol(alice, ( host_bob.host_id, alice_basis, alice_bits, alice_encoded, )) t2 = host_bob.run_protocol(bob, ( host_alice.host_id, bob_basis, )) t1.join() t2.join() network.stop(True) exit()
def main(): # Testing eve_interception = False # Set it as true to test interception b = BB84() # Network initialization network = Network.get_instance() nodes = ['Alice', 'Bob', 'Eve'] network.start(nodes) host_alice = Host('Alice') host_alice.add_connection('Eve') host_alice.start() host_eve = Host('Eve') host_eve.add_connection('Bob') host_eve.add_connection('Alice') host_eve.start() host_bob = Host('Bob') host_bob.add_connection('Eve') host_bob.start() network.add_host(host_alice) network.add_host(host_bob) network.add_host(host_eve) # Eve intercepts if (eve_interception): host_eve.q_relay_sniffing = True host_eve.q_relay_sniffing_fn = b.eve_sniffing_quantum network.draw_quantum_network() network.draw_classical_network() KEY_LENGTH = 500 # the size of the key in bit np.random.seed(0) secret_key = np.random.randint(2, size=KEY_LENGTH) print("Secret Key: ", secret_key) # Step 1: Alice's Prepares encoding basis and choose a random bitstring alice_bitstring, alice_bases = b.select_encoding(KEY_LENGTH) print("alice_bitstring: ", alice_bitstring[:10]) print("alice_bases: ", alice_bases[:10]) # Step 2: Bob selects bases for measurement bob_bases = b.select_measurement(KEY_LENGTH) print("Bob_bases: ", bob_bases[:10]) # Step 3: Alice encodes the qubits encoded_qubits = b.encode(host_alice, alice_bitstring, alice_bases) print('Encoded bits: ', encoded_qubits) # Step 4: Alice sends the qubits q_ids = b.send(host_alice, host_bob, encoded_qubits) print(q_ids) # Step 5: Bob measures the received qubits Bob_received_message = b.receive(host_bob, host_alice, q_ids) print('Bob received qubits', Bob_received_message) bob_bits, bob_bitstring = b.measurement(bob_bases, Bob_received_message) print('Bob measured ', bob_bitstring) # Step 6: Bob uses Alice's announced bases to see where they agreed with # his decoding bases seq_no = b.send_classically(host_alice, alice_bases, KEY_LENGTH, host_bob) print(seq_no) bases_bob_received_arr, bases_bob_received_string = b.receive_classically( host_bob, host_alice, seq_no) print('Bob Received Bases from Alice', bases_bob_received_string) Bob_agreeing_bases = b.compare_bases(bases_bob_received_string, bob_bases) print(Bob_agreeing_bases) # Bob announces where they agreed on encoding and # decoding bases classically. seq_no = b.send_classically(host_bob, bob_bases, KEY_LENGTH, host_alice) print(seq_no) Alice_received_bases, Alice_received_bases_string = b.receive_classically( host_alice, host_bob, seq_no) print('Alice Received Bases from Bob', Alice_received_bases_string) Alice_agreeing_bases = b.compare_bases(alice_bases, Alice_received_bases_string) print(Alice_agreeing_bases) # Step 7: Alice and Bob construct their keys alice_key = b.construct_key_from_indices( alice_bitstring, Alice_agreeing_bases) # Alice Side bob_key = b.construct_key_from_indices(bob_bitstring, Bob_agreeing_bases) # Preview the first 10 elements of each Key: print("alice_key: ", alice_key[:10]) print("bob_key: ", bob_key[:10]) print("Alice's key is equal to Bob's key: ", alice_key == bob_key) if (alice_key == bob_key): message = "QKD is cool!" print("Original Messge:", message) encrypted_message = b.encrypt_message(message, alice_key) print("Encrypted message:", encrypted_message) decrypted_message = b.decrypt_message(encrypted_message, bob_key) print("Decrypted message:", decrypted_message)