def computeDelta(matlabEng, arrData_bin_1, n, k, m, strCoder): """ compute the delta for reconciliation """ # find corresponding codeword of data 1 arrMsg_bin_1 = None arrCodeword_bin_1 = None if(strCoder == CODER_GOLAY): n = 23 k = 12 arrMsg_bin_1 = golay.decode(matlabEng, arrData_bin_1, n) arrCodeword_bin_1 = golay.encode(matlabEng, arrMsg_bin_1, k) elif (strCoder == CODER_RS): arrMsg_bin_1 = rs.decode(matlabEng, arrData_bin_1, n, k, m) arrCodeword_bin_1 = rs.encode(matlabEng, arrMsg_bin_1, n, k, m) elif(strCoder == CODER_HAMMING): arrMsg_bin_1 = fec.decode(matlabEng, arrData_bin_1, n, k, strCoder) arrCodeword_bin_1 = fec.encode(matlabEng, arrMsg_bin_1, n, k, strCoder) else: raise ValueError("Unkown coder") # compute the difference arrDelta = np.bitwise_xor(arrData_bin_1, arrCodeword_bin_1) return arrDelta
def reconciliate(matlabEng, arrDelta, arrData_bin_2, n, k, m, strCoder): """ Given the delta, try to deduce data1 via reconciliation """ arrMsg_bin_2 = None arrCodeword_bin_2 = None if (strCoder == CODER_GOLAY): n = 23 k = 12 arrMsg_bin_2 = golay.decode(matlabEng, np.bitwise_xor(arrData_bin_2, arrDelta), n) arrCodeword_bin_2 = golay.encode(matlabEng, arrMsg_bin_2, k) elif (strCoder == CODER_RS): arrMsg_bin_2 = rs.decode(matlabEng, np.bitwise_xor(arrData_bin_2, arrDelta), n, k, m) arrCodeword_bin_2 = rs.encode(matlabEng, arrMsg_bin_2, n, k, m) elif (strCoder == CODER_HAMMING): arrMsg_bin_2 = fec.decode(matlabEng, np.bitwise_xor(arrData_bin_2, arrDelta), n, k, strCoder) arrCodeword_bin_2 = fec.encode(matlabEng, arrMsg_bin_2, n, k) else: raise ValueError("Unkown coder") # deduce data 1 from data 2 + delta arrDeducedData_bin_1 = np.bitwise_xor(arrCodeword_bin_2, arrDelta) return arrDeducedData_bin_1
def test_random_secret(matlabEng, n, k, nErrorBits=1): # data 1 arrData_1 = np.random.randint(0, 2, n) arrData_bin_1 = ct.toBinaryArray(arrData_1) print "d1 (%d): "%len(arrData_bin_1), arrData_bin_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 print "d2 (%d): "%len(arrData_bin_1), arrData_bin_2 # create secret at random arrSecret = np.random.randint(0, 2, k) arrSecret_bin = ct.toBinaryArray(arrSecret) arrCodeword_bin = fec.encode(matlabEng, arrSecret_bin, n, k, 'hamming/binary') # compute the difference arrDelta = np.bitwise_xor(arrData_bin_1, arrCodeword_bin) # deduce securet arrCodeword_bin_2 = np.bitwise_xor(arrData_bin_2, arrDelta) arrSecret_bin_2 = fec.decode(matlabEng, arrCodeword_bin_2, n, k, 'hamming/binary') # compute the BER nMismatchedBits = np.size(arrSecret_bin) - \ np.sum(arrSecret_bin==arrSecret_bin_2) print "error bit=", nMismatchedBits, ", BER=",\ nMismatchedBits*1.0/np.size(arrSecret_bin)