-
Notifications
You must be signed in to change notification settings - Fork 4
/
puzzle.py
233 lines (172 loc) · 5.91 KB
/
puzzle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import ecdsa
import binascii
import hashlib
import base58
import random
import time
import codecs
import multiprocessing
from cryptotools.BTC import PrivateKey, Address
# Parameters :
# If you don't need to recreate the dataset, set it to False
need_setup_dataset = True
# Number of thread (max 2 per core, more will be useless)
nb_thread_max = 8
# Number of address that a unique thread will have to process (WARNING : the lower this value is, the bigger the dataset will be)
addr_per_thread = 7000
#Number of temp file to create (the higher this value is, the lower RAM is used)
num_of_temp_file = 10
#The searched address
pub_addr_searched = "13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so"
# Range in wich you want to search the address
start = "1"
end = "186A00"
nomber_ite = int((int(end, 16) - int(start, 16))/addr_per_thread)
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
def get_addr(private_key):
Private_key = bytes.fromhex(private_key)
signing_key = ecdsa.SigningKey.from_string(Private_key, curve = ecdsa.SECP256k1)
verifying_key = signing_key.get_verifying_key()
public_key = bytes.fromhex("04") + verifying_key.to_string()
public_key = public_key.hex()
if (ord(bytearray.fromhex(public_key[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += public_key[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
# Obtain key:
key_hash = '00' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( bytearray.fromhex(key_hash) )
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]
return str(base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum))))
def thread_f(start_l, thread_no, q):
# Progress bar
progress = -0.01
for e in range(0, addr_per_thread) :
# Show progress (can be disabled)
if e%(addr_per_thread/10000) == 0:
progress += 0.01
f = open("nohup.out", "a")
f.write(str(thread_no) + " => " + str(progress) + "%\n")
f.close()
# Getting the current private key
current = str(hex(int(start_l,16) + e)[2:])
while(len(current) < 64):
current = "0" + current
#Searching the address associated
addrp2pkh = (get_addr(current)[2:])[:-1]
# If you find the result, it will be written in this file
if(addrp2pkh == pub_addr_searched):
f = open("result.txt", "a")
f.write(current)
f.close()
print("found")
q.put((thread_no, "finished"))
return
def setup_dataset(from_file_no = 0, to_file_no = num_of_temp_file):
# Create a dataset and shuffle it
# If you have a memory error in this part, you can increase the value of the num_of_temp_file variable
print("Setting up dataset from file " + str(from_file_no) + " to file " + str(to_file_no) + "..")
step = int((int(end, 16) - int(start, 16)) / num_of_temp_file)
from_addr = step * from_file_no + int(start, 16)
to_addr = step * to_file_no + int(start, 16) - 1
files = []
file_selected = 0
for e in range(from_file_no, to_file_no):
files.append(open("todo_" + str(e) + ".txt", "w"))
for e in range(from_addr, to_addr, addr_per_thread):
files[file_selected].write(hex(e)[2:]+"\n")
file_selected = 0 if file_selected == (to_file_no - from_file_no)-1 else file_selected+1
for e in range(0, to_file_no-from_file_no):
files[e].close()
print("Done !\n")
def shuffle_dataset(from_file_no = 0, to_file_no = num_of_temp_file):
# We shuffle each temp file and we join them into one bigger file
print("Shuffling")
file_list = [e for e in range(from_file_no, to_file_no)]
# We shuffle each file
for e in range(from_file_no, to_file_no):
file = open("todo_" + str(e) + ".txt", "r")
content = file.read().split("\n")
file.close()
random.shuffle(content)
file = open("todo_" + str(e) + ".txt", "w")
for line in content:
file.write(line + "\n")
file.close()
def join_dataset(from_file_no = 0, to_file_no = num_of_temp_file):
# We create a list of open files
file_iterators = []
for file in range(from_file_no, to_file_no):
file_iterators.append(open("todo_" + str(file) + ".txt"))
# We randomly read these values and apend it to the main file
todo_file = open("todo.txt", "w")
while len(file_iterators) > 0:
file_no = random.randint(0, len(file_iterators)-1)
file_iterator = file_iterators[file_no]
try:
line = next(file_iterator)
if line == "\n":
continue
todo_file.write(line)
except Exception as e:
# If the file is empty
file_iterator.close()
file_iterators.remove(file_iterator)
todo_file.close()
for e in range(from_file_no, to_file_no):
# We remove temp files
os.remove("todo_" + str(e) + ".txt")
print("Done !")
if __name__ == "__main__":
time1 = time.time()
thread_l = []
if need_setup_dataset:
for e in range(nb_thread_max):
from_file_no = int(e * (num_of_temp_file/nb_thread_max))
to_file_no = int((e+1) * (num_of_temp_file/nb_thread_max))
x = multiprocessing.Process(target=setup_dataset, args=(from_file_no, to_file_no,))
x.start()
thread_l.append(x)
for e in thread_l:
e.join()
thread_l = []
for e in range(nb_thread_max):
from_file_no = int(e * (num_of_temp_file/nb_thread_max))
to_file_no = int((e+1) * (num_of_temp_file/nb_thread_max))
x = multiprocessing.Process(target=shuffle_dataset, args=(from_file_no, to_file_no,))
x.start()
thread_l.append(x)
for e in thread_l:
e.join()
thread_l = []
join_dataset()
todo_f = open("todo.txt", 'r')
count = 0
active_t = 0
q = multiprocessing.SimpleQueue()
for count in range(0, nomber_ite+1):
line = next(todo_f)[:-1]
if active_t != nb_thread_max:
print(count, "/", nomber_ite)
x = multiprocessing.Process(target=thread_f, args=(line, active_t, q,))
x.start()
active_t+=1
else:
q.get()
count-=1
active_t-=1
time2 = time.time()
print('%s function took', (time2-time1)*1000.0, 'ms')