X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=contrib%2Fchash%2Fchash.php;h=ed9265f38afb3585d976d65106998c3b780650ee;hb=2ee664c52d9a8f3bd882212b19057a7c1096b7cf;hp=a1e91c28c568cc741185092e906a2af57598c1fb;hpb=f9eece01f1c8c387e82daef7a0599b7ab320e1b1;p=core.git diff --git a/contrib/chash/chash.php b/contrib/chash/chash.php index a1e91c28..ed9265f3 100644 --- a/contrib/chash/chash.php +++ b/contrib/chash/chash.php @@ -1,6 +1,18 @@ array()); + /** * Continued-hashing * @@ -18,22 +30,31 @@ error_reporting(E_ALL | E_STRICT); */ function hashString ($str) { // Calculate strong hash from given string - $hash = mhash(MHASH_RIPEMD320, $str); + $hash = mhash(HASH_ALGO, $str); // Return it hexadecimal-encoded return bin2hex($hash); } /** - * Double-hashes given string. This is done by hashing the given string and + * Multiple-hashes given string. This is done by hashing the given string and * then hashing the generated hash again. * * @param $str The string to be hashed 4 times * @return $hash The generated hash */ -function doubleHashString ($str) { +function multipleHashString ($str) { + // One less to go (see below) + $totalHashes = $GLOBALS['cycles'] - 1; + // Generate hash from given hash - $hash = hashString(hashString($str)); + $hash = hashString($str); + + // Now over-hash it + for ($idx = 0; $idx < $totalHashes; $idx++) { + // Over-hash the given hash + $hash = hashString($hash); + } // END - for // Return it return $hash; @@ -142,40 +163,96 @@ function padHex ($num) { return $hex; } +/** + * Calculates sum from given hash + * + * @param $hash Hash to calculate sum from + * @return $sum Sum from given hash + */ +function calculateSumFromHash ($hash) { + // Everything starts with zero ... + $sum = 0; + + // Loop through hash + for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) { + // And add it + $sum = $sum + (hexdec(substr($hash, $idx, 2)) * $idx & 256); + } // END - for + + // And return it + return $sum; +} + +/** + * Calculates new nonce + * + * @param $nonce Old nonce to be used + * @return $newNonce New nonce + */ +function calculateNonce ($nonce) { + // Linear incrementation + $newNonce = $nonce + NONCE_INCREMENT; + + // Return new value + return $newNonce; +} + +/** + * Writes/flushes check-point file + * + * @param $nonce Nonce + * @param $modulaHash Modula hash (or hash to save) + * @return void + */ +function flushCheckPointFile ($nonce, $modulaHash) { + // Display message + print ('FLUSHING: Writing ' . count($GLOBALS['found_hashes']) . ' blocks ...' . PHP_EOL); + + // Start timer + $timer = microtime(TRUE); + + // Flush data + file_put_contents(CHECK_POINT, $GLOBALS['total_blocks'] . ':' . $GLOBALS['total_hashes'] . ':' . $GLOBALS['cycles'] . ':' . base64_encode($nonce) . ':' . $modulaHash . ':' . base64_encode(gzcompress(serialize($GLOBALS['found_hashes'])))); + + // Set time + $GLOBALS['time_flush'] = microtime(TRUE); + print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL); +} + /* * Calculate "genesis" hashes, please note that these "genesis strings" are now * known to the public as you can read them here in source code and therefore I * will not use them for the real genesis hashes. */ -$hashes = array( +$gensisHashes = array( // A famous quote from Deus Ex 2 - Invisible War - doublehashString('"Informations must be free." - AI Helios'), + multiplehashString('"Informations must be free." - AI Helios from Deus Ex'), // My name + URL of my first StatusNet instance - doubleHashString('Roland Haeder, http://status.mxchange.org'), + multipleHashString('Roland Haeder, https://status.mxchange.org'), // A famous quote from Linus Torwalds - doubleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'), + multipleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'), // Possible truth ;-) - doubleHashString('September 11 is a big lie.'), + multipleHashString('September 11 is a big lie.'), // GNU is not Uni* - doubleHashString('GNU is Not Uni*.'), + multipleHashString('GNU is Not Uni*.'), // WINE is not an emulator - doubleHashString('WINE Is Not an Emulator.'), + multipleHashString('WINE Is Not an Emulator.'), // FlightGear - Fly free! - doubleHashString('FlightGear - Fly free!'), + multipleHashString('FlightGear - Fly free!'), // Linus Torwalds Quote - doubleHashString('Your code is shit.. your argument is shit.'), + multipleHashString('Your code is shit.. your argument is shit.'), ); // Calculate "modula hash" from 1st/4th and 2nd/3rd $modulaHashes = array( // "Block" 0 - modulaHash($hashes[0], $hashes[3]), - modulaHash($hashes[1], $hashes[2]), + modulaHash($gensisHashes[0], $gensisHashes[3]), + modulaHash($gensisHashes[1], $gensisHashes[2]), // "Block" 1 - modulaHash($hashes[4], $hashes[7]), - modulaHash($hashes[5], $hashes[6]), + modulaHash($gensisHashes[4], $gensisHashes[7]), + modulaHash($gensisHashes[5], $gensisHashes[6]), ); // Calculate "sqrt hash" @@ -185,13 +262,187 @@ $sqrtHashes = array( ); // Calulcate modula hash -$modulaHash = modulaHash($sqrtHashes[0], $sqrtHashes[1]); +$modulaHash = multipleHashString(modulaHash($sqrtHashes[0], $sqrtHashes[1])); + +// This is also the "genesis hash" +$genesisHash = $modulaHash; // Output results -print ('hashes=' . print_r($hashes, TRUE)); +print ('hashes=' . print_r($gensisHashes, TRUE)); print ('modulaHashes=' . print_r($modulaHashes, TRUE)); print ('sqrtHashes=' . print_r($sqrtHashes, TRUE)); print ('modulaHash=' . $modulaHash . PHP_EOL); +// Total reward + hashes +$totalReward = 0; +$GLOBALS['total_hashes'] = 0; +$GLOBALS['total_blocks'] = 0; + +// Is the check point there? +if (is_readable(CHECK_POINT)) { + // Then load it + $checkPoint = file_get_contents(CHECK_POINT); + + // Explode it + $data = explode(':', $checkPoint); + + // Assert on count + assert(count($data) == 6); + + // 1st element is nonce, 2nd hash, 3rd found hashes + $GLOBALS['total_blocks'] = $data[0]; + $GLOBALS['total_hashes'] = $data[1]; + $GLOBALS['cycles'] = intval($data[2]); + $nonce = base64_decode($data[3]); + $modulaHash = $data[4]; + $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = unserialize(gzuncompress(base64_decode($data[5]))); +} else { + // Create nonce (small) + $nonce = 1 / mt_rand(); +} + +// Output again +print ('modulaHash=' . $modulaHash . PHP_EOL); +print ('nonce=' . $nonce . PHP_EOL); +print ('found=' . count($GLOBALS['found_hashes'][$GLOBALS['total_blocks']]) . PHP_EOL); + +// Start "mining" +while (TRUE) { + // Init hash-per-block counter and hashrate + $hashesPerBlock = 0; + $hashrate = 0; + + // Wait for BLOCK_SIZE iterations (= found hashes). This is one block + $timeBlock = microtime(TRUE); + $timeDisplay = $timeBlock; + $GLOBALS['time_flush'] = $timeBlock; + + // Time waited for a good block again (no iteration) + $timeBadHashes = 0; + + while (count($GLOBALS['found_hashes'][$GLOBALS['total_blocks']]) <= BLOCK_SIZE) { + // Create hash from modulaHash ("genesis hash") and nonce + $nonceHash = multipleHashString($modulaHash . $nonce); + + // Calculate sums + $sumNonce = calculateSumFromHash($nonceHash); + $sumModula = calculateSumFromHash($modulaHash); + + // Init counter + $iter = 0; + $iterSecond = 0; + + // Now start the "mining" ... + $timeHash = microtime(TRUE); + while ($sumNonce >= $sumModula) { + // Calculate new nonce + $nonce = calculateNonce($nonce); + + // And hash again + $nonceHash = multipleHashString($modulaHash . $nonce); + + // Calculate sums + $sumNonce = calculateSumFromHash($nonceHash); + + // Time spend in loop + $testTime = abs(microtime(TRUE) - $timeDisplay); + + // Calculate hashrate/sec + $hashrate = 1 / $testTime * $iterSecond * $GLOBALS['cycles']; + + // Only every second + if ($testTime >= 1) { + // Display hash rate + print ('hashrate=' . round($hashrate) . ' hashes/sec,iterSecond=' . $iterSecond . ' iterations/sec' . PHP_EOL); + + // Reset timer + $timeDisplay = microtime(TRUE); + $iterSecond = 0; + } // END - if + + // Time spend from last flush + $testTime = abs(microtime(TRUE) - $GLOBALS['time_flush']); + + // Only once per 10 seconds + if ($testTime >= 10) { + // Flush check-point file + flushCheckPointFile($nonce, $modulaHash); + } // END - if + + // Next round + $iter++; + $iterSecond++; + //print ('nonce=' . $nonce . ',iter=' . $iter . PHP_EOL); + //print ('nonceHash=' . $nonceHash . PHP_EOL); + //print ('sumNonce=' . $sumNonce . PHP_EOL); + //print ('sumModula=' . $sumModula . PHP_EOL); + } // END - while + + // If the iteration is zero, then no hash is found + if ($iter == 0) { + // Bad hash found + $timeBadHashes += abs(microtime(TRUE) - $timeHash); + + // And next round + //print('BAD:nonce=' . $nonce . PHP_EOL); + + // Nothing found, so calculate new nonce + $nonce = calculateNonce($nonce); + continue; + } // END - if + + // Add amount of hashes per block (multiple-hash) + $hashesPerBlock += $iter * $GLOBALS['cycles'] + $GLOBALS['cycles']; + + // Push found hash + array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], array( + 'modula_hash' => $modulaHash, + 'genesis_hash' => $genesisHash, + 'nonce' => $nonce, + 'iter' => $iter, + 'hashes_block' => $hashesPerBlock, + 'nonce_hash' => $nonceHash + )); + + // Found hash: + print ('FOUND: nonceHash=' . $nonceHash . ',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL); + + // Flush check-point file after new hash is found + flushCheckPointFile($nonce, $nonceHash); + + // Use nonceHash as next modula hash + $modulaHash = $nonceHash; + } // END - while + + // Time taken for one block + $timeBlock = abs(microtime(TRUE) - $timeBlock); + + // Calculate reward + $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000; + print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL); + + // Block completed + $GLOBALS['total_hashes'] += $hashesPerBlock; + $GLOBALS['total_blocks']++; + $hashesPerBlock = 0; + + // Init next block + $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = array(); + + // Calculate new nonce + $nonce = calculateNonce($nonce); + + // Add reward to total + $totalReward += $reward; + + // Calculate average block value + $blockValue = $totalReward / $GLOBALS['total_blocks'] * $GLOBALS['total_hashes'] / (BLOCK_SIZE * $GLOBALS['total_blocks']); + + // Calculate reward per hour (= 3600 seconds) + $rewardPerHour = $totalReward / abs(microtime(TRUE) - START_TIME) * 3600; + + print ('totalReward=' . $totalReward . ',blockValue=' . $blockValue . ',rewardPerHour=' . $rewardPerHour . PHP_EOL); +} // END - while + // [EOF] ?>