-/**
- * Calculates a simple but stronger hash from given string. No salts are being
- * added here.
- *
- * @param $str The string to be hashed
- * @return $hash The hash from string $str
- */
-function hashString ($str) {
- // Calculate strong hash from given string
- $hash = mhash($GLOBALS['hash_algo'], $str);
-
- // Return it hexadecimal-encoded
- return bin2hex($hash);
-}
-
-/**
- * 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 multipleHashString ($str) {
- // Generate hash from given hash
- $hash = hashString($str);
-
- // Now over-hash it
- for ($idx = 0; $idx < ($GLOBALS['hash_cycles'] - 1); $idx++) {
- // Over-hash the given hash
- $hash = hashString($hash);
- } // END - for
-
- // Return it
- return $hash;
-}
-
-/**
- * Calculates a "modula-hash" based given two hashes.
- *
- * @param $hash1 Hash 1
- * @param $hash2 Hash 2
- */
-function modulaHash ($hash1, $hash2) {
- // Both must have same length
- assert(strlen($hash1) === strlen($hash2));
-
- // Init new hash
- $modulaHash = '';
-
- // "Walk" trough first hash and get every 2 byte of both hashes
- for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
- // Init modula value
- $mod = 0;
-
- // Get both hash parts and convert to ASCII number
- $part1 = hexdec(substr($hash1, $idx, 2));
- $part2 = hexdec(substr($hash2, $idx, 2));
-
- /*
- * If part1 is larget part2, part1 is divident and vise-versa. But don't do it
- * if one is zero
- */
- if (($part1 > $part2) && ($part2 > 0)) {
- // 'part1' is larger than 'part2'
- $mod = $part1 % $part2;
- } elseif (($part1 < $part2) && ($part1 > 0)) {
- // 'part2' is larger than 'part1'
- $mod = $part2 % $part1;
- }
-
- // "Invert" the result against 255
- $mod = 255 - $mod;
-
- // Encode to hex, pre-pad it with zeros and add to new hash
- $modulaHash .= padHex($mod);
- } // END - for
-
- // Modula hash must have same length as input hash
- assert(strlen($modulaHash) === strlen($hash1));
-
- // Return modula hash
- return $modulaHash;
-}
-
-/**
- * Calculates a "sqrt-hash" based given two hashes and single-hash it
- *
- * @param $hash1 Hash 1
- * @param $hash2 Hash 2
- */
-function sqrtHash ($hash1, $hash2) {
- // Both must have same length
- assert(strlen($hash1) === strlen($hash2));
-
- // Init new hash
- $sqrtHash = '';
-
- // "Walk" trough first hash and get every 2 byte of both hashes
- for ($idx = 0; $idx < strlen($hash1); $idx += 2) {
- // Init modula value
- $mod = 0;
-
- // Get both hash parts and convert to ASCII number
- $part1 = hexdec(substr($hash1, $idx, 2));
- $part2 = hexdec(substr($hash2, $idx, 2));
-
- // Calculate square root of both parts being multiplied and round up, then "invert" it against 255
- $sqrt = intval(255 - ceil(sqrt($part1 * $part2)));
-
- // Encode to hex, pre-pad it with zeros and add to new hash
- $sqrtHash .= padHex($sqrt);
- } // END - for
-
- // "sqrt-hash" must have same length as input hash
- assert(strlen($sqrtHash) === strlen($hash1));
-
- // Hash reversed "sqrt-hash" again and return it
- return hashString(strrev($sqrtHash));
-}
-
-/**
- * Converts a number between 0 and 255 into a zero-padded hexadecimal string
- *
- * @param $num Number between 0 and 255
- * @return $hex Hexadecimal string, padded with zeros
- */
-function padHex ($num) {
- // Must be a integer number and between 0 and 255
- assert(is_int($num));
- assert($num >= 0);
- assert($num <= 255);
-
- // Convert it
- $hex = str_pad(dechex($num), 2, '0', STR_PAD_LEFT);
-
- // ... and return it
- 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));
- } // END - for
-
- // And return it
- return $sum;
-}
-
-/**
- * Calculates new nonce
- *
- * @return void
- */
-function calculateNonce () {
- // Linear incrementation
- $GLOBALS['nonce'] += $GLOBALS['none_increment'];
-}
-
-/**
- * Writes/flushes check-point file
- *
- * @param $hash Modula hash (or hash to save)
- * @return void
- */
-function flushCheckPointFile ($hash) {
- // 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_reward'] . ':' . $GLOBALS['total_hashes'] . ':' . $GLOBALS['hash_cycles'] . ':' . base64_encode($GLOBALS['nonce']) . ':' . $hash . ':' . $GLOBALS['root_hash'] . ':' . base64_encode(gzcompress(serialize($GLOBALS['found_hashes']))));
-
- // Set time
- $GLOBALS['time_flush'] = microtime(TRUE);
- print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL);
-}
-
-/**
- * Adds a found hash and flushes the checkpoint file
- *
- * @param $hash Hash to save
- */
-function addFoundHash ($hash) {
- // Increment counter
- $GLOBALS['current_hashes']++;
-
- // Add hash to array
- array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], array(
- 'modula_hash' => $GLOBALS['modula_hash'],
- 'genesis_hash' => $GLOBALS['genesis_hash'],
- 'root_hash' => $GLOBALS['root_hash'],
- 'nonce' => $GLOBALS['nonce'],
- 'iter' => $GLOBALS['iteration'],
- 'hashes_block' => $GLOBALS['hashes_block'],
- 'hash_cycles' => $GLOBALS['hash_cycles'],
- 'nonce_hash' => $hash
- ));
-
- // Found hash:
- print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',current_hashes=' . $GLOBALS['current_hashes'] . PHP_EOL);
-
- // Set time as a new hash was found
- $GLOBALS['found_time'] = microtime(TRUE);
-
- // Flush check-point file after new hash is found
- flushCheckPointFile($hash);
-
- // Use nonceHash as next modula hash
- setModulaHash($hash);
-}
-
-/**
- * Initializes nonce
- *
- * @return void
- */
-function initNonce () {
- $GLOBALS['nonce'] = 1 / (mt_rand() ^ pi());
- print (__FUNCTION__ . ': nonce=' . $GLOBALS['nonce'] . PHP_EOL);
-}
-
-/**
- * Sets modula hash and calculates sum of it
- *
- * @param $hash Hash to set as "modula hash"
- * @return void
- */
-function setModulaHash ($hash) {
- $GLOBALS['modula_hash'] = $hash;
- $GLOBALS['sum_modula'] = calculateSumFromHash($GLOBALS['modula_hash']);