2015-10-02 15:04:16 +02:00
|
|
|
#!/usr/bin/env python2.7
|
2015-01-19 18:39:02 +01:00
|
|
|
from struct import pack, unpack
|
|
|
|
import time
|
|
|
|
import hashlib
|
2015-06-20 09:54:15 +02:00
|
|
|
import random
|
2015-10-02 15:04:16 +02:00
|
|
|
import os
|
2015-01-19 18:39:02 +01:00
|
|
|
|
|
|
|
ctx = False
|
|
|
|
queue = False
|
|
|
|
program = False
|
|
|
|
|
|
|
|
try:
|
2015-10-02 15:04:16 +02:00
|
|
|
import numpy
|
|
|
|
import pyopencl as cl
|
|
|
|
hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)])
|
|
|
|
if (len(cl.get_platforms()) > 0):
|
|
|
|
ctx = cl.create_some_context()
|
|
|
|
queue = cl.CommandQueue(ctx)
|
|
|
|
full_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
f = open(os.path.join(full_path, 'kernel.cl'), 'r')
|
|
|
|
fstr = ''.join(f.readlines())
|
|
|
|
program = cl.Program(ctx, fstr).build(options="")
|
2015-09-30 10:22:41 +02:00
|
|
|
except Exception as e:
|
2015-10-02 15:04:16 +02:00
|
|
|
print "opencl fail:" + str(e)
|
|
|
|
ctx = False
|
2015-01-19 18:39:02 +01:00
|
|
|
|
|
|
|
def has_opencl():
|
|
|
|
return (ctx != False)
|
|
|
|
|
|
|
|
def do_opencl_pow(hash, target):
|
|
|
|
output = numpy.zeros(1, dtype=[('v', numpy.uint64, 1)])
|
|
|
|
if (ctx == False):
|
|
|
|
return output[0][0]
|
|
|
|
|
|
|
|
data = numpy.zeros(1, dtype=hash_dt, order='C')
|
|
|
|
data[0]['v'] = ("0000000000000000" + hash).decode("hex")
|
|
|
|
data[0]['target'] = target
|
|
|
|
|
|
|
|
hash_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=data)
|
|
|
|
dest_buf = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, output.nbytes)
|
|
|
|
|
|
|
|
kernel = program.kernel_sha512
|
2015-10-02 15:04:16 +02:00
|
|
|
worksize = kernel.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, cl.get_platforms()[0].get_devices()[1])
|
2015-01-19 18:39:02 +01:00
|
|
|
|
|
|
|
kernel.set_arg(0, hash_buf)
|
|
|
|
kernel.set_arg(1, dest_buf)
|
|
|
|
|
|
|
|
start = time.time()
|
2015-06-20 09:54:15 +02:00
|
|
|
progress = 0
|
2015-01-19 18:39:02 +01:00
|
|
|
globamt = worksize*2000
|
|
|
|
|
|
|
|
while output[0][0] == 0:
|
2015-06-20 10:17:39 +02:00
|
|
|
kernel.set_arg(2, pack("<Q", progress))
|
2015-01-19 18:39:02 +01:00
|
|
|
cl.enqueue_nd_range_kernel(queue, kernel, (globamt,), (worksize,))
|
|
|
|
cl.enqueue_read_buffer(queue, dest_buf, output)
|
|
|
|
queue.finish()
|
2015-06-20 09:54:15 +02:00
|
|
|
progress += globamt
|
2015-01-19 18:39:02 +01:00
|
|
|
sofar = time.time() - start
|
2015-06-20 09:54:15 +02:00
|
|
|
print sofar, progress / sofar, "hashes/sec"
|
2015-01-19 18:39:02 +01:00
|
|
|
taken = time.time() - start
|
2015-06-20 09:54:15 +02:00
|
|
|
print progress, taken
|
2015-01-19 18:39:02 +01:00
|
|
|
return output[0][0]
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
target = 54227212183L
|
|
|
|
initialHash = "3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3".decode("hex")
|
2015-06-20 09:54:15 +02:00
|
|
|
nonce = do_opencl_pow(initialHash.encode("hex"), target)
|
2015-01-19 18:39:02 +01:00
|
|
|
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
|
|
|
|
print "{} - value {} < {}".format(nonce, trialValue, target)
|
|
|
|
|