diff --git a/src_common/uint256.c b/src_common/uint256.c index 5accf5e..771936f 100644 --- a/src_common/uint256.c +++ b/src_common/uint256.c @@ -24,52 +24,52 @@ static const char HEXDIGITS[] = "0123456789abcdef"; -static uint64_t readUint64BE(uint8_t *buffer) { +static uint64_t readUint64BE(const uint8_t *const buffer) { return (((uint64_t) buffer[0]) << 56) | (((uint64_t) buffer[1]) << 48) | (((uint64_t) buffer[2]) << 40) | (((uint64_t) buffer[3]) << 32) | (((uint64_t) buffer[4]) << 24) | (((uint64_t) buffer[5]) << 16) | (((uint64_t) buffer[6]) << 8) | (((uint64_t) buffer[7])); } -void readu128BE(uint8_t *buffer, uint128_t *target) { +void readu128BE(const uint8_t *const buffer, uint128_t *target) { UPPER_P(target) = readUint64BE(buffer); LOWER_P(target) = readUint64BE(buffer + 8); } -void readu256BE(uint8_t *buffer, uint256_t *target) { +void readu256BE(const uint8_t *const buffer, uint256_t *target) { readu128BE(buffer, &UPPER_P(target)); readu128BE(buffer + 16, &LOWER_P(target)); } -bool zero128(uint128_t *number) { +bool zero128(const uint128_t *const number) { return ((LOWER_P(number) == 0) && (UPPER_P(number) == 0)); } -bool zero256(uint256_t *number) { +bool zero256(const uint256_t *const number) { return (zero128(&LOWER_P(number)) && zero128(&UPPER_P(number))); } -void copy128(uint128_t *target, uint128_t *number) { +void copy128(uint128_t *const target, const uint128_t *const number) { UPPER_P(target) = UPPER_P(number); LOWER_P(target) = LOWER_P(number); } -void copy256(uint256_t *target, uint256_t *number) { +void copy256(uint256_t *const target, const uint256_t *const number) { copy128(&UPPER_P(target), &UPPER_P(number)); copy128(&LOWER_P(target), &LOWER_P(number)); } -void clear128(uint128_t *target) { +void clear128(uint128_t *const target) { UPPER_P(target) = 0; LOWER_P(target) = 0; } -void clear256(uint256_t *target) { +void clear256(uint256_t *const target) { clear128(&UPPER_P(target)); clear128(&LOWER_P(target)); } -void shiftl128(uint128_t *number, uint32_t value, uint128_t *target) { +void shiftl128(const uint128_t *const number, uint32_t value, uint128_t *const target) { if (value >= 128) { clear128(target); } else if (value == 64) { @@ -88,7 +88,7 @@ void shiftl128(uint128_t *number, uint32_t value, uint128_t *target) { } } -void shiftl256(uint256_t *number, uint32_t value, uint256_t *target) { +void shiftl256(const uint256_t *const number, uint32_t value, uint256_t *const target) { if (value >= 256) { clear256(target); } else if (value == 128) { @@ -113,7 +113,7 @@ void shiftl256(uint256_t *number, uint32_t value, uint256_t *target) { } } -void shiftr128(uint128_t *number, uint32_t value, uint128_t *target) { +void shiftr128(const uint128_t *const number, uint32_t value, uint128_t *const target) { if (value >= 128) { clear128(target); } else if (value == 64) { @@ -134,7 +134,7 @@ void shiftr128(uint128_t *number, uint32_t value, uint128_t *target) { } } -void shiftr256(uint256_t *number, uint32_t value, uint256_t *target) { +void shiftr256(const uint256_t *const number, uint32_t value, uint256_t *const target) { if (value >= 256) { clear256(target); } else if (value == 128) { @@ -159,7 +159,7 @@ void shiftr256(uint256_t *number, uint32_t value, uint256_t *target) { } } -uint32_t bits128(uint128_t *number) { +uint32_t bits128(const uint128_t *const number) { uint32_t result = 0; if (UPPER_P(number)) { result = 64; @@ -178,7 +178,7 @@ uint32_t bits128(uint128_t *number) { return result; } -uint32_t bits256(uint256_t *number) { +uint32_t bits256(const uint256_t *const number) { uint32_t result = 0; if (!zero128(&UPPER_P(number))) { result = 128; @@ -199,44 +199,48 @@ uint32_t bits256(uint256_t *number) { return result; } -bool equal128(uint128_t *number1, uint128_t *number2) { +bool equal128(const uint128_t *const number1, const uint128_t *const number2) { return (UPPER_P(number1) == UPPER_P(number2)) && (LOWER_P(number1) == LOWER_P(number2)); } -bool equal256(uint256_t *number1, uint256_t *number2) { +bool equal256(const uint256_t *const number1, const uint256_t *const number2) { return (equal128(&UPPER_P(number1), &UPPER_P(number2)) && equal128(&LOWER_P(number1), &LOWER_P(number2))); } -bool gt128(uint128_t *number1, uint128_t *number2) { +bool gt128(const uint128_t *const number1, const uint128_t *const number2) { if (UPPER_P(number1) == UPPER_P(number2)) { return (LOWER_P(number1) > LOWER_P(number2)); } return (UPPER_P(number1) > UPPER_P(number2)); } -bool gt256(uint256_t *number1, uint256_t *number2) { +bool gt256(const uint256_t *const number1, const uint256_t *const number2) { if (equal128(&UPPER_P(number1), &UPPER_P(number2))) { return gt128(&LOWER_P(number1), &LOWER_P(number2)); } return gt128(&UPPER_P(number1), &UPPER_P(number2)); } -bool gte128(uint128_t *number1, uint128_t *number2) { +bool gte128(const uint128_t *const number1, const uint128_t *const number2) { return gt128(number1, number2) || equal128(number1, number2); } -bool gte256(uint256_t *number1, uint256_t *number2) { +bool gte256(const uint256_t *const number1, const uint256_t *const number2) { return gt256(number1, number2) || equal256(number1, number2); } -void add128(uint128_t *number1, uint128_t *number2, uint128_t *target) { +void add128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target) { UPPER_P(target) = UPPER_P(number1) + UPPER_P(number2) + ((LOWER_P(number1) + LOWER_P(number2)) < LOWER_P(number1)); LOWER_P(target) = LOWER_P(number1) + LOWER_P(number2); } -void add256(uint256_t *number1, uint256_t *number2, uint256_t *target) { +void add256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target) { uint128_t tmp; add128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target)); add128(&LOWER_P(number1), &LOWER_P(number2), &tmp); @@ -249,13 +253,17 @@ void add256(uint256_t *number1, uint256_t *number2, uint256_t *target) { add128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target)); } -void sub128(uint128_t *number1, uint128_t *number2, uint128_t *target) { +void sub128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target) { UPPER_P(target) = UPPER_P(number1) - UPPER_P(number2) - ((LOWER_P(number1) - LOWER_P(number2)) > LOWER_P(number1)); LOWER_P(target) = LOWER_P(number1) - LOWER_P(number2); } -void sub256(uint256_t *number1, uint256_t *number2, uint256_t *target) { +void sub256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target) { uint128_t tmp; sub128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target)); sub128(&LOWER_P(number1), &LOWER_P(number2), &tmp); @@ -268,17 +276,23 @@ void sub256(uint256_t *number1, uint256_t *number2, uint256_t *target) { sub128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target)); } -void or128(uint128_t *number1, uint128_t *number2, uint128_t *target) { +void or128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target) { UPPER_P(target) = UPPER_P(number1) | UPPER_P(number2); LOWER_P(target) = LOWER_P(number1) | LOWER_P(number2); } -void or256(uint256_t *number1, uint256_t *number2, uint256_t *target) { +void or256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target) { or128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target)); or128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target)); } -void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target) { +void mul128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target) { uint64_t top[4] = {UPPER_P(number1) >> 32, UPPER_P(number1) & 0xffffffff, LOWER_P(number1) >> 32, @@ -323,7 +337,7 @@ void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target) { add128(&tmp, &tmp2, target); } -void write_u64_be(uint8_t *buffer, uint64_t value) { +void write_u64_be(uint8_t *const buffer, uint64_t value) { buffer[0] = ((value >> 56) & 0xff); buffer[1] = ((value >> 48) & 0xff); buffer[2] = ((value >> 40) & 0xff); @@ -334,7 +348,7 @@ void write_u64_be(uint8_t *buffer, uint64_t value) { buffer[7] = (value & 0xff); } -void read_u64_be(uint8_t *in, uint64_t *out) { +void read_u64_be(const uint8_t *const in, uint64_t *const out) { uint8_t *out_ptr = (uint8_t *) out; *out_ptr++ = in[7]; *out_ptr++ = in[6]; @@ -346,7 +360,9 @@ void read_u64_be(uint8_t *in, uint64_t *out) { *out_ptr = in[0]; } -void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target) { +void mul256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target) { uint8_t num1[INT256_LENGTH], num2[INT256_LENGTH], result[INT256_LENGTH * 2]; memset(&result, 0, sizeof(result)); for (uint8_t i = 0; i < 4; i++) { @@ -359,7 +375,10 @@ void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target) { } } -void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv, uint128_t *retMod) { +void divmod128(const uint128_t *const l, + const uint128_t *const r, + uint128_t *const retDiv, + uint128_t *const retMod) { uint128_t copyd, adder, resDiv, resMod; uint128_t one; UPPER(one) = 0; @@ -390,7 +409,10 @@ void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv, uint128_t *retMod) } } -void divmod256(uint256_t *l, uint256_t *r, uint256_t *retDiv, uint256_t *retMod) { +void divmod256(const uint256_t *const l, + const uint256_t *const r, + uint256_t *const retDiv, + uint256_t *const retMod) { uint256_t copyd, adder, resDiv, resMod; uint256_t one; clear256(&one); @@ -432,7 +454,10 @@ static void reverseString(char *str, uint32_t length) { } } -bool tostring128(uint128_t *number, uint32_t baseParam, char *out, uint32_t outLength) { +bool tostring128(const uint128_t *const number, + uint32_t baseParam, + char *const out, + uint32_t outLength) { uint128_t rDiv; uint128_t rMod; uint128_t base; @@ -456,7 +481,10 @@ bool tostring128(uint128_t *number, uint32_t baseParam, char *out, uint32_t outL return true; } -bool tostring256(uint256_t *number, uint32_t baseParam, char *out, uint32_t outLength) { +bool tostring256(const uint256_t *const number, + uint32_t baseParam, + char *const out, + uint32_t outLength) { uint256_t rDiv; uint256_t rMod; uint256_t base; diff --git a/src_common/uint256.h b/src_common/uint256.h index ebf6da3..837bfe4 100644 --- a/src_common/uint256.h +++ b/src_common/uint256.h @@ -39,38 +39,57 @@ typedef struct uint256_t { #define UPPER(x) x.elements[0] #define LOWER(x) x.elements[1] -void readu128BE(uint8_t *buffer, uint128_t *target); -void readu256BE(uint8_t *buffer, uint256_t *target); -void write_u64_be(uint8_t *buffer, uint64_t value); -bool zero128(uint128_t *number); -bool zero256(uint256_t *number); -void copy128(uint128_t *target, uint128_t *number); -void copy256(uint256_t *target, uint256_t *number); -void clear128(uint128_t *target); -void clear256(uint256_t *target); -void shiftl128(uint128_t *number, uint32_t value, uint128_t *target); -void shiftr128(uint128_t *number, uint32_t value, uint128_t *target); -void shiftl256(uint256_t *number, uint32_t value, uint256_t *target); -void shiftr256(uint256_t *number, uint32_t value, uint256_t *target); -uint32_t bits128(uint128_t *number); -uint32_t bits256(uint256_t *number); -bool equal128(uint128_t *number1, uint128_t *number2); -bool equal256(uint256_t *number1, uint256_t *number2); -bool gt128(uint128_t *number1, uint128_t *number2); -bool gt256(uint256_t *number1, uint256_t *number2); -bool gte128(uint128_t *number1, uint128_t *number2); -bool gte256(uint256_t *number1, uint256_t *number2); -void add128(uint128_t *number1, uint128_t *number2, uint128_t *target); -void add256(uint256_t *number1, uint256_t *number2, uint256_t *target); -void sub128(uint128_t *number1, uint128_t *number2, uint128_t *target); -void sub256(uint256_t *number1, uint256_t *number2, uint256_t *target); -void or128(uint128_t *number1, uint128_t *number2, uint128_t *target); -void or256(uint256_t *number1, uint256_t *number2, uint256_t *target); -void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target); -void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target); -void divmod128(uint128_t *l, uint128_t *r, uint128_t *div, uint128_t *mod); -void divmod256(uint256_t *l, uint256_t *r, uint256_t *div, uint256_t *mod); -bool tostring128(uint128_t *number, uint32_t base, char *out, uint32_t outLength); -bool tostring256(uint256_t *number, uint32_t base, char *out, uint32_t outLength); +void readu128BE(const uint8_t *const buffer, uint128_t *target); +void readu256BE(const uint8_t *const buffer, uint256_t *target); +void write_u64_be(uint8_t *const buffer, uint64_t value); +void read_u64_be(const uint8_t *const in, uint64_t *const out); +bool zero128(const uint128_t *const number); +bool zero256(const uint256_t *const number); +void copy128(uint128_t *const target, const uint128_t *const number); +void copy256(uint256_t *const target, const uint256_t *const number); +void clear128(uint128_t *const target); +void clear256(uint256_t *const target); +void shiftl128(const uint128_t *const number, uint32_t value, uint128_t *const target); +void shiftr128(const uint128_t *const number, uint32_t value, uint128_t *const target); +void shiftl256(const uint256_t *const number, uint32_t value, uint256_t *const target); +void shiftr256(const uint256_t *const number, uint32_t value, uint256_t *const target); +uint32_t bits128(const uint128_t *const number); +uint32_t bits256(const uint256_t *const number); +bool equal128(const uint128_t *const number1, const uint128_t *const number2); +bool equal256(const uint256_t *const number1, const uint256_t *const number2); +bool gt128(const uint128_t *const number1, const uint128_t *const number2); +bool gt256(const uint256_t *const number1, const uint256_t *const number2); +bool gte128(const uint128_t *const number1, const uint128_t *const number2); +bool gte256(const uint256_t *const number1, const uint256_t *const number2); +void add128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target); +void add256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target); +void sub128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target); +void sub256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target); +void or128(const uint128_t *const number1, const uint128_t *const number2, uint128_t *const target); +void or256(const uint256_t *const number1, const uint256_t *const number2, uint256_t *const target); +void mul128(const uint128_t *const number1, + const uint128_t *const number2, + uint128_t *const target); +void mul256(const uint256_t *const number1, + const uint256_t *const number2, + uint256_t *const target); +void divmod128(const uint128_t *const l, + const uint128_t *const r, + uint128_t *const div, + uint128_t *const mod); +void divmod256(const uint256_t *const l, + const uint256_t *const r, + uint256_t *const div, + uint256_t *const mod); +bool tostring128(const uint128_t *const number, uint32_t base, char *const out, uint32_t outLength); +bool tostring256(const uint256_t *const number, uint32_t base, char *const out, uint32_t outLength); #endif // _UINT256_H_