Example #1
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    backend = CQCBackend()
    network.start(nodes, backend)
    network.delay = 0.7

    hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}

    network.delay = 0
    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    q1 = Qubit(hosts['alice'])
    hosts['alice'].send_qubit('Bob', q1, await_ack=True)
    q1 = Qubit(hosts['alice'])
    hosts['alice'].send_qubit('Bob', q1, await_ack=True)
    q1 = Qubit(hosts['alice'])
    hosts['alice'].send_qubit('Bob', q1, await_ack=True)
    q1 = Qubit(hosts['bob'])
    hosts['bob'].send_qubit('Alice', q1, await_ack=True)
    network.stop(True)
    exit()
Example #2
0
 def __init__(self):
     self.host = Host(ZARDUS_ID)
     self.host.add_connection(HACKER_ID)
     self.host.add_connection(ADAMD_ID)
     self.host.delay = 0
     self.host.start()
     self.qubits = []
     self.q_ids = []
     self.bases = []
Example #3
0
    def test_connections(self):
        name = "A"
        neighbor = [str(x) for x in range(10)]
        a = Host(name)
        for x in neighbor:
            a.add_connection(x)

        connections = a.get_connections()
        for i in connections:
            self.assertTrue(i['connection'] in neighbor)
        a.backend.stop()
Example #4
0
class Zardus(Player, SecretPlayer):
    def __init__(self):
        self.host = Host(ZARDUS_ID)
        self.host.add_connection(HACKER_ID)
        self.host.add_connection(ADAMD_ID)
        self.host.delay = 0
        self.host.start()
        self.qubits = []
        self.q_ids = []
        self.bases = []

    def bet(self, gameid, referee):
        qubit = self.qubits[gameid]
        if referee == 1:
            qubit.H()
            res = qubit.measure(non_destructive=True)
            qubit.H()
        else:
            res = qubit.measure(non_destructive=True)
        self.bases.append(referee)
        return res

    def chat(self, host, adamd_id, qubits_n):
        q_i = 0
        msg_buff = []
        secret_key = []

        for q_i, qubit in enumerate(self.qubits):
            self.host.send_qubit(adamd_id, qubit, await_ack=True)

            message = self.get_next_classical_message(adamd_id, msg_buff, q_i)
            print(f"{host.host_id} receives from {adamd_id}: {message}")

            if message == f"{q_i}:{self.bases[q_i]}":
                if self.bases[q_i] == 1:
                    qubit.H()
                    res = qubit.measure(non_destructive=True)
                    qubit.H()
                else:
                    res = qubit.measure(non_destructive=True)

                secret_key.append(res)
                msg = f"{q_i}:0"
            else:
                msg = f"{q_i}:1"

            self.host.send_classical(adamd_id, msg, await_ack=True)

        nonce_msg = self.get_next_classical_message(adamd_id, msg_buff, -1)
        ciphertext_msg = self.get_next_classical_message(
            adamd_id, msg_buff, -2)
        print(f"{host.host_id} receives from {adamd_id}: {nonce_msg}")
        print(f"{host.host_id} receives from {adamd_id}: {ciphertext_msg}")
Example #5
0
    def test_sequence_numbers(self):
        a = Host('A')
        neighbor = [str(x) for x in range(10)]
        random = [randint(0, 200) for _ in range(10)]
        for n, i in zip(neighbor, random):
            for _ in range(i):
                _ = a.get_next_sequence_number(n)

        for n, i in zip(neighbor, random):
            self.assertEqual(i, a.get_next_sequence_number(n))

        a.backend.stop()
Example #6
0
    def test_add_hosts(self):
        network = Network.get_instance()
        a = Host('A')
        b = Host('B')
        c = Host('C')

        network.add_hosts([a, b])
        self.assertEqual(network.num_hosts, 2)

        network.add_host(c)
        self.assertEqual(network.num_hosts, 3)
        network.stop(True)
Example #7
0
    def setUpClass(cls):
        global network
        global hosts

        nodes = ["Alice", "Bob"]
        backend = EQSNBackend()
        network.start(nodes=nodes, backend=backend)
        hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}
        hosts['alice'].add_connection('Bob')
        hosts['bob'].add_connection('Alice')
        hosts['alice'].start()
        hosts['bob'].start()
        for h in hosts.values():
            network.add_host(h)
Example #8
0
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes, backend)
    network.delay = 0.7

    hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}

    network.delay = 0
    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    # send messages to Bob without waiting for ACKs
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob one',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob two',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob three',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob four',
                                  await_ack=False)

    # Wait for all Acks from Bob
    hosts['alice'].await_remaining_acks(hosts['bob'].host_id)

    saw_ack = [False, False, False, False]
    messages = hosts['alice'].classical
    for m in messages:
        if m.content == Constants.ACK:
            saw_ack[m.seq_num] = True

    for ack in saw_ack:
        assert ack
    print("All tests succesfull!")
    network.stop(True)
Example #9
0
    def test_remove_hosts(self):
        network = Network.get_instance()
        a = Host('A')
        b = Host('B')
        c = Host('C')

        network.add_hosts([a, b, c])
        self.assertEqual(network.num_hosts, 3)

        network.remove_host(a)
        self.assertEqual(network.num_hosts, 2)

        network.remove_host(b)
        self.assertEqual(network.num_hosts, 1)

        network.remove_host(c)
        self.assertEqual(network.num_hosts, 0)
        network.stop(True)
Example #10
0
    def test_get_data_qubits(self):
        host = Host('A')
        q1 = Qubit(host)
        q2 = Qubit(host)
        q3 = Qubit(host)

        host.add_data_qubit('B', q1)

        qs = host.get_data_qubits('B')
        self.assertEqual(len(qs), 1)

        host.add_data_qubit('B', q2)
        host.add_data_qubit('B', q3)

        qs = host.get_data_qubits('B')
        self.assertEqual(len(qs), 3)

        qs = host.get_data_qubits('B', remove_from_storage=True)
        self.assertEqual(len(qs), 3)

        qs = host.get_data_qubits('B', remove_from_storage=True)
        self.assertEqual(len(qs), 0)
Example #11
0
def setup_network(num_hosts, backend):
    network = Network.get_instance()
    network.start(nodes=[str(i) for i in range(num_hosts)], backend=backend)
    network.delay = 0.0

    hosts = []
    for i in range(num_hosts):
        h = Host(str(i), backend=backend)
        h.delay = 0
        h.start()
        if i < num_hosts - 1:
            h.add_connection(str(i + 1))
        if i > 0:
            h.add_connection(str(i - 1))
        hosts.append(h)

    network.add_hosts(hosts)

    return network, hosts
Example #12
0
def main():
    network = Network.get_instance()
    network.start()
    hosts = {'alice': Host('Alice'), 'bob': Host('Bob'), 'eve': Host('Eve')}

    network.delay = 0

    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    # B <-> E
    hosts['bob'].add_connection('Eve')
    hosts['eve'].add_connection('Bob')

    hosts['alice'].start()
    hosts['bob'].start()
    hosts['eve'].start()

    for h in hosts.values():
        network.add_host(h)

    hosts['alice'].send_classical(hosts['eve'].host_id, 'testing123')

    messages = hosts['eve'].classical
    i = 0
    while i < MAX_WAIT and len(messages) == 0:
        messages = hosts['eve'].classical
        i += 1
        time.sleep(1)

    assert len(messages) > 0
    assert messages[0].sender == hosts['alice'].host_id
    assert messages[0].content == 'testing123'
    print("All tests succesfull!")
    network.stop(True)
    exit()
Example #13
0
def main():
    network = Network.get_instance()
    network.start()
    network.delay = 0.2

    host_alice = Host('Alice')
    host_alice.add_connection('Bob')
    host_alice.start()

    host_bob = Host('Bob')
    host_bob.add_connections(['Alice', 'Eve'])
    host_bob.start()

    host_eve = Host('Eve')
    host_eve.add_connections(['Bob', 'Dean'])
    host_eve.start()

    host_dean = Host('Dean')
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_hosts([host_alice, host_bob, host_eve, host_dean])

    print('alice sends message')
    host_alice.send_classical('Bob', 'hello1')
    host_alice.send_classical('Bob', 'hello2')
    host_alice.send_classical('Bob', 'hello3')
    host_alice.send_classical('Bob', 'hello4')
    host_alice.send_classical('Bob', 'hello5')
    host_alice.send_classical('Bob', 'hello6')
    host_alice.send_classical('Bob', 'hello7')
    host_alice.send_classical('Bob', 'hello8')
    host_alice.send_classical('Bob', 'hello9')
    host_alice.send_classical('Bob', 'hello10')

    start_time = time.time()
    while time.time() - start_time < 10:
        pass

    network.stop(True)
    exit()
Example #14
0
def main():
    network = Network.get_instance()

    # backend = ProjectQBackend()
    backend = CQCBackend()

    nodes = ['A', 'B', 'C']
    network.start(nodes, backend)
    network.delay = 0.1

    host_A = Host('A', backend)
    host_A.add_connection('B')
    host_A.delay = 0
    host_A.start()

    host_B = Host('B', backend)
    host_B.add_connections(['A', 'C'])
    host_B.delay = 0
    host_B.start()

    host_C = Host('C', backend)
    host_C.add_connection('B')
    host_C.delay = 0
    host_C.start()

    network.add_host(host_A)
    network.add_host(host_B)
    network.add_host(host_C)

    t1 = host_A.run_protocol(protocol_1, (host_C.host_id, ))
    t2 = host_C.run_protocol(protocol_2, (host_A.host_id, ))

    t1.join()
    t2.join()
    network.stop(True)
Example #15
0
def main():
    network = Network.get_instance()
    nodes = ['A', 'B', 'C', 'D', 'E']
    network.start(nodes)

    host_A = Host('A')
    host_A.add_connections(['B', 'C', 'D', 'E'])
    host_A.start()
    host_B = Host('B')
    host_B.add_c_connections(['C', 'D', 'E'])
    host_B.start()
    host_C = Host('C')
    host_C.add_c_connections(['B', 'D', 'E'])
    host_C.start()
    host_D = Host('D')
    host_D.add_c_connections(['B', 'C', 'E'])
    host_D.start()
    host_E = Host('E')
    host_E.add_c_connections(['B', 'C', 'D'])
    host_E.start()

    network.add_hosts([host_A, host_B, host_C, host_D, host_E])

    for i in range(10):
        # The ID of the generated secret EPR pair has to be agreed upon in advance
        epr_id = '123'
        host_A.run_protocol(distribute, ([host_B.host_id, host_C.host_id, host_D.host_id, host_E.host_id],))
        host_B.run_protocol(node, (host_A.host_id,))
        host_C.run_protocol(node, (host_A.host_id,))
        host_D.run_protocol(sender, (host_A.host_id, host_E.host_id, epr_id))
        host_E.run_protocol(receiver, (host_A.host_id, host_D.host_id, epr_id), blocking=True)
        time.sleep(0.5)
    network.stop(True)
Example #16
0
def main():
    network = Network.get_instance()

    nodes = ["Alice", "Bob"]
    network.x_error_rate = 0
    network.delay = 0.5
    network.start(nodes)

    host_alice = Host('Alice')
    host_alice.add_connection('Bob')
    host_alice.max_ack_wait = 10
    host_alice.delay = 0.2
    host_alice.start()

    host_bob = Host('Bob')
    host_bob.max_ack_wait = 10
    host_bob.delay = 0.2
    host_bob.add_connection('Alice')
    host_bob.start()

    network.add_host(host_alice)
    network.add_host(host_bob)

    q_size = 6

    host_alice.run_protocol(qudp_sender, (q_size, host_bob.host_id))
    host_bob.run_protocol(qudp_receiver, (q_size, host_alice.host_id))

    start_time = time.time()
    while time.time() - start_time < 50:
        pass

    network.stop(stop_hosts=True)
Example #17
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    back = ProjectQBackend()
    network.start(nodes, back)

    network.delay = 0.1

    host_alice = Host('Alice', back)
    host_alice.add_connection('Bob')
    host_alice.add_connection('Eve')
    host_alice.start()

    host_bob = Host('Bob', back)
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve', back)
    host_eve.add_connection('Bob')
    host_eve.add_connection('Dean')
    host_eve.add_connection('Alice')
    host_eve.start()

    host_dean = Host('Dean', back)
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    share_list = ["Bob", "Eve", "Dean"]
    q_id1 = host_alice.send_ghz(share_list, no_ack=True)

    q1 = host_alice.get_ghz('Alice', q_id1, wait=10)
    q2 = host_bob.get_ghz('Alice', q_id1, wait=10)
    q3 = host_eve.get_ghz('Alice', q_id1, wait=10)
    q4 = host_dean.get_ghz('Alice', q_id1, wait=10)

    if q1 is None:
        raise ValueError("Q1 is none")
    if q2 is None:
        raise ValueError("Q2 is none")
    if q3 is None:
        raise ValueError("Q3 is none")
    if q4 is None:
        raise ValueError("Q4 is none")

    m1 = q1.measure()
    m2 = q2.measure()
    m3 = q3.measure()
    m4 = q4.measure()

    print("results of measurements are %d, %d, %d, and %d." % (m1, m2, m3, m4))

    network.stop(True)
    exit()
Example #18
0
class Adamd(Player):
    def __init__(self):
        self.host = Host(ADAMD_ID)
        self.host.add_connection(ZARDUS_ID)
        self.host.delay = 0
        self.host.start()
        self.wait_time = 1

    def chat(self, host, zardus_id, qubits_n):

        msg_buff = []
        q_i = 0
        secret_key = []

        while q_i < qubits_n:
            qubit = self.host.get_data_qubit(zardus_id, wait=self.wait_time)
            while qubit is None:
                qubit = self.host.get_data_qubit(zardus_id,
                                                 wait=self.wait_time)
            basis = random.randint(0, 1)
            self.host.send_classical(zardus_id,
                                     f"{q_i}:{basis}",
                                     await_ack=True)
            msg = self.get_next_classical_message(zardus_id, msg_buff, q_i)
            if msg == f"{q_i}:0":
                if basis == 1:
                    qubit.H()
                bit = qubit.measure(non_destructive=True)
                secret_key.append(bit)
            q_i += 1

        key = self.key_array_to_key_string(secret_key)
        cipher = AES.new(key, AES.MODE_EAX)
        nonce = cipher.nonce
        with open('/flag', 'rb') as f:
            data = f.read()
        ciphertext = cipher.encrypt(data)
        self.host.send_classical(zardus_id,
                                 f"-1:{nonce.hex()}",
                                 await_ack=True)
        self.host.send_classical(zardus_id,
                                 f"-2:{ciphertext.hex()}",
                                 await_ack=True)
Example #19
0
File: b92.py Project: tqsd/QuNetSim
def main():

    intercept = True
    key_size = 100
    sample_len = int(key_size / 4)

    network = Network.get_instance()
    nodes = ['Alice', 'Eve', 'Bob']
    network.start(nodes)

    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.5
    host_bob.start()

    network.add_host(host_alice)
    network.add_host(host_eve)
    network.add_host(host_bob)

    secret_key = np.random.randint(2, size=key_size)

    if intercept:
        host_eve.q_relay_sniffing = True
        host_eve.q_relay_sniffing_fn = eve_intercept_quantum

    t1 = host_alice.run_protocol(protocol_alice,
                                 (host_bob.host_id, secret_key, sample_len))
    t2 = host_bob.run_protocol(protocol_bob,
                               (host_alice.host_id, key_size, sample_len))

    t1.join()
    t2.join()
    network.stop(True)
Example #20
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes)
    network.delay = 0.5

    host_alice = Host('Alice')
    host_alice.add_connection('Bob')
    host_alice.max_ack_wait = 30
    host_alice.delay = 0.2
    host_alice.start()

    host_bob = Host('Bob')
    host_bob.max_ack_wait = 30
    host_bob.delay = 0.2
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve')
    host_eve.max_ack_wait = 30
    host_eve.delay = 0.2
    host_eve.add_connection('Bob')
    host_eve.add_connection('Dean')
    host_eve.start()

    host_dean = Host('Dean')
    host_dean.max_ack_wait = 30
    host_dean.delay = 0.2
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    network.x_error_rate = 0
    network.packet_drop_rate = 0

    q_size = 6
    checksum_per_qubit = 2

    host_alice.run_protocol(checksum_sender, (q_size, host_dean.host_id, checksum_per_qubit))
    host_dean.run_protocol(checksum_receiver, (q_size, host_alice.host_id, checksum_per_qubit))

    start_time = time.time()
    while time.time() - start_time < 150:
        pass

    network.stop(stop_hosts=True)
    exit()
Example #21
0
def main():
    global thread_1_return
    global thread_2_return

    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes)
    network.delay = 0.5

    host_alice = Host('alice')
    host_alice.add_connection('bob')
    host_alice.max_ack_wait = 30
    host_alice.delay = 0.2
    host_alice.start()

    host_bob = Host('bob')
    host_bob.max_ack_wait = 30
    host_bob.delay = 0.2
    host_bob.add_connection('alice')
    host_bob.add_connection('eve')
    host_bob.start()

    host_eve = Host('eve')
    host_eve.max_ack_wait = 30
    host_eve.delay = 0.2
    host_eve.add_connection('bob')
    host_eve.add_connection('dean')
    host_eve.start()

    host_dean = Host('dean')
    host_dean.max_ack_wait = 30
    host_dean.delay = 0.2
    host_dean.add_connection('eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    host_alice.run_protocol(retransmission_sender,
                            (host_dean.host_id, MAX_TRIAL_NUM))
    host_dean.run_protocol(retransmission_receiver,
                           (host_alice.host_id, MAX_TRIAL_NUM))

    start_time = time.time()
    while time.time() - start_time < 150:
        pass

    network.stop(stop_hosts=True)
    exit()
Example #22
0
def main():
    # network.classical_routing_algo = routing_algorithm
    nodes = ['A', 'node_1', 'node_2', 'B']
    network.use_hop_by_hop = False
    network.set_delay = 0.1
    network.start(nodes)

    A = Host('A')
    A.add_connection('node_1')
    A.add_connection('node_2')
    A.start()

    node_1 = Host('node_1')
    node_1.add_connection('A')
    node_1.add_connection('B')
    node_1.start()

    node_2 = Host('node_2')
    node_2.add_connection('A')
    node_2.add_connection('B')
    node_2.start()

    B = Host('B')
    B.add_connection('node_1')
    B.add_connection('node_2')
    B.start()

    hosts = [A, node_1, node_2, B]
    for h in hosts:
        network.add_host(h)

    node_1.run_protocol(generate_entanglement)
    node_2.run_protocol(generate_entanglement)

    print('---- BUILDING ENTANGLEMENT   ----')
    # Let the network build up entanglement
    for i in range(10):
        print('building...')
        time.sleep(1)
    print('---- DONE BUILDING ENTANGLEMENT   ----')

    network.quantum_routing_algo = routing_algorithm
    choices = ['00', '11', '10', '01']
    for _ in range(5):
        print('----  sending superdense  ----')
        A.send_superdense(B.host_id, random.choice(choices), await_ack=True)
        time.sleep(1)

    print('stopping')
    try:
        network.stop(stop_hosts=True)
    except Exception:
        print('')
Example #23
0
def main():
    network = Network.get_instance()
    network.delay = 0.1
    nodes = ["Alice", "Bob", "Eve"]
    network.start(nodes)

    host_alice = Host('Alice')
    host_alice.add_connection('Bob')
    host_alice.start()

    host_bob = Host('Bob')
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve')
    host_eve.add_connection('Bob')
    host_eve.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)

    host_alice.send_superdense('Eve', '11', await_ack=True)
    host_alice.send_superdense('Eve', '10', await_ack=True)
    host_alice.send_superdense('Eve', '00', await_ack=True)

    messages = host_eve.get_classical('Alice')

    for m in messages:
        print('----')
        print(m)
        print('----')

    network.stop(True)
Example #24
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    back = EQSNBackend()
    network.start(nodes, back)

    network.delay = 0.1

    host_alice = Host('Alice', back)
    host_alice.add_connection('Bob')
    host_alice.add_connection('Eve')
    host_alice.start()

    host_bob = Host('Bob', back)
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve', back)
    host_eve.add_connection('Bob')
    host_eve.add_connection('Dean')
    host_eve.add_connection('Alice')
    host_eve.start()

    host_dean = Host('Dean', back)
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    share_list = ["Bob", "Eve", "Dean"]
    q_id1, ack_received = host_alice.send_w(share_list, await_ack=True)

    print("Alice received ACK from all? " + str(ack_received))

    q1 = host_alice.get_w('Alice', q_id1, wait=10)
    q2 = host_bob.get_w('Alice', q_id1, wait=10)
    q3 = host_eve.get_w('Alice', q_id1, wait=10)
    q4 = host_dean.get_w('Alice', q_id1, wait=10)

    m1 = q1.measure()
    m2 = q2.measure()
    m3 = q3.measure()
    m4 = q4.measure()

    print("\nResults of measurements are %d, %d, %d, %d." % (m1, m2, m3, m4))

    network.stop(True)
    exit()
Example #25
0
def main():
    # Initialize a network
    network = Network.get_instance()

    nodes = ['Alice', 'Bob', 'Eve']
    network.delay = 0.0
    network.start(nodes)

    host_alice = Host('Alice')
    host_alice.add_connection('Bob')
    host_alice.start()

    host_bob = Host('Bob')
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve')
    host_eve.add_connection('Bob')
    host_eve.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)

    print('Starting transfer')
    t1 = host_alice.run_protocol(banker_protocol, (host_eve.host_id,))
    t2 = host_eve.run_protocol(customer_protocol, (host_alice.host_id,))

    t1.join()
    t2.join()

    network.stop(True)
Example #26
0
def main():
    # Initialize a network
    network = Network.get_instance()

    # Define the host IDs in the network
    nodes = ['Alice', 'Bob', 'Eve']

    network.delay = 0.0

    # Start the network with the defined hosts
    network.start(nodes)

    # Initialize the host Alice
    host_alice = Host('Alice')

    # Add a one-way connection (classical and quantum) to Bob
    host_alice.add_connection('Bob')
    host_alice.delay = 0

    # Start listening
    host_alice.start()

    host_bob = Host('Bob')
    # Bob adds his own one-way connection to Alice and Eve
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.delay = 0
    host_bob.start()

    host_eve = Host('Eve')
    host_eve.add_connection('Bob')
    host_eve.delay = 0
    host_eve.start()

    # Add the hosts to the network
    # The network is: Alice <--> Bob <--> Eve
    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)

    # Generate random key
    key_size = 8  # the size of the key in bit
    secret_key = np.random.randint(2, size=key_size)

    # Concatentate functions
    def alice_func(alice):
        msg_buff = []
        alice_qkd(alice, msg_buff, secret_key, host_eve.host_id)
        alice_send_message(alice, secret_key, host_eve.host_id)

    def eve_func(eve):
        msg_buff = []
        eve_key = eve_qkd(eve, msg_buff, key_size, host_alice.host_id)
        eve_receive_message(eve, msg_buff, eve_key, host_alice.host_id)

    # Run Bob and Alice

    t1 = host_alice.run_protocol(alice_func, ())
    t2 = host_eve.run_protocol(eve_func, ())

    t1.join()
    t2.join()

    network.stop(True)
Example #27
0
File: e91.py Project: tqsd/QuNetSim
def main():
    network = Network.get_instance()

    backend = EQSNBackend()

    number_of_entanglement_pairs = 50

    nodes = ['A', 'B']
    network.start(nodes, backend)
    network.delay = 0.1

    host_A = Host('A', backend)
    host_A.add_connection('B')
    host_A.delay = 0
    host_A.start()

    host_B = Host('B', backend)
    host_B.add_connection('A')
    host_B.delay = 0
    host_B.start()

    network.add_host(host_A)
    network.add_host(host_B)

    t1 = host_A.run_protocol(alice,
                             (host_B.host_id, number_of_entanglement_pairs))
    t2 = host_B.run_protocol(bob,
                             (host_A.host_id, number_of_entanglement_pairs))

    t1.join()
    t2.join()
    network.stop(True)
Example #28
0
def main():
    network = Network.get_instance()
    nodes = ['Alice', 'Bob', 'Eve']
    network.delay = 0.2
    network.start(nodes)

    host_alice = Host('Alice')
    host_bob = Host('Bob')
    host_eve = Host('Eve')

    host_alice.add_connection('Bob')
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_eve.add_connection('Bob')

    host_alice.start()
    host_bob.start()
    host_eve.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)

    q = Qubit(host_alice)
    print(q.id)
    q.X()

    host_alice.send_epr('Eve', await_ack=True)
    print('done')
    host_alice.send_teleport('Eve', q, no_ack=True)
    q_eve = host_eve.get_data_qubit(host_alice.host_id, q.id, wait=5)

    assert q_eve is not None
    print(q.id)
    print('Eve measures: %d' % q_eve.measure())
    network.stop(True)
Example #29
0
def main():
    # Initialize a network
    network = Network.get_instance()
    nodes = ['Bank', 'Customer', 'Eve']
    network.delay = 0.2
    network.start(nodes)

    host_bank = Host('Bank')
    host_bank.add_connection('Eve')
    host_bank.delay = 0.3
    host_bank.start()

    host_eve = Host('Eve')
    host_eve.add_connection('Bank')
    host_eve.add_connection('Customer')
    host_eve.start()

    host_customer = Host('Customer')
    host_customer.add_connection('Eve')
    host_customer.delay = 0.3
    host_customer.start()

    network.add_host(host_bank)
    network.add_host(host_eve)
    network.add_host(host_customer)

    host_eve.q_relay_sniffing = True
    host_eve.q_relay_sniffing_fn = sniffing_quantum

    print('Starting transfer')

    t = host_customer.run_protocol(customer_protocol, (host_bank.host_id, ))
    host_bank.run_protocol(banker_protocol, (host_customer.host_id, ),
                           blocking=True)
    t.join()

    network.stop(True)
Example #30
0
 def __init__(self):
     self.host = Host(ADAMD_ID)
     self.host.add_connection(ZARDUS_ID)
     self.host.delay = 0
     self.host.start()
     self.wait_time = 1