Title: nBits into Difficulty in PHP? Post by: LittleDuck on June 28, 2015, 02:43:05 AM Looking at how the difficulty is being calculated https://en.bitcoin.it/wiki/Difficulty and trying to port / implement nBits to difficulty in PHP but not having much luck :(
$nBits = 404103235; $difficulty = calculate_difficulty($nBits); echo "difficulty = $difficulty" . PHP_EOL; // 404103235 nBits should be difficulty: 49,692,386,354.894 function calculate_target($nBits){ $shift = 8 * ((($nBits >> 24) & 0xff) - 3); echo $shift . PHP_EOL; $bits = $nBits & 0x7fffff; echo $bits . PHP_EOL; $sign = (nBits & 0x800000) ? -1 : 1; echo $sign . PHP_EOL; return ($shift >= 0) ? $sign * ($bits << $shift) : $sign * ($bits >> -$shift); } function target_to_difficulty($target){ return ((1 << 224) - 1) * 1000 / ($target + 1) / 1000.0; } function calculate_difficulty($nBits){ return target_to_difficulty(calculate_target($nBits)); } Output: 168 1450051 1 difficulty = 2.6938707666768E-9 Expecting something like: 168 1450051 1 542528489142608155505707877213460200687386787807972294656 I guess I need something like a BigNum in C/C++? Can this be done in PHP? Any ideas? Title: Re: nBits into Difficulty in PHP? Post by: tspacepilot on June 30, 2015, 01:26:49 AM I can probably help you with the php, but I'm not sure what you mean about "nbits". Can you provide me with the backround on nbits or at least a link?
EDIT: it does look like some kind of overflow problem. This stackoverflow post http://stackoverflow.com/questions/8647125/using-long-int-in-php suggests that you ought to use a package called BC Math for long long ints http://php.net/manual/en/book.bc.php Hope this helps! Title: Re: nBits into Difficulty in PHP? Post by: fbueller on June 30, 2015, 01:47:08 AM I think this will do what you need - https://github.com/bit-wasp/bitcoin-php/blob/src/Chain/Difficulty.php. Use composer to install `bitwasp/bitcoin`.
Edit: correct link is https://github.com/bit-wasp/bitcoin-php/blob/master/src/Chain/Difficulty.php Code: $difficultyBits = \BitWasp\Buffertools\Buffer::hex('1d00ffff'); It's not perfect, and won't handle overflow or negative bits, but should work for most cases.. Title: Re: nBits into Difficulty in PHP? Post by: tspacepilot on June 30, 2015, 04:28:39 AM I think this will do what you need - https://github.com/bit-wasp/bitcoin-php/blob/src/Chain/Difficulty.php. Use composer to install `bitwasp/bitcoin`. Code: $difficultyBits = \BitWasp\Buffertools\Buffer::hex('1d00ffff'); It's not perfect, and won't handle overflow or negative bits, but should work for most cases.. The url you linked to gives me a 404. And is it really okay to use path literals with backslashes in PHP like that? UNIX paths are /, but in most programming languages I've used, path strings need to be quoted and if you're doing package paths it's usually a dot or a colon. Just curious as I don't use php all the time, I tend to write in python or perl for scripting stuff. Title: Re: nBits into Difficulty in PHP? Post by: fbueller on June 30, 2015, 12:15:32 PM Edited with the correct link: https://github.com/bit-wasp/bitcoin-php/blob/master/src/Chain/Difficulty.php
Yeah it's normal, those indicate namespaces. You could use the appropriate 'use' statement to just use `new Difficulty()`to keep things tidy Title: Re: nBits into Difficulty in PHP? Post by: tspacepilot on June 30, 2015, 05:57:35 PM Edited with the correct link: https://github.com/bit-wasp/bitcoin-php/blob/master/src/Chain/Difficulty.php Yeah it's normal, those indicate namespaces. You could use the appropriate 'use' statement to just use `new Difficulty()`to keep things tidy I looked a bit at the php documentation and it seems that those namespaces are translated into file paths by the php interpreter. If I understood it correctly, those backslashed paths would fail on a standard Unix-like machine (because \ is an escape character). On the other hand, I believe that Windows these days recognizes the standard path delimter /. So maybe forward slashes are going to be more portable. |