test send
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <khash/khash.h>
|
||||
#include <crypto/crypto.h>
|
||||
#include <string.h>
|
||||
#include <uint256.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t bytes[32];
|
||||
@@ -16,7 +17,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
uint8_t address[32]; // For now just the SHA-256 of the public key; allows representation in different encodings (base58, bech32, etc) without changing the underlying data structure
|
||||
uint64_t balance;
|
||||
uint256_t balance;
|
||||
// TODO: Additional things
|
||||
} balance_sheet_entry_t;
|
||||
|
||||
|
||||
@@ -55,6 +55,53 @@ static inline bool uint256_add(uint256_t* a, const uint256_t* b) {
|
||||
return carry > 0;
|
||||
}
|
||||
|
||||
static inline bool uint256_subtract_u64(uint256_t* balance, uint64_t amount) {
|
||||
if (!balance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (balance->limbs[0] >= amount) {
|
||||
balance->limbs[0] -= amount;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t borrow = amount - balance->limbs[0];
|
||||
balance->limbs[0] = UINT64_MAX - borrow + 1ULL;
|
||||
|
||||
for (int i = 1; i < 4; ++i) {
|
||||
if (balance->limbs[i] > 0) {
|
||||
balance->limbs[i]--;
|
||||
return false;
|
||||
}
|
||||
|
||||
balance->limbs[i] = UINT64_MAX;
|
||||
}
|
||||
|
||||
return true; // underflow past 256 bits
|
||||
}
|
||||
|
||||
static inline bool uint256_subtract(uint256_t* a, const uint256_t* b) {
|
||||
// Check if a < b to prevent underflow
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
if (a->limbs[i] > b->limbs[i]) break;
|
||||
if (a->limbs[i] < b->limbs[i]) return false; // Underflow
|
||||
}
|
||||
|
||||
uint64_t borrow = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint64_t old_a = a->limbs[i];
|
||||
a->limbs[i] -= b->limbs[i] + borrow;
|
||||
|
||||
// Detect borrow: if we subtracted more than we had, or we were at zero and had a borrow
|
||||
if (borrow) {
|
||||
borrow = (a->limbs[i] >= old_a);
|
||||
} else {
|
||||
borrow = (a->limbs[i] > old_a);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two uint256_t values in a greater-than manner.
|
||||
* Returns [-1, 0, 1] if a > b, a < b, or a == b respectively.
|
||||
@@ -67,4 +114,35 @@ static inline int uint256_cmp(const uint256_t* a, const uint256_t* b) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void uint256_serialize(const uint256_t* value, char* out) {
|
||||
if (!value || !out) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert into string of decimal digits for easier readability; max 78 digits for 256 bits
|
||||
char digits[80];
|
||||
size_t digitCount = 0;
|
||||
uint256_t tmp = *value;
|
||||
while (tmp.limbs[0] != 0 || tmp.limbs[1] != 0 || tmp.limbs[2] != 0 || tmp.limbs[3] != 0) {
|
||||
uint64_t remainder = 0;
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
__uint128_t cur = ((__uint128_t)remainder << 64) | tmp.limbs[i];
|
||||
tmp.limbs[i] = (uint64_t)(cur / 10u);
|
||||
remainder = (uint64_t)(cur % 10u);
|
||||
}
|
||||
|
||||
if (digitCount < sizeof(digits) - 1) {
|
||||
digits[digitCount++] = (char)('0' + remainder);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
digits[digitCount] = '\0';
|
||||
|
||||
for (size_t i = 0; i < digitCount; ++i) {
|
||||
out[i] = digits[digitCount - 1 - i];
|
||||
}
|
||||
out[digitCount] = '\0';
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user