Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def xor(a, b):
- return eval("%s^%s" % (a, b))
- def generatePoly(deg, numberOfVariables, numberOfPoly, file):
- variables=[]
- for i in range(1,numberOfVariables+1):
- variables.append('x'+str(i))
- R = BooleanPolynomialRing(names=variables)
- R.inject_variables()
- poly = []
- for _ in range(0,numberOfPoly):
- poly = R.random_element(degree=deg,terms=+infinity)
- poly.append(poly)
- save(poly, file)
- def loadPoly(file):
- return load(file)
- def getFeedback():
- feedback = []
- tmp = []
- tmp = random_vector(GF(2), 256)
- for i in range(0, 256):
- if tmp[i] == 1:
- feedback.append(i)
- return feedback
- def genPermutation():
- return Permutations(256).random_element()
- def BlackBox(IV, key, polynomy2, polynomy8, feedback, permutacia):
- LFSR = []
- ivAkey = vector(list(IV) + list(key))
- for i in range(0, 256):
- #-----------------This is tricky part-------------------------------------------
- LFSR.append(polynomy2[i](*ivAkey))
- #---------------------------------------------------------------------------------------------------
- for _ in range(0,2000):
- #count feedback
- bit = 0
- for i in feedback:
- bit += LFSR[i]
- #shift
- for j in range(0,255):
- LFSR[j] = LFSR[j+1]
- #assign feedback
- LFSR[255] = bit
- indexInputs = []
- inputs = []
- outputs = []
- for i in range(0, 32):
- indexInputs = permutacia[i*8:i*8 + 8]
- for k in range(0, 8):
- inputs.append(LFSR[indexInputs[k] - 1])
- outputs.append(polynomy8[i](*inputs))
- del inputs[:]
- result = xor(outputs[0], outputs[1])
- for i in range(2, 32):
- result = xor(result, outputs[i])
- return result
- #-------------------Called one time-------------------------------------------------------
- generatePoly(2, 256, 256, '/home/pro/Plocha/Diplomovka/polynomials2.sobj')
- generatePoly(8, 8, 32, '/home/pro/Plocha/Diplomovka/polynomials8.sobj')
- #------------------------------------------------------------------------------------------------------------------
- #===========================================Main==================================================================
- polynomy2 = load('/home/pro/Plocha/Diplomovka/polynomials2.sobj')
- polynomy8 = load('/home/pro/Plocha/Diplomovka/polynomials8.sobj')
- feedback = getSpatnaVazba()
- permutation = genPermutation()
- IV = random_vector(GF(2), 128)
- key = random_vector(GF(2), 128)
- #-------------------This takes too much time-------------------------------------------------------------------
- result = BlackBox(IV, key, polynomy2, polynomy8, feedback, permutation)
- #Here the function is called once but I need to call it more then 2^15 times
- #----------------------------------------------------------------------------------------------------------------
- #=============================================================================================================
RAW Paste Data