matej_prochazka

BlackBox

Nov 25th, 2015
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def xor(a, b):
  2.     return eval("%s^%s" % (a, b))
  3.  
  4. def generatePoly(deg, numberOfVariables, numberOfPoly, file):
  5.     variables=[]
  6.     for i in range(1,numberOfVariables+1):
  7.         variables.append('x'+str(i))
  8.     R = BooleanPolynomialRing(names=variables)
  9.     R.inject_variables()
  10.    
  11.     poly = []
  12.     for _ in range(0,numberOfPoly):
  13.         poly = R.random_element(degree=deg,terms=+infinity)
  14.         poly.append(poly)
  15.     save(poly, file)
  16.  
  17. def loadPoly(file):
  18.     return load(file)
  19.  
  20. def getFeedback():
  21.     feedback = []
  22.     tmp = []
  23.     tmp = random_vector(GF(2), 256)
  24.     for i in range(0, 256):
  25.         if tmp[i] == 1:
  26.             feedback.append(i)
  27.     return feedback
  28.  
  29. def genPermutation():
  30.     return Permutations(256).random_element()
  31.  
  32. def BlackBox(IV, key, polynomy2, polynomy8, feedback, permutacia):
  33.     LFSR = []
  34.     ivAkey = vector(list(IV) + list(key))
  35.     for i in range(0, 256):
  36.         #-----------------This is tricky part-------------------------------------------
  37.         LFSR.append(polynomy2[i](*ivAkey))
  38.         #---------------------------------------------------------------------------------------------------
  39.  
  40.     for _ in range(0,2000):
  41.         #count feedback
  42.         bit = 0
  43.         for i in feedback:
  44.             bit += LFSR[i]
  45.         #shift
  46.         for j in range(0,255):
  47.             LFSR[j] = LFSR[j+1]    
  48.         #assign feedback
  49.         LFSR[255] = bit
  50.  
  51.  
  52.     indexInputs = []
  53.     inputs = []
  54.     outputs = []
  55.     for i in range(0, 32):
  56.         indexInputs = permutacia[i*8:i*8 + 8]
  57.         for k in range(0, 8):
  58.             inputs.append(LFSR[indexInputs[k] - 1])
  59.         outputs.append(polynomy8[i](*inputs))        
  60.         del inputs[:]
  61.  
  62.     result = xor(outputs[0], outputs[1])
  63.     for i in range(2, 32):
  64.         result = xor(result, outputs[i])
  65.  
  66.     return result
  67.  
  68. #-------------------Called one time-------------------------------------------------------
  69. generatePoly(2, 256, 256, '/home/pro/Plocha/Diplomovka/polynomials2.sobj')
  70. generatePoly(8, 8, 32, '/home/pro/Plocha/Diplomovka/polynomials8.sobj')
  71. #------------------------------------------------------------------------------------------------------------------
  72.  
  73. #===========================================Main==================================================================
  74. polynomy2 = load('/home/pro/Plocha/Diplomovka/polynomials2.sobj')
  75.  
  76. polynomy8 = load('/home/pro/Plocha/Diplomovka/polynomials8.sobj')
  77.  
  78. feedback = getSpatnaVazba()
  79.  
  80. permutation = genPermutation()
  81.  
  82. IV = random_vector(GF(2), 128)
  83. key = random_vector(GF(2), 128)
  84.  
  85. #-------------------This takes too much time-------------------------------------------------------------------
  86. result = BlackBox(IV, key, polynomy2, polynomy8, feedback, permutation)
  87. #Here the function is called once but I need to call it more then 2^15 times
  88. #----------------------------------------------------------------------------------------------------------------
  89. #=============================================================================================================
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×