-

@ aa643d5c:f3afe1cb
2025-02-24 18:28:06
first implementation of finding statistically better than random guess input values from output values. this is currently just a template and not a working implementation of such function.
this code creates two plots.
//
from collections import defaultdict
import sys
import hashlib
import matplotlib.pyplot as plt
def sha256_binary(input_bytes):
sha256_hash = hashlib.sha256()
sha256_hash.update(input_bytes)
digest = sha256_hash.digest()
return int.from_bytes(digest, byteorder='big')
mappings = {}
for i in range(1000000):
inputbytes = i.to_bytes((i.bit_length() + 7) // 8, byteorder='big')
mappings[sha256_binary(inputbytes)] = int.from_bytes(inputbytes, byteorder='big')
#for i in range(10000):
# mappings[i+1] = i
#print("mappings", mappings)
def hamming_distance(b1, b2):
x = b1 ^ b2
return bin(x).count('1')
def estimate_input(z):
best_candidate = None
min_distance = float('inf')
for key, value in mappings.items():
distance = hamming_distance(key, z)
if distance < min_distance:
min_distance = distance
best_candidate = value
return best_candidate
inputnum = 1000002
input_bytes = inputnum.to_bytes((inputnum.bit_length() + 7) // 8, byteorder='big')
#input_bytes = "aaaab345345435".encode()
z = sha256_binary(input_bytes)
i = estimate_input(z)
outputbinstr = bin(z)[2:].zfill(256)
ibytes = i.to_bytes((i.bit_length() + 7) // 8, byteorder='big')
inputbinstr = bin(inputnum)[2:].zfill(256)
estimatedbinstr = bin(i)[2:].zfill(256)
print(f"The estimated input for D({outputbinstr}) is bin: {estimatedbinstr}, int: {i}, bytes: {ibytes.decode('utf-8', errors='ignore')}")
print("inputvals", list(inputbinstr))
inputvalues = [int(v) for v in list(inputbinstr)]
estimatedinputvalues = [int(v) for v in list(outputbinstr)]
for idx, value in enumerate(inputvalues):
plt.plot(idx, value, marker='o', markersize=0.5)
plt.title(f'input values')
plt.xlabel('bit index')
plt.ylabel('Probability')
plt.savefig(f'plot-in.png', dpi=800)
plt.close()
#plt.plot(estimatedinputvalues, marker='o', linestyle="None", markersize=0.5)
for idx, value in enumerate(estimatedinputvalues):
plt.plot(idx, value, marker='o', markersize=0.5)
plt.title(f'estimated input values')
plt.xlabel('bit index')
plt.ylabel('Probability')
plt.savefig(f'plot-in-estimated.png', dpi=800)
plt.close()