Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

no numpy

  • Loading branch information...
commit 247ecef0e4a2701939da17e4547a40dee74d1b9b 1 parent 9641ad4
@m0mchil authored
View
25 BFLMiner.py
@@ -1,13 +1,12 @@
from Miner import Miner
from Queue import Empty
-from binascii import unhexlify
from ioutil import find_udev, find_serial_by_id, find_com_ports
from log import say_line, say_exception
from serial.serialutil import SerialException
+from struct import pack, unpack, error
from sys import maxint
from time import time, sleep
-from util import Object
-import numpy as np
+from util import Object, uint32, bytereverse
import serial
CHECK_INTERVAL = 0.01
@@ -90,8 +89,8 @@ def put_job(self):
response = request(self.device, b'ZDX')
if self.is_ok(response):
if self.switch.update_time:
- self.job.time = (np.uint32(time()) - self.job.time_delta).byteswap()
- data = b''.join([self.job.state.tostring(), self.job.merkle_end.tostring(), self.job.time.tostring(), self.job.difficulty.tostring()])
+ self.job.time = bytereverse(uint32(long(time())) - self.job.time_delta)
+ data = b''.join([pack('8I', *self.job.state), pack('3I', self.job.merkle_end, self.job.time, self.job.difficulty)])
response = request(self.device, b''.join([b'>>>>>>>>', data, b'>>>>>>>>']))
if self.is_ok(response):
self.busy = True
@@ -110,7 +109,7 @@ def put_job(self):
self.last_job.miner = self
self.check_interval = CHECK_INTERVAL
- if not self.switch.update_time or self.job.time.byteswap() - self.job.original_time.byteswap() > 55:
+ if not self.switch.update_time or bytereverse(self.job.time) - bytereverse(self.job.original_time) > 55:
self.update = True
self.job = None
else:
@@ -122,14 +121,14 @@ def put_job(self):
def get_temperature(self):
response = request(self.device, b'ZLX')
- if response[0] != b'T' or len(response) < 23 or response[-1:] != b'\n':
+ if len(response) < 23 or response[0] != b'T' or response[-1:] != b'\n':
say_line('%s: bad response for temperature: %s', (self.id(), response))
return 0
return float(response[23:-1])
def check_result(self):
response = request(self.device, b'ZFX')
- if response[0] == b'B': return False
+ if response.startswith(b'B'): return False
if response == b'NO-NONCE\n': return response
if response[:12] != 'NONCE-FOUND:' or response[-1:] != '\n':
say_line('%s: bad response checking result: %s', (self.id(), response))
@@ -140,8 +139,8 @@ def nonce_generator(self, nonces):
for nonce in nonces.split(b','):
if len(nonce) != 8: continue
try:
- yield np.fromstring(unhexlify(nonce)[::-1], dtype=np.uint32, count=1)[0]
- except TypeError:
+ yield unpack('I', nonce.decode('hex')[::-1])[0]
+ except error:
pass
def mining_thread(self):
@@ -175,8 +174,8 @@ def mining_thread(self):
continue
targetQ = self.job.targetQ
self.job.original_time = self.job.time
- self.job.time_delta = np.uint32(time()) - self.job.time.byteswap()
-
+ self.job.time_delta = uint32(long(time())) - bytereverse(self.job.time)
+
if not self.busy:
self.put_job()
else:
@@ -200,7 +199,7 @@ def mining_thread(self):
if result != b'NO-NONCE\n':
r.nonces = result
self.switch.put(r)
-
+
sleep(self.min_interval - (CHECK_INTERVAL * 2))
else:
if result is None:
View
93 OpenCLMiner.py
@@ -6,8 +6,7 @@
from struct import pack
from threading import Lock
from time import sleep, time
-from util import if_else, uint32, Object, bytereverse, patch, tokenize
-import numpy as np
+from util import if_else, uint32, Object, bytereverse, patch, tokenize, bytearray_to_uint32
import sys
@@ -139,10 +138,11 @@ def id(self):
return str(self.options.platform) + ':' + str(self.device_index) + ':' + self.device_name
def nonce_generator(self, nonces):
- for i in xrange(self.output_size):
- if nonces[i]:
- yield nonces[i]
-
+ for i in xrange(0, len(nonces) - 4, 4):
+ nonce = bytearray_to_uint32(nonces[i:i+4])
+ if nonce:
+ yield nonce
+
def mining_thread(self):
say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name))
@@ -160,7 +160,7 @@ def mining_thread(self):
last_rated_pace = last_rated = last_n_time = last_temperature = time()
base = last_hash_rate = threads_run_pace = threads_run = 0
- output = np.zeros(self.output_size + 1, np.uint32)
+ output = bytearray((self.output_size + 1) * 4)
output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output)
self.kernel.set_arg(20, output_buffer)
@@ -179,30 +179,31 @@ def mining_thread(self):
if not work: continue
nonces_left = hashspace
state = work.state
- state2 = work.state2
- f = work.f
-
- self.kernel.set_arg(0, state[0])
- self.kernel.set_arg(1, state[1])
- self.kernel.set_arg(2, state[2])
- self.kernel.set_arg(3, state[3])
- self.kernel.set_arg(4, state[4])
- self.kernel.set_arg(5, state[5])
- self.kernel.set_arg(6, state[6])
- self.kernel.set_arg(7, state[7])
-
- self.kernel.set_arg(8, state2[1])
- self.kernel.set_arg(9, state2[2])
- self.kernel.set_arg(10, state2[3])
- self.kernel.set_arg(11, state2[5])
- self.kernel.set_arg(12, state2[6])
- self.kernel.set_arg(13, state2[7])
-
- self.kernel.set_arg(15, f[0])
- self.kernel.set_arg(16, f[1])
- self.kernel.set_arg(17, f[2])
- self.kernel.set_arg(18, f[3])
- self.kernel.set_arg(19, f[4])
+ f = [0] * 8
+ state2 = partial(state, work.merkle_end, work.time, work.difficulty, f)
+ calculateF(state, work.merkle_end, work.time, work.difficulty, f, state2)
+
+ self.kernel.set_arg(0, pack('I', state[0]))
+ self.kernel.set_arg(1, pack('I', state[1]))
+ self.kernel.set_arg(2, pack('I', state[2]))
+ self.kernel.set_arg(3, pack('I', state[3]))
+ self.kernel.set_arg(4, pack('I', state[4]))
+ self.kernel.set_arg(5, pack('I', state[5]))
+ self.kernel.set_arg(6, pack('I', state[6]))
+ self.kernel.set_arg(7, pack('I', state[7]))
+
+ self.kernel.set_arg(8, pack('I', state2[1]))
+ self.kernel.set_arg(9, pack('I', state2[2]))
+ self.kernel.set_arg(10, pack('I', state2[3]))
+ self.kernel.set_arg(11, pack('I', state2[5]))
+ self.kernel.set_arg(12, pack('I', state2[6]))
+ self.kernel.set_arg(13, pack('I', state2[7]))
+
+ self.kernel.set_arg(15, pack('I', f[0]))
+ self.kernel.set_arg(16, pack('I', f[1]))
+ self.kernel.set_arg(17, pack('I', f[2]))
+ self.kernel.set_arg(18, pack('I', f[3]))
+ self.kernel.set_arg(19, pack('I', f[4]))
if temperature < self.cutoff_temp:
self.kernel.set_arg(14, pack('I', base))
@@ -243,21 +244,21 @@ def mining_thread(self):
cl.enqueue_read_buffer(queue, output_buffer, output)
queue.finish()
- if output[self.output_size]:
+ if output[-1]:
result = Object()
result.header = work.header
result.merkle_end = work.merkle_end
result.time = work.time
result.difficulty = work.difficulty
result.target = work.target
- result.state = np.array(state)
- result.nonces = np.array(output)
+ result.state = list(state)
+ result.nonces = output[:]
result.job_id = work.job_id
result.extranonce2 = work.extranonce2
result.server = work.server
result.miner = self
self.switch.put(result)
- output.fill(0)
+ output[:] = b'\x00' * len(output)
cl.enqueue_write_buffer(queue, output_buffer, output)
if not self.switch.update_time:
@@ -271,17 +272,17 @@ def mining_thread(self):
work.time = bytereverse(bytereverse(work.time) + 1)
state2 = partial(state, work.merkle_end, work.time, work.difficulty, f)
calculateF(state, work.merkle_end, work.time, work.difficulty, f, state2)
- self.kernel.set_arg(8, state2[1])
- self.kernel.set_arg(9, state2[2])
- self.kernel.set_arg(10, state2[3])
- self.kernel.set_arg(11, state2[5])
- self.kernel.set_arg(12, state2[6])
- self.kernel.set_arg(13, state2[7])
- self.kernel.set_arg(15, f[0])
- self.kernel.set_arg(16, f[1])
- self.kernel.set_arg(17, f[2])
- self.kernel.set_arg(18, f[3])
- self.kernel.set_arg(19, f[4])
+ self.kernel.set_arg(8, pack('I', state2[1]))
+ self.kernel.set_arg(9, pack('I', state2[2]))
+ self.kernel.set_arg(10, pack('I', state2[3]))
+ self.kernel.set_arg(11, pack('I', state2[5]))
+ self.kernel.set_arg(12, pack('I', state2[6]))
+ self.kernel.set_arg(13, pack('I', state2[7]))
+ self.kernel.set_arg(15, pack('I', f[0]))
+ self.kernel.set_arg(16, pack('I', f[1]))
+ self.kernel.set_arg(17, pack('I', f[2]))
+ self.kernel.set_arg(18, pack('I', f[3]))
+ self.kernel.set_arg(19, pack('I', f[4]))
last_n_time = now
self.update_time_counter += 1
if self.update_time_counter >= self.switch.max_update_time:
View
40 Switch.py
@@ -1,15 +1,14 @@
from copy import copy
from log import say_exception, say_line, say_quiet
-from sha256 import sha256, STATE, partial, calculateF, hash
+from sha256 import hash, sha256, STATE
from struct import pack, unpack
from threading import RLock
from time import time, sleep
-from util import if_else, Object, chunks, bytereverse, belowOrEquals
+from util import if_else, Object, chunks, bytereverse, belowOrEquals, uint32
import GetworkSource
import StratumSource
import log
-import numpy as np
import socks
@@ -155,25 +154,20 @@ def stop(self):
def decode(self, server, block_header, target, job_id = None, extranonce2 = None):
if block_header:
job = Object()
-
+
binary_data = block_header.decode('hex')
- data0 = np.zeros(64, np.uint32)
- data0 = np.insert(data0, [0] * 16, unpack('IIIIIIIIIIIIIIII', binary_data[:64]))
-
- job.target = np.array(unpack('IIIIIIII', target.decode('hex')), dtype=np.uint32)
- job.header = binary_data[:68]
- job.merkle_end = np.uint32(unpack('I', binary_data[64:68])[0])
- job.time = np.uint32(unpack('I', binary_data[68:72])[0])
- job.difficulty = np.uint32(unpack('I', binary_data[72:76])[0])
- job.state = sha256(STATE, data0)
- job.f = np.zeros(8, np.uint32)
- job.state2 = partial(job.state, job.merkle_end, job.time, job.difficulty, job.f)
- job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16)
- job.job_id = job_id
- job.extranonce2 = extranonce2
- job.server = server
-
- calculateF(job.state, job.merkle_end, job.time, job.difficulty, job.f, job.state2)
+ data0 = list(unpack('16I', binary_data[:64])) + ([0] * 48)
+
+ job.target = unpack('8I', target.decode('hex'))
+ job.header = binary_data[:68]
+ job.merkle_end = uint32(unpack('I', binary_data[64:68])[0])
+ job.time = uint32(unpack('I', binary_data[68:72])[0])
+ job.difficulty = uint32(unpack('I', binary_data[72:76])[0])
+ job.state = sha256(STATE, data0)
+ job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16)
+ job.job_id = job_id
+ job.extranonce2 = extranonce2
+ job.server = server
if job.difficulty != self.difficulty:
self.set_difficulty(job.difficulty)
@@ -182,10 +176,10 @@ def decode(self, server, block_header, target, job_id = None, extranonce2 = None
def set_difficulty(self, difficulty):
self.difficulty = difficulty
- bits = '%08x' % difficulty.byteswap()
+ bits = '%08x' % bytereverse(difficulty)
true_target = '%064x' % (int(bits[2:], 16) * 2 ** (8 * (int(bits[:2], 16) - 3)),)
true_target = ''.join(list(chunks(true_target, 2))[::-1])
- self.true_target = np.array(unpack('IIIIIIII', true_target.decode('hex')), dtype=np.uint32)
+ self.true_target = unpack('8I', true_target.decode('hex'))
def send(self, result, send_callback):
for nonce in result.miner.nonce_generator(result.nonces):
View
2  setup.py
@@ -20,7 +20,7 @@
'author': 'Momchil Georgiev',
'author_email': 'pishtov@gmail.com',
'url': 'https://github.com/m0mchil/poclbm/',
- 'install_requires': ['numpy', 'pyserial>=2.6'],
+ 'install_requires': ['pyserial>=2.6'],
'scripts': ['poclbm.py'],
}
View
50 sha256.py
@@ -1,16 +1,17 @@
from util import uint32
-import numpy as np
-K = np.array( [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2], np.uint32)
-STATE = np.array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19], np.uint32)
+K = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
+
+STATE = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
+
def rotr(x, y):
return (x>>y | x<<(32-y))
@@ -27,7 +28,7 @@ def sharound(a,b,c,d,e,f,g,h,x,K):
return (uint32(d + t1), uint32(t1+t2))
def partial(state, merkle_end, time, difficulty, f):
- state2 = np.array(state)
+ state2 = list(state)
data = [merkle_end, time, difficulty]
for i in xrange(3):
(state2[~(i-4)&7], state2[~(i-8)&7]) = sharound(state2[(~(i-1)&7)],state2[~(i-2)&7],state2[~(i-3)&7],state2[~(i-4)&7],state2[~(i-5)&7],state2[~(i-6)&7],state2[~(i-7)&7],state2[~(i-8)&7],data[i],K[i])
@@ -46,35 +47,46 @@ def calculateF(state, merkle_end, time, difficulty, f, state2):
data = [merkle_end, time, difficulty]
rot = lambda x,y: x>>y | x<<(32-y)
#W2
- f[0] = np.uint32(data[2])
+ f[0] = uint32(data[2])
#W16
- f[1] = np.uint32(data[0] + (rot(data[1], 7) ^ rot(data[1], 18) ^
+ f[1] = uint32(data[0] + (rot(data[1], 7) ^ rot(data[1], 18) ^
(data[1] >> 3)))
#W17
- f[2] = np.uint32(data[1] + (rot(data[2], 7) ^ rot(data[2], 18) ^
+ f[2] = uint32(data[1] + (rot(data[2], 7) ^ rot(data[2], 18) ^
(data[2] >> 3)) + 0x01100000)
#2 parts of the first SHA round
- f[3] = np.uint32(state[4] + (rot(state2[1], 6) ^
+ f[3] = uint32(state[4] + (rot(state2[1], 6) ^
rot(state2[1], 11) ^ rot(state2[1], 25)) +
(state2[3] ^ (state2[1] & (state2[2] ^
state2[3]))) + 0xe9b5dba5)
- f[4] = np.uint32((rot(state2[5], 2) ^
+ f[4] = uint32((rot(state2[5], 2) ^
rot(state2[5], 13) ^ rot(state2[5], 22)) +
((state2[5] & state2[6]) | (state2[7] &
(state2[5] | state2[6]))))
def sha256(state, data):
- digest = np.copy(state)
+ digest = list(state)
for i in xrange(64):
if i > 15:
data[i] = R(data[i-2], data[i-7], data[i-15], data[i-16])
(digest[~(i-4)&7], digest[~(i-8)&7]) = sharound(digest[(~(i-1)&7)],digest[~(i-2)&7],digest[~(i-3)&7],digest[~(i-4)&7],digest[~(i-5)&7],digest[~(i-6)&7],digest[~(i-7)&7],digest[~(i-8)&7],data[i],K[i])
- return np.add(digest, state)
+
+ result = []
+ result.append(uint32(digest[0] + state[0]))
+ result.append(uint32(digest[1] + state[1]))
+ result.append(uint32(digest[2] + state[2]))
+ result.append(uint32(digest[3] + state[3]))
+ result.append(uint32(digest[4] + state[4]))
+ result.append(uint32(digest[5] + state[5]))
+ result.append(uint32(digest[6] + state[6]))
+ result.append(uint32(digest[7] + state[7]))
+
+ return result
def hash(midstate, merkle_end, time, difficulty, nonce):
- work = np.zeros(64, np.uint32)
+ work = [[0] * 64][0]
work[0]=merkle_end; work[1]=time; work[2]=difficulty; work[3]=nonce
work[4]=0x80000000; work[5]=0x00000000; work[6]=0x00000000; work[7]=0x00000000
work[8]=0x00000000; work[9]=0x00000000; work[10]=0x00000000; work[11]=0x00000000
View
5 util.py
@@ -11,8 +11,11 @@ def uint32(x):
def bytereverse(x):
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) | (((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
+def bytearray_to_uint32(x):
+ return uint32(((x[3]) << 24) | ((x[2]) << 16) | ((x[1]) << 8) | x[0])
+
def belowOrEquals(hash_, target):
- for i in range(len(hash_) - 1, -1, -1):
+ for i in xrange(len(hash_) - 1, -1, -1):
reversed_ = bytereverse(hash_[i])
if reversed_ < target[i]:
return True
Please sign in to comment.
Something went wrong with that request. Please try again.