Exemple #1
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}")
Exemple #2
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()