Post
Topic
Board Bitcoin Discussion
Re: Bitcoin puzzle transaction ~32 BTC prize to who solves it
by
mitkopasa
on 09/04/2025, 19:20:55 UTC
Could you please convert the following Python code to C++ using the libraries in Mutagen? In this code, I am creating a 69-bit binary, but 32 to 35 of them are 1's and 1's can come in a row at most 6 times.  This python speed is very low. I think i can search faster with sha256_avx2 and ripemd160_avx2. However, I don't know C++ and I couldn't figure out how to integrate the codes in the mutagen.

Code:
import random
import numpy as np
import time
import os
import multiprocessing
from multiprocessing import Pool
import secp256k1 as ice
from random import sample


r = 0
cores=1

def generate_random_bits(n, k):
    while True:
        # Initially all digits are 0
        bits = ['0'] * n
        # Generate random 1
        positions = random.sample(range(n), k)
        for pos in positions:
            bits[pos] = '1'
       
        # How many 1's can in a row?
        if is_valid_combination(bits,max_consecutive_ones):
            return ''.join(bits)

def is_valid_combination(bits,max_consecutive_ones):
    count = 0
    for bit in bits:
        if bit == '1':
            count += 1
            if count > max_consecutive_ones:  # limit for consecutive ones
                return False
        else:
            count = 0
    return True

def priv_key(n, k):
    while True:
        random_bits = generate_random_bits(n, k)
        decimal_representation = int(random_bits, 2)
        if 2**(n-1) < decimal_representation < 2**n:
           return decimal_representation

# Parameters
n = 69  # Puzzle number
max_consecutive_ones = 6  # limit for consecutive ones

def seek(r):
    global num_threads
    LOG_EVERY_N = 100000
    start_time = time.time()
    i = 0
    print("Core " + str(r) +":  Searching Private Key..")
    db = '61eb8a50c86b0584bb727dd65bed8d2400d6d5aa'
    while True:
        i=i+1
        k = random.randrange(32, 35) # How many 1's are there?
        private_key = priv_key(n,k)
        #print(private_key)
#public_key = privtopub(private_key)       
        addr = ice.privatekey_to_h160(0, True, private_key).hex()
        time_diff = time.time() - start_time
        if (i % LOG_EVERY_N) == 0:
            print('Itaration :'+str(i)+" K/s = "+ str(i / time_diff))

        if addr in db:
            f = open('KEYFOUND_KEYFOUND_KEYFOUND.txt','a')
            print ('------------------------------------------------------------------------- ')
            print ('!!! ---- KEYFOUND KEYFOUND KEYFOUND!!! ---- ' )
            print('Key: ', hex(private_key)[2:] )
            print('Adress: ', addr )
            f.write(hex(private_key)[2:])
            f.write('     ')
            f.write(addr)
            f.write('\n')
            f.close()
            time.sleep(10)
            print ('------------------------------------------------------------------------- ')


if __name__ == '__main__':
jobs = []
for r in range(cores):
p = multiprocessing.Process(target=seek, args=(r,))
jobs.append(p)
p.start()