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 // 0 1 2 3 4 5 6 7 8 9 10 11
92 sprintf('%d|%s|%d|%d|%d|%d|%s|%d|%s|%s|%s|%s',
94 $GLOBALS['total_blocks'],
96 $GLOBALS['total_reward'],
98 $GLOBALS['total_hashes'],
100 $GLOBALS['total_found'],
102 $GLOBALS['total_restarts'],
104 $GLOBALS['hash_cycles'],
108 $GLOBALS['difficulty'],
110 base64_encode((float) $GLOBALS['nonce']),
114 $GLOBALS['root_hash'],
116 base64_encode(gzcompress(json_encode($GLOBALS['found_hashes'])))
121 $GLOBALS['time_flush'] = microtime(true);
122 print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL);
126 * Adds a found hash and flushes the checkpoint file
128 * @param $hash Hash to save
130 function addFoundHash ($hash) {
132 $GLOBALS['total_found']++;
135 array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], [
136 'current_hash' => $GLOBALS['current_hash'],
137 'root_hash' => $GLOBALS['root_hash'],
138 'nonce' => (float) $GLOBALS['nonce'],
139 'iter' => $GLOBALS['iteration'],
140 'hashes_block' => $GLOBALS['hashes_block'],
141 'hash_cycles' => $GLOBALS['hash_cycles'],
142 'difficulty' => $GLOBALS['difficulty'],
143 'nonce_hash' => $hash,
147 //* NOISY-DEBUG: */ print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',total_found=' . $GLOBALS['total_found'] . PHP_EOL);
149 // Set time as a new hash was found
150 $GLOBALS['found_time'] = microtime(true);
158 function initNonce () {
159 $GLOBALS['nonce'] = 1 / (mt_rand() ^ (1 / pi()));
160 print (__FUNCTION__ . ': nonce=' . $GLOBALS['nonce'] . PHP_EOL);
164 * Sums all hex parts of the hash to one final sum
166 * @param $hash Hex-hash to sum
167 * @return Sum of hash
169 function sumHash ($hash) {
173 for ($i = 0; $i < (strlen($hash) / 2); $i++) {
174 $sum += hexdec(substr($hash, $i, 2));
181 * Loads check-point file, if found
185 function loadCheckpointFile () {
186 // Is the check point there?
187 if (is_readable(CHECKPOINT_FILE)) {
189 $checkPoint = file_get_contents(CHECKPOINT_FILE);
192 $data = explode('|', $checkPoint);
195 assert(count($data) == 10);
197 // 1st element is nonce, 2nd hash, 3rd found hashes
198 $GLOBALS['total_blocks'] = $data[0];
199 $GLOBALS['total_reward'] = $data[1];
200 $GLOBALS['total_hashes'] = $data[2];
201 $GLOBALS['total_found'] = $data[3];
202 $GLOBALS['total_restarts'] = $data[4];
203 $GLOBALS['hash_cycles'] = intval($data[5]);
204 $GLOBALS['salt'] = $data[6];
205 $GLOBALS['difficulty'] = $data[7];
206 $GLOBALS['nonce'] = (float) base64_decode($data[8]);
207 $GLOBALS['current_hash'] = $data[9];
208 $GLOBALS['root_hash'] = $data[10];
209 $GLOBALS['found_hashes'] = json_decode(gzuncompress(base64_decode($data[11])), TRUE);