3 * Calculates a simple but stronger hash from given string. No salts are being
6 * @param $str The string to be hashed
7 * @return $hash The hash from string $str
9 function hashString ($str) {
10 // Calculate strong hash from given string
11 $hash = Scrypt::hash($str, $GLOBALS['salt'], $GLOBALS['difficulty']);
18 * Multiple-hashes given string. This is done by hashing the given string and
19 * then hashing the generated hash again.
21 * @param $str The string to be hashed 4 times
22 * @return $hash The generated hash
24 function multipleHashString ($str) {
25 // Generate hash from given hash
26 $hash = hashString($str);
29 for ($idx = 0; $idx < ($GLOBALS['hash_cycles'] - 1); $idx++) {
30 // Over-hash the given hash
31 $hash = hashString($hash);
39 * Calculates sum from given hash
41 * @param $hash Hash to calculate sum from
42 * @return $sum Sum from given hash
44 function calculateSumFromHash ($hash) {
45 // Everything starts with zero ...
49 for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) {
51 $sum = $sum + hexdec(substr($hash, $idx, 2));
59 * Calculates new nonce
63 function calculateNonce () {
64 // Linear incrementation
65 $GLOBALS['nonce'] += $GLOBALS['none_increment'];
69 * Writes/flushes check-point file
71 * @param $hash Found hash or gensis hash
74 function flushCheckPointFile ($hash) {
76 print ('FLUSHING: Writing ' . count($GLOBALS['found_hashes']) . ' blocks ...' . PHP_EOL);
79 $timer = microtime(true);
84 $GLOBALS['total_blocks'] . '|' .
85 $GLOBALS['total_reward'] . '|' .
86 $GLOBALS['total_hashes'] . '|' .
87 $GLOBALS['total_found'] . '|' .
88 $GLOBALS['total_restarts'] . '|' .
89 $GLOBALS['hash_cycles'] . '|' .
90 $GLOBALS['salt'] . '|' .
91 $GLOBALS['difficulty'] . '|' .
92 base64_encode((float) $GLOBALS['nonce']) . '|' .
94 $GLOBALS['root_hash'] . '|' .
95 base64_encode(gzcompress(json_encode($GLOBALS['found_hashes'])))
99 $GLOBALS['time_flush'] = microtime(true);
100 print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL);
104 * Adds a found hash and flushes the checkpoint file
106 * @param $hash Hash to save
108 function addFoundHash ($hash) {
110 $GLOBALS['total_found']++;
113 array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], array(
114 'current_hash' => $GLOBALS['current_hash'],
115 'root_hash' => $GLOBALS['root_hash'],
116 'nonce' => (float) $GLOBALS['nonce'],
117 'iter' => $GLOBALS['iteration'],
118 'hashes_block' => $GLOBALS['hashes_block'],
119 'hash_cycles' => $GLOBALS['hash_cycles'],
120 'difficulty' => $GLOBALS['difficulty'],
121 'nonce_hash' => $hash,
125 print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',total_found=' . $GLOBALS['total_found'] . PHP_EOL);
127 // Set time as a new hash was found
128 $GLOBALS['found_time'] = microtime(true);
130 // Flush check-point file after new hash is found
131 flushCheckPointFile($hash);
139 function initNonce () {
140 $GLOBALS['nonce'] = 1 / (mt_rand() ^ pi());
141 print (__FUNCTION__ . ': nonce=' . $GLOBALS['nonce'] . PHP_EOL);
145 * Sums all hex parts of the hash to one final sum
147 * @param $hash Hex-hash to sum
148 * @return Sum of hash
150 function sumHash ($hash) {
154 for ($i = 0; $i < (strlen($hash) / 2); $i++) {
155 $sum += hexdec(substr($hash, $i, 2));
162 * Loads check-point file, if found
166 function loadCheckpointFile () {
167 // Is the check point there?
168 if (is_readable(CHECK_POINT)) {
170 $checkPoint = file_get_contents(CHECK_POINT);
173 $data = explode('|', $checkPoint);
176 assert(count($data) == 10);
178 // 1st element is nonce, 2nd hash, 3rd found hashes
179 $GLOBALS['total_blocks'] = $data[0];
180 $GLOBALS['total_reward'] = $data[1];
181 $GLOBALS['total_hashes'] = $data[2];
182 $GLOBALS['total_found'] = $data[3];
183 $GLOBALS['total_restarts'] = $data[4];
184 $GLOBALS['hash_cycles'] = intval($data[5]);
185 $GLOBALS['salt'] = $data[6];
186 $GLOBALS['difficulty'] = $data[7];
187 $GLOBALS['nonce'] = (float) base64_decode($data[8]);
188 $GLOBALS['current_hash'] = $data[9];
189 $GLOBALS['root_hash'] = $data[9];
190 $GLOBALS['found_hashes'] = json_decode(gzuncompress(base64_decode($data[11])));