Hi!
Sorry for my poor Englisch, but it's not my native tongue, since I'm from Germany.
I have read about the bitcoin concept a while ago and already installed a miner on my Radeon 5850. Works ago, but I'd like to get a small miner going on my daily coding machine, which has a GeForce 7. So I got me BrookGPU, got some demo apps going and want to write some code for it.
Since I have not much clue about the whole mining process, I thought that I should start with a very simple demo app, called hashcode and modify it to do something useful. So I ported it to the cc65 compiler and run it on Vice (a c64 emulator) just to have some fun with it and to understand the code etc.
My current problem is the difficulty <=> hash comparison. I wrote a decode function for the difficulty according to the Wiki entry:
/**
* Decode the target into a 256 bit int. See the target entry in the bitcoin wiki for details.
*/
void decodeDifficulty( uint8 *targetArray, uint32 target) {
uint32 offset = ( ( target >> 24) - 3) << 3;
int bitshift = offset & 7;
int currentByte = offset >> 3;
uint32 targetBits = target & (uint32)0x00ffffff;
if( targetBits & 0x00800000) { // Is the difficulty negative?
targetBits |= (uint32)0xff000000;
bzero( targetArray, sizeof(uint8) * currentByte); // Empty everything below the bitmask
memset( &targetArray[ currentByte + 4], 0xff, sizeof( uint8) * ( SHA256_DIGEST_LENGTH - 4 - currentByte)); // Fill bits over the bitmask with 1 for sign extension
} else {
bzero( targetArray, sizeof(uint8) * SHA256_DIGEST_LENGTH); // Empty the target array
}
targetArray[ currentByte++] = (uint8)( targetBits & 0xff);
targetBits >>= 8;
targetArray[ currentByte++] = (uint8)( targetBits & 0xff);
targetBits >>= 8;
targetArray[ currentByte++] = (uint8)( targetBits & 0xff);
targetBits >>= 8;
targetArray[ currentByte] = (uint8)( targetBits & 0xff);
}
and a compare function for 32 byte integers:
/**
* Compare 2 256 bit integer values.
*
* @return a value > 0, if hash1 > hash2 , a value < 0, if hash1 < hash2 and 0 if they are equal.
*/
int compareHash( uint8 *hash1, uint8 *hash2) {
uint8 msb1, msb2;
int currentByte;
int byteDifference;
// Check, if both numbers have the same sign..
msb1 = hash1[ SHA256_DIGEST_LENGTH - 1] & 0x80;
msb2 = hash2[ SHA256_DIGEST_LENGTH - 1] & 0x80;
if( msb1 != msb2) { // Both numbers differ in sign
return msb1 ? -1 : 1;
}
// We have to compare byte by byte... :-(
for( currentByte = SHA256_DIGEST_LENGTH - 1; currentByte >= 0; --currentByte) {
byteDifference = hash1[ currentByte] - hash2[ currentByte]; // Compute the difference of the current bytes-.
if( byteDifference) { // If the bytes differ.
return byteDifference; // just return the difference.
}
}
return 0; // both ints are equal.
}
, but what happens if the hash is greater then 0x8000.... => is negative? It's smaller in my current code, so it would be a solution, which is wrong as I understand it. So should I compare abs(hash) < difficulty ?
Thanks in advance,
Andreas