Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
VinIVaderr
on 25/08/2024, 03:18:26 UTC
This code will generate a random string.
Then it will adjust each column range(1, 16)
If column 1 is 0x2 then the next string is 0x3, 0x4, 0x5 - 0x1
It will do this for each column.


Code:
from bitcoin import privtopub, pubtoaddr
import random
import string
import multiprocessing
import time
import os
import secrets
def create_random_slider_field(columns):
    return [random.choice(string.hexdigits.lower()) for _ in range(columns)]

def adjust_slider(slider, column, direction):
    hex_chars = string.hexdigits.lower()
    current_index = hex_chars.index(slider[column])
    new_index = (current_index + direction) % len(hex_chars)
    slider[column] = hex_chars[new_index]
    return slider

def check_address(private_key, target_address):
    try:
        address = pubtoaddr(privtopub(private_key))
        return address == target_address
    except Exception as e:
        print(f"Error generating address: {e}")
        return False

def search_process(target_address, process_attempts, result_queue):
    byte_values = list(range(256))
    random_bytes = [secrets.choice(byte_values) for _ in range(8)]
    random.seed(int.from_bytes(random_bytes))

    slider_columns = 19 
    attempts = 0
    hex_chars = string.hexdigits.lower()

    while attempts < process_attempts:
        slider = create_random_slider_field(slider_columns)
       
        for column in range(slider_columns):
            original_value = slider[column]
            original_index = hex_chars.index(original_value)
           
            # Check original value
            private_key = '0' * 46 + '1' + ''.join(slider)     # Updated prefix, adjust leading zeros.
            if check_address(private_key, target_address):
                result_queue.put((private_key, attempts))
                return
            attempts += 1

            # Optimized range checking
            for i in range(1, 16):
                # Check increasing values
                new_index = (original_index + i) % 16
                slider[column] = hex_chars[new_index]
                private_key = '0' * 46 + '1' + ''.join(slider)   # Updated prefix, adjust leading zeros.
                if check_address(private_key, target_address):
                    result_queue.put((private_key, attempts))
                    return
                attempts += 1
 
   # Reset to original value before moving to next column
            slider[column] = original_value

        if attempts % 1000 == 0:
            print(f"Process {multiprocessing.current_process().name} Attempts: {attempts}, Current slider: {''.join(slider)}")

    result_queue.put((None, attempts))

def multiprocessing_search(target_address, max_attempts=1000000, num_processes=4):   #max_attempts / 4 cores
    processes = []
    result_queue = multiprocessing.Queue()
    attempts_per_process = max_attempts // num_processes

    start_time = time.time()

    for i in range(num_processes):
        p = multiprocessing.Process(
            target=search_process,
            args=(target_address, attempts_per_process, result_queue)
        )
        processes.append(p)
        p.start()

    total_attempts = 0
    for _ in range(num_processes):
        result, attempts = result_queue.get()
        total_attempts += attempts
        if result:
            # Stop all processes
            for p in processes:
                p.terminate()
            return result, total_attempts

    # Wait for all processes to complete
    for p in processes:
        p.join()

    end_time = time.time()
    print(f"Total time: {end_time - start_time:.2f} seconds")
    return None, total_attempts

# Main program
if __name__ == "__main__":
    target_address = "19vkiEajfhuZ8bs8Zu2jgmC6oqZbWqhxhG"    #  puzzle address
    print(f"Target Bitcoin Address: {target_address}")

    result, attempts = multiprocessing_search(target_address)

    if result:
        f = open("keys.txt", "a")
        f.write(result + '\n')
        f.close()
        print(f"Matching private key found: {result}")
        print(f"Total attempts: {attempts}")
        f = open("keys.txt", "a")
        f.write(result + '\n')
        f.close()
    else:
        print(f"No match found after {attempts} attempts")