This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssprg.py
74 lines (60 loc) · 1.71 KB
/
ssprg.py
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
from secrets import randbelow
import datetime
import json
def dump_json(path, var):
with open(path, 'w') as fh:
json.dump(var, fh)
def load_json(path):
with open(path, 'r') as fh:
return json.load(fh)
def listToInt(_list):
return int("".join(map(lambda x: str(x), _list)) , 2)
def intToList(_int, pad = 0):
_list = list(map(lambda x: int(x), "{0:b}".format(_int)))
if (pad == 0 or len(_list) >= pad):
return _list
_list = [0] * (pad - len(_list) ) +_list
return _list
def seed_prg(seed):
n = 256
q = 2**(2*256)
a = load_json('a_seed')
result = 0
for i in range(n):
result += (a[i]*seed[i]) % q
result %= q
return intToList(result, pad = n)[:n]
def my_prg(seed, bytes):
n = 256
q = 2**(2*256)
a = load_json('a_prg')
def prg(seed, n):
result = 0
for i in range(n):
result += (a[i]*seed[i]) % q
result %= q
return result
result = []
si = seed
while (bytes>0):
prgOutput = intToList(prg(si, n), pad = n)
result += prgOutput
si = seed_prg(si)
bytes -= (len(prgOutput))/8
result += si
return result
# create seed:
def new_seed(_len):
return [randbelow(2) for i in range(_len)]
# create vector a:
def create_a(q,n):
return [randbelow(q) for a_ix in range(n)]
def test(debug = False):
seed = load_json('test_seed')
result = my_prg(seed, 2048), len(my_prg(seed, 2048))
print(result)
if (debug):
data = {"date": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"result": result}
dump_json('debug', data)
test(True)