Advertisement
DigitalMag

Improved search of const combinations for the Diffie_Hellman

Jul 5th, 2020
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1.  
  2. import random
  3. import itertools
  4. import functools
  5. import sys
  6. from collections import OrderedDict
  7.  
  8.  
  9. import time
  10. from numba import jit
  11.  
  12. isprime = lambda number: all(number % i for i in range(2, number))
  13.  
  14. # @jit(nopython=True)
  15. def is_prime(number):
  16.     n = number
  17.     counter = 0
  18.     for i in range(1, n + 1):
  19.         if n % i == 0:
  20.             counter += 1
  21.     return True if counter == 2 else False
  22.  
  23. def get_primes(start, edge):
  24.     primes = []
  25.  
  26.     t = time.clock()
  27.     for i in range(start, edge):
  28.         if i % 1000 == 0: print(i)
  29.         if is_prime(i): primes.append(i)
  30.  
  31.     print ("primes: "+len(primes).__str__(), ' for ', (time.clock() - t))
  32.  
  33.     return primes
  34.  
  35.  
  36.  
  37.  
  38. statement = jit(nopython=True)(lambda x, n, rest: n**x % rest)
  39. post_statement = jit(nopython=True)(lambda a, b, rest: a**b % rest)
  40.  
  41.  
  42.  
  43. class Private(object):
  44.  
  45.     def __init__(self, n, rest):
  46.         self.rest = rest
  47.         self.n = n
  48.  
  49. ##    statement = lambda self, x: statement(x, self.n, self.rest)
  50. ##    post_statement = lambda self, a, b: statement(a, b, self.rest)
  51.  
  52.     statement = (lambda self, x: (self.n**x) % self.rest)
  53.     post_statement = (lambda self, a, b: (a**b) % self.rest)
  54.  
  55.  
  56. RAND = 99
  57.  
  58. # by new: 99/(378, 83) == 4% (79, 83) 4.5%
  59.  
  60. def cry_test(secure):
  61.  
  62.     skey = random.randint(2 ,RAND)
  63.     open_skey = secure.statement(skey)
  64.  
  65.     # client:
  66.     rkey = random.randint(2, RAND)
  67.     r_open_key = secure.statement(rkey)
  68.  
  69.     psw = secure.post_statement(open_skey, rkey)
  70.  
  71.     # server
  72.     spsw = secure.post_statement(r_open_key, skey)
  73.     # print psw
  74.  
  75.     return psw, skey, rkey
  76.  
  77.  
  78. def unit_test(i,j):
  79.  
  80.     secure = Private(i,j)
  81.  
  82.     n = 1000
  83.     l = []; sk = []; rk = []
  84.     for k in range(n):
  85.         a,b,c = cry_test(secure)
  86.         l.append(a)
  87.  
  88.     return [int(l.count(c))/n*100 for c in range(j)]
  89.  
  90.  
  91.  
  92. def start_test():
  93.     dct = {}
  94.  
  95.     t = time.clock()
  96.  
  97.     n = 100
  98.     rest = 100
  99.     primes = get_primes(62, rest)
  100.  
  101.     for j in primes:                                                               # range(1,n):
  102.         for i in range(n):
  103.             dct[(i,j)] = unit_test(i,j)
  104.             # print ( max(dct[(i,j)]).__str__() + '% - ' + (i,j).__str__())
  105.         print(j)
  106.  
  107.     print(time.clock() - t)
  108.  
  109.  
  110.  
  111.     if sys.version_info.major == 2:
  112.         rez = {k:max(v) for k, v in dct.iteritems()}
  113.         items = list(rez.iteritems())
  114.     else:
  115.         rez = {k:max(v) for k, v in dct.items()}
  116.         items = list(rez.items())
  117.     items.sort(key=lambda i: i[1])
  118.  
  119.  
  120.     od = OrderedDict()
  121.     cnt = 0
  122.     for item in items:
  123.  
  124.         if item[1] > 5:
  125.             cnt+=1
  126.             continue
  127.  
  128.         key = str(item[1]) + '%'
  129.         if key not in od:
  130.             od[key] = [item[0]]
  131.         else:
  132.             od[key].append(item[0])
  133.         # od[str(item[1]) + '%'] = item[0]
  134.  
  135.     print (od)
  136.     print(len(primes)*n - cnt, ' из ', len(primes)*n)
  137.  
  138.     return od
  139.     # print time.clock() - t
  140.  
  141.  
  142.  
  143.     # print [sk.count(c) for c in range(67)]
  144.     # print [rk.count(c) for c in range(67)]
  145.  
  146.  
  147. md = []
  148. for i in range(3):
  149.     print('step ' + i.__str__())
  150.     md.append(start_test())
  151.     time.sleep(1)
  152.  
  153. avr = {}
  154. for od in md:
  155.     for k,vals in od.items():
  156.         # print ('...........')
  157.  
  158.         for val in vals:
  159.             if val in avr:
  160.                 # print( '-------------')
  161.                 avr[val].append(k)
  162.             else:
  163.                 # print('+++++++++++++')
  164.                 avr[val] = [k]
  165.  
  166. aod = OrderedDict()
  167. items = list(avr.items())
  168. items.sort(key=lambda i: [float(j.replace('%','')) for j in i[1]])
  169. for k,v in items:
  170.     if len(v)>1: aod[k]=v
  171.  
  172.  
  173. print (aod)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement