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
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
from configparser import BMConfigParser
|
2017-01-11 17:00:00 +01:00
|
|
|
import paths
|
2017-01-14 23:20:15 +01:00
|
|
|
from state import shutdown
|
2015-11-26 02:38:55 +01:00
|
|
|
from debug import logger
|
|
|
|
|
|
|
|
libAvailable = True
|
2015-01-19 18:39:02 +01:00
|
|
|
ctx = False
|
|
|
|
queue = False
|
|
|
|
program = False
|
2015-11-26 02:38:55 +01:00
|
|
|
gpus = []
|
2016-11-10 21:43:10 +01:00
|
|
|
enabledGpus = []
|
|
|
|
vendors = []
|
2015-11-26 02:38:55 +01:00
|
|
|
hash_dt = None
|
2015-01-19 18:39:02 +01:00
|
|
|
|
|
|
|
try:
|
2015-10-02 15:04:16 +02:00
|
|
|
import numpy
|
|
|
|
import pyopencl as cl
|
2015-11-26 02:38:55 +01:00
|
|
|
except:
|
|
|
|
libAvailable = False
|
|
|
|
|
|
|
|
def initCL():
|
2016-11-14 20:21:46 +01:00
|
|
|
global ctx, queue, program, gpus, enabledGpus, hash_dt, vendors
|
2015-11-26 02:38:55 +01:00
|
|
|
try:
|
|
|
|
hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)])
|
2016-11-10 21:43:10 +01:00
|
|
|
try:
|
|
|
|
for platform in cl.get_platforms():
|
|
|
|
gpus.extend(platform.get_devices(device_type=cl.device_type.GPU))
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().safeGet("bitmessagesettings", "opencl") == platform.vendor:
|
2016-11-10 21:43:10 +01:00
|
|
|
enabledGpus.extend(platform.get_devices(device_type=cl.device_type.GPU))
|
|
|
|
if platform.vendor not in vendors:
|
|
|
|
vendors.append(platform.vendor)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if (len(enabledGpus) > 0):
|
|
|
|
ctx = cl.Context(devices=enabledGpus)
|
2015-11-26 02:38:55 +01:00
|
|
|
queue = cl.CommandQueue(ctx)
|
2017-01-11 17:00:00 +01:00
|
|
|
f = open(os.path.join(paths.codePath(), "bitmsghash", 'bitmsghash.cl'), 'r')
|
2015-11-26 02:38:55 +01:00
|
|
|
fstr = ''.join(f.readlines())
|
|
|
|
program = cl.Program(ctx, fstr).build(options="")
|
|
|
|
logger.info("Loaded OpenCL kernel")
|
|
|
|
else:
|
|
|
|
logger.info("No OpenCL GPUs found")
|
2016-11-10 21:43:10 +01:00
|
|
|
enabledGpus = []
|
2015-11-26 02:38:55 +01:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("OpenCL fail: ", exc_info=True)
|
2016-11-10 21:43:10 +01:00
|
|
|
enabledGpus = []
|
2015-01-19 18:39:02 +01:00
|
|
|
|
2016-11-10 21:43:10 +01:00
|
|
|
def openclAvailable():
|
|
|
|
return (len(gpus) > 0)
|
|
|
|
|
|
|
|
def openclEnabled():
|
|
|
|
return (len(enabledGpus) > 0)
|
2015-01-19 18:39:02 +01:00
|
|
|
|
|
|
|
def do_opencl_pow(hash, target):
|
2016-11-10 21:43:10 +01:00
|
|
|
output = numpy.zeros(1, dtype=[('v', numpy.uint64, 1)])
|
|
|
|
if (len(enabledGpus) == 0):
|
|
|
|
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
|
|
|
|
worksize = kernel.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, enabledGpus[0])
|
|
|
|
|
|
|
|
kernel.set_arg(0, hash_buf)
|
|
|
|
kernel.set_arg(1, dest_buf)
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
progress = 0
|
|
|
|
globamt = worksize*2000
|
|
|
|
|
|
|
|
while output[0][0] == 0 and shutdown == 0:
|
|
|
|
kernel.set_arg(2, pack("<Q", progress))
|
|
|
|
cl.enqueue_nd_range_kernel(queue, kernel, (globamt,), (worksize,))
|
|
|
|
cl.enqueue_read_buffer(queue, dest_buf, output)
|
|
|
|
queue.finish()
|
|
|
|
progress += globamt
|
|
|
|
sofar = time.time() - start
|
|
|
|
# logger.debug("Working for %.3fs, %.2f Mh/s", sofar, (progress / sofar) / 1000000)
|
|
|
|
if shutdown != 0:
|
|
|
|
raise Exception ("Interrupted")
|
|
|
|
taken = time.time() - start
|
|
|
|
# logger.debug("Took %d tries.", progress)
|
|
|
|
return output[0][0]
|
2015-01-19 18:39:02 +01:00
|
|
|
|
2015-11-26 02:38:55 +01:00
|
|
|
if libAvailable:
|
|
|
|
initCL()
|
|
|
|
|
2015-01-19 18:39:02 +01:00
|
|
|
if __name__ == "__main__":
|
2016-11-10 21:43:10 +01:00
|
|
|
target = 54227212183L
|
|
|
|
initialHash = "3758f55b5a8d902fd3597e4ce6a2d3f23daff735f65d9698c270987f4e67ad590b93f3ffeba0ef2fd08a8dc2f87b68ae5a0dc819ab57f22ad2c4c9c8618a43b3".decode("hex")
|
|
|
|
nonce = do_opencl_pow(initialHash.encode("hex"), target)
|
|
|
|
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
|
|
|
|
print "{} - value {} < {}".format(nonce, trialValue, target)
|
2015-01-19 18:39:02 +01:00
|
|
|
|