-
Notifications
You must be signed in to change notification settings - Fork 56
/
floatsha256.py
54 lines (49 loc) · 1.52 KB
/
floatsha256.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
import os
from hashlib import sha256
LIMIT = 1000000
def read_program():
print('Your program:')
program = []
while True:
line = input().strip()
if line == 'EOF':
break
if len(program) >= LIMIT:
raise ValueError('Program too long')
nums = line.split()
if len(nums) == 1:
program.append(float(nums[0]))
elif len(nums) == 2:
program.append((int(nums[0]), int(nums[1])))
else:
raise ValueError('Invalid input')
return program
def run_program(program, data, output_size):
mem = [float(b) for b in data]
for line in program:
if isinstance(line, float):
mem.append(line)
else:
index0, index1 = line
assert index0 in range(len(mem)), 'Index out of range'
assert index1 in range(len(mem)), 'Index out of range'
mem.append(mem[index0] - mem[index1])
assert len(mem) >= output_size
output = []
for x in mem[-output_size:]:
b = int(x)
assert float(b) == x, 'Output is not an integer'
assert b in range(256), 'Output not in range'
output.append(b)
return bytes(output)
def main():
prog = read_program()
for i in range(10):
print(f'Testing {i}')
data = os.urandom(32)
if sha256(data).digest() != run_program(prog, data, 32):
print(f'Wrong answer at input {data.hex()}')
exit(-1)
print(open('flag').read())
if __name__ == "__main__":
main()