Author

Topic: nBits into Difficulty in PHP? (Read 1200 times)

legendary
Activity: 1456
Merit: 1083
I may write code in exchange for bitcoins.
June 30, 2015, 12: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.
sr. member
Activity: 412
Merit: 287
June 30, 2015, 07:15:32 AM
#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
legendary
Activity: 1456
Merit: 1083
I may write code in exchange for bitcoins.
June 29, 2015, 11:28:39 PM
#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.
sr. member
Activity: 412
Merit: 287
June 29, 2015, 08:47:08 PM
#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..
legendary
Activity: 1456
Merit: 1083
I may write code in exchange for bitcoins.
June 29, 2015, 08:26:49 PM
#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!
newbie
Activity: 1
Merit: 0
June 27, 2015, 09:43:05 PM
#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?

Jump to: