def attitute(varsNum, lst): OribitList = finalOribit(varsNum) dicIndexList = initRSBF(varsNum, lst, OribitList) truthTable = TruthTableSelect(varsNum, dicIndexList) flag = False if truthTable == pow(2, varsNum): flag = True res1 = nonlinearityCompute(varsNum, truthTable, innerProductSelect(varsNum)) if res1 > 236: res2 = transparencyCompute(varsNum, truthTable, dicIndexList) with open("gen_res.txt", mode = "a+", encoding="utf-8") as f: f.write(str(lst) + " " + str(res1) + str(flag) + str(res2) + "\n") elif flag == True and res1 >= 234: res2 = transparencyCompute(varsNum, truthTable, dicIndexList) with open("gen_res.txt", mode="a+", encoding="utf-8") as f: f.write(str(lst) + " " + str(res1) + str(flag) + str(res2) + "\n")
def nonlinearityAndTransparency(varsNum, lst): OribitList = finalOribit(varsNum) dicIndexList = initRSBF(varsNum, lst, OribitList) truthTable = TruthTableSelect(varsNum, dicIndexList) res1 = nonlinearityCompute(varsNum, truthTable) res2 = transparencyCompute(varsNum, truthTable, dicIndexList) return res1, res2, len(dicIndexList)
def Function(): varsNum = 8 hexTruthTable = "18CA9ED8BC4EC1AFE2F4C023FA63E78949455BC59DB873BE79409BAE4B289029" truthTable = hexToBinary(hexTruthTable) dicIndexList = normalIndexSelect(varsNum, truthTable) res1 = nonlinearityCompute(varsNum, truthTable) res2 = transparencyCompute(varsNum, truthTable, dicIndexList) print("nonlinearity = ", res1) print("transparency = ", res2) res = non_absolute_indicatorSelect(varsNum, truthTable, dicIndexList) print("non_absolute_indicator = ", res)
def nonlinearityAndTransparency(varsNum, lst, innerProduct, OribitList): dicIndexList = initRSBF(varsNum, lst, OribitList) truthTable = TruthTableSelect(varsNum, dicIndexList) balanceFlag = "" if truthTable.count(1) == pow(2, varsNum - 1): balanceFlag = "balanced function" else: balanceFlag = "unbalanced function" res1 = nonlinearityCompute(varsNum, truthTable, innerProduct) res2 = transparencyCompute(varsNum, truthTable, dicIndexList) # print("nonlinearity = " + str(res1), " transparency = " + str(res2)) return res1, res2, balanceFlag
def perprority(varsNum, lst): OribitList = finalOribit(varsNum) dicIndexList = initRSBF(varsNum, lst, OribitList) truthTable = TruthTableSelect(varsNum, dicIndexList) res1 = nonlinearityCompute(varsNum, truthTable, innerProductSelect(varsNum)) print(res1) if res1 < 230: return flag = "" if truthTable.count(1) == pow(2, varsNum): flag = "balanced" res2 = transparencyCompute(varsNum, truthTable, dicIndexList) with open("best.txt", mode="a+", encoding="utf-8") as f: f.write( str(lst) + " nonlinearity = " + str(res1) + " transparency = " + str(res2) + " " + flag + "\n") print("nonlinearity = ", res1) print("transparency", res2)
def classifier(varsNum, truthTable): count = -1 oneNumList = [] zeroNumList = [] for ele in truthTable: count = count + 1 if ele == 1: oneNumList.append(count) elif ele == 0: zeroNumList.append(count) else: print("fail") oneNumdic = {} oneNumcount = 0 for ele in oneNumList: oneNumdic[oneNumcount] = ele oneNumcount = oneNumcount + 1 # print(oneNumdic) zeroNumdic = {} zeroNumcount = 0 for ele in zeroNumList: zeroNumdic[zeroNumcount] = ele zeroNumcount = zeroNumcount + 1 # print(zeroNumdic) randList = np.random.randint(0, 128, 64) one_zero_list = flipTruthTable(oneNumdic, zeroNumdic, randList) newTruthTable = reformTruthtable(varsNum, one_zero_list) alltruthTable = EightVars_AllTruthTable() indexDicList = trans(8, newTruthTable, alltruthTable) nonlinearity = nonlinearityCompute(varsNum, newTruthTable) transparency = transparencyCompute(varsNum, newTruthTable, indexDicList) if nonlinearity > 110: print(f"the nonlinearity = {nonlinearity}") print(f"the transparency = {transparency}") return newTruthTable
def possibleTable(varsNum, beginPos, endPos): start = time.time() count = 0 allOribit = finalOribit(varsNum) for i in range(beginPos, endPos): tmp = str(bin(i)).split("0b") for elem in tmp: if elem == '': continue tmpList = [] for element in elem: tmpList.append(int(element)) if len(tmpList) != 36: for i in range(36 - len(tmpList)): tmpList.insert(0, 0) oribitLocList = oneNumToOribit(tmpList) # print(oribitLocList) balancedFlag = isBalanced(varsNum, oribitLocList, allOribit) if balancedFlag == False: break indexList = initRSBF(varsNum, oribitLocList, allOribit) truthTable = TruthTableSelect(varsNum, indexList) nonlinearity = nonlinearityCompute(varsNum, truthTable) # print(nonlinearity) if nonlinearity >= 112: transparency = transparencyCompute(varsNum, truthTable, indexList) with open("result/pow_35_" + str(endPos) + "_search.txt", mode = "a+", encoding = "utf-8") as f: f.write(str(oribitLocList) + " " + str(nonlinearity) + " " + str(transparency) + "\n") count += 1 if count % 10000000 == 0: print(str(beginPos) + "->" + str(endPos) + "check ", count) end = time.time() print(str(beginPos) + "->" + str(endPos) + "during time1 is ", end - start) print(count)
def nonlinearityAndTransparency(self): midRes = self.__midProcess() return nonlinearityCompute(self.varsNum, midRes[0]), \ transparencyCompute(self.varsNum, midRes[0], midRes[1])
def transparency(self): midRes = self.__midProcess() return transparencyCompute(self.varsNum, midRes[0], midRes[1])
if FlagList.count(True) == len(TableOribitList): print("Rotation Symmetric Prove") else: print("The function is not Rotation Symmetric") if __name__ == '__main__': start = time.time() varsNum = 8 truthTable = functionTruthTable() print(truthTable) innerProduct = innerProductSelect(varsNum) isRotationSymmetric(varsNum, truthTable) print(truthTable.count(1)) alltruthTable = AllTruthTableSelect(varsNum) indexDicList = trans(varsNum, truthTable, alltruthTable) nonlinearity = nonlinearityCompute(varsNum, truthTable, innerProduct) end = time.time() transparency = transparencyCompute(varsNum, truthTable, indexDicList) print("during time is ", (end - start) / 60) print(f"the nonlinearity = {nonlinearity}") print(f"the transparency = {transparency}") # # # varsNum = 9 # oneNumList = [2, 4, 5, 9, 11, 12, 13, 14, 19, 20, 22, 23, 27, 28, 29, 31, 37, 38, 39, 43, 44, 45, 47, 50, 52, 53, 54, 56, 58, 59] # OribitList = finalOribit(varsNum) # dicIndexList = initRSBF(varsNum, oneNumList, OribitList) # truthTable = TruthTableSelect(varsNum, dicIndexList) # isRotationSymmetric(9, truthTable)
from transparency import transparencyCompute, truthTableAndIndexLixt if __name__ == '__main__': varsNum = int(input("请输入布尔函数变元个数:")) truthTable, dicIndexList = truthTableAndIndexLixt(varsNum) print(dicIndexList) balancedFlag = False oneNum = 0 zeroNum = 0 for ele in truthTable: if ele == 1: oneNum = oneNum + 1 else: zeroNum = zeroNum + 1 if oneNum == zeroNum: balancedFlag = True print("this is a balanced boolean function") nonlinearity = nonlinearityCompute(varsNum, truthTable) print(f"the nonlinearity = {nonlinearity}") transparency = transparencyCompute(varsNum, truthTable, dicIndexList) print(f"the transparency = {transparency}") # 输出为0的输入形式 [{'x1': 1, 'x2': 1, 'x3': 0}, {'x1': 0, 'x2': 1, 'x3': 1}] # [[[0, 0, 0, 0]], [[0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 0, 0], [0, 1, 1, 0]]]
def twoStep(varsNum, Dict_ADJcost, functionTruthTableList, F_MIN): index_TruthTable = {} i = 0 for ele in Dict_ADJcost.keys(): index_TruthTable[ele] = functionTruthTableList[i] i += 1 # 对字典依照value值进行排序 DictSortList = sorted(Dict_ADJcost.items(), key=lambda x : x[1], reverse = True) DictSortListLen = len(DictSortList) if DictSortListLen > 1: for i in range(DictSortListLen): indexList = index_TruthTable[DictSortList[DictSortListLen - i - 1][0]] tmpDic = {} tmpDic[DictSortList[DictSortListLen - i - 1][1]] = indexList sortList1 = DictSorted(tmpDic) tmpDic[DictSortList[DictSortListLen - i - 1][1]] = sortList1 if tmpDic not in F_MIN: dicTmp = {} dicTmp[DictSortList[DictSortListLen - i - 1][1]] = indexList sortList2 = DictSorted(dicTmp) dicTmp[DictSortList[DictSortListLen - i - 1][1]] = sortList2 # print(dicTmp) F_MIN.append(dicTmp) break else: indexList = functionTruthTableList[0] tmpDic = {} tmpDic[0] = indexList sortList1 = DictSorted(tmpDic) tmpDic[DictSortList[0]] = sortList1 if tmpDic not in F_MIN: dicTmp = {} dicTmp[DictSortList[0][1]] = indexList sortList2 = DictSorted(dicTmp) dicTmp[DictSortList[0][1]] = sortList2 F_MIN.append(dicTmp) tmpList = [] for ele in list(F_MIN[-1].values()): for elem in ele: tmpList.append(elem) if len(tmpList) == pow(2, varsNum - 1): info = "balanced boolean function" else: info = "unbalanced boolean function" oneNumList = [] allOribit = finalOribit(varsNum) for ele in tmpList: for i in range(len(allOribit)): if ele in allOribit[i]: oneNumList.append(i + 1) dicIndexList = initRSBF(varsNum, list(set(oneNumList)), allOribit) truthTable = TruthTableSelect(varsNum, dicIndexList) res1 = nonlinearityCompute(varsNum, truthTable) if res1 < 980: print("no write") if res1 >= 980: res2 = transparencyCompute(varsNum, truthTable, dicIndexList) with open("search_result/nobalanced_nonlinearity_" + str(res1) + "_resFile.txt", mode = "a", encoding= "utf-8") as f: f.write(str(list(set(oneNumList))) + " nonlinearity = " + str(res1) + " transparency = " + str(res2) + " " + str(info) + "\n") return list(set(oneNumList))
def costFunction2(varsNum, truthTable, dicIndexList): return transparencyCompute(varsNum, truthTable, dicIndexList)
"8 2 E 8 F E 4 B 1 2 E 3 A 3 A 2 E 8 A F 4 E 6 6 8 4 4 A 2 A 0 C " "8 0 E 2 E 9 7 E 5 1 B C 8 B D F F 8 "] tmpList = [] for ele in TruthTableList1: tmpList = ele.split(" ") binaryListTmp = hexToBinary(tmpList) binaryList = [] for ele in binaryListTmp: for elem in ele: binaryList.append(elem) # print(binaryList) return binaryList if __name__ == '__main__': truthTable = functionTruthTable() alltruthTable = EightVars_AllTruthTable() indexDicList = trans(8, truthTable, alltruthTable) # print(indexDicList) # print(indexDicList) nonlinearity = nonlinearityCompute(8, truthTable) transparency = transparencyCompute(8, truthTable, indexDicList) # print(f"the nonlinearity = {nonlinearity}") print(f"the transparency = {transparency}")
def costFunction(varsNum, truthTable, indexList, nonlinearity): return -(nonlinearity / 241) + transparencyCompute(varsNum, truthTable, indexList)