2 error_reporting(E_ALL | E_STRICT);
4 define('HASH_ALGO', MHASH_RIPEMD320);
5 define('BLOCK_SIZE', 1000);
6 define('NONCE_INCREMENT', 0.00001);
7 define('START_TIME', microtime(TRUE));
12 * @author Roland Haeder <roland@mxchange.org>
13 * @copyright Copyright (c) 2013 by Core Developer Team
14 * @license See LICENSE (public-domain)
18 * Calculates a simple but stronger hash from given string. No salts are being
21 * @param $str The string to be hashed
22 * @return $hash The hash from string $str
24 function hashString ($str) {
25 // Calculate strong hash from given string
26 $hash = mhash(HASH_ALGO, $str);
28 // Return it hexadecimal-encoded
29 return bin2hex($hash);
33 * Double-hashes given string. This is done by hashing the given string and
34 * then hashing the generated hash again.
36 * @param $str The string to be hashed 4 times
37 * @return $hash The generated hash
39 function doubleHashString ($str) {
40 // Generate hash from given hash
41 $hash = hashString(hashString($str));
48 * Calculates a "modula-hash" based given two hashes.
50 * @param $hash1 Hash 1
51 * @param $hash2 Hash 2
53 function modulaHash ($hash1, $hash2) {
54 // Both must have same length
55 assert(strlen($hash1) === strlen($hash2));
60 // "Walk" trough first hash and get every 2 byte of both hashes
61 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
65 // Get both hash parts and convert to ASCII number
66 $part1 = hexdec(substr($hash1, $idx, 2));
67 $part2 = hexdec(substr($hash2, $idx, 2));
70 * If part1 is larget part2, part1 is divident and vise-versa. But don't do it
73 if (($part1 > $part2) && ($part2 > 0)) {
74 // 'part1' is larger than 'part2'
75 $mod = $part1 % $part2;
76 } elseif (($part1 < $part2) && ($part1 > 0)) {
77 // 'part2' is larger than 'part1'
78 $mod = $part2 % $part1;
81 // "Invert" the result against 255
84 // Encode to hex, pre-pad it with zeros and add to new hash
85 $modulaHash .= padHex($mod);
88 // Modula hash must have same length as input hash
89 assert(strlen($modulaHash) === strlen($hash1));
96 * Calculates a "sqrt-hash" based given two hashes and single-hash it
98 * @param $hash1 Hash 1
99 * @param $hash2 Hash 2
101 function sqrtHash ($hash1, $hash2) {
102 // Both must have same length
103 assert(strlen($hash1) === strlen($hash2));
108 // "Walk" trough first hash and get every 2 byte of both hashes
109 for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
113 // Get both hash parts and convert to ASCII number
114 $part1 = hexdec(substr($hash1, $idx, 2));
115 $part2 = hexdec(substr($hash2, $idx, 2));
117 // Calculate square root of both parts being multiplied and round up, then "invert" it against 255
118 $sqrt = intval(255 - ceil(sqrt($part1 * $part2)));
120 // Encode to hex, pre-pad it with zeros and add to new hash
121 $sqrtHash .= padHex($sqrt);
124 // "sqrt-hash" must have same length as input hash
125 assert(strlen($sqrtHash) === strlen($hash1));
127 // Hash reversed "sqrt-hash" again and return it
128 return hashString(strrev($sqrtHash));
132 * Converts a number between 0 and 255 into a zero-padded hexadecimal string
134 * @param $num Number between 0 and 255
135 * @return $hex Hexadecimal string, padded with zeros
137 function padHex ($num) {
138 // Must be a integer number and between 0 and 255
139 assert(is_int($num));
144 $hex = str_pad(dechex($num), 2, '0', STR_PAD_LEFT);
151 * Calculates sum from given hash
153 * @param $hash Hash to calculate sum from
154 * @return $sum Sum from given hash
156 function calculateSumFromHash ($hash) {
157 // Everything starts with zero ...
161 for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) {
163 $sum = $sum + (hexdec(substr($hash, $idx, 2)) * $idx & 256);
171 * Calculates new nonce
173 * @param $nonce Old nonce to be used
174 * @return $newNonce New nonce
176 function calculateNonce ($nonce) {
177 // Linear incrementation
178 $newNonce = $nonce + NONCE_INCREMENT;
185 * Calculate "genesis" hashes, please note that these "genesis strings" are now
186 * known to the public as you can read them here in source code and therefore I
187 * will not use them for the real genesis hashes.
190 // A famous quote from Deus Ex 2 - Invisible War
191 doublehashString('"Informations must be free." - AI Helios from Deus Ex'),
192 // My name + URL of my first StatusNet instance
193 doubleHashString('Roland Haeder, https://status.mxchange.org'),
194 // A famous quote from Linus Torwalds
195 doubleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'),
196 // Possible truth ;-)
197 doubleHashString('September 11 is a big lie.'),
200 doubleHashString('GNU is Not Uni*.'),
201 // WINE is not an emulator
202 doubleHashString('WINE Is Not an Emulator.'),
203 // FlightGear - Fly free!
204 doubleHashString('FlightGear - Fly free!'),
205 // Linus Torwalds Quote
206 doubleHashString('Your code is shit.. your argument is shit.'),
209 // Calculate "modula hash" from 1st/4th and 2nd/3rd
210 $modulaHashes = array(
212 modulaHash($hashes[0], $hashes[3]),
213 modulaHash($hashes[1], $hashes[2]),
216 modulaHash($hashes[4], $hashes[7]),
217 modulaHash($hashes[5], $hashes[6]),
220 // Calculate "sqrt hash"
222 sqrtHash($modulaHashes[0], $modulaHashes[1]),
223 sqrtHash($modulaHashes[2], $modulaHashes[3])
226 // Calulcate modula hash
227 $modulaHash = doubleHashString(modulaHash($sqrtHashes[0], $sqrtHashes[1]));
230 print ('hashes=' . print_r($hashes, TRUE));
231 print ('modulaHashes=' . print_r($modulaHashes, TRUE));
232 print ('sqrtHashes=' . print_r($sqrtHashes, TRUE));
233 print ('modulaHash=' . $modulaHash . PHP_EOL);
235 // Total reward + hashes
240 // Create nonce (small)
241 $nonce = 1 / mt_rand();
244 // Init hash-per-block counter and hashrate
248 // Wait for BLOCK_SIZE iterations (= found hashes). This is one block
249 $timeBlock = microtime(TRUE);
250 $timeDisplay = $timeBlock;
252 // Time waited for a good block again (no iteration)
255 while ($hashesPerBlock <= BLOCK_SIZE) {
256 // Create hash from modulaHash ("genesis hash") and nonce
257 $nonceHash = doubleHashString($modulaHash . $nonce);
260 $sumNonce = calculateSumFromHash($nonceHash);
261 $sumModula = calculateSumFromHash($modulaHash);
266 // Now start the "mining" ...
267 $timeHash = microtime(TRUE);
268 while ($sumNonce >= $sumModula) {
269 // Calculate new nonce
270 $nonce = calculateNonce($nonce);
273 $nonceHash = doubleHashString($modulaHash . $nonce);
276 $sumNonce = calculateSumFromHash($nonceHash);
277 //print('hashesPerBlock=' . $hashesPerBlock . PHP_EOL);
279 // Time spend in loop
280 $testTime = abs(microtime(TRUE) - $timeDisplay);
282 // Calculate hashrate/sec
283 $hashrate = 1 / $testTime * $iter * 2 + 2;
286 if ($testTime >= 1) {
288 print ('hashesPerBlock=' . $hashesPerBlock . ',hashrate=' . $hashrate . ' hashes/sec.' . PHP_EOL);
291 $timeDisplay = microtime(TRUE);
296 //print ('nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
297 //print ('nonceHash=' . $nonceHash . PHP_EOL);
298 //print ('sumNonce=' . $sumNonce . PHP_EOL);
299 //print ('sumModula=' . $sumModula . PHP_EOL);
302 // Add amount of hashes to block (double-hash)
303 $hashesPerBlock += $iter * 2 + 2;
305 // If the iteration is zero, then no hash is found
308 $timeBadHashes += abs(microtime(TRUE) - $timeHash);
311 //print('bad:nonce=' . $nonce . PHP_EOL);
313 // Nothing found, so calculate new nonce
314 $nonce = calculateNonce($nonce);
319 //print ('nonceHash=' . $nonceHash .',iter=' . $iter . PHP_EOL);
321 // Use nonceHash as next modula hash
322 $modulaHash = $nonceHash;
325 // Time taken for one block
326 $timeBlock = abs(microtime(TRUE) - $timeBlock);
327 //print ('calculateSumFromHash(modulaHash)=' . calculateSumFromHash($modulaHash) . PHP_EOL);
328 //print ('calculateSumFromHash(nonceHash)=' . calculateSumFromHash($nonceHash) . PHP_EOL);
331 $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000;
332 //print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
335 $totalHashes += $hashesPerBlock;
339 // Calculate new nonce
340 $nonce = calculateNonce($nonce);
342 // Add reward to total
343 $totalReward += $reward;
345 // Calculate average block value
346 $blockValue = $totalReward / $totalBlocks * $totalHashes / (BLOCK_SIZE * $totalBlocks);
348 // Calculate reward per hour (= 3600 seconds)
349 $rewardPerHour = $totalReward / abs(microtime(TRUE) - START_TIME) * 3600;
351 print ('totalReward=' . $totalReward . ',blockValue=' . $blockValue . ',rewardPerHour=' . $rewardPerHour . PHP_EOL);