-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (127 loc) · 4.46 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import cv2
import hashlib
import time
import random
import textwrap
import numpy as np
def simple_capture(seeds):
cap = cv2.VideoCapture(0)
print("Starting entropy collector...")
while cap.isOpened():
ret, frame = cap.read()
if ret:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
readable_hash = hashlib.sha256(image).hexdigest()
print(readable_hash)
seeds.append(readable_hash)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Stopping entropy collection.")
break
else:
break
cap.release()
cv2.destroyAllWindows()
return seeds
def max_brightness(frame, value=255):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value
final_hsv = cv2.merge((h, s, v))
frame = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return frame
def capture(seeds):
cap = cv2.VideoCapture(0)
last_frame = None
start = time.time()
while cap.isOpened():
ret, curr_frame = cap.read()
if not ret:
break
if last_frame is not None:
frame = curr_frame
# 1. diff two frames
frame = cv2.absdiff(last_frame, curr_frame)
# 2. maximize the luminance
frame = max_brightness(frame)
# 3. convert to black-and-white
frame = cv2.threshold(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 127,
255, cv2.THRESH_BINARY)[1]
shake = hashlib.shake_128()
shake.update(bytes(frame))
data = textwrap.wrap(str(int(shake.hexdigest(8192), 32)), 8)
for i in data:
seeds.append(int(i))
# fifo.write(i+"\n")
# fifo.flush()
cv2.imshow('webcam noise', frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
last_frame = curr_frame
end = time.time()
print(f"Runtime: {end - start}\nSeeds: {len(seeds)}\nSeeds/s: {len(seeds)/(end-start)}")
cap.release()
cv2.destroyAllWindows()
return seeds
def coinflip(seeds):
sides = ["Heads", "Tails"]
random.seed(seeds[0])
seeds.pop(0)
result = random.choice(sides)
return result, seeds
def roll(seeds, roll):
result = ""
for i in range(0, roll[0]):
# random.seed(seeds[0])
number = 1 + seeds[0] % roll[1]
seeds.pop(0)
# number = random.randint(0, roll[1])
result += f", {number}"
return result[2:], seeds
def main():
seeds = []
while True:
command = input("> ")
if command == "exit":
break
elif command == "help":
print("List of available commands:\nhelp, entropy, collect, coinflip, exit.")
elif command == "entropy":
print(f"Available entropy: {len(seeds)}")
elif command == "collect":
seeds = capture(seeds)
print(f"Entropy collected successfully. Available entropy: {len(seeds)}")
elif command == "coinflip":
if len(seeds) == 0:
print("At least one seed is required for a coin flip.")
else:
result, seeds = coinflip(seeds)
print(result)
elif command[:4] == "roll":
dice = command[5:].split("d")
dice[0] = int(dice[0])
dice[1] = int(dice[1])
required = dice[0]
if required > len(seeds):
print(f"Required entropy: {required}.\nAvailable entropy: {len(seeds)}.")
else:
result, seeds = roll(seeds, dice)
print(result)
elif command[:11] == "montecarlo ":
from montecarlo import monte_carlo
seeds = monte_carlo(seeds, command[11:])
elif command[:14] == "montecarlotrig":
from montecarlo import monte_carlo_trig
seeds = monte_carlo_trig(seeds, command[15:])
elif command == "montecarlopi":
from montecarlo import monte_carlo_pi
result, seeds = monte_carlo_pi(seeds)
print(f"Pi estimate: {result}")
elif command[:8] == "simulate":
amount, rtype = command[9:].split(" ")
from simulation import simulate
seeds = simulate(seeds, int(amount), int(rtype))
if __name__ == "__main__":
main()