Skip to content

Commit

Permalink
Merge pull request #164 from edtubbs/0.1.3-dev-validation-oct31
Browse files Browse the repository at this point in the history
validation: updated scrypt and pow
  • Loading branch information
xanimo authored Nov 1, 2023
2 parents c2b91f3 + cbd1ae6 commit 7e4d096
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 103 deletions.
9 changes: 4 additions & 5 deletions include/dogecoin/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ struct uint_err {
};

typedef struct base_uint_ {
int WIDTH; // BITS / 32
uint32_t pn[8]; // pn[WIDTH]
} base_uint_;

typedef base_uint_ arith_uint256;

arith_uint256 init_arith_uint256();
arith_uint256 set_compact(arith_uint256 hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow);
uint256* arith_to_uint256(const arith_uint256 a);
arith_uint256 uint_to_arith(const uint256* a);
arith_uint256* init_arith_uint256();
arith_uint256* set_compact(arith_uint256* hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow);
uint8_t* arith_to_uint256(const arith_uint256* a);
arith_uint256* uint_to_arith(const uint256* a);
uint64_t get_low64(arith_uint256 pn);

LIBDOGECOIN_END_DECL
Expand Down
1 change: 1 addition & 0 deletions include/dogecoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef struct dogecoin_auxpow_block_ {
uint256* parent_coinbase_merkle;
uint32_t parent_merkle_index;
uint8_t aux_merkle_count;
uint256* aux_merkle_branch;
uint32_t aux_merkle_index;
dogecoin_block_header* parent_header;
} dogecoin_auxpow_block;
Expand Down
2 changes: 2 additions & 0 deletions include/dogecoin/dogecoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ typedef uint8_t uint256[32];
typedef uint8_t uint160[20];
typedef uint8_t SEED[MAX_SEED_SIZE];

static const int WIDTH = 0x0000100/32;

LIBDOGECOIN_END_DECL

#endif // __LIBDOGECOIN_DOGECOIN_H__
2 changes: 1 addition & 1 deletion include/dogecoin/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LIBDOGECOIN_API uint32_t get_chainid(uint32_t version);
LIBDOGECOIN_API dogecoin_bool is_auxpow(uint32_t version);
LIBDOGECOIN_API dogecoin_bool is_legacy(uint32_t version);
LIBDOGECOIN_API dogecoin_bool check_auxpow(dogecoin_auxpow_block block, dogecoin_chainparams* params);
LIBDOGECOIN_API dogecoin_bool dogecoin_block_header_scrypt_hash(cstring* s, uint256 hash);
LIBDOGECOIN_API dogecoin_bool dogecoin_block_header_scrypt_hash(cstring* s, uint256* hash);

LIBDOGECOIN_END_DECL

Expand Down
82 changes: 60 additions & 22 deletions src/arith_uint256.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,66 @@

#include <dogecoin/arith_uint256.h>

arith_uint256 init_arith_uint256() {
arith_uint256 x;
x.WIDTH = 8;
dogecoin_mem_zero(x.pn, x.WIDTH);
arith_uint256* init_arith_uint256() {
arith_uint256* x = dogecoin_calloc(8, sizeof(uint32_t));
int i = 0;
for (; i < WIDTH; i++) {
x->pn[i] = 0;
}
return x;
}

arith_uint256 set_compact(arith_uint256 hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow) {
void arith_shift_left(arith_uint256* input, unsigned int shift) {
// Temporary storage for the input as we're going to overwrite the data
arith_uint256 temp = *input;

// Clear the input
for (int i = 0; i < WIDTH; i++) {
input->pn[i] = 0;
}

int k = shift / 32; // Number of full word shifts
shift = shift % 32; // Remaining shift

// Perform the shift operation
for (int i = 0; i < WIDTH; i++) {
if (i + k + 1 < WIDTH && shift != 0)
input->pn[i + k + 1] |= (temp.pn[i] >> (32 - shift));
if (i + k < WIDTH)
input->pn[i + k] |= (temp.pn[i] << shift);
}
}

void arith_shift_right(arith_uint256* input, unsigned int shift) {
// Temporary storage for the input as we're going to overwrite the data
arith_uint256 temp = *input;

// Clear the input
for (int i = 0; i < WIDTH; i++) {
input->pn[i] = 0;
}

int k = shift / 32; // Number of full word shifts
shift = shift % 32; // Remaining shift

// Perform the shift operation
for (int i = 0; i < WIDTH; i++) {
if (i - k - 1 >= 0 && shift != 0)
input->pn[i - k - 1] |= (temp.pn[i] << (32 - shift));
if (i - k >= 0)
input->pn[i - k] |= (temp.pn[i] >> shift);
}
}

arith_uint256* set_compact(arith_uint256* hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow) {
int size = compact >> 24;
uint32_t word = compact & 0x007fffff;
if (size <= 3) {
word >>= 8 * (3 - size);
memcpy_safe(&hash, &word, sizeof word);
memcpy_safe(&hash->pn[0], &word, sizeof word);
} else {
word <<= 8 * (size - 3);
memcpy_safe(&hash, &word, sizeof word);
memcpy_safe(&hash->pn[0], &word, sizeof word);
arith_shift_left(hash, 8 * (size - 3));
}
if (pf_negative) *pf_negative = word != 0 && (compact & 0x00800000) != 0;
if (pf_overflow) *pf_overflow = word != 0 && ((size > 34) ||
Expand All @@ -49,25 +93,19 @@ arith_uint256 set_compact(arith_uint256 hash, uint32_t compact, dogecoin_bool *p
return hash;
}

arith_uint256 uint_to_arith(const uint256* a)
arith_uint256* uint_to_arith(const uint256* a)
{
arith_uint256 b;
b.WIDTH = 8;
int x = 0;
for(; x < b.WIDTH; ++x)
b.pn[x] = read_le32((const unsigned char*)a + x * 4);
return b;
static arith_uint256 b;
memcpy_safe(b.pn, a, sizeof(b.pn));
return &b;
}

uint256* arith_to_uint256(const arith_uint256 a) {
uint256* b = dogecoin_uint256_vla(1);
int x = 0;
for(; x < a.WIDTH; ++x)
write_le32((unsigned char*)b + x * 4, a.pn[x]);
return b;
uint8_t* arith_to_uint256(const arith_uint256* a) {
static uint256 b = {0};
memcpy_safe(b, a->pn, sizeof(uint256));
return &b[0];
}

uint64_t get_low64(arith_uint256 a) {
assert(a.WIDTH >= 2);
return a.pn[0] | (uint64_t)a.pn[1] << 32;
}
37 changes: 22 additions & 15 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ void dogecoin_auxpow_block_free(dogecoin_auxpow_block* block) {
if (!block) return;
dogecoin_block_header_free(block->header);
dogecoin_tx_free(block->parent_coinbase);
free(block->parent_coinbase_merkle);
dogecoin_free(block->parent_coinbase_merkle);
dogecoin_free(block->aux_merkle_branch);
block->parent_merkle_count = 0;
block->aux_merkle_count = 0;
block->aux_merkle_index = 0;
block->parent_merkle_index = 0;
remove_all_hashes();
dogecoin_block_header_free(block->parent_header);
dogecoin_free(block);
Expand All @@ -187,7 +190,7 @@ void print_transaction(dogecoin_tx* x) {
// serialize tx & print raw hex:
cstring* tx = cstr_new_sz(1024);
dogecoin_tx_serialize(tx, x);
char tx_hex[tx->len*2];
char tx_hex[2048];
utils_bin_to_hex((unsigned char *)tx->str, tx->len, tx_hex);
printf("block->parent_coinbase (hex): %s\n", tx_hex); // uncomment to see raw hexadecimal transactions

Expand Down Expand Up @@ -222,30 +225,35 @@ void print_transaction(dogecoin_tx* x) {

void print_block_header(dogecoin_block_header* header) {
printf("block->header->version: %i\n", header->version);
printf("block->header->prev_block: %s\n", to_string(header->prev_block));
printf("block->header->merkle_root: %s\n", to_string(header->merkle_root));
printf("block->header->prev_block: %s\n", hash_to_string(header->prev_block));
printf("block->header->merkle_root: %s\n", hash_to_string(header->merkle_root));
printf("block->header->timestamp: %u\n", header->timestamp);
printf("block->header->bits: %x\n", header->bits);
printf("block->header->nonce: %x\n", header->nonce);
}

void print_parent_header(dogecoin_auxpow_block* block) {
printf("block->parent_hash: %s\n", to_string(block->parent_hash));
printf("block->parent_hash: %s\n", hash_to_string(block->parent_hash));
printf("block->parent_merkle_count: %d\n", block->parent_merkle_count);
size_t j = 0;
for (; j < block->parent_merkle_count; j++) {
printf("block->parent_coinbase_merkle[%zu]: "
"%s\n", j, to_string((uint8_t*)block->parent_coinbase_merkle[j]));
"%s\n", j, hash_to_string((uint8_t*)block->parent_coinbase_merkle[j]));
}
printf("block->parent_merkle_index: %d\n", block->parent_merkle_index);
printf("block->aux_merkle_count: %d\n", block->aux_merkle_count);
j = 0;
for (; j < block->aux_merkle_count; j++) {
printf("block->aux_merkle_branch[%zu]: "
"%s\n", j, hash_to_string((uint8_t*)block->aux_merkle_branch[j]));
}
printf("block->aux_merkle_index: %d\n", block->aux_merkle_index);
printf("block->parent_header.version: %i\n", block->parent_header->version);
printf("block->parent_header.prev_block: %s\n", to_string(block->parent_header->prev_block));
printf("block->parent_header.merkle_root: %s\n", to_string(block->parent_header->merkle_root));
printf("block->parent_header.timestamp: %u\n", block->parent_header->timestamp);
printf("block->parent_header.bits: %x\n", block->parent_header->bits);
printf("block->parent_header.nonce: %u\n\n", block->parent_header->nonce);
printf("block->parent_header->version: %i\n", block->parent_header->version);
printf("block->parent_header->prev_block: %s\n", hash_to_string(block->parent_header->prev_block));
printf("block->parent_header->merkle_root: %s\n", hash_to_string(block->parent_header->merkle_root));
printf("block->parent_header->timestamp: %u\n", block->parent_header->timestamp);
printf("block->parent_header->bits: %x\n", block->parent_header->bits);
printf("block->parent_header->nonce: %u\n\n", block->parent_header->nonce);
}

void print_block(dogecoin_auxpow_block* block) {
Expand Down Expand Up @@ -336,13 +344,12 @@ int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const
printf("%s:%d:%s:%s\n", __FILE__, __LINE__, __func__, strerror(errno));
return false;
}
block->aux_merkle_branch = dogecoin_calloc(block->aux_merkle_count, sizeof(uint256));
for (i = 0; i < block->aux_merkle_count; i++) {
hash* aux_merkle_branch = new_hash();
if (!deser_u256((uint8_t*)aux_merkle_branch->data.u8, buffer)) {
if (!deser_u256(block->aux_merkle_branch[i], buffer)) {
printf("%d:%s:%d\n", __LINE__, __func__, i);
return false;
}
dogecoin_free(aux_merkle_branch);
}

if (!deser_u32(&block->aux_merkle_index, buffer)) {
Expand Down
Loading

0 comments on commit 7e4d096

Please sign in to comment.