Created
August 21, 2021 14:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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