コード例 #1
0
def measure_half_qbits(qbit_list):
    """ Utility function: measure half the qubit of the specified list """
    udr.dbg_print("Eve: *attack*: measuring 1/2 of Alice's qbits")
    measureInPlace = True
    flag = False
    for i in range(len(qbit_list)):
        flag = not flag
        if flag:
            qbit_list[i].measure(measureInPlace)
コード例 #2
0
def execute_attack(attack, qbit_list):
    """ Execute the attack requested by the user via command line """
    if attack == udr.AttackType.NoAttack:
        udr.dbg_print("Eve: no attack")
    elif attack == udr.AttackType.MeasureAllQbits:
        measure_all_qbits(qbit_list)
    elif attack == udr.AttackType.MeasureHalfQbits:
        measure_half_qbits(qbit_list)
    else:
        raise RuntimeError("Eve: invalid attack code={}".format(attack))
コード例 #3
0
def run_protocol(Eve, cacClient):

    # Retrieve test global parameters
    num_bb84_qbits = udr.get_config_num_qbits()
    attack = udr.get_config_attack_type()

    # Receive Alice's bb84 qbits and forward them to Bob
    alice_qbits = udr.recv_qbit_list(Eve, num_bb84_qbits)

    execute_attack(attack, alice_qbits)

    udr.dbg_print("Eve: received Alice's qbits - forwarding to Bob")

    # Forward Alice's qbits to Bob
    udr.send_qbit_list(Eve, "Bob", alice_qbits)

    udr.dbg_print("Eve: THE END")
コード例 #4
0
def measure_all_qbits(qbit_list):
    """ Utility function: measure all the qubit of the specified list """
    udr.dbg_print("Eve: *attack*: measuring all of Alice's qbits")
    measureInPlace = True
    for qbit in qbit_list:
        qbit.measure(measureInPlace)
コード例 #5
0
ファイル: bobTest.py プロジェクト: uderos/QuCrypto-Final
def run_protocol(Bob, cacClient):

    # Retrieve test global parameters
    num_bb84_qbits = udr.get_config_num_qbits()

    # Receive bb84 qbits from Alice
    udr.dbg_print("Bob: waiting for bb84 qbits from Alice")
    qbit_list = udr.recv_qbit_list(Bob, num_bb84_qbits)

    #Generate a set of random classical bits (theta)
    theta_bob = udr.generate_random_bits(num_bb84_qbits)

    # Measure Alice's bb84 qbits
    x = udr.measure_bb84_qbit_list(qbit_list, theta_bob)
    udr.dbg_print("Bob: x={}".format(x))

    # Send Alice an acknowledge message
    udr.dbg_print("Bob: sending ack to ALice")
    cacClient.sendAck('Alice')

    # Receive Alices's basis string
    udr.dbg_print("Bob: waiting for Alice basis string")
    theta_alice = cacClient.getValueList('Alice')

    # Send Alice out basis string
    udr.dbg_print("Bob: sending Alice my basis string")
    cacClient.sendValueList("Alice", theta_bob)

    #Discard bits measured in different basis
    x1 = udr.discard_bits(x, theta_alice, theta_bob)
    udr.dbg_print("Bob: x1={}".format(x1))
    n1 = len(x1)
    if not n1 > 1:
        raise udr.bb84Error_NoBitsBasisCkeck(
            "Bob: only {} bits left after basis check".format(n1))

    # Receive test string indexes and values from Alice
    udr.dbg_print("Bob: waiting for Alice's test index list")
    idx_test_list = cacClient.getValueList('Alice')
    udr.dbg_print("Bob: waiting for Alice's test bit string")
    xt_alice = cacClient.getValueList('Alice')

    # Generate the test list and send it to Alice
    nt = len(idx_test_list)
    xt_bob = udr.generate_sublist_from_idx(x1, idx_test_list)
    udr.dbg_print("Bob: sending Alice test list")
    cacClient.sendValueList("Alice", xt_bob)

    # Peform error check
    if not xt_alice == xt_bob:
        raise udr.bb84Error_TestCheck("Bob: Error check failure: {}:{}".format(
            xt_alice, xt_bob))

    # Remove test bits from bit string
    x2 = udr.generate_sublist_removing_idx(x1, idx_test_list)
    udr.dbg_print("Bob: x2={}".format(x2))
    n2 = len(x2)
    if not n2 > 0:
        raise udr.bb84Error_TestCheck(
            "Bob: only {} bits left after error check".format(n2))

    # Calculate the key as xor of the remaining bits
    key_bit = udr.calculate_bit_list_xor(x2)
    udr.dbg_print("Bob: key_bit={}".format(key_bit))

    # We are done !
    return key_bit
コード例 #6
0
ファイル: aliceTest.py プロジェクト: uderos/QuCrypto-Final
def run_protocol(Alice, cacClient):

    # Retrieve test global parameters
    num_bb84_qbits = udr.get_config_num_qbits()

    #Generate a set of random classical bits (x)
    x = udr.generate_random_bits(num_bb84_qbits)
    udr.dbg_print("Alice: x={}".format(x))

    #Generate a set of random classical bits (theta)
    theta_alice = udr.generate_random_bits(num_bb84_qbits)

    #Generate the set of bb84 qbits
    qbits_list = udr.create_bb84_states(Alice, x, theta_alice)

    #Send the qbits to Bob (via Eve)
    udr.dbg_print(
        "Alice: sending {} qbits to Bob via Eve".format(num_bb84_qbits))
    udr.send_qbit_list(Alice, "Eve", qbits_list)

    # Wait for acknowledge from Bob
    udr.dbg_print("Alice: waiting for Bob's acknowledge")
    cacClient.getAck('Bob')

    # Send Bob our basis string
    udr.dbg_print("Alice: sending Bob the basis string")
    cacClient.sendValueList('Bob', theta_alice)

    # Receive Bob's basis string
    udr.dbg_print("Alice: waiting for Bob basis string")
    theta_bob = cacClient.getValueList('Bob')
    udr.dbg_print("Alice: got Bob basis string")
    udr.dbg_print("Alice: alice_basis={}".format(theta_alice))
    udr.dbg_print("Alice:   bob_basis={}".format(theta_bob))

    #Discard bits measured in different basis
    x1 = udr.discard_bits(x, theta_alice, theta_bob)
    udr.dbg_print("Alice: x1={}".format(x1))
    n1 = len(x1)
    if not n1 > 1:
        raise udr.bb84Error_NoBitsBasisCkeck(
            "Alice: only {} bits left after basis check".format(n1))

    # Generate test string indexes and test bits
    nt = n1 // 2
    idx_test_list = udr.generate_random_indexes(n1, nt)
    xt_alice = udr.generate_sublist_from_idx(x1, idx_test_list)

    # Send test string indexes and values to Bob
    udr.dbg_print("Alice: Sending Bob the test index list")
    cacClient.sendValueList('Bob', idx_test_list)
    time.sleep(2)
    udr.dbg_print("Alice: Sending Bob test bit string")
    cacClient.sendValueList('Bob', xt_alice)

    # Receive test values from Bob and peform error check
    udr.dbg_print("Alice: waiting for Bob's error checking bit list")
    xt_bob = cacClient.getValueList('Bob')
    udr.dbg_print("Alice: xt_alice={}".format(xt_alice))
    udr.dbg_print("Alice:   xt_bob={}".format(xt_bob))
    if not xt_alice == xt_bob:
        raise udr.bb84Error_TestCheck(
            "Alice: Error check failure: {}:{}".format(xt_alice, xt_bob))

    # Remove test bits from bit string
    x2 = udr.generate_sublist_removing_idx(x1, idx_test_list)
    udr.dbg_print("Alice: x2={}".format(x2))
    n2 = len(x2)
    if not n2 > 0:
        raise udr.bb84Error_TestCheck(
            "Alice: only {} bits left after error check".format(n2))

    # Calculate the key as xor of the remaining bits
    key_bit = udr.calculate_bit_list_xor(x2)
    udr.dbg_print("Alice: key_bit={}".format(key_bit))

    # We are done !
    return key_bit