Ejemplo n.º 1
0
def main():
    network = Network.get_instance()
    network.start()

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

    host_alice.add_connection('Bob')
    host_bob.add_connections(['Alice', 'Eve'])
    host_eve.add_connection('Bob')

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

    network.add_hosts([host_alice, host_bob, 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, await_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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes)
    network.delay = 0.1

    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.add_connection('Dean')
    host_eve.start()

    host_dean = Host('Dean')
    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)

    # Create a qubit owned by Alice
    q = Qubit(host_alice)
    # Put the qubit in the excited state
    q.X()
    # Send the qubit and await an ACK from Dean
    q_id = host_alice.send_qubit('Dean', q, no_ack=True)

    # Get the qubit on Dean's side from Alice
    q_rec = host_dean.get_data_qubit('Alice', q_id)

    # Ensure the qubit arrived and then measure and print the results.
    if q_rec is not None:
        m = q_rec.measure()
        print("Results of the measurements for q_id are ", str(m))
    else:
        print('q_rec is none')

    network.stop(True)
    exit()
Ejemplo n.º 4
0
def receiver_protocol(host: Host, sender: str):
    q = host.get_data_qubit(sender, wait=10)
    print(f"{host.host_id} received qubit and measured the state {q.measure()}.")