Bitcoin Forum
May 21, 2018, 05:20:14 PM
 News: Latest stable version of Bitcoin Core: 0.16.0  [Torrent]. (New!)
 Home Help Search Donate Login Register
 Pages: [1]
 Author Topic: A newbie wants to understand the bitcoin mining concept  (Read 1130 times)
daybyter
Legendary

Offline

Activity: 965
Merit: 1000

 January 16, 2012, 01:16:09 PM

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:

Code:
/**
* 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:

Code:
/**
* 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 ?

Andreas

Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1526923214
Hero Member

Offline

Posts: 1526923214

Ignore
 1526923214

1526923214
 Report to moderator
pieppiep
Hero Member

Offline

Activity: 697
Merit: 500

 January 16, 2012, 02:22:10 PM

You now look at the 32 byte integer as if it is a signed integer but it should be an unsigned 32 byte integer.

 ▄▄▄█████████████▄▄▄           ▄████████▀▀▀▀▀▀▀████████▄        ▄██████▀▀             ▀▀██████▄      ▄█████▀▀                    ▀▀█████▄     █████▀                          ▀█████    ████▀          ▄▄███████▄▄         ▀████   ████▌        ▄██▀▀▀    ▀▀▀██▄        ▐████  ████▌       ▄██▀            ▀██▄       ▐████ ▐████       ██▀   ▄▄█▀▀▀█▄▄    ▀██       ████▌ ████▌      ▐█▌   █▀  ▄▄   ▀▀             ▐████▐████       ██  █▌  █▌ █████████████      ████▌▐████       ██  ▐█  ▐█                     ████▌▐████       ██  █▌  █▌ █████████████      ████▌ ████▌      ▐█▌   █▄  ▀▀   ▄▄    ██▀      ▐████ ▐████       ██▄   ▀▀█▄▄▄█▀▀    ██▌       ████▌  █████       ▀██▄            ▄██▀       █████   █████        ▀██▄▄▄    ▄▄▄██▀        █████    █████          ▀▀███████▀▀         █████     █████▄                          ▄█████      ▀█████▄▄                    ▄▄█████▀        ▀██████▄▄             ▄▄██████▀           ▀████████▄▄▄▄▄▄▄████████▀              ▀▀▀█████████████▀▀▀ Global Cryptocurrency           ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ •• DECENTRALISING PRODUCTION, LOGISTICS AND PAYMENT                  ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬   3D SERVICE      32 BAY     GCC WEBWALLET▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ •• C-CEX EXCHANGE  FACEBOOK  TWITTER  TELEGRAM▬▬▬  MEDIUM  ▬▬▬
daybyter
Legendary

Offline

Activity: 965
Merit: 1000

 January 16, 2012, 05:19:38 PM

Thanks a lot for your comment. I had the option of a negative difficulty in my code, because the wiki said, that it's a signed value. From a practical point of view, a negative difficulty doesn't make much sense, so I will fix my code.

Andreas

daybyter
Legendary

Offline

Activity: 965
Merit: 1000

 January 16, 2012, 06:53:48 PM

The current code is available here:

http://www.forum64.de/wbb3/board25-coder-unter-sich/board308-programmieren/board29-cross-development/45342-bitcoin-mining-auf-dem-c64/#post579246

At the moment, it's just a hack to get something going. The sha256 code is a mixture of some implementations, found at various place of the net. Some of it is from Christophe Devine, but I couldn't use the optimized code with the unrolled loops due to memory constraints. The Makefile does no real speed optimization yet, since I wanted the code to be optimized for size and not speed.  The networking part is completely missing yet, but I doubt if it makes sense to request actual blocks, since the breadbox is just too slow for productive work (100 nonces in 274 seconds at the moment).

Ciao,
Andreas

codymanix
Full Member

Offline

Activity: 203
Merit: 108

Gir: I'm gonna sing the Doom Song now..

 January 17, 2012, 08:42:45 PM

Since the hashes are declared as uint8, you should see it as unsigned, so no they will never be negative.

Programming tutorials, Tools, Games & Humor http://deutronium.de.vu
My Vircurex referral code for persistant 0.05% trade discount: 750-33267 or use
https://vircurex.com/welcome/index?referral_id=750-33267
daybyter
Legendary

Offline

Activity: 965
Merit: 1000

 January 21, 2012, 09:23:00 PM

Since the hashes are declared as uint8, you should see it as unsigned, so no they will never be negative.

Thanks...I was just confused about the difficulty, since the wiki says it's signed...

pieppiep
Hero Member

Offline

Activity: 697
Merit: 500

 January 21, 2012, 09:27:40 PM

You are going to mine bitcoins on a commodore 64 ??
How cool!

 ▄▄▄█████████████▄▄▄           ▄████████▀▀▀▀▀▀▀████████▄        ▄██████▀▀             ▀▀██████▄      ▄█████▀▀                    ▀▀█████▄     █████▀                          ▀█████    ████▀          ▄▄███████▄▄         ▀████   ████▌        ▄██▀▀▀    ▀▀▀██▄        ▐████  ████▌       ▄██▀            ▀██▄       ▐████ ▐████       ██▀   ▄▄█▀▀▀█▄▄    ▀██       ████▌ ████▌      ▐█▌   █▀  ▄▄   ▀▀             ▐████▐████       ██  █▌  █▌ █████████████      ████▌▐████       ██  ▐█  ▐█                     ████▌▐████       ██  █▌  █▌ █████████████      ████▌ ████▌      ▐█▌   █▄  ▀▀   ▄▄    ██▀      ▐████ ▐████       ██▄   ▀▀█▄▄▄█▀▀    ██▌       ████▌  █████       ▀██▄            ▄██▀       █████   █████        ▀██▄▄▄    ▄▄▄██▀        █████    █████          ▀▀███████▀▀         █████     █████▄                          ▄█████      ▀█████▄▄                    ▄▄█████▀        ▀██████▄▄             ▄▄██████▀           ▀████████▄▄▄▄▄▄▄████████▀              ▀▀▀█████████████▀▀▀ Global Cryptocurrency           ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ •• DECENTRALISING PRODUCTION, LOGISTICS AND PAYMENT                  ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬   3D SERVICE      32 BAY     GCC WEBWALLET▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ •• C-CEX EXCHANGE  FACEBOOK  TWITTER  TELEGRAM▬▬▬  MEDIUM  ▬▬▬
daybyter
Legendary

Offline

Activity: 965
Merit: 1000

 January 22, 2012, 01:53:59 PM

Uh....not really...

I just write some code and want to keep it portable, so compiling it for the c64 is a good test.

Problem is the poor performance of the old breadbox. It computes about 2 hashes per second at the moment...

Maybe a c64 cluster would be a solution...

But exactly the same code without any modifications, 32-bit optimizations, multithreading etc. computes about 2 million hashed on my Ahtlon 840 3,2 GHz.

My actual target platform are cheap GPUs. Older NVidia cards and such. Maybe the idle times of such machines could be used for a low performance pool, or so...

Ciao,
Andreas

xqus
Full Member

Offline

Activity: 173
Merit: 100

 January 22, 2012, 02:50:58 PM

Maybe a c64 cluster would be a solution...

Maybe if you get all c64 ever sold in your cluster..

PGP fingerprint: B17233A1 || Bitrated user: xqus ≡ Free trust agent || LocalBitcoins ≡ Buy bitcoins locally
Wallet and Exchange security ≡ A security overview of wallets and exchanges. (forum thread)
 Pages: [1]