Skip to content

Commit

Permalink
sanitizers error fixing; corner case bug fixing,
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Nov 4, 2022
1 parent b35d6bc commit 00e3487
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/keccak/keccak.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if !__has_builtin(__builtin_memcpy) && !defined(__GNUC__)
#include <string.h>
#define __builtin_memcpy memcpy
#define __builtin_memset memset
#endif

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

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

// fill the last word unfilled space with bytes until it's full
while(last_word_unfilled_size > 0 && size > 0)
Expand Down
18 changes: 10 additions & 8 deletions test/unittests/test_keccak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <gtest/gtest.h>

#include <random>

using namespace ethash;

struct keccak_test_case
Expand Down Expand Up @@ -292,7 +294,7 @@ TEST(keccak, iuf_test_simple)
const auto h256 = keccak256(data, t.input_size);
ASSERT_EQ(to_hex(h256), t.expected_hash256) << t.input_size;

struct ethash_keccak256_context ctx;
struct ethash_keccak256_context ctx = {};
keccak256_init(&ctx);
keccak256_update(&ctx, data, t.input_size);
const auto h2561 = keccak256_final(&ctx);
Expand Down Expand Up @@ -328,7 +330,7 @@ TEST(keccak, iuf_test_simple)
i = 0;
while(i < t.input_size)
{
step = (size_t)rand() % 300;
step = (size_t)arc4random() % 300;
size_t l = t.input_size - i >= step ? step : t.input_size - i;
keccak256_update(&ctx, &data[i], l);
i = i + step;
Expand All @@ -345,7 +347,7 @@ TEST(keccak, iuf_test_simple_2)
for (auto& t : test_cases)
{
{
struct ethash_keccak256_context ctx;
struct ethash_keccak256_context ctx = {};
keccak256_init_2(&ctx);
keccak256_update_2(&ctx, data, t.input_size);
const auto h256 = keccak256_final_2(&ctx);
Expand All @@ -355,7 +357,7 @@ TEST(keccak, iuf_test_simple_2)
{
size_t i;

struct ethash_keccak256_context ctx;
struct ethash_keccak256_context ctx = {};
keccak256_init_2(&ctx);
for(i = 0; i < t.input_size; ++i)
{
Expand All @@ -368,7 +370,7 @@ TEST(keccak, iuf_test_simple_2)
{
size_t i;
size_t step = 0;
struct ethash_keccak256_context ctx;
struct ethash_keccak256_context ctx = {};
for(step = 1; step < 256; ++step)
{
keccak256_init_2(&ctx);
Expand All @@ -384,14 +386,14 @@ TEST(keccak, iuf_test_simple_2)
}

{
struct ethash_keccak256_context ctx;
struct ethash_keccak256_context ctx = {};
keccak256_init_2(&ctx);

size_t i = 0;
size_t step = 0;
while(i < t.input_size)
{
step = (size_t)rand() % 300;
step = (size_t)arc4random() % 300;
size_t l = t.input_size - i >= step ? step : t.input_size - i;
keccak256_update_2(&ctx, &data[i], l);
i = i + step;
Expand All @@ -400,4 +402,4 @@ TEST(keccak, iuf_test_simple_2)
ASSERT_EQ(to_hex(h256), t.expected_hash256) << t.input_size;
}
}
}
}

0 comments on commit 00e3487

Please sign in to comment.