def test_error_correction(arrEncoded, nErrorBits):
    # data1
    arrEncoded_1 = arrEncoded
    arrEncoded_bin_1 = ct.toBinaryArray(arrEncoded_1)

    # data2
    arrEncoded_bin_2 = np.copy(arrEncoded_bin_1)
    # introduce some errors
    arrErrorPos = np.random.choice(range(len(arrEncoded_bin_2)), nErrorBits,
                                   replace=False)
    for i in arrErrorPos:
        arrEncoded_bin_2[i] = (arrEncoded_bin_2[i] + 1)%2
    
    # error correction
    n = 7
    k = 4
    lsh = LSHash(k, n)
    arrDecoded_bin_1 = lshDecode(ct.zeroPadding(arrEncoded_bin_1, n)[0], 
                                 lsh, n, k)
    arrDecoded_bin_2 = lshDecode(ct.zeroPadding(arrEncoded_bin_2, n)[0], 
                                 lsh, n, k)
    
    
    # BER
    nErrorBits, dBER = ct.computeBER(arrEncoded_bin_1, arrEncoded_bin_2)
    nErrorBits_ec, dBER_ec = ct.computeBER(arrDecoded_bin_1, arrDecoded_bin_2)
    
    return nErrorBits, dBER, nErrorBits_ec, dBER_ec
Пример #2
0
def evaluateReconciliation(lsSrcCode_bin_key, dcReconciliation):
    """
        Evaluate the performance of reconciliation
    """
    dcPerformance = {}    
    for i in [DATA_ID_USER, DATA_ID_ATTACKER]:
        # before reconciliation
        arrSrcCode1 = lsSrcCode_bin_key[i]
        arrSrcCode2 = lsSrcCode_bin_key[DATA_ID_PAYEND]
        nErrorBits_src, dBER_src =ct.computeBER(arrSrcCode1,
                                                arrSrcCode2)
        # after reconciliation
        arrData_bin_1 = dcReconciliation[i][DATA_1]
        arrDeduced_1 = dcReconciliation[i][DEDUCED_D1]

        nErrorBit_ec, dBER_ec =ct.computeBER(arrData_bin_1,
                                             arrDeduced_1)
                                             
        if(i == DATA_ID_USER):
            dcPerformance[BER_USER_SRC] = dBER_src
            dcPerformance[BER_USER_EC] = dBER_ec
            dcPerformance[ERR_USER_SRC] = nErrorBits_src
            dcPerformance[ERR_USER_EC] = nErrorBit_ec
        elif (i == DATA_ID_ATTACKER):
            dcPerformance[BER_ATTACKER_SRC] = dBER_src
            dcPerformance[BER_ATTACKER_EC] = dBER_ec
            dcPerformance[ERR_ATTACKER_SRC] = nErrorBits_src
            dcPerformance[ERR_ATTACKER_EC] = nErrorBit_ec
        else:
            raise ValueError("Unknown data id: %d" % i)
            
    return dcPerformance