Bitcoin Forum
May 06, 2024, 06:47:09 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Calculate Hash Target and Probability in PHP  (Read 10577 times)
mizerydearia (OP)
Hero Member
*****
Offline Offline

Activity: 574
Merit: 507



View Profile
July 17, 2010, 04:37:09 AM
Last edit: July 17, 2010, 10:42:22 PM by mizerydearia
 #1

I am trying to prepare PHP code to calculate exact value for Hash Target similar as to displayed at http://www.alloscomp.com/bitcoin/calculator.php (currently: 1.48501965484E+65).

Can someone help me to refine my code?

http://jsonrpcphp.org/?page=download&lang=en

Calculate current hash target:
Code:
<?
header("Content-type: text/plain");
require_once 'jsonRPCClient.php';
$data=new jsonRPCClient('http://127.0.0.1:8332');
$difficulty = floatval($data->getdifficulty());
//     $a      /         $b
// (2^256 - 1) / (2^32 * difficulty)

bcscale(256);

$a = bcsub(bcpow(2,256),1);
//$a = gmp_strval(gmp_sub(gmp_pow(2,256),1));

$b = bcmul(bcpow(2,32),$difficulty);
//$b = pow(2,32) * $difficulty;

bcscale(0);

$target = bcdiv($a,$b);
//$target = gmp_strval(gmp_div($a,$b));
//$target = bcdiv($a,$b);
//$target = "148504231478890412392775945444335243545681910455595839046778120430";
//$target = "148504231478000000000000000000000000000000000000000000000000000000";
//$target = "148501965484000000000000000000000000000000000000000000000000000000";

//000000000168fd00000000000000000000000000000000000000000000000000
//         168fcfffffee48119ddbfdc811138960d70605cc300000000000000

$targethex = gmp_strval(gmp_sub($target,0),16);

echo "Current Hash Target: Dec($target)    Hex($targethex)";
?>

Calculate probability:
Code:
<?
bcscale(256);
$pph = bcdiv($target,bcpow(2,256)); // probability per hash
if (isset($_GET["r"]) && $_GET["r"] != "") { // user-defined rate
$rate = $_GET["r"];
if (is_numeric($rate)) {
function humantime($secs) {
if ($secs<0) return false;
$m = (int)($secs / 60); $s = $secs % 60; $s = ($s <= 9) ? "0$s" : $s;
$h = (int)($m / 60); $m = $m % 60; $m = ($m <= 9) ? "0$m" : $m;
$d = (int)($h / 24); $h = $h % 24;
return $d."d $h:$m:$s";
}
$pps = bcdiv(bcmul($target,1000),bcpow(2,256)); // probability per second
bcscale(16);
$ppr = bcmul($pps,$rate);
$etaAvg = humantime(bcdiv(1,$ppr));
$eta25 = humantime(bcdiv(-log(.75),$ppr));
$eta50 = humantime(bcdiv(-log(.5),$ppr));
$eta75 = humantime(bcdiv(-log(.25),$ppr));
$eta95 = humantime(bcdiv(-log(.05),$ppr));
$eta99 = humantime(bcdiv(-log(.01),$ppr));
echo "ProbabilityPerSecond($ppr) Avg($etaAvg) 25%($eta25) 50%($eta50) 75%($eta75) 95%($eta95) 99%($eta99)";
} else echo "This requires either no argument or a numeric argument representing khash/sec.";
}
else {
bcscale(32);
$pph = bcmul($pph,1);
echo "ProbabilityPerHash($pph)";
}
?>
1714978029
Hero Member
*
Offline Offline

Posts: 1714978029

View Profile Personal Message (Offline)

Ignore
1714978029
Reply with quote  #2

1714978029
Report to moderator
1714978029
Hero Member
*
Offline Offline

Posts: 1714978029

View Profile Personal Message (Offline)

Ignore
1714978029
Reply with quote  #2

1714978029
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
Insti
Sr. Member
****
Offline Offline

Activity: 294
Merit: 252


Firstbits: 1duzy


View Profile
July 17, 2010, 08:53:58 AM
 #2

The difficulty value is derived from the current target.
You can find the current target by "grep"ing 'After' in the debug.log


Edit: Oh ok, you already know that, nevermind.
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5194
Merit: 12972


View Profile
July 17, 2010, 11:41:22 PM
Last edit: July 18, 2010, 02:22:57 AM by theymos
 #3

This is how I do probability per hash:
Code:
$hash=$hextarget;
$hash=strtoupper($hash);
$probability=`echo "obase=10;ibase=16;scale=37;$hash/FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |bc`;
(The tickmarks execute the command on the shell.)

Then you do 1/$probability to get average number of hashes. Divide this by the number of hashes you're getting per second to get the number of seconds.

You can calculate the target like this:
Code:
26959535291011309493156476344723991336010898738574164086137773096960/difficulty
"Difficulty" is the number returned by getDifficulty. (The huge number is the maximum target, which getDifficulty is based on.) This calculation is not exact.

I search debug.log to get the target, but the result of the last retarget seems to be less accurate than the calculation above. I'm not sure why this is. Maybe the code changed. (Edit: I now think that this is just an artifact from the "nBits" decompression.)

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
lachesis
Full Member
***
Offline Offline

Activity: 210
Merit: 104


View Profile
July 23, 2010, 01:15:51 AM
 #4

"Difficulty" is the number returned by getDifficulty. (The huge number is the maximum target, which getDifficulty is based on.) This calculation is not exact.

I search debug.log to get the target, but the result of the last retarget seems to be less accurate than the calculation above. I'm not sure why this is. Maybe the code changed. (Edit: I now think that this is just an artifact from the "nBits" decompression.)
The difficulty is based on the *minimum* target, and this target is generated rather inexactly as 00000000ffff000000000000000... (eight 0s, four Fs, 0 to taste Cheesy)

Bitcoin Calculator | Scallion | GPG Key | WoT Rating | 1QGacAtYA7E8V3BAiM7sgvLg7PZHk5WnYc
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5194
Merit: 12972


View Profile
July 23, 2010, 02:25:42 AM
 #5

The difficulty is based on the *minimum* target, and this target is generated rather inexactly as 00000000ffff000000000000000... (eight 0s, four Fs, 0 to taste Cheesy)

No. That target (which I included in decimal in my second formula) is the maximum allowed target. The target is lowered to increase difficulty. The highest target is the lowest difficulty.

This is because the probability of winning with one hash is target/max. Reducing target reduces the probability of winning.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
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!