Skip to content

Commit

Permalink
chore: clang format increment column width (#22)
Browse files Browse the repository at this point in the history
* chore: clang-formatter increment column width to 120

* chore: run fmt
  • Loading branch information
MarcosNicolau authored Jan 19, 2025
1 parent 1800526 commit c38d299
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 491 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 80
ColumnLimit: 120
26 changes: 10 additions & 16 deletions libs/hashes/src/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@
*
*/
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};

// 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); };
Expand All @@ -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++) {
Expand Down
30 changes: 12 additions & 18 deletions libs/hashes/tests/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit c38d299

Please sign in to comment.