import hashlib
import random
If I prove to you that I can find some arbitrary range distribution where the sequential method is better, with your exact code as it is, will you leave the forum forever? If not, I will. How's that for a bet?
# Configuration
TOTAL_SIZE = 100_000
RANGE_SIZE = 5_000
PREFIX_LENGTH = 3
SIMULATIONS = 200
SECP256K1_ORDER = int("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
print(f"""
=== Configuration ===
Total numbers: {TOTAL_SIZE:,}
Block size: {RANGE_SIZE:,}
Prefix: {PREFIX_LENGTH} characters (16^{PREFIX_LENGTH} combinations)
Simulations: {SIMULATIONS}
secp256k1 Order: {SECP256K1_ORDER}
""")
def generate_h160(data):
h = hashlib.new('ripemd160', str(data).encode('utf-8'))
return h.hexdigest()
def shuffled_range(n):
arr = list(range(n + 1))
random.shuffle(arr)
return arr
def sequential_search(dataset, block, target_hash, order):
checks = 0
for idx in order:
start = idx * block
end = start + block
for i in range(start, end):
checks += 1
if generate_h160(dataset[i]) == target_hash:
return {"checks": checks, "found": True}
return {"checks": checks, "found": False}
def precise_search(dataset, block, prefix, target_hash, order):
prefix_hash = target_hash[:prefix]
checks = 0
ranges = []
for idx in order:
start = idx * block
end = start + block
found_prefix = False
for i in range(start, end):
checks += 1
h = generate_h160(dataset[i])
if h == target_hash:
return {"checks": checks, "found": True}
if not found_prefix and h.startswith(prefix_hash):
found_prefix = True
ranges.append({"start": i + 1, "end": end})
break
for r in ranges:
for i in range(r["end"] - 1, r["start"] - 1, -1):
checks += 1
if generate_h160(dataset[i]) == target_hash:
return {"checks": checks, "found": True}
return {"checks": checks, "found": False}
def compare_methods():
results = {
"sequential": {"wins": 0, "total_checks": 0},
"precise": {"wins": 0, "total_checks": 0},
"ties": 0
}
for i in range(SIMULATIONS):
max_range = SECP256K1_ORDER - TOTAL_SIZE - 1
random_offset = random.randrange(max_range)
R = 1 + random_offset
dataset = [R + i for i in range(TOTAL_SIZE)]
target_num = random.choice(dataset)
target_hash = generate_h160(target_num)
blocks = TOTAL_SIZE // RANGE_SIZE
order = shuffled_range(blocks - 1)
print(f"\nSimulation {i + 1}:")
print(f"\nRange= {hex(R)}:{hex(R+TOTAL_SIZE)}")
print(f"\nTarget= {target_hash}")
seq_result = sequential_search(dataset, RANGE_SIZE, target_hash, order)
pre_result = precise_search(dataset, RANGE_SIZE, PREFIX_LENGTH, target_hash, order)
# Suma de checks globales
results["sequential"]["total_checks"] += seq_result["checks"]
results["precise"]["total_checks"] += pre_result["checks"]
if seq_result["checks"] < pre_result["checks"]:
results["sequential"]["wins"] += 1
elif seq_result["checks"] > pre_result["checks"]:
results["precise"]["wins"] += 1
else:
results["ties"] += 1
print(f"Checks: Sequential = {seq_result['checks']} | Prefix = {pre_result['checks']}")
# Calcular avg success rate
avg_success_rate_sequential = (results["sequential"]["total_checks"] / results["sequential"]["wins"]
if results["sequential"]["wins"] > 0 else float('inf'))
avg_success_rate_precise = (results["precise"]["total_checks"] / results["precise"]["wins"]
if results["precise"]["wins"] > 0 else float('inf'))
print(f"""
=== FINAL RESULTS ===
Wins:
Sequential: {results['sequential']['wins']}
Prefix: {results['precise']['wins']}
Ties: {results['ties']}
Total Checks:
Sequential: {results['sequential']['total_checks']}
Prefix: {results['precise']['total_checks']}
Average Success Rates:
Total Avg / Wins
Sequential(1 victory for each): {avg_success_rate_sequential:.2f}
Prefix(1 1 victory for each): {avg_success_rate_precise:.2f}
""")
if __name__ == '__main__':
compare_methods()