Bitcoin Forum
December 15, 2024, 05:38:29 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: nBits into Difficulty in PHP?  (Read 1192 times)
LittleDuck (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
June 28, 2015, 02:43:05 AM
 #1

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 Sad


$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?

tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1081


I may write code in exchange for bitcoins.


View Profile
June 30, 2015, 01:26:49 AM
 #2

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!
fbueller
Sr. Member
****
Offline Offline

Activity: 412
Merit: 287


View Profile
June 30, 2015, 01:47:08 AM
Last edit: June 30, 2015, 12:14:05 PM by fbueller
Merited by ABCbits (1)
 #3

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');
$diff = new \BitWasp\Bitcoin\Chain\Difficulty(new \BitWasp\Bitcoin\Math\Math());

echo $diff->getDifficulty($difficultyBits) . "\n";

It's not perfect, and won't handle overflow or negative bits, but should work for most cases..

Bitwasp Developer.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1081


I may write code in exchange for bitcoins.


View Profile
June 30, 2015, 04:28:39 AM
 #4

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');
$diff = new \BitWasp\Bitcoin\Chain\Difficulty(new \BitWasp\Bitcoin\Math\Math());

echo $diff->getDifficulty($difficultyBits) . "\n";

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.
fbueller
Sr. Member
****
Offline Offline

Activity: 412
Merit: 287


View Profile
June 30, 2015, 12:15:32 PM
 #5

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

Bitwasp Developer.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1081


I may write code in exchange for bitcoins.


View Profile
June 30, 2015, 05:57:35 PM
 #6

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!