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
def test_reconciliation(nTrials, matlabEng, nKeySize, nErrorBits=1, n=7, k=4, m=3, strCoder='hamming/binary'): """ test the reconciliation method mentioned in 'proximate, mobisys'11' """ lsStat = [] for i in xrange(nTrials): # data 1 arrData_1 = np.random.randint(0, 2, nKeySize) arrData_bin_1 = ct.toBinaryArray(arrData_1) # data2 arrData_bin_2 = np.copy(arrData_bin_1) # introduce some errors arrErrorPos = np.random.choice(range(len(arrData_bin_2)), nErrorBits, replace=False) for i in arrErrorPos: arrData_bin_2[i] = (arrData_bin_2[i] + 1)%2 # padding nPaddingSize = m*n if strCoder == CODER_RS else n arrData_bin_1 = ct.zeroPadding(arrData_bin_1, nPaddingSize)[0] arrData_bin_2 = ct.zeroPadding(arrData_bin_2, nPaddingSize)[0] # compute the difference arrDelta = computeDelta(matlabEng, arrData_bin_1, n, k, m, strCoder) # reconciliation arrDeducedData_bin_1 = reconciliate(matlabEng, arrDelta, arrData_bin_2, n, k, m, strCoder) # compute the BER nMismatchedBits = np.size(arrData_bin_1) - \ np.sum(arrData_bin_1==arrDeducedData_bin_1) dBER = nMismatchedBits*1.0/np.size(arrData_bin_1) lsStat.append({'err_bits': nMismatchedBits, 'BER': dBER}) if (nMismatchedBits != 0): print nMismatchedBits, arrErrorPos # print "error bit=", nMismatchedBits, ", BER=", dBER dAvgErrBits = np.mean([i['err_bits'] for i in lsStat ]) dAvgBER = np.mean([i['BER'] for i in lsStat ]) print "----\n AvgErrorBits=%.3f, BER=%.3f" % (dAvgErrBits, dAvgBER)
def reconciliation(lsSrcCode_bin, eng, strCoder, n, k, m): """ Perform reconciliation """ dcReconciliation = {} # compute delta arrData_bin_user = lsSrcCode_bin[DATA_ID_USER] nPadding = n*m if strCoder==ecc.CODER_RS else n arrData_bin_user, nPd = ct.zeroPadding(arrData_bin_user, nPadding) arrDelta = ecc.computeDelta(eng, arrData_bin_user, n, k, m, strCoder) # reconciliation btw user & payend arrData_bin_payend = lsSrcCode_bin[DATA_ID_PAYEND] arrData_bin_payend, nPd = ct.zeroPadding(arrData_bin_payend, nPadding) arrDeduced_bin_user_payend = ecc.reconciliate(eng, arrDelta, arrData_bin_payend, n, k, m, strCoder) dcReconciliation[DATA_ID_USER] = {DATA_1: arrData_bin_user, DATA_2: arrData_bin_payend, DEDUCED_D1: arrDeduced_bin_user_payend} # reconciliation btw attacker & payend arrData_bin_attacker = lsSrcCode_bin[DATA_ID_ATTACKER] arrData_bin_attacker, nPd = ct.zeroPadding(arrData_bin_attacker, nPadding) arrDeduced_bin_user_attacker = ecc.reconciliate(eng, arrDelta, arrData_bin_attacker, n, k, m, strCoder) dcReconciliation[DATA_ID_ATTACKER] = {DATA_1: arrData_bin_user, DATA_2: arrData_bin_attacker, DEDUCED_D1: arrDeduced_bin_user_attacker} return dcReconciliation
def repetitiveDecode(arrEncoded, k): """ error correction via repetive coding """ # zeroPadding arrEncoded, nPadding = ct.zeroPadding(arrEncoded, k) # decode lsDecoded = [] for nStart in xrange(0, len(arrEncoded), k): arrBin = arrEncoded[nStart: nStart+k] dcValueCount = {} for i in arrBin: nCount = dcValueCount.get(i, 0) dcValueCount[i] = nCount + 1 nCodeWord = max(dcValueCount.iteritems(), key=operator.itemgetter(1))[0] lsDecoded.append(nCodeWord) return np.array(lsDecoded)
def interleave(eng, arrData_bin, nParam, strMethod=INTRLV_MATRIX): """ interleave data in a block way """ data_intrlv = None nPadding = None if(strMethod == INTRLV_MATRIX): nCols = nParam arrData_bin, nPadding = ct.zeroPadding(arrData_bin, nCols) nRows = len(arrData_bin)/nCols data = matlab.int64(list(arrData_bin)) data_intrlv = eng.matintrlv(data, nRows, nCols) elif(strMethod == INTRLV_RANDOM): nSeed = 4831 data = matlab.int64(list(arrData_bin)) data_intrlv = eng.randintrlv(data, nSeed) else: raise ValueError("Unsupported interleaving method: %s" % strMethod) arrData_intrlv = np.array([i for i in data_intrlv[0]]) return arrData_intrlv, nPadding