From a8861d6552e5cad5a032f3a9be58b39d33e1e068 Mon Sep 17 00:00:00 2001 From: Kenny Peng Date: Thu, 27 May 2021 17:21:50 -0700 Subject: [PATCH] Remove unused functions, including extend_cache --- src/counter.c | 6 --- src/counter.h | 1 - src/mine_DUCO_S1.c | 105 ++------------------------------------- src/mine_DUCO_S1.h | 92 +--------------------------------- src/nonceMiner.c | 4 +- src/nonceMiner_minimal.c | 4 +- 6 files changed, 9 insertions(+), 203 deletions(-) diff --git a/src/counter.c b/src/counter.c index 9a3c41e..295ea40 100644 --- a/src/counter.c +++ b/src/counter.c @@ -11,12 +11,6 @@ void init_counter_state(struct counter_state *state){ state->length = 1; } -int counter_to_string(char *buf, struct counter_state *state){ - memcpy(buf, state->buf+12-state->length, state->length); - buf[state->length] = '\0'; - return state->length; -} - void increment_counter(struct counter_state *state){ if(state->middle_digits == 9999 && state->lower_digits == 9999){ state->upper_digits += 1; diff --git a/src/counter.h b/src/counter.h index 42c19fd..682f634 100644 --- a/src/counter.h +++ b/src/counter.h @@ -10,7 +10,6 @@ struct counter_state{ }; void init_counter_state(struct counter_state *state); -int counter_to_string(char *buf, struct counter_state *state); void increment_counter(struct counter_state *state); int count_digits(long num); diff --git a/src/mine_DUCO_S1.c b/src/mine_DUCO_S1.c index 25eee6a..d38224e 100644 --- a/src/mine_DUCO_S1.c +++ b/src/mine_DUCO_S1.c @@ -1,45 +1,5 @@ #include "mine_DUCO_S1.h" -long _get_divisor(long x){ - if(x == 0) return 1; // In case a single zero digit is received - long temp = 1; - while(x >= temp) temp *= 10; - return temp / 10; -} - -void set_sha1_base( - SHA_CTX *ctx_ptr, - const unsigned char *input_prefix, - int prefix_length) -{ - SHA1_Init(ctx_ptr); - SHA1_Update(ctx_ptr, input_prefix, prefix_length); -} - -void modify_sha1_ctx(SHA_CTX *ctx_ptr, long nonce){ - for(long divisor = _get_divisor(nonce); divisor != 0; divisor /= 10){ - const unsigned char digit = (unsigned char)((nonce%(divisor*10))/divisor)+'0'; - SHA1_Update(ctx_ptr, &digit, 1); - } -} - -void modify_sha1_ctx_one_digit(SHA_CTX *ctx_ptr, int nonce){ - const unsigned char digit = nonce+'0'; - SHA1_Update(ctx_ptr, &digit, 1); -} - -void modify_sha1_ctx_two_digits(SHA_CTX *ctx_ptr, int nonce){ - const unsigned char digits[2] = { - nonce/10 + '0', - nonce%10 + '0' - }; - SHA1_Update(ctx_ptr, digits, 2); -} - -void complete_sha1_hash(unsigned char hash[HASH_SIZE], SHA_CTX *ctx_ptr){ - SHA1_Final(hash, ctx_ptr); // Wipes context at ctx_ptr when complete -} - int compare_hash( const unsigned char hex_digest[2*HASH_SIZE], const unsigned char byte_digest[HASH_SIZE]) @@ -60,27 +20,8 @@ long mine_DUCO_S1( int difficulty) { SHA_CTX base_ctx; - set_sha1_base(&base_ctx, input_prefix, prefix_length); - long maximum = 100*difficulty+1; - - for(long i = 0; i < maximum; i++){ - unsigned char temp_hash[HASH_SIZE]; - SHA_CTX temp_ctx = base_ctx; - modify_sha1_ctx(&temp_ctx, i); - complete_sha1_hash(temp_hash, &temp_ctx); - if(compare_hash(target_hexdigest, temp_hash)) return i; - } - return -1; -} - -long mine_DUCO_S1_lookup( - const unsigned char *input_prefix, - int prefix_length, - const unsigned char target_hexdigest[HASH_SIZE*2], - int difficulty) -{ - SHA_CTX base_ctx; - set_sha1_base(&base_ctx, input_prefix, prefix_length); + SHA1_Init(&base_ctx); + SHA1_Update(&base_ctx, input_prefix, prefix_length); long maximum = 100*difficulty+1; struct counter_state state; @@ -88,46 +29,8 @@ long mine_DUCO_S1_lookup( unsigned char temp_hash[HASH_SIZE]; SHA_CTX temp_ctx = base_ctx; SHA1_Update(&temp_ctx, state.buf+12-state.length, state.length); - complete_sha1_hash(temp_hash, &temp_ctx); + SHA1_Final(temp_hash, &temp_ctx); if(compare_hash(target_hexdigest, temp_hash)) return state.as_long_integer; } return -1; -} - -long mine_DUCO_S1_extend_cache( - const unsigned char *input_prefix, - int prefix_length, - const unsigned char target_hexdigest[HASH_SIZE*2], - int difficulty) -{ - SHA_CTX base_ctx; - set_sha1_base(&base_ctx, input_prefix, prefix_length); - long maximum = 100*difficulty+1; - long cache_size = maximum/100; - SHA_CTX *cache_ctx = (SHA_CTX*)malloc(cache_size*sizeof(SHA_CTX)); - - for(long i = 0; i < maximum; i++){ - unsigned char temp_hash[HASH_SIZE]; - SHA_CTX temp_ctx; - if(i < 10){ // If nothing but the context with prefix is cached yet... - temp_ctx = base_ctx; - modify_sha1_ctx_one_digit(&temp_ctx, i); - } - else if(i < 10*cache_size){ // If context with nonce upper digits (tens and beyond) is cached... - temp_ctx = cache_ctx[i/10]; - modify_sha1_ctx_one_digit(&temp_ctx, i%10); - } - else{ // If context with nonce upper digits (hundreds and beyond) is cached... - temp_ctx = cache_ctx[i/100]; - modify_sha1_ctx_two_digits(&temp_ctx, i%100); - } - if(i < cache_size) cache_ctx[i] = temp_ctx; // Cache the SHA1 context - complete_sha1_hash(temp_hash, &temp_ctx); - if(compare_hash(target_hexdigest, temp_hash)){ - free(cache_ctx); - return i; - } - } - free(cache_ctx); - return -1; -} +} \ No newline at end of file diff --git a/src/mine_DUCO_S1.h b/src/mine_DUCO_S1.h index 9d9ef9d..0c93476 100644 --- a/src/mine_DUCO_S1.h +++ b/src/mine_DUCO_S1.h @@ -13,68 +13,8 @@ extern "C" { #endif -// private functions /* - * _get_divisor - returns the largest divisor of x that is an order of 10 - * - * Useful for splitting a number into digits from left to right. Only used in the basic - * mine_DUCO_S1, not mine_DUCO_S1_extend_cache. - * */ -long _get_divisor(long x); - -// public functions -/* - * set_sha1_base - Initializes SHA1 context with the prefix - * - * input_prefix should point to the beginning of the prefix. Because the prefix can be - * either 40 or 16 characters long, the prefix_length parameter is exposed. Pass in - * whichever is the correct length. - * */ -void set_sha1_base( - SHA_CTX *ctx_ptr, - const unsigned char *input_prefix, - int prefix_length); - -/* - * modify_sha1_ctx - Updates SHA1 context with the full nonce - * - * Encodes each digit in ASCII then updates the context with that digit. Only used in the - * basic mine_DUCO_S1, not mine_DUCO_S1_extend_cache. - * */ -void modify_sha1_ctx( - SHA_CTX *ctx_ptr, - long nonce); - -/* - * modify_sha1_ctx_one_digit - Updates SHA1 context with a single digit - * - * Encodes passed digit in an UTF-8 char then updates the context with that digit. - * */ -void modify_sha1_ctx_one_digit( - SHA_CTX *ctx_ptr, - int nonce); - -/* - * modify_sha1_ctx_two_digits - Updates SHA1 context with two digits - * - * Splits a two-digit number into two digits, encodes both in UTF-8 into a array, then - * updates the context with that array. - * */ -void modify_sha1_ctx_two_digits( - SHA_CTX *ctx_ptr, - int nonce); - -/* - * complete_sha1_hash - Completes the SHA1 hash, putting a byte string into hash[HASH_SIZE] - * - * A wrapper around SHA1_Final. Note: A byte-string is NOT an ordinary hex digest. - * */ -void complete_sha1_hash( - unsigned char hash[HASH_SIZE], - SHA_CTX *ctx_ptr); - -/* - * complete_sha1_hash - Compares a byte string and a hex digest, returning 1 if matched + * compare_hash - Compares a byte string and a hex digest, returning 1 if matched * * Compares two hex characters from the hex digest with the corresponding byte in the byte * string for every byte, from left to right. However, it will immediately return 0 if @@ -87,10 +27,6 @@ int compare_hash( /* * mine_DUCO_S1 - Returns the nonce that generated target_hexdigest * - * This is the basic version. It uses a very small amount of memory but gives up a - * significant amount of speed compared to mine_DUCO_S1_extend_cache. Still, it is - * faster compared to stock. - * * input_prefix should point to the beginning of the prefix. Because the prefix can be * either 40 or 16 characters long, the prefix_length parameter is exposed. Pass in * whichever is the correct length. target_hexdigest should point to the beginning of the @@ -102,32 +38,6 @@ long mine_DUCO_S1( const unsigned char target_hexdigest[HASH_SIZE*2], int difficulty); -long mine_DUCO_S1_lookup( - const unsigned char *input_prefix, - int prefix_length, - const unsigned char target_hexdigest[HASH_SIZE*2], - int difficulty); - -/* - * mine_DUCO_S1_extend_cache - Returns the nonce that generated target_hexdigest - * - * This is the fastest availible. In addition to caching the midstate initialized with - * the prefix, it also caches midstates with the upper digits of the nonce, saving even - * more work. Per thread running this function, it uses roughly 100MB of memory on the - * EXTREME difficulty, but this can be higher or lower depending on job difficulty the - * server sends. - * - * input_prefix should point to the beginning of the prefix. Because the prefix can be - * either 40 or 16 characters long, the prefix_length parameter is exposed. Pass in - * whichever is the correct length. target_hexdigest should point to the beginning of the - * target hex digest. - * */ -long mine_DUCO_S1_extend_cache( - const unsigned char *input_prefix, - int prefix_length, - const unsigned char target_hexdigest[HASH_SIZE*2], - int difficulty); - #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/src/nonceMiner.c b/src/nonceMiner.c index 17be343..54b6ac5 100644 --- a/src/nonceMiner.c +++ b/src/nonceMiner.c @@ -114,7 +114,7 @@ void* mining_routine(void* arg){ long nonce; if(buf[40] == ','){ // If the prefix is a SHA1 hex digest (40 chars long)... diff = atoi((const char*) &buf[82]); - nonce = mine_DUCO_S1_lookup( + nonce = mine_DUCO_S1( (const unsigned char*) &buf[0], 40, (const unsigned char*) &buf[41], @@ -123,7 +123,7 @@ void* mining_routine(void* arg){ } else{ // Else the prefix is probably an XXHASH hex digest (16 chars long)... diff = atoi((const char*) &buf[58]); - nonce = mine_DUCO_S1_lookup( + nonce = mine_DUCO_S1( (const unsigned char*) &buf[0], 16, (const unsigned char*) &buf[17], diff --git a/src/nonceMiner_minimal.c b/src/nonceMiner_minimal.c index 7f18c37..3e00865 100644 --- a/src/nonceMiner_minimal.c +++ b/src/nonceMiner_minimal.c @@ -56,7 +56,7 @@ int main(){ long nonce; if(buf[40] == ','){ // If the prefix is a SHA1 hex digest (40 chars long)... diff = atoi((const char*) &buf[82]); - nonce = mine_DUCO_S1_lookup( + nonce = mine_DUCO_S1( (const unsigned char*) &buf[0], 40, (const unsigned char*) &buf[41], @@ -65,7 +65,7 @@ int main(){ } else{ // Else the prefix is probably an XXHASH hex digest (16 chars long)... diff = atoi((const char*) &buf[58]); - nonce = mine_DUCO_S1_lookup( + nonce = mine_DUCO_S1( (const unsigned char*) &buf[0], 16, (const unsigned char*) &buf[17],