2 error_reporting(E_ALL | E_STRICT);
4 define('HASH_ALGO', MHASH_RIPEMD320);
5 define('BLOCK_SIZE', 100);
6 define('NONCE_INCREMENT', 0.0000000000000001);
7 define('START_TIME', microtime(TRUE));
10 $foundHashes = array();
15 * @author Roland Haeder <roland@mxchange.org>
16 * @copyright Copyright (c) 2013 by Core Developer Team
17 * @license See LICENSE (public-domain)
21 * Calculates a simple but stronger hash from given string. No salts are being
24 * @param $str The string to be hashed
25 * @return $hash The hash from string $str
27 function hashString ($str) {
28 // Calculate strong hash from given string
29 $hash = mhash(HASH_ALGO, $str);
31 // Return it hexadecimal-encoded
32 return bin2hex($hash);
36 * Double-hashes given string. This is done by hashing the given string and
37 * then hashing the generated hash again.
39 * @param $str The string to be hashed 4 times
40 * @return $hash The generated hash
42 function doubleHashString ($str) {
43 // Generate hash from given hash
44 $hash = hashString(hashString($str));
51 * Calculates a "modula-hash" based given two hashes.
53 * @param $hash1 Hash 1
54 * @param $hash2 Hash 2
56 function modulaHash ($hash1, $hash2) {
57 // Both must have same length
58 assert(strlen($hash1) === strlen($hash2));
63 // "Walk" trough first hash and get every 2 byte of both hashes
64 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
68 // Get both hash parts and convert to ASCII number
69 $part1 = hexdec(substr($hash1, $idx, 2));
70 $part2 = hexdec(substr($hash2, $idx, 2));
73 * If part1 is larget part2, part1 is divident and vise-versa. But don't do it
76 if (($part1 > $part2) && ($part2 > 0)) {
77 // 'part1' is larger than 'part2'
78 $mod = $part1 % $part2;
79 } elseif (($part1 < $part2) && ($part1 > 0)) {
80 // 'part2' is larger than 'part1'
81 $mod = $part2 % $part1;
84 // "Invert" the result against 255
87 // Encode to hex, pre-pad it with zeros and add to new hash
88 $modulaHash .= padHex($mod);
91 // Modula hash must have same length as input hash
92 assert(strlen($modulaHash) === strlen($hash1));
99 * Calculates a "sqrt-hash" based given two hashes and single-hash it
101 * @param $hash1 Hash 1
102 * @param $hash2 Hash 2
104 function sqrtHash ($hash1, $hash2) {
105 // Both must have same length
106 assert(strlen($hash1) === strlen($hash2));
111 // "Walk" trough first hash and get every 2 byte of both hashes
112 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
116 // Get both hash parts and convert to ASCII number
117 $part1 = hexdec(substr($hash1, $idx, 2));
118 $part2 = hexdec(substr($hash2, $idx, 2));
120 // Calculate square root of both parts being multiplied and round up, then "invert" it against 255
121 $sqrt = intval(255 - ceil(sqrt($part1 * $part2)));
123 // Encode to hex, pre-pad it with zeros and add to new hash
124 $sqrtHash .= padHex($sqrt);
127 // "sqrt-hash" must have same length as input hash
128 assert(strlen($sqrtHash) === strlen($hash1));
130 // Hash reversed "sqrt-hash" again and return it
131 return hashString(strrev($sqrtHash));
135 * Converts a number between 0 and 255 into a zero-padded hexadecimal string
137 * @param $num Number between 0 and 255
138 * @return $hex Hexadecimal string, padded with zeros
140 function padHex ($num) {
141 // Must be a integer number and between 0 and 255
142 assert(is_int($num));
147 $hex = str_pad(dechex($num), 2, '0', STR_PAD_LEFT);
154 * Calculates sum from given hash
156 * @param $hash Hash to calculate sum from
157 * @return $sum Sum from given hash
159 function calculateSumFromHash ($hash) {
160 // Everything starts with zero ...
164 for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) {
166 $sum = $sum + (hexdec(substr($hash, $idx, 2)) * $idx & 256);
174 * Calculates new nonce
176 * @param $nonce Old nonce to be used
177 * @return $newNonce New nonce
179 function calculateNonce ($nonce) {
180 // Linear incrementation
181 $newNonce = $nonce + NONCE_INCREMENT;
188 * Calculate "genesis" hashes, please note that these "genesis strings" are now
189 * known to the public as you can read them here in source code and therefore I
190 * will not use them for the real genesis hashes.
193 // A famous quote from Deus Ex 2 - Invisible War
194 doublehashString('"Informations must be free." - AI Helios from Deus Ex'),
195 // My name + URL of my first StatusNet instance
196 doubleHashString('Roland Haeder, https://status.mxchange.org'),
197 // A famous quote from Linus Torwalds
198 doubleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'),
199 // Possible truth ;-)
200 doubleHashString('September 11 is a big lie.'),
203 doubleHashString('GNU is Not Uni*.'),
204 // WINE is not an emulator
205 doubleHashString('WINE Is Not an Emulator.'),
206 // FlightGear - Fly free!
207 doubleHashString('FlightGear - Fly free!'),
208 // Linus Torwalds Quote
209 doubleHashString('Your code is shit.. your argument is shit.'),
212 // Calculate "modula hash" from 1st/4th and 2nd/3rd
213 $modulaHashes = array(
215 modulaHash($hashes[0], $hashes[3]),
216 modulaHash($hashes[1], $hashes[2]),
219 modulaHash($hashes[4], $hashes[7]),
220 modulaHash($hashes[5], $hashes[6]),
223 // Calculate "sqrt hash"
225 sqrtHash($modulaHashes[0], $modulaHashes[1]),
226 sqrtHash($modulaHashes[2], $modulaHashes[3])
229 // Calulcate modula hash
230 $modulaHash = doubleHashString(modulaHash($sqrtHashes[0], $sqrtHashes[1]));
233 print ('hashes=' . print_r($hashes, TRUE));
234 print ('modulaHashes=' . print_r($modulaHashes, TRUE));
235 print ('sqrtHashes=' . print_r($sqrtHashes, TRUE));
236 print ('modulaHash=' . $modulaHash . PHP_EOL);
238 // Total reward + hashes
243 // Create nonce (small)
244 $nonce = 1 / mt_rand();
247 // Init hash-per-block counter and hashrate
251 // Wait for BLOCK_SIZE iterations (= found hashes). This is one block
252 $timeBlock = microtime(TRUE);
253 $timeDisplay = $timeBlock;
255 // Time waited for a good block again (no iteration)
258 while (count($foundHashes) <= BLOCK_SIZE) {
259 // Create hash from modulaHash ("genesis hash") and nonce
260 $nonceHash = doubleHashString($modulaHash . $nonce);
263 $sumNonce = calculateSumFromHash($nonceHash);
264 $sumModula = calculateSumFromHash($modulaHash);
270 // Now start the "mining" ...
271 $timeHash = microtime(TRUE);
272 while ($sumNonce >= $sumModula) {
273 // Calculate new nonce
274 $nonce = calculateNonce($nonce);
277 $nonceHash = doubleHashString($modulaHash . $nonce);
280 $sumNonce = calculateSumFromHash($nonceHash);
282 // Time spend in loop
283 $testTime = abs(microtime(TRUE) - $timeDisplay);
285 // Calculate hashrate/sec
286 $hashrate = 1 / $testTime * $iterSecond * 2;
289 if ($testTime >= 1) {
291 print ('hashrate=' . $hashrate . ' hashes/sec,iterSecond=' . $iterSecond . ' iterations/sec' . PHP_EOL);
294 $timeDisplay = microtime(TRUE);
301 //print ('nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
302 //print ('nonceHash=' . $nonceHash . PHP_EOL);
303 //print ('sumNonce=' . $sumNonce . PHP_EOL);
304 //print ('sumModula=' . $sumModula . PHP_EOL);
307 // If the iteration is zero, then no hash is found
310 $timeBadHashes += abs(microtime(TRUE) - $timeHash);
313 //print('BAD:nonce=' . $nonce . PHP_EOL);
315 // Nothing found, so calculate new nonce
316 $nonce = calculateNonce($nonce);
320 // Add amount of hashes per block (double-hash)
321 $hashesPerBlock += $iter * 2 + 2;
322 array_push($foundHashes, $nonceHash);
325 print ('FOUND: nonceHash=' . $nonceHash .',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
327 // Use nonceHash as next modula hash
328 $modulaHash = $nonceHash;
331 // Time taken for one block
332 $timeBlock = abs(microtime(TRUE) - $timeBlock);
335 $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000;
336 print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
339 $totalHashes += $hashesPerBlock;
342 $foundHashes = array();
344 // Calculate new nonce
345 $nonce = calculateNonce($nonce);
347 // Add reward to total
348 $totalReward += $reward;
350 // Calculate average block value
351 $blockValue = $totalReward / $totalBlocks * $totalHashes / (BLOCK_SIZE * $totalBlocks);
353 // Calculate reward per hour (= 3600 seconds)
354 $rewardPerHour = $totalReward / abs(microtime(TRUE) - START_TIME) * 3600;
356 print ('totalReward=' . $totalReward . ',blockValue=' . $blockValue . ',rewardPerHour=' . $rewardPerHour . PHP_EOL);