1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| prob = 0
def findPath(left, right, round): global dt3, dt5, prob print("+ + + + + + attackOneRound + + + + + +") print(left) print(right) temp = left for i in range(4): for j in range(4): if temp[i][j]!=0: if i%2==0: target = findTarget(dt3, temp[i][j]) prob += math.log(8/dt3[temp[i][j]][target], 2) else: target = findTarget(dt5, temp[i][j]) prob += math.log(32/dt5[temp[i][j]][target], 2) temp[i][j] = target
temp = bitRot(mixRow(temp)) xor = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) for i in range(4): for j in range(4): xor[i][j] = temp[i][j]^right[i][j] print("round", round+1) print("probability", prob) if prob > 128: return findPath(xor, left, round+1)
def findTarget(table, row): for col in range(len(table)): if table[row][col]!=0: return col
def decimal2array(val, k): res = [] while val!=0: res.append(val%2) val = int(val/2) res = res[::-1] res2 = [] if len(res)<k: for i in range(k-len(res)): res2.append(0) res2.extend(res) return res2
def array2decimal(arr): res = 0 arr2 = arr[::-1] for i in range(len(arr)): if (arr2[i]==1): res += pow(2,i) return res
if __name__=='__main__': dt3, dt5 = getDt() left = np.array([ [ 0, 0, 0, 0], [ 0, 0, 0, 0], [ 0, 0, 0, 1], [ 0, 0, 0, 0] ]) right = np.array([ [ 0, 0, 0, 0], [ 0, 0, 0, 0], [ 0, 0, 0, 0], [ 0, 0, 0, 0] ]) findPath(left, right, 0)
|