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 ...
46 //* NOISY-DEBUG: */ printf('[%s:%d]: hash(%d)=%s - CALLED!'. PHP_EOL, __FUNCTION__, __LINE__, strlen($hash), $hash);
49 // Part of the hash is not decodeable
50 $decodeA = explode('$', $hash);
51 $decode = $decodeA[4];
54 //* NOISY-DEBUG: */ printf('[%s:%d]: decode=%s' . PHP_EOL, __FUNCTION__, __LINE__, $decode);
55 for ($idx = 0; $idx < (strlen($decode) / 2); $idx++) {
57 $sum = $sum + hexdec(substr($decode, $idx, 2));
61 //* NOISY-DEBUG: */ printf('[%s:%d]: sum=%d - EXIT!' . PHP_EOL, __FUNCTION__, __LINE__, $sum);
66 * Calculates new nonce
70 function calculateNonce () {
71 // Linear incrementation
72 $GLOBALS['nonce'] += $GLOBALS['none_increment'];
76 * Writes/flushes check-point file
78 * @param $hash Found hash or gensis hash
81 function flushCheckPointFile ($hash) {
83 print ('FLUSHING: Writing ' . count($GLOBALS['found_hashes']) . ' blocks ...' . PHP_EOL);
86 $timer = microtime(true);
91 $GLOBALS['total_blocks'] . '|' .
92 $GLOBALS['total_reward'] . '|' .
93 $GLOBALS['total_hashes'] . '|' .
94 $GLOBALS['total_found'] . '|' .
95 $GLOBALS['total_restarts'] . '|' .
96 $GLOBALS['hash_cycles'] . '|' .
97 $GLOBALS['salt'] . '|' .
98 $GLOBALS['difficulty'] . '|' .
99 base64_encode((float) $GLOBALS['nonce']) . '|' .
101 $GLOBALS['root_hash'] . '|' .
102 base64_encode(gzcompress(json_encode($GLOBALS['found_hashes'])))
106 $GLOBALS['time_flush'] = microtime(true);
107 print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL);
111 * Adds a found hash and flushes the checkpoint file
113 * @param $hash Hash to save
115 function addFoundHash ($hash) {
117 $GLOBALS['total_found']++;
120 array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], [
121 'current_hash' => $GLOBALS['current_hash'],
122 'root_hash' => $GLOBALS['root_hash'],
123 'nonce' => (float) $GLOBALS['nonce'],
124 'iter' => $GLOBALS['iteration'],
125 'hashes_block' => $GLOBALS['hashes_block'],
126 'hash_cycles' => $GLOBALS['hash_cycles'],
127 'difficulty' => $GLOBALS['difficulty'],
128 'nonce_hash' => $hash,
132 print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',total_found=' . $GLOBALS['total_found'] . PHP_EOL);
134 // Set time as a new hash was found
135 $GLOBALS['found_time'] = microtime(true);
137 // Flush check-point file after new hash is found
138 flushCheckPointFile($hash);
146 function initNonce () {
147 $GLOBALS['nonce'] = 1 / (mt_rand() ^ (1 / pi()));
148 print (__FUNCTION__ . ': nonce=' . $GLOBALS['nonce'] . PHP_EOL);
152 * Sums all hex parts of the hash to one final sum
154 * @param $hash Hex-hash to sum
155 * @return Sum of hash
157 function sumHash ($hash) {
161 for ($i = 0; $i < (strlen($hash) / 2); $i++) {
162 $sum += hexdec(substr($hash, $i, 2));
169 * Loads check-point file, if found
173 function loadCheckpointFile () {
174 // Is the check point there?
175 if (is_readable(CHECKPOINT_FILE)) {
177 $checkPoint = file_get_contents(CHECKPOINT_FILE);
180 $data = explode('|', $checkPoint);
183 assert(count($data) == 10);
185 // 1st element is nonce, 2nd hash, 3rd found hashes
186 $GLOBALS['total_blocks'] = $data[0];
187 $GLOBALS['total_reward'] = $data[1];
188 $GLOBALS['total_hashes'] = $data[2];
189 $GLOBALS['total_found'] = $data[3];
190 $GLOBALS['total_restarts'] = $data[4];
191 $GLOBALS['hash_cycles'] = intval($data[5]);
192 $GLOBALS['salt'] = $data[6];
193 $GLOBALS['difficulty'] = $data[7];
194 $GLOBALS['nonce'] = (float) base64_decode($data[8]);
195 $GLOBALS['current_hash'] = $data[9];
196 $GLOBALS['root_hash'] = $data[9];
197 $GLOBALS['found_hashes'] = json_decode(gzuncompress(base64_decode($data[11])), TRUE);