Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- foxhill @ news.ycombinator
- i wrote some code in javascript and it ran slow and it used all my memory and how do even?
- *sigh*
- build with
- gcc -std=c99 -Ofast -fopenmp -o mh bf.c && ./mh
- notes:
- search space is 2^2108 (with all chars), but i'm searching through 64 chars.
- but my nonce counter is only 64 bits, so the search space will *never* be entirely searched.
- actually, funny story, there *isn't enough energy in the universe* to *iterate* a 2^128 counter :)
- so searching the entire space is literally impossible.
- you can restrict the search space even more by changing the alphabet to lower case chars
- (you'd need to make sure that the bitshifts are updated. powers of two make things a lot easier.)
- i've been lazy and hard coded things. sorry.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <string.h>
- //code editor uses clang, which has no OpenMP support..
- #if !defined(__clang__)
- #include "omp.h"
- #endif
- //i don't usually do global persistent state, but it's needed for the exit func..
- uint64_t num_searched = 0;
- uint64_t num_found = 0;
- double time_start = 0.0;
- //(yes i know zeroing them out is needless)
- //print some stuff when we quit
- void
- search_finish
- (int sig);
- //for a given string, hash it and see if it == 666
- bool
- search_hash
- (const char *str, size_t len);
- //create a search string from a given nonce
- size_t
- search_str
- (uint64_t k, char buf[32]);
- //actually, creating the string is needless. do generation and hash in one step
- //return true if hash == 666
- bool
- search_nonce
- (uint64_t k);
- int
- main
- (int num_args, char **args)
- {
- printf("searching.. this search never ends, so hit ^C when you're bored\n");
- const int num_threads = 4;
- const int lim = 1000000;
- signal(SIGQUIT, search_finish);
- signal(SIGINT, search_finish);
- omp_set_num_threads(num_threads);
- time_start = omp_get_wtime();
- #pragma omp parallel
- while (true)
- {
- int tn = omp_get_thread_num();
- #pragma omp for
- for (int i=0; i<lim; i++)
- {
- uint64_t k = (num_searched*lim + i)*num_threads + tn;
- if (search_nonce(k))
- {
- //print out the string if it was valid
- char buf[32]; memset(buf, 0, 32);
- search_str(k, buf);
- printf("found: %s\n", buf);
- #pragma omp critical
- num_found += 1;
- }
- }
- #pragma omp master
- num_searched += 1;
- }
- return 0;
- }
- void
- search_finish
- (int sig)
- {
- double time_end = omp_get_wtime();
- printf("searched %lld names, found %lld devil names in %.2lfs (%.0lf Mh/s)\n",
- num_searched*1000000*4,
- num_found,
- time_end - time_start,
- (num_searched*4)/((time_end - time_start))
- );
- char buf[32];memset(buf, 0, 32);
- search_str(num_searched*1000000*4 + 2341, buf);
- printf("last str was: %s\n", buf);
- exit(0);
- }
- size_t
- search_str
- (uint64_t k, char buf[32])
- {
- static const char alphabet[] = {
- "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_-"
- };
- size_t p = 0;
- do
- {
- unsigned char val = k & 63;
- buf[p] = alphabet[val];
- p += 1;
- k = k >> 6;
- } while( k > 0 );
- return p;
- }
- bool
- search_hash
- (const char *str, size_t len)
- {
- /*
- String.prototype.hashCode = function(){
- var hash = 0, i, char;
- if (this.length == 0) return hash;
- for (var i = 0, l = this.length; i < l; i++) {
- char = this.charCodeAt(i);
- hash = ((hash<<5)-hash)+char;
- hash |= 0; // Convert to 32bit integer
- }
- return hash;
- };
- */
- uint32_t hash = 0;
- for (int i=0; i<len; i++)
- hash = ((hash << 5) - hash) + str[i];
- return (hash == 666);
- }
- bool
- search_nonce
- (uint64_t k)
- {
- static const char a[] = {
- "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_-"
- };
- uint32_t h = 0;
- do
- {
- unsigned char val = k & 63;
- h = ((h << 5) - h) + a[val];
- k = k >> 6;
- } while( k > 0 );
- return (h == 666);
- }
- /*
- no offence intended to any parties with my opening comment :) it was a dig at JS in general.
- thanks,
- */
Advertisement
Add Comment
Please, Sign In to add comment