diff --git a/.clang-format b/.clang-format index d180071..ed5e6cc 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,3 @@ BasedOnStyle: LLVM IndentWidth: 4 -ColumnLimit: 80 +ColumnLimit: 120 diff --git a/libs/hashes/src/sha2.c b/libs/hashes/src/sha2.c index bb998ae..ec5f49f 100644 --- a/libs/hashes/src/sha2.c +++ b/libs/hashes/src/sha2.c @@ -6,17 +6,14 @@ * */ static const uint32_t k[64] = { - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, - 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, - 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, - 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, - 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; static const uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; @@ -24,9 +21,7 @@ static const uint32_t h[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, // equivalent to a left rotation by (w-n). Here w = 32 uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }; uint32_t ch(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (~x & z); }; -uint32_t major(uint32_t x, uint32_t y, uint32_t z) { - return (x & y) ^ (x & z) ^ (y & z); -}; +uint32_t major(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (x & z) ^ (y & z); }; uint32_t Sigma0(uint32_t x) { return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); }; uint32_t Sigma1(uint32_t x) { return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); } uint32_t sigma0(uint32_t x) { return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3); }; @@ -41,8 +36,7 @@ void sha256_process(sha256 *hash) { for (int i = 0, j = 0; i < 16; i++, j += 4) { // This operation merges the first 4 bytes starting from the jth // position - w[i] = (hash->bytes[j] << 24) | (hash->bytes[j + 1] << 16) | - (hash->bytes[j + 2] << 8) | (hash->bytes[j + 3]); + w[i] = (hash->bytes[j] << 24) | (hash->bytes[j + 1] << 16) | (hash->bytes[j + 2] << 8) | (hash->bytes[j + 3]); } for (int i = 16; i < 64; i++) { diff --git a/libs/hashes/tests/sha2.c b/libs/hashes/tests/sha2.c index 62d8172..d0bed76 100644 --- a/libs/hashes/tests/sha2.c +++ b/libs/hashes/tests/sha2.c @@ -11,9 +11,8 @@ void test_sha256_empty() { sha256_update(&hash, (uint8_t *)"", 0); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "1029873362495540970295352123225813227897999006481980349933793970011156" - "65086549"; + char *expected_result = "1029873362495540970295352123225813227897999006481980349933793970011156" + "65086549"; assert_that(strcmp(str, expected_result) == 0); } @@ -23,9 +22,8 @@ void test_sha256_single_char() { sha256_update(&hash, bytes, 1); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "9163488015244361753484262128703993804158108125491405800297860105017955" - "6493499"; + char *expected_result = "9163488015244361753484262128703993804158108125491405800297860105017955" + "6493499"; assert_that(strcmp(str, expected_result) == 0); } @@ -35,9 +33,8 @@ void test_sha256_short_string() { sha256_update(&hash, bytes, 3); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "8434236848709080036652383492814226366010488369501651437746298582971681" - "7089965"; // SHA-256("abc") result + char *expected_result = "8434236848709080036652383492814226366010488369501651437746298582971681" + "7089965"; // SHA-256("abc") result assert_that(strcmp(str, expected_result) == 0); } @@ -47,9 +44,8 @@ void test_sha256_long_string() { sha256_update(&hash, bytes, strlen((char *)bytes)); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "9754582991727437845042049306863340363436609792361092711364013968352019" - "4405778"; + char *expected_result = "9754582991727437845042049306863340363436609792361092711364013968352019" + "4405778"; assert_that(strcmp(str, expected_result) == 0); } @@ -59,9 +55,8 @@ void test_sha256_binary_data() { sha256_update(&hash, bytes, sizeof(bytes)); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "1155050113059766760852447818468967315849566989347365055840175606142539" - "40679982"; // SHA-256 of binary data + char *expected_result = "1155050113059766760852447818468967315849566989347365055840175606142539" + "40679982"; // SHA-256 of binary data assert_that(strcmp(str, expected_result) == 0); } @@ -73,9 +68,8 @@ void test_sha256_repeated_updates() { sha256_update(&hash, part2, strlen((char *)part2)); u256 digest = sha256_finalize(&hash); char *str = u256_to_string(digest); - char *expected_result = - "2233181402739248830710573607548020574234866647396933363417373207145921" - "5699411"; + char *expected_result = "2233181402739248830710573607548020574234866647396933363417373207145921" + "5699411"; assert_that(strcmp(str, expected_result) == 0); } diff --git a/libs/primitive-types/include/uint.h b/libs/primitive-types/include/uint.h index 9119c29..1d69166 100644 --- a/libs/primitive-types/include/uint.h +++ b/libs/primitive-types/include/uint.h @@ -19,9 +19,9 @@ * uint64_t parts[4]; * } u256; */ -#define DEFINE_UINT_DATA_TYPE(NAME, WORDS) \ - typedef struct { \ - uint64_t parts[WORDS]; \ +#define DEFINE_UINT_DATA_TYPE(NAME, WORDS) \ + typedef struct { \ + uint64_t parts[WORDS]; \ } NAME; /** \ @@ -34,10 +34,10 @@ * - `overflow`: An integer flag indicating whether an overflow \ * occurred (1 if true, 0 if false). \ */ -#define DEFINE_UINT_OVERFLOW_OP(NAME) \ - typedef struct { \ - NAME res; \ - int overflow; \ +#define DEFINE_UINT_OVERFLOW_OP(NAME) \ + typedef struct { \ + NAME res; \ + int overflow; \ } NAME##_overflow_op; /** \ @@ -48,10 +48,10 @@ * - `rem`: A field of type `NAME`, representing the remainder. \ * \ */ -#define DEFINE_UINT_DIV_OP(NAME) \ - typedef struct { \ - NAME quot; \ - NAME rem; \ +#define DEFINE_UINT_DIV_OP(NAME) \ + typedef struct { \ + NAME quot; \ + NAME rem; \ } NAME##_div_op; /** @@ -59,39 +59,37 @@ * * Returns a structure containing the result and an overflow flag. */ -#define DEFINE_UINT_OVERFLOW_ADD(NAME, WORDS) \ - NAME##_overflow_op NAME##_overflow_add(NAME a, NAME b) { \ - uint64_t carry = 0; \ - NAME##_overflow_op op; \ - for (int i = 0; i < WORDS; i++) { \ - u64_overflow_op addition = \ - u64_overflow_add(a.parts[i], b.parts[i]); \ - u64_overflow_op carry_addition = \ - u64_overflow_add(addition.res, carry); \ - op.res.parts[i] = carry_addition.res; \ - carry = addition.overflow + carry_addition.overflow; \ - } \ - op.overflow = (carry > 0); \ - return op; \ +#define DEFINE_UINT_OVERFLOW_ADD(NAME, WORDS) \ + NAME##_overflow_op NAME##_overflow_add(NAME a, NAME b) { \ + uint64_t carry = 0; \ + NAME##_overflow_op op; \ + for (int i = 0; i < WORDS; i++) { \ + u64_overflow_op addition = u64_overflow_add(a.parts[i], b.parts[i]); \ + u64_overflow_op carry_addition = u64_overflow_add(addition.res, carry); \ + op.res.parts[i] = carry_addition.res; \ + carry = addition.overflow + carry_addition.overflow; \ + } \ + op.overflow = (carry > 0); \ + return op; \ } -/** \ - * Subtracts one unsigned integer from another and detects overflow. \ - * \ - * Returns a structure containing the result and an overflow flag. \ - */ \ -#define DEFINE_UINT_OVERFLOW_SUB(NAME, WORDS) \ - NAME##_overflow_op NAME##_overflow_sub(NAME a, NAME b) { \ - uint64_t carry = 0; \ - NAME##_overflow_op op; \ - for (int i = 0; i < WORDS; i++) { \ - u64_overflow_op sub = u64_overflow_sub(a.parts[i], b.parts[i]); \ - u64_overflow_op carry_sub = u64_overflow_sub(sub.res, carry); \ - op.res.parts[i] = carry_sub.res; \ - carry = sub.overflow + carry_sub.overflow; \ - } \ - op.overflow = (carry > 0); \ - return op; \ +/** \ + * Subtracts one unsigned integer from another and detects overflow. \ + * \ + * Returns a structure containing the result and an overflow flag. \ + */ \ +#define DEFINE_UINT_OVERFLOW_SUB(NAME, WORDS) \ + NAME##_overflow_op NAME##_overflow_sub(NAME a, NAME b) { \ + uint64_t carry = 0; \ + NAME##_overflow_op op; \ + for (int i = 0; i < WORDS; i++) { \ + u64_overflow_op sub = u64_overflow_sub(a.parts[i], b.parts[i]); \ + u64_overflow_op carry_sub = u64_overflow_sub(sub.res, carry); \ + op.res.parts[i] = carry_sub.res; \ + carry = sub.overflow + carry_sub.overflow; \ + } \ + op.overflow = (carry > 0); \ + return op; \ } /** @@ -99,12 +97,12 @@ * * Returns the result of `a & b`. */ -#define DEFINE_UINT_BITADD(NAME, WORDS) \ - NAME NAME##_bitand(NAME a, NAME b) { \ - NAME result = NAME##_zero(); \ - for (int i = 0; i < WORDS; i++) \ - result.parts[i] = a.parts[i] & b.parts[i]; \ - return result; \ +#define DEFINE_UINT_BITADD(NAME, WORDS) \ + NAME NAME##_bitand(NAME a, NAME b) { \ + NAME result = NAME##_zero(); \ + for (int i = 0; i < WORDS; i++) \ + result.parts[i] = a.parts[i] & b.parts[i]; \ + return result; \ } /** @@ -112,79 +110,76 @@ * * Returns the result of `a | b`. */ -#define DEFINE_UINT_BITOR(NAME, WORDS) \ - NAME NAME##_bitor(NAME a, NAME b) { \ - NAME result = NAME##_zero(); \ - for (int i = 0; i < WORDS; i++) \ - result.parts[i] = a.parts[i] | b.parts[i]; \ - return result; \ +#define DEFINE_UINT_BITOR(NAME, WORDS) \ + NAME NAME##_bitor(NAME a, NAME b) { \ + NAME result = NAME##_zero(); \ + for (int i = 0; i < WORDS; i++) \ + result.parts[i] = a.parts[i] | b.parts[i]; \ + return result; \ } -/** \ - * Performs a bitwise XOR operation. \ - * \ - * Returns the result of `a ^ b`. \ - */ \ -#define DEFINE_UINT_BITXOR(NAME, WORDS) \ - NAME NAME##_bitxor(NAME a, NAME b) { \ - NAME result = NAME##_zero(); \ - for (int i = 0; i < WORDS; i++) \ - result.parts[i] = a.parts[i] ^ b.parts[i]; \ - return result; \ +/** \ + * Performs a bitwise XOR operation. \ + * \ + * Returns the result of `a ^ b`. \ + */ \ +#define DEFINE_UINT_BITXOR(NAME, WORDS) \ + NAME NAME##_bitxor(NAME a, NAME b) { \ + NAME result = NAME##_zero(); \ + for (int i = 0; i < WORDS; i++) \ + result.parts[i] = a.parts[i] ^ b.parts[i]; \ + return result; \ } -/** \ - * Performs a bitwise NOT operation. \ - * \ - * Returns the result of `~a`. \ - */ \ -#define DEFINE_UINT_BITNOT(NAME, WORDS) \ - NAME NAME##_bitnot(NAME a) { \ - NAME result = NAME##_zero(); \ - for (int i = 0; i < WORDS; i++) \ - result.parts[i] = ~a.parts[i]; \ - return result; \ +/** \ + * Performs a bitwise NOT operation. \ + * \ + * Returns the result of `~a`. \ + */ \ +#define DEFINE_UINT_BITNOT(NAME, WORDS) \ + NAME NAME##_bitnot(NAME a) { \ + NAME result = NAME##_zero(); \ + for (int i = 0; i < WORDS; i++) \ + result.parts[i] = ~a.parts[i]; \ + return result; \ } /** \ * Multiplies two unsigned integers and detects overflow. \ * \ * Returns a structure containing the result and an overflow flag. \ */ -#define DEFINE_UINT_OVERFLOW_MUL(NAME, WORDS) \ - NAME##_overflow_op NAME##_overflow_mul(NAME a, NAME b) { \ - uint64_t result[WORDS * 2]; \ - for (int i = 0; i < WORDS * 2; i++) \ - result[i] = 0; \ - for (int i = 0; i < WORDS; i++) { \ - uint64_t carry = 0; \ - for (int j = 0; j < WORDS; j++) { \ - /* calculate result */ \ - u64_mul_op mul = u64_mul(a.parts[j], b.parts[i]); \ - uint64_t current = result[i + j]; \ - u64_overflow_op addition = u64_overflow_add(mul.res, current); \ - result[i + j] = addition.res; \ - /* calculate carry */ \ - uint64_t carry_current = result[i + j + 1]; \ - u64_overflow_op carry_addition = \ - u64_overflow_add(mul.carry + addition.overflow, carry); \ - u64_overflow_op current_carry_addition = \ - u64_overflow_add(carry_addition.res, carry_current); \ - result[i + j + 1] = current_carry_addition.res; \ - carry = \ - carry_addition.overflow | current_carry_addition.overflow; \ - } \ - } \ - int overflow = 0; \ - for (int i = 4; i < WORDS * 2; i++) { \ - if (result[i] != 0) { \ - overflow = 1; \ - break; \ - } \ - } \ - NAME##_overflow_op op; \ - for (int i = 0; i < WORDS; i++) \ - op.res.parts[i] = result[i]; \ - op.overflow = overflow; \ - return op; \ +#define DEFINE_UINT_OVERFLOW_MUL(NAME, WORDS) \ + NAME##_overflow_op NAME##_overflow_mul(NAME a, NAME b) { \ + uint64_t result[WORDS * 2]; \ + for (int i = 0; i < WORDS * 2; i++) \ + result[i] = 0; \ + for (int i = 0; i < WORDS; i++) { \ + uint64_t carry = 0; \ + for (int j = 0; j < WORDS; j++) { \ + /* calculate result */ \ + u64_mul_op mul = u64_mul(a.parts[j], b.parts[i]); \ + uint64_t current = result[i + j]; \ + u64_overflow_op addition = u64_overflow_add(mul.res, current); \ + result[i + j] = addition.res; \ + /* calculate carry */ \ + uint64_t carry_current = result[i + j + 1]; \ + u64_overflow_op carry_addition = u64_overflow_add(mul.carry + addition.overflow, carry); \ + u64_overflow_op current_carry_addition = u64_overflow_add(carry_addition.res, carry_current); \ + result[i + j + 1] = current_carry_addition.res; \ + carry = carry_addition.overflow | current_carry_addition.overflow; \ + } \ + } \ + int overflow = 0; \ + for (int i = 4; i < WORDS * 2; i++) { \ + if (result[i] != 0) { \ + overflow = 1; \ + break; \ + } \ + } \ + NAME##_overflow_op op; \ + for (int i = 0; i < WORDS; i++) \ + op.res.parts[i] = result[i]; \ + op.overflow = overflow; \ + return op; \ } /** \ @@ -192,15 +187,14 @@ * \ * Returns the number of bits required to represent `a`. \ */ -#define DEFINE_UINT_BITS(NAME, WORDS) \ - int NAME##_bits(NAME a) { \ - for (int i = 1; i < WORDS; i++) { \ - if (a.parts[WORDS - i] > 0) { \ - return 64 * (WORDS - i + 1) - \ - u64_leading_zeros(a.parts[WORDS - i]); \ - } \ - } \ - return 64 - u64_leading_zeros(a.parts[0]); \ +#define DEFINE_UINT_BITS(NAME, WORDS) \ + int NAME##_bits(NAME a) { \ + for (int i = 1; i < WORDS; i++) { \ + if (a.parts[WORDS - i] > 0) { \ + return 64 * (WORDS - i + 1) - u64_leading_zeros(a.parts[WORDS - i]); \ + } \ + } \ + return 64 - u64_leading_zeros(a.parts[0]); \ } /** \ @@ -208,25 +202,24 @@ * \ * Returns the result of shifting `a` by `shift` bits to the left. \ */ -#define DEFINE_UINT_SHL(NAME, WORDS) \ - NAME NAME##_shl(NAME a, int shift) { \ - NAME result = NAME##_zero(); \ - int shift_start = shift / 64; \ - int shift_mod = shift % 64; \ - \ - for (int i = shift_start; i < WORDS; i++) { \ - result.parts[i] = a.parts[i - shift_start] << shift_mod; \ - } \ - \ - /* calculate carry */ \ - if (shift_mod > 0) { \ - for (int i = shift_start + 1; i < WORDS; i++) { \ - result.parts[i] += \ - a.parts[i - shift_start - 1] >> (64 - shift_mod); \ - } \ - } \ - \ - return result; \ +#define DEFINE_UINT_SHL(NAME, WORDS) \ + NAME NAME##_shl(NAME a, int shift) { \ + NAME result = NAME##_zero(); \ + int shift_start = shift / 64; \ + int shift_mod = shift % 64; \ + \ + for (int i = shift_start; i < WORDS; i++) { \ + result.parts[i] = a.parts[i - shift_start] << shift_mod; \ + } \ + \ + /* calculate carry */ \ + if (shift_mod > 0) { \ + for (int i = shift_start + 1; i < WORDS; i++) { \ + result.parts[i] += a.parts[i - shift_start - 1] >> (64 - shift_mod); \ + } \ + } \ + \ + return result; \ } /** \ @@ -234,25 +227,24 @@ * \ * Returns the result of shifting `a` by `shift` bits to the right. \ */ -#define DEFINE_UINT_SHR(NAME, WORDS) \ - NAME NAME##_shr(NAME a, int shift) { \ - NAME result = NAME##_zero(); \ - int shift_start = shift / 64; \ - int shift_mod = shift % 64; \ - \ - for (int i = shift_start; i < WORDS; i++) { \ - result.parts[i - shift_start] = a.parts[i] >> shift_mod; \ - } \ - \ - /* calculate carry */ \ - if (shift_mod > 0) { \ - for (int i = shift_start + 1; i < WORDS; i++) { \ - result.parts[i - shift_start - 1] += a.parts[i] \ - << (64 - shift_mod); \ - } \ - } \ - \ - return result; \ +#define DEFINE_UINT_SHR(NAME, WORDS) \ + NAME NAME##_shr(NAME a, int shift) { \ + NAME result = NAME##_zero(); \ + int shift_start = shift / 64; \ + int shift_mod = shift % 64; \ + \ + for (int i = shift_start; i < WORDS; i++) { \ + result.parts[i - shift_start] = a.parts[i] >> shift_mod; \ + } \ + \ + /* calculate carry */ \ + if (shift_mod > 0) { \ + for (int i = shift_start + 1; i < WORDS; i++) { \ + result.parts[i - shift_start - 1] += a.parts[i] << (64 - shift_mod); \ + } \ + } \ + \ + return result; \ } /** \ @@ -261,41 +253,40 @@ * \ * Returns a structure containing the quotient and remainder of `a / b`. \ */ -#define DEFINE_UINT_DIV_MOD(NAME, WORDS) \ - NAME##_div_op NAME##_divmod(NAME a, NAME b) { \ - int a_bits = NAME##_bits(a); \ - int b_bits = NAME##_bits(b); \ - NAME quot = NAME##_zero(); \ - NAME rem = a; \ - assert(b_bits != 0); \ - if (a_bits < b_bits) \ - return (NAME##_div_op){.quot = quot, .rem = rem}; \ - \ - int shift = a_bits - b_bits; \ - NAME shift_copy = NAME##_shl(b, shift); \ - while (1) { \ - /* rem >= shift_copy */ \ - if (NAME##_cmp(rem, shift_copy) >= 0) { \ - quot.parts[shift / 64] |= \ - ((uint64_t)1 << (uint64_t)(shift % 64)); \ - NAME##_overflow_op sub = NAME##_overflow_sub(rem, shift_copy); \ - rem = sub.res; \ - } \ - if (shift == 0) \ - break; \ - shift -= 1; \ - shift_copy = NAME##_shr(shift_copy, 1); \ - } \ - \ - return (NAME##_div_op){.quot = quot, .rem = rem}; \ +#define DEFINE_UINT_DIV_MOD(NAME, WORDS) \ + NAME##_div_op NAME##_divmod(NAME a, NAME b) { \ + int a_bits = NAME##_bits(a); \ + int b_bits = NAME##_bits(b); \ + NAME quot = NAME##_zero(); \ + NAME rem = a; \ + assert(b_bits != 0); \ + if (a_bits < b_bits) \ + return (NAME##_div_op){.quot = quot, .rem = rem}; \ + \ + int shift = a_bits - b_bits; \ + NAME shift_copy = NAME##_shl(b, shift); \ + while (1) { \ + /* rem >= shift_copy */ \ + if (NAME##_cmp(rem, shift_copy) >= 0) { \ + quot.parts[shift / 64] |= ((uint64_t)1 << (uint64_t)(shift % 64)); \ + NAME##_overflow_op sub = NAME##_overflow_sub(rem, shift_copy); \ + rem = sub.res; \ + } \ + if (shift == 0) \ + break; \ + shift -= 1; \ + shift_copy = NAME##_shr(shift_copy, 1); \ + } \ + \ + return (NAME##_div_op){.quot = quot, .rem = rem}; \ } -#define DEFINE_UINT_ZERO(NAME, WORDS) \ - NAME NAME##_zero() { \ - NAME result; \ - for (int i = 0; i < WORDS; i++) \ - result.parts[i] = 0; \ - return result; \ +#define DEFINE_UINT_ZERO(NAME, WORDS) \ + NAME NAME##_zero() { \ + NAME result; \ + for (int i = 0; i < WORDS; i++) \ + result.parts[i] = 0; \ + return result; \ } /** \ @@ -303,93 +294,83 @@ * \ * Returns the unsigned integer represented by the string `str`. \ */ -#define DEFINE_UINT_FROM_DEC_STRING(NAME, WORDS) \ - NAME NAME##_from_dec_string(char *str) { \ - NAME result = NAME##_zero(); \ - int len = strlen(str); \ - for (int i = 0; i < len; i++) { \ - uint64_t digit = str[i] - '0'; \ - NAME##_overflow_op mul = \ - NAME##_overflow_mul(result, NAME##_from_u64(10)); \ - NAME##_overflow_op addition = \ - NAME##_overflow_add(mul.res, NAME##_from_u64(digit)); \ - result = addition.res; \ - } \ - return result; \ +#define DEFINE_UINT_FROM_DEC_STRING(NAME, WORDS) \ + NAME NAME##_from_dec_string(char *str) { \ + NAME result = NAME##_zero(); \ + int len = strlen(str); \ + for (int i = 0; i < len; i++) { \ + uint64_t digit = str[i] - '0'; \ + NAME##_overflow_op mul = NAME##_overflow_mul(result, NAME##_from_u64(10)); \ + NAME##_overflow_op addition = NAME##_overflow_add(mul.res, NAME##_from_u64(digit)); \ + result = addition.res; \ + } \ + return result; \ } -#define DEFINE_UINT_FROM_U64(NAME, WORDS) \ - NAME NAME##_from_u64(uint64_t a) { \ - NAME result = NAME##_zero(); \ - result.parts[0] = a; \ - return result; \ +#define DEFINE_UINT_FROM_U64(NAME, WORDS) \ + NAME NAME##_from_u64(uint64_t a) { \ + NAME result = NAME##_zero(); \ + result.parts[0] = a; \ + return result; \ } -#define DEFINE_UINT_FROM_BYTES_BIG_ENDIAN(NAME, WORDS) \ - NAME NAME##_from_bytes_big_endian(uint8_t bytes[32]) { \ - NAME result = NAME##_zero(); \ - for (int i = WORDS - 1, j = 0; i >= 0; i--, j++) { \ - result.parts[i] = ((uint64_t)bytes[j * 8] << 56) | \ - ((uint64_t)bytes[j * 8 + 1] << 48) | \ - ((uint64_t)bytes[j * 8 + 2] << 40) | \ - ((uint64_t)bytes[j * 8 + 3] << 32) | \ - ((uint64_t)bytes[j * 8 + 4] << 24) | \ - ((uint64_t)bytes[j * 8 + 5] << 16) | \ - ((uint64_t)bytes[j * 8 + 6] << 8) | \ - ((uint64_t)bytes[j * 8 + 7]); \ - } \ - return result; \ +#define DEFINE_UINT_FROM_BYTES_BIG_ENDIAN(NAME, WORDS) \ + NAME NAME##_from_bytes_big_endian(uint8_t bytes[32]) { \ + NAME result = NAME##_zero(); \ + for (int i = WORDS - 1, j = 0; i >= 0; i--, j++) { \ + result.parts[i] = ((uint64_t)bytes[j * 8] << 56) | ((uint64_t)bytes[j * 8 + 1] << 48) | \ + ((uint64_t)bytes[j * 8 + 2] << 40) | ((uint64_t)bytes[j * 8 + 3] << 32) | \ + ((uint64_t)bytes[j * 8 + 4] << 24) | ((uint64_t)bytes[j * 8 + 5] << 16) | \ + ((uint64_t)bytes[j * 8 + 6] << 8) | ((uint64_t)bytes[j * 8 + 7]); \ + } \ + return result; \ } -#define DEFINE_UINT_GET_BYTES_BIG_ENDIAN(NAME, WORDS) \ - void NAME##_get_bytes_big_endian(uint8_t buffer[32], NAME value) { \ - for (int i = WORDS - 1, j = 0; i >= 0; i--, j++) { \ - buffer[i * 8] = (value.parts[j] >> 56) & 0xFF; \ - buffer[i * 8 + 1] = (value.parts[j] >> 48) & 0xFF; \ - buffer[i * 8 + 2] = (value.parts[j] >> 40) & 0xFF; \ - buffer[i * 8 + 3] = (value.parts[j] >> 32) & 0xFF; \ - buffer[i * 8 + 4] = (value.parts[j] >> 24) & 0xFF; \ - buffer[i * 8 + 5] = (value.parts[j] >> 16) & 0xFF; \ - buffer[i * 8 + 6] = (value.parts[j] >> 8) & 0xFF; \ - buffer[i * 8 + 7] = value.parts[j] & 0xFF; \ - } \ +#define DEFINE_UINT_GET_BYTES_BIG_ENDIAN(NAME, WORDS) \ + void NAME##_get_bytes_big_endian(uint8_t buffer[32], NAME value) { \ + for (int i = WORDS - 1, j = 0; i >= 0; i--, j++) { \ + buffer[i * 8] = (value.parts[j] >> 56) & 0xFF; \ + buffer[i * 8 + 1] = (value.parts[j] >> 48) & 0xFF; \ + buffer[i * 8 + 2] = (value.parts[j] >> 40) & 0xFF; \ + buffer[i * 8 + 3] = (value.parts[j] >> 32) & 0xFF; \ + buffer[i * 8 + 4] = (value.parts[j] >> 24) & 0xFF; \ + buffer[i * 8 + 5] = (value.parts[j] >> 16) & 0xFF; \ + buffer[i * 8 + 6] = (value.parts[j] >> 8) & 0xFF; \ + buffer[i * 8 + 7] = value.parts[j] & 0xFF; \ + } \ } -#define DEFINE_UINT_FROM_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ - NAME NAME##_from_bytes_little_endian(uint8_t bytes[32]) { \ - NAME result = NAME##_zero(); \ - for (int i = 0; i < WORDS; i++) { \ - result.parts[i] = ((uint64_t)bytes[i * 8]) | \ - ((uint64_t)bytes[i * 8 + 1] << 8) | \ - ((uint64_t)bytes[i * 8 + 2] << 16) | \ - ((uint64_t)bytes[i * 8 + 3] << 24) | \ - ((uint64_t)bytes[i * 8 + 4] << 32) | \ - ((uint64_t)bytes[i * 8 + 5] << 40) | \ - ((uint64_t)bytes[i * 8 + 6] << 48) | \ - ((uint64_t)bytes[i * 8 + 7] << 56); \ - } \ - return result; \ +#define DEFINE_UINT_FROM_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ + NAME NAME##_from_bytes_little_endian(uint8_t bytes[32]) { \ + NAME result = NAME##_zero(); \ + for (int i = 0; i < WORDS; i++) { \ + result.parts[i] = ((uint64_t)bytes[i * 8]) | ((uint64_t)bytes[i * 8 + 1] << 8) | \ + ((uint64_t)bytes[i * 8 + 2] << 16) | ((uint64_t)bytes[i * 8 + 3] << 24) | \ + ((uint64_t)bytes[i * 8 + 4] << 32) | ((uint64_t)bytes[i * 8 + 5] << 40) | \ + ((uint64_t)bytes[i * 8 + 6] << 48) | ((uint64_t)bytes[i * 8 + 7] << 56); \ + } \ + return result; \ } -#define DEFINE_UINT_GET_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ - void NAME##_get_bytes_little_endian(uint8_t buffer[32], NAME value) { \ - for (int i = 0; i < WORDS; i++) { \ - buffer[i * 8] = value.parts[i] & 0xFF; \ - buffer[i * 8 + 1] = (value.parts[i] >> 8) & 0xFF; \ - buffer[i * 8 + 2] = (value.parts[i] >> 16) & 0xFF; \ - buffer[i * 8 + 3] = (value.parts[i] >> 24) & 0xFF; \ - buffer[i * 8 + 4] = (value.parts[i] >> 32) & 0xFF; \ - buffer[i * 8 + 5] = (value.parts[i] >> 40) & 0xFF; \ - buffer[i * 8 + 6] = (value.parts[i] >> 48) & 0xFF; \ - buffer[i * 8 + 7] = (value.parts[i] >> 56) & 0xFF; \ - } \ +#define DEFINE_UINT_GET_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ + void NAME##_get_bytes_little_endian(uint8_t buffer[32], NAME value) { \ + for (int i = 0; i < WORDS; i++) { \ + buffer[i * 8] = value.parts[i] & 0xFF; \ + buffer[i * 8 + 1] = (value.parts[i] >> 8) & 0xFF; \ + buffer[i * 8 + 2] = (value.parts[i] >> 16) & 0xFF; \ + buffer[i * 8 + 3] = (value.parts[i] >> 24) & 0xFF; \ + buffer[i * 8 + 4] = (value.parts[i] >> 32) & 0xFF; \ + buffer[i * 8 + 5] = (value.parts[i] >> 40) & 0xFF; \ + buffer[i * 8 + 6] = (value.parts[i] >> 48) & 0xFF; \ + buffer[i * 8 + 7] = (value.parts[i] >> 56) & 0xFF; \ + } \ } -#define DEFINE_UINT_ONE(NAME, WORDS) \ - NAME NAME##_one() { \ - NAME result = NAME##_zero(); \ - result.parts[0] = 1; \ - return result; \ +#define DEFINE_UINT_ONE(NAME, WORDS) \ + NAME NAME##_one() { \ + NAME result = NAME##_zero(); \ + result.parts[0] = 1; \ + return result; \ } /** @@ -398,30 +379,29 @@ * Returns a heap allocated string representation of the unsigned integer, make * sure to free the pointer after using it. */ -#define DEFINE_UINT_TO_STRING(NAME, WORDS) \ - char *NAME##_to_string(NAME a) { \ - char *result = \ - malloc(WORDS * 20 + 1); /* multiply by 20, since each part can \ - take as much as 20 bytes*/ \ - int i = WORDS * 20 - 1; \ - NAME ten = NAME##_from_u64(10); \ - while (1) { \ - NAME##_div_op div = NAME##_divmod(a, ten); \ - a = div.quot; \ - int digit = div.rem.parts[0] + '0'; \ - result[i] = digit; \ - if (NAME##_is_zero(a)) \ - break; \ - i -= 1; \ - } \ - char *dst = malloc(WORDS * 20 + 1 - i); \ - int k = 0; \ - for (int j = i; j < WORDS * 20 + 1; j++) { \ - dst[k] = result[i + k]; \ - k++; \ - } \ - free(result); \ - return dst; \ +#define DEFINE_UINT_TO_STRING(NAME, WORDS) \ + char *NAME##_to_string(NAME a) { \ + char *result = malloc(WORDS * 20 + 1); /* multiply by 20, since each part can \ + take as much as 20 bytes*/ \ + int i = WORDS * 20 - 1; \ + NAME ten = NAME##_from_u64(10); \ + while (1) { \ + NAME##_div_op div = NAME##_divmod(a, ten); \ + a = div.quot; \ + int digit = div.rem.parts[0] + '0'; \ + result[i] = digit; \ + if (NAME##_is_zero(a)) \ + break; \ + i -= 1; \ + } \ + char *dst = malloc(WORDS * 20 + 1 - i); \ + int k = 0; \ + for (int j = i; j < WORDS * 20 + 1; j++) { \ + dst[k] = result[i + k]; \ + k++; \ + } \ + free(result); \ + return dst; \ } /** @@ -443,52 +423,52 @@ * - Returns `0` if `a == b` * - Returns `1` if `a > b` */ -#define DEFINE_UINT_COMPARE(NAME, WORDS) \ - int NAME##_cmp(NAME a, NAME b) { \ - for (int i = WORDS - 1; i >= 0; i--) { \ - uint64_t a_i = (uint64_t)a.parts[i]; \ - uint64_t b_i = (uint64_t)b.parts[i]; \ - if (a_i < b_i) \ - return -1; \ - else if (a_i > b_i) \ - return 1; \ - } \ - return 0; \ +#define DEFINE_UINT_COMPARE(NAME, WORDS) \ + int NAME##_cmp(NAME a, NAME b) { \ + for (int i = WORDS - 1; i >= 0; i--) { \ + uint64_t a_i = (uint64_t)a.parts[i]; \ + uint64_t b_i = (uint64_t)b.parts[i]; \ + if (a_i < b_i) \ + return -1; \ + else if (a_i > b_i) \ + return 1; \ + } \ + return 0; \ } -/** \ - * Prints an array-like format of the internal \ - * `parts` array of the `NAME` structure. \ - */ \ -#define DEFINE_UINT_RAW_PRINTLN(NAME, WORDS) \ - void NAME##_raw_println(NAME a) { \ - printf("["); \ - for (int i = 0; i < WORDS; i++) { \ - printf("%lu", a.parts[i]); \ - if (i != WORDS - 1) \ - printf(","); \ - } \ - printf("]\n"); \ +/** \ + * Prints an array-like format of the internal \ + * `parts` array of the `NAME` structure. \ + */ \ +#define DEFINE_UINT_RAW_PRINTLN(NAME, WORDS) \ + void NAME##_raw_println(NAME a) { \ + printf("["); \ + for (int i = 0; i < WORDS; i++) { \ + printf("%lu", a.parts[i]); \ + if (i != WORDS - 1) \ + printf(","); \ + } \ + printf("]\n"); \ } -/** \ - * Print the string representation of the structure followed by a newline. \ - */ \ -#define DEFINE_UINT_PRINTLN(NAME, WORDS) \ - void NAME##_println(NAME a) { \ - char *str = NAME##_to_string(a); \ - printf("%s\n", str); \ - free(str); \ +/** \ + * Print the string representation of the structure followed by a newline. \ + */ \ +#define DEFINE_UINT_PRINTLN(NAME, WORDS) \ + void NAME##_println(NAME a) { \ + char *str = NAME##_to_string(a); \ + printf("%s\n", str); \ + free(str); \ } -/** \ - * Print the string representation of the structure. \ - */ \ -#define DEFINE_UINT_PRINT(NAME, WORDS) \ - void NAME##_print(NAME a) { \ - char *str = NAME##_to_string(a); \ - printf("%s\n", str); \ - free(str); \ +/** \ + * Print the string representation of the structure. \ + */ \ +#define DEFINE_UINT_PRINT(NAME, WORDS) \ + void NAME##_print(NAME a) { \ + char *str = NAME##_to_string(a); \ + printf("%s\n", str); \ + free(str); \ } /** * Given the UINT `a` @@ -496,37 +476,37 @@ * - 1 if `a` is zero. * - 0 otherwise. */ -#define DEFINE_UINT_IS_ZERO(NAME) \ +#define DEFINE_UINT_IS_ZERO(NAME) \ int NAME##_is_zero(NAME a) { return NAME##_cmp(a, NAME##_zero()) == 0; } -#define DEFINE_UINT(NAME, WORDS) \ - DEFINE_UINT_DATA_TYPE(NAME, WORDS) \ - DEFINE_UINT_OVERFLOW_OP(NAME) \ - DEFINE_UINT_DIV_OP(NAME) \ - DEFINE_UINT_RAW_PRINTLN(NAME, WORDS) \ - DEFINE_UINT_COMPARE(NAME, WORDS) \ - DEFINE_UINT_ZERO(NAME, WORDS) \ - DEFINE_UINT_ONE(NAME, WORDS) \ - DEFINE_UINT_IS_ZERO(NAME) \ - DEFINE_UINT_FROM_U64(NAME, WORDS) \ - DEFINE_UINT_FROM_BYTES_BIG_ENDIAN(NAME, WORDS) \ - DEFINE_UINT_FROM_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ - DEFINE_UINT_GET_BYTES_BIG_ENDIAN(NAME, WORDS) \ - DEFINE_UINT_GET_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ - DEFINE_UINT_OVERFLOW_ADD(NAME, WORDS) \ - DEFINE_UINT_OVERFLOW_SUB(NAME, WORDS) \ - DEFINE_UINT_OVERFLOW_MUL(NAME, WORDS) \ - DEFINE_UINT_BITADD(NAME, WORDS) \ - DEFINE_UINT_BITOR(NAME, WORDS) \ - DEFINE_UINT_BITXOR(NAME, WORDS) \ - DEFINE_UINT_BITNOT(NAME, WORDS) \ - DEFINE_UINT_SHL(NAME, WORDS) \ - DEFINE_UINT_SHR(NAME, WORDS) \ - DEFINE_UINT_BITS(NAME, WORDS) \ - DEFINE_UINT_DIV_MOD(NAME, WORDS) \ - DEFINE_UINT_FROM_DEC_STRING(NAME, WORDS) \ - DEFINE_UINT_TO_STRING(NAME, WORDS) \ - DEFINE_UINT_PRINT(NAME, WORDS) \ +#define DEFINE_UINT(NAME, WORDS) \ + DEFINE_UINT_DATA_TYPE(NAME, WORDS) \ + DEFINE_UINT_OVERFLOW_OP(NAME) \ + DEFINE_UINT_DIV_OP(NAME) \ + DEFINE_UINT_RAW_PRINTLN(NAME, WORDS) \ + DEFINE_UINT_COMPARE(NAME, WORDS) \ + DEFINE_UINT_ZERO(NAME, WORDS) \ + DEFINE_UINT_ONE(NAME, WORDS) \ + DEFINE_UINT_IS_ZERO(NAME) \ + DEFINE_UINT_FROM_U64(NAME, WORDS) \ + DEFINE_UINT_FROM_BYTES_BIG_ENDIAN(NAME, WORDS) \ + DEFINE_UINT_FROM_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ + DEFINE_UINT_GET_BYTES_BIG_ENDIAN(NAME, WORDS) \ + DEFINE_UINT_GET_BYTES_LITTLE_ENDIAN(NAME, WORDS) \ + DEFINE_UINT_OVERFLOW_ADD(NAME, WORDS) \ + DEFINE_UINT_OVERFLOW_SUB(NAME, WORDS) \ + DEFINE_UINT_OVERFLOW_MUL(NAME, WORDS) \ + DEFINE_UINT_BITADD(NAME, WORDS) \ + DEFINE_UINT_BITOR(NAME, WORDS) \ + DEFINE_UINT_BITXOR(NAME, WORDS) \ + DEFINE_UINT_BITNOT(NAME, WORDS) \ + DEFINE_UINT_SHL(NAME, WORDS) \ + DEFINE_UINT_SHR(NAME, WORDS) \ + DEFINE_UINT_BITS(NAME, WORDS) \ + DEFINE_UINT_DIV_MOD(NAME, WORDS) \ + DEFINE_UINT_FROM_DEC_STRING(NAME, WORDS) \ + DEFINE_UINT_TO_STRING(NAME, WORDS) \ + DEFINE_UINT_PRINT(NAME, WORDS) \ DEFINE_UINT_PRINTLN(NAME, WORDS) #endif diff --git a/libs/primitive-types/tests/u256.c b/libs/primitive-types/tests/u256.c index 425fc28..6323cbd 100644 --- a/libs/primitive-types/tests/u256.c +++ b/libs/primitive-types/tests/u256.c @@ -3,13 +3,10 @@ #include void test_u256_overflow_add() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; - u256 second = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; + u256 second = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; u256_overflow_op result = u256_overflow_add(first, second); - u256 expected_result = {{18446744073709551614ULL, 18446744073709551615ULL, - 2199023255551ULL, 0}}; + u256 expected_result = {{18446744073709551614ULL, 18446744073709551615ULL, 2199023255551ULL, 0}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 0); @@ -19,21 +16,17 @@ void test_u256_overflow_add_with_overflow() { u256 first = {{UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}}; u256 second = {{UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}}; u256_overflow_op result = u256_overflow_add(first, second); - u256 expected_result = { - {UINT64_MAX - 1, UINT64_MAX, UINT64_MAX, UINT64_MAX}}; + u256 expected_result = {{UINT64_MAX - 1, UINT64_MAX, UINT64_MAX, UINT64_MAX}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 1); } void test_u256_overflow_sub() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; - u256 second = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; u256_overflow_op result = u256_overflow_sub(first, second); - u256 expected_result = { - {15526763422372331520ULL, 4427218577690292387ULL, 1088516511498ULL, 0}}; + u256 expected_result = {{15526763422372331520ULL, 4427218577690292387ULL, 1088516511498ULL, 0}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 0); @@ -43,8 +36,7 @@ void test_u256_overflow_sub_with_overflow() { u256 first = {{0, 0, 0, 0}}; u256 second = {{1, 1, 1, 1}}; u256_overflow_op result = u256_overflow_sub(first, second); - u256 expected_result = { - {UINT64_MAX, UINT64_MAX - 1, UINT64_MAX - 1, UINT64_MAX - 1}}; + u256 expected_result = {{UINT64_MAX, UINT64_MAX - 1, UINT64_MAX - 1, UINT64_MAX - 1}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 1); @@ -54,86 +46,69 @@ void test_u256_overflow_mul() { u256 first = {{18446744073709551615ULL, 0, 0, 0}}; u256 second = {{2919980651337220095ULL, 0, 0, 0}}; u256_overflow_op result = u256_overflow_mul(first, second); - u256 expected_result = { - {15526763422372331521ULL, 2919980651337220094ULL, 0, 0}}; + u256 expected_result = {{15526763422372331521ULL, 2919980651337220094ULL, 0, 0}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 0); } void test_u256_overflow_mul_with_overflow() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; - u256 second = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; u256_overflow_op result = u256_overflow_mul(first, second); - u256 expected_result = {{15526763422372331521ULL, 4427218577690292387ULL, - 17870282210899384074ULL, 14019525494141808257ULL}}; + u256 expected_result = { + {15526763422372331521ULL, 4427218577690292387ULL, 17870282210899384074ULL, 14019525494141808257ULL}}; assert_that(u256_cmp(result.res, expected_result) == 0); assert_that(result.overflow == 1); } void test_u256_bitand() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 1ULL}}; - u256 second = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 1ULL}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; u256 result = u256_bitand(first, second); - u256 expected_result = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; + u256 expected_result = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_bitor() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; - u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, - 10995116277ULL, 1ULL}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 1ULL}}; u256 result = u256_bitor(first, second); - u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 1}}; + u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 1}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_bitxor() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 1}}; - u256 second = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 1}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 1}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 1}}; u256 result = u256_bitxor(first, second); - u256 expected_result = { - {15526763422372331520ULL, 4427218577690292387ULL, 1088516511498ULL, 0}}; + u256 expected_result = {{15526763422372331520ULL, 4427218577690292387ULL, 1088516511498ULL, 0}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_bitnot() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; u256 result = u256_bitnot(first); - u256 expected_result = { - {0, 0, 18446742974197923840ULL, 18446744073709551615ULL}}; + u256 expected_result = {{0, 0, 18446742974197923840ULL, 18446744073709551615ULL}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_shl() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; int shift = 130; u256 result = u256_shl(first, shift); - u256 expected_result = { - {0, 0, 18446744073709551612ULL, 18446744073709551615ULL}}; + u256 expected_result = {{0, 0, 18446744073709551612ULL, 18446744073709551615ULL}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_shr() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; int shift = 130; u256 result = u256_shr(first, shift); u256 expected_result = {{274877906943, 0, 0, 0}}; @@ -142,10 +117,8 @@ void test_u256_shr() { } void test_u256_divmod_with_rem() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; - u256 second = { - {2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; + u256 second = {{2919980651337220095ULL, 14019525496019259228ULL, 10995116277ULL, 0}}; u256_div_op result = u256_divmod(first, second); u256 expected_quot = {{100, 0, 0, 0}}; u256 expected_rem = {{3149840045630816355, 0, 0, 0}}; @@ -155,12 +128,10 @@ void test_u256_divmod_with_rem() { } void test_u256_divmod_without_rem() { - u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 first = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; u256 second = {{3, 0, 0, 0}}; u256_div_op result = u256_divmod(first, second); - u256 expected_quot = { - {6148914691236517205ULL, 6148914691236517205ULL, 366503875925ULL, 0}}; + u256 expected_quot = {{6148914691236517205ULL, 6148914691236517205ULL, 366503875925ULL, 0}}; u256 expected_rem = {{0, 0, 0, 0}}; assert_that(u256_cmp(result.quot, expected_quot) == 0); @@ -168,20 +139,15 @@ void test_u256_divmod_without_rem() { } void test_u256_from_string() { - u256 result = u256_from_dec_string( - "374144419156711147060143317175368453031918731001855"); - u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 result = u256_from_dec_string("374144419156711147060143317175368453031918731001855"); + u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_to_string() { - char *result = - u256_to_string((u256){{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}); - char *expected_result = - "374144419156711147060143317175368453031918731001855"; + char *result = u256_to_string((u256){{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}); + char *expected_result = "374144419156711147060143317175368453031918731001855"; assert_that(strcmp(result, expected_result) == 0); } @@ -199,21 +165,17 @@ void test_u256_from_bytes_little_endian() { bytes[i] = 255; } u256 result = u256_from_bytes_little_endian(bytes); - u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, - 1099511627775ULL, 0}}; + u256 expected_result = {{18446744073709551615ULL, 18446744073709551615ULL, 1099511627775ULL, 0}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_get_bytes_little_endian() { - u256 number = u256_from_dec_string( - "374144419156711147060143317175368453031918731001855"); + u256 number = u256_from_dec_string("374144419156711147060143317175368453031918731001855"); uint8_t buffer[32]; u256_get_bytes_little_endian(buffer, number); - uint8_t expected_result[32] = {255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t expected_result[32] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (int i = 0; i < 32; i++) { assert_that(expected_result[i] == buffer[i]); @@ -227,21 +189,17 @@ void test_u256_from_bytes_big_endian() { bytes[i] = 255; } u256 result = u256_from_bytes_big_endian(bytes); - u256 expected_result = {{0, 18446744073692774400ULL, - 18446744073709551615ULL, 18446744073709551615ULL}}; + u256 expected_result = {{0, 18446744073692774400ULL, 18446744073709551615ULL, 18446744073709551615ULL}}; assert_that(u256_cmp(result, expected_result) == 0); } void test_u256_get_bytes_big_endian() { - u256 number = u256_from_dec_string( - "374144419156711147060143317175368453031918731001855"); + u256 number = u256_from_dec_string("374144419156711147060143317175368453031918731001855"); uint8_t buffer[32]; u256_get_bytes_big_endian(buffer, number); - uint8_t expected_result[32] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255}; + uint8_t expected_result[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; for (int i = 0; i < 32; i++) { assert_that(expected_result[i] == buffer[i]); diff --git a/libs/utils/include/test.h b/libs/utils/include/test.h index e245a92..b2bbd76 100644 --- a/libs/utils/include/test.h +++ b/libs/utils/include/test.h @@ -15,21 +15,21 @@ static jmp_buf env; -#define test(test_fn, ...) \ - do { \ - printf("\n=============== %s ===============\n", #test_fn); \ - if (setjmp(env) == 0) { \ - test_fn(__VA_ARGS__); \ - printf("%spassed%s\n", GRN, RESET); \ - } \ +#define test(test_fn, ...) \ + do { \ + printf("\n=============== %s ===============\n", #test_fn); \ + if (setjmp(env) == 0) { \ + test_fn(__VA_ARGS__); \ + printf("%spassed%s\n", GRN, RESET); \ + } \ } while (0) -#define BEGIN_TEST() \ - printf("\n==============================\n"); \ +#define BEGIN_TEST() \ + printf("\n==============================\n"); \ printf("Test suite %s at %s\n", __FILE__, __func__); -#define END_TEST() \ - printf("\nTest suite %s at %s", __FILE__, __func__); \ +#define END_TEST() \ + printf("\nTest suite %s at %s", __FILE__, __func__); \ printf("\n==============================\n"); void test_assert(int expression, char *file, int line) {