Advertisement
DigitalMag

checking apportionment for big numbers

Jul 5th, 2020
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. from collections import OrderedDict
  2. import random
  3. import time
  4. # import secure
  5. # from secure import greet
  6. from numba import jit
  7. import numpy as np
  8. import numpy
  9. import os.path
  10. import pickle
  11.  
  12. # print (greet("world"))
  13.  
  14. isprime = lambda number: all(number % i for i in range(2, number))
  15.  
  16. @jit(nopython=True)
  17. def issimple(n):# получаем число
  18.     d=2 # олучить остаток от деления на 2, значит - простое
  19.     while n % d!=0:# пока число mod 2<>0 переберем делители, так как , простое, делится неа 2 и на себя,
  20.         d+=1# перебрасываем все, кроме 2
  21.     return d==n # возвращаем результат
  22.  
  23.  
  24. ##@jit(nopython=True)
  25. ##def istatement(n,x,rest):
  26. ##    return n**x % rest
  27.  
  28. class Secure(object):
  29.  
  30.     def __init__(self, n, rest):
  31.         self.rest = rest
  32.         self.n = n
  33.  
  34.     # wrong reslts:
  35.     @staticmethod
  36.     @jit(nopython=True)
  37.     def istatement(n,x,rest):
  38.         return n**x % rest
  39.  
  40.  
  41.     # statement = lambda self, x: secure.istatement(self.n, x, self.rest)                # lambda self, x: self.n**x % self.rest
  42.     statement = lambda self, x: (self.n**x) % self.rest
  43.     post_statement = lambda self, a, b: (a**b) % self.rest
  44.  
  45.  
  46. R_LIMIT = 1864 # 9586 # 5864
  47.  
  48. # # 199 == (37, 979999998077) => 1000000 итераций => 50сек = 50_000 мс
  49.  
  50. def cry_test(secure):
  51.  
  52.     skey = random.randint(2 ,R_LIMIT)
  53.     open_skey = secure.statement(skey)
  54.  
  55.     # client:
  56.     rkey = random.randint(2, R_LIMIT)
  57.     r_open_key = secure.statement(rkey)
  58.  
  59.     psw = secure.post_statement(open_skey, rkey)
  60.  
  61.     # server
  62.     spsw = secure.post_statement(r_open_key, skey)
  63.     # print psw
  64.  
  65.     if psw != spsw: raise "not asset"
  66.  
  67.     return psw, skey, rkey
  68.  
  69.  
  70. def Cry_Test(clst,start,end):
  71. # def Cry_test(clst):
  72.     if type(clst) is list: lst = clst
  73.     else:
  74.         lst = clst.get()
  75.     for i in range(start,end):
  76.         if i%100 == 0: print (i)
  77.         a,b,c = cry_test(secure)
  78.         lst.append(a)
  79.  
  80. def creator(q,l):
  81.     q.put(l)
  82.  
  83. def test():
  84.  
  85.     # params = (17, 979999998077)       # 764
  86.     params = (17, 979998077)
  87.  
  88. ##    if (issimple(params[1]) == False):
  89. ##        print('no prime')
  90. ##        return
  91.     secure = Secure(*params)
  92.  
  93.  
  94.     """
  95.    w scopes:
  96.    9586 # 5864 /  (17, 979999998077), (5, 979999998077)==0.03%
  97.    """
  98.  
  99.     """
  100.  
  101.    w/o scopes:
  102.    '7%', (793, 83)
  103.    '8%', (118, 97)
  104.  
  105.    Big Simple digits
  106.    [97999807, 97999813, 97999823, 97999831, 97999859, 97999861, 97999877, 97999879, 97999919, 97999927, 97999943, 97999949]
  107.    [979999817,979999849,979999883,979999903,979999907,979999913,979999957,979999973,979999981]
  108.  
  109.  
  110.    # остаток до 100: (19, 89) - 7%, (145, 89) - 8%, ('9%', (14, 89)
  111.    """
  112.  
  113.     from multiprocessing import Process, Queue
  114.  
  115.     t = time.clock()
  116.     n = 1000000
  117.  
  118.  
  119.     l = []; sk = []; rk = []
  120.     def Cry_test(clst):
  121.         if type(clst) is list: lst = clst
  122.         else:
  123.             lst = clst.get()
  124.         for i in range(n):
  125.             if i%1000 == 0: print (i)
  126.             a,b,c = cry_test(secure)
  127.             lst.append(a)
  128.     ##        sk.append(b)
  129.     ##        rk.append(c)
  130.  
  131.     Cry_test(l)
  132.  
  133.     print(time.clock() - t)
  134.  
  135.     na = numpy.array(l)
  136.  
  137.     unique, counts = numpy.unique(na, return_counts=True)
  138.     print(l[:10])
  139.     print('productive ', len(counts)/n*100,'%')
  140.     print(len([i for i in counts if i>0]))
  141.     print('efective: ', (n-max(counts))/n*100, ' %')
  142.  
  143.     data_rez = ({k:v for k,v in dict(zip(unique, counts)).items() if v>15})
  144.     print(len(data_rez.keys()))
  145.     print(data_rez)
  146.  
  147.     fl_name = 'last.data'
  148.     if os.path.exists(fl_name):
  149.         with open(fl_name, 'rb') as fl:
  150.             data_last = pickle.load(fl)
  151.             print('last data: ', data_last)
  152.             keys = set(data_rez.keys()) & set(data_last.keys())
  153.             rdata = { (data_last[k]+data_rez[k]):k for k in keys}
  154.             od = OrderedDict()
  155.             for k in sorted(rdata.keys()):
  156.                 od[k]=rdata[k]
  157.             print(od)
  158.  
  159.     with open(fl_name, 'wb') as fl:
  160.         pickle.dump(data_rez, fl)
  161.  
  162.  
  163.  
  164. ##    allotment = ([l.count(c) for c in range(n)]) #
  165. ##
  166. ##    print(max(allotment))
  167.  
  168.     # print(l.count(1)) # params[1]
  169.     # print [sk.count(c) for c in range(params[1])]
  170.     # print [rk.count(c) for c in range(params[1])]
  171.  
  172.     print (time.clock() - t)
  173.  
  174. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement