Skip to content
This repository was archived by the owner on Jun 8, 2025. It is now read-only.

Commit f061fee

Browse files
committed
sanitizers error fixing; corner case bug fixing,
1 parent b35d6bc commit f061fee

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

lib/keccak/keccak.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if !__has_builtin(__builtin_memcpy) && !defined(__GNUC__)
99
#include <string.h>
1010
#define __builtin_memcpy memcpy
11+
#define __builtin_memset memset
1112
#endif
1213

1314
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
@@ -399,10 +400,10 @@ static inline ALWAYS_INLINE void keccak_update(
399400
size_t i;
400401

401402
size_t block_size_b = ctx->block_size / word_size; // block size in bytes
402-
size_t last_word_unfilled_size = // calculate unfilled space in last word, ignore if all empty
403-
(word_size - (size_t)(ctx->last_word_iter - (uint8_t*)&(ctx->last_word))) % word_size;
404-
size_t state_unfilled_size = // calculate unfilled space in state, ignore if all empty
405-
(block_size_b - (size_t)(ctx->state_iter - ctx->state)) % block_size_b;
403+
size_t last_word_unfilled_size = // calculate unfilled space in last word
404+
(word_size - (size_t)(ctx->last_word_iter - (uint8_t*)&(ctx->last_word)));
405+
size_t state_unfilled_size = // calculate unfilled space in state
406+
(block_size_b - (size_t)(ctx->state_iter - ctx->state));
406407

407408
// fill the last word unfilled space with bytes until it's full
408409
while(last_word_unfilled_size > 0 && size > 0)

test/unittests/test_keccak.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ TEST(helpers, to_hash256_empty)
283283
EXPECT_EQ(h, hash256{});
284284
}
285285

286+
static size_t next_data_size_to_load()
287+
{
288+
static size_t next = 1;
289+
static const size_t sizes[216]
290+
= {225, 50, 73, 72, 285, 273, 139, 299, 193, 243, 43, 102, 294, 262, 209, 114, 88, 94, 179, 233, 248,
291+
182, 34, 246, 106, 148, 198, 77, 160, 289, 115, 249, 198, 26, 203, 21, 24, 65, 222, 187, 170, 281,
292+
285, 185, 261, 121, 130, 270, 22, 227, 235, 206, 116, 205, 171, 30, 163, 94, 66, 98, 112, 271, 98,
293+
91, 181, 181, 145, 290, 12, 261, 183, 82, 40, 25, 40, 164, 293, 150, 96, 195, 218, 122, 240, 99, 7,
294+
71, 155, 165, 180, 238, 131, 271, 286, 186, 255, 62, 65, 110, 205, 86, 2, 83, 182, 225, 5, 156, 138,
295+
139, 146, 3, 250, 51, 184, 282, 9, 109, 34, 245, 169, 63, 240, 100, 151, 134, 74, 125, 225, 30, 215,
296+
199, 275, 131, 128, 89, 37, 195, 286, 220, 117, 183, 32, 132, 51, 122, 244, 104, 59, 73, 45, 188, 115,
297+
231, 179, 217, 16, 110, 58, 142, 186, 215, 201, 223, 198, 152, 64, 9, 113, 39, 141, 209, 170, 84, 209,
298+
16, 92, 64, 85, 239, 128, 88, 298, 153, 28, 269, 30, 38, 46, 97, 242, 119, 102, 299, 103, 288, 251, 187,
299+
192, 8, 57, 293, 221, 43, 184, 207, 50, 129, 203, 45, 139, 0, 129, 82, 285, 254, 214, 213};
300+
301+
next = (next + 1) % 216;
302+
return sizes[next];
303+
}
304+
286305
TEST(keccak, iuf_test_simple)
287306
{
288307
const uint8_t* const data = reinterpret_cast<const uint8_t*>(test_text);
@@ -292,7 +311,7 @@ TEST(keccak, iuf_test_simple)
292311
const auto h256 = keccak256(data, t.input_size);
293312
ASSERT_EQ(to_hex(h256), t.expected_hash256) << t.input_size;
294313

295-
struct ethash_keccak256_context ctx;
314+
struct ethash_keccak256_context ctx = {};
296315
keccak256_init(&ctx);
297316
keccak256_update(&ctx, data, t.input_size);
298317
const auto h2561 = keccak256_final(&ctx);
@@ -328,11 +347,11 @@ TEST(keccak, iuf_test_simple)
328347
i = 0;
329348
while(i < t.input_size)
330349
{
331-
step = (size_t)rand() % 300;
350+
step = next_data_size_to_load();
332351
size_t l = t.input_size - i >= step ? step : t.input_size - i;
333352
keccak256_update(&ctx, &data[i], l);
334353
i = i + step;
335-
}
354+
}
336355
const auto h2563 = keccak256_final(&ctx);
337356
ASSERT_EQ(to_hex(h2563), t.expected_hash256) << t.input_size;
338357
}
@@ -345,7 +364,7 @@ TEST(keccak, iuf_test_simple_2)
345364
for (auto& t : test_cases)
346365
{
347366
{
348-
struct ethash_keccak256_context ctx;
367+
struct ethash_keccak256_context ctx = {};
349368
keccak256_init_2(&ctx);
350369
keccak256_update_2(&ctx, data, t.input_size);
351370
const auto h256 = keccak256_final_2(&ctx);
@@ -355,7 +374,7 @@ TEST(keccak, iuf_test_simple_2)
355374
{
356375
size_t i;
357376

358-
struct ethash_keccak256_context ctx;
377+
struct ethash_keccak256_context ctx = {};
359378
keccak256_init_2(&ctx);
360379
for(i = 0; i < t.input_size; ++i)
361380
{
@@ -368,7 +387,7 @@ TEST(keccak, iuf_test_simple_2)
368387
{
369388
size_t i;
370389
size_t step = 0;
371-
struct ethash_keccak256_context ctx;
390+
struct ethash_keccak256_context ctx = {};
372391
for(step = 1; step < 256; ++step)
373392
{
374393
keccak256_init_2(&ctx);
@@ -384,14 +403,14 @@ TEST(keccak, iuf_test_simple_2)
384403
}
385404

386405
{
387-
struct ethash_keccak256_context ctx;
406+
struct ethash_keccak256_context ctx = {};
388407
keccak256_init_2(&ctx);
389408

390409
size_t i = 0;
391410
size_t step = 0;
392411
while(i < t.input_size)
393412
{
394-
step = (size_t)rand() % 300;
413+
step = next_data_size_to_load();
395414
size_t l = t.input_size - i >= step ? step : t.input_size - i;
396415
keccak256_update_2(&ctx, &data[i], l);
397416
i = i + step;
@@ -400,4 +419,4 @@ TEST(keccak, iuf_test_simple_2)
400419
ASSERT_EQ(to_hex(h256), t.expected_hash256) << t.input_size;
401420
}
402421
}
403-
}
422+
}

0 commit comments

Comments
 (0)