Skip to content

Instantly share code, notes, and snippets.

@rgov
Created March 31, 2018 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgov/e9d6145731dc2839a8b110d0551ff0ca to your computer and use it in GitHub Desktop.
Save rgov/e9d6145731dc2839a8b110d0551ff0ca to your computer and use it in GitHub Desktop.
import itertools, struct, sys
inout = '''
2bf256b8 02df1f6d04e4
5bfe5eb8 32eb276d54f8
ab315fb8 821e286d84a5
cbe057b8 a2cd206de4da
db391710 b226e0c5d4bd
dbb457b8 b2a1206dd426
a506b5a2 7cf37e578ef0
b7c02639 8eadefeef83a
3be863b8 12d52c6d74d2
cba564b8 a2922d6de411
9669f992 6d56c2479f6d
27d80239 ffc6ccef0ac0
69ef7b65 41dd451b48e9
659fb2a2 3d8d7c584c19
a7912739 7f7ff1ef8a07
57ba0239 2fa8ccef5a3e
c7640839 9e51d1eee856
4389a546 1a766efb6c0d
e7a50139 be92caeec811
850fb2a2 5cfc7b57ae8b
1ed7c505 f5c48eba17c3
'''[1:-1]
input, output = [], []
for line in inout.split('\n'):
a, _, b = line.partition(' ')
input.append(bytes.fromhex(a))
output.append(bytes.fromhex(b))
operations = {
'-': lambda a, b: a - b,
'+': lambda a, b: a + b,
'*': lambda a, b: a * b,
'/': lambda a, b: a // b,
'%': lambda a, b: a % b,
'<<': lambda a, b: a << b,
'>>': lambda a, b: a >> b,
'&': lambda a, b: a & b,
'|': lambda a, b: a | b,
'^': lambda a, b: a ^ b,
}
b = 5
for arg1 in itertools.chain(['a', 'b', 'c', 'd'], range(256)):
for arg2 in itertools.chain(['a', 'b', 'c', 'd'], range(256)):
for arg3 in itertools.chain(['a', 'b', 'c', 'd'], range(256)):
for op1, op1fn in operations.items():
for op2, op2fn in operations.items():
count = 0
for inp, outp in zip(input, output):
a1 = { 'a': inp[0], 'b': inp[1], 'c': inp[2], 'd': inp[3] }.get(arg1, arg1)
a2 = { 'a': inp[0], 'b': inp[1], 'c': inp[2], 'd': inp[3] }.get(arg2, arg2)
a3 = { 'a': inp[0], 'b': inp[1], 'c': inp[2], 'd': inp[3] }.get(arg3, arg3)
try:
if op2fn(op1fn(a1, a2) & 0xFF, a3) & 0xFF == outp[b]:
count += 1
except:
continue
if count >= 16:
print(count, 'output[%i] = ((%s %s %s) %s %s)' % (b, str(arg1), op1, str(arg2), op2, str(arg3)))
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment