Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Remove unused functions, including extend_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
colonelwatch committed May 28, 2021
1 parent dc79bf8 commit a8861d6
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 203 deletions.
6 changes: 0 additions & 6 deletions src/counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
105 changes: 4 additions & 101 deletions src/mine_DUCO_S1.c
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -60,74 +20,17 @@ 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;
for(init_counter_state(&state); state.as_long_integer < maximum; increment_counter(&state)){
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;
}
}
92 changes: 1 addition & 91 deletions src/mine_DUCO_S1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/nonceMiner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions src/nonceMiner_minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand Down

0 comments on commit a8861d6

Please sign in to comment.