Skip to content

Instantly share code, notes, and snippets.

@superfashi
Created August 21, 2021 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superfashi/9d1ccea3513b7f52951d7b88da052855 to your computer and use it in GitHub Desktop.
Save superfashi/9d1ccea3513b7f52951d7b88da052855 to your computer and use it in GitHub Desktop.
Adding values consists of limited bytes to a target number using modular arithmetic
import itertools
from collections import defaultdict
target_byte_size = 4
target_size = 1 << (target_byte_size * 8)
usable_bytes = (0x05, 0x50, 0xc3)
reverse_mapping = defaultdict(list)
def reachable(input_byte):
reached = set()
for i in range(0x100 + 1):
t = (i * input_byte) & 0xFF
if t in reached:
return range(i)
reached.add(t)
def generate_reverse():
for possible in itertools.product(*(reachable(b) for b in usable_bytes)):
reverse_mapping[sum(a * b for a, b in zip(usable_bytes, possible)) & 0xFF].append(possible)
for k in reverse_mapping:
reverse_mapping[k] = sorted(reverse_mapping[k], key=sum)
generate_reverse()
def search(target, path=[], total=None, depth=0):
if depth == target_byte_size:
numbers = [0] * total
for i, p in enumerate(path):
cur = 0
for j, c in enumerate(p):
for _ in range(c):
numbers[cur] |= (usable_bytes[j] << (i * 8))
cur += 1
return numbers
for possible in reverse_mapping[target & 0xFF]:
if total is not None and total != sum(possible):
continue
num = sum(a * b for a, b in zip(usable_bytes, possible))
ans = search(((target - num) % target_size) >> 8, path + [possible], sum(possible), depth + 1)
if ans:
return ans
print(*map(hex, search(0xdeadbeef)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment