Skip to content

Commit

Permalink
Fletcher32: Add incremental API
Browse files Browse the repository at this point in the history
  • Loading branch information
bergzand committed Oct 18, 2023
1 parent 3a4ec8d commit 31df2ba
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
40 changes: 31 additions & 9 deletions sys/checksum/fletcher32.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,43 @@
#include "unaligned.h"
#include "checksum/fletcher32.h"

uint32_t fletcher32(const uint16_t *data, size_t words)
static inline void _reduce(fletcher32_ctx_t *ctx)
{
uint32_t sum1 = 0xffff, sum2 = 0xffff;
ctx->sum1 = (ctx->sum1 & 0xffff) + (ctx->sum1 >> 16);
ctx->sum2 = (ctx->sum2 & 0xffff) + (ctx->sum2 >> 16);
}

void fletcher32_init(fletcher32_ctx_t *ctx)
{
ctx->sum1 = 0xffff;
ctx->sum2 = 0xffff;
}

uint32_t fletcher32_finish(fletcher32_ctx_t *ctx)
{
/* Second reduction step to reduce sums to 8 bits */
_reduce(ctx);
return (ctx->sum2 << 16) | ctx->sum1;
}

void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words)
{
const uint16_t *u16_data = (const uint16_t*)data;
while (words) {
unsigned tlen = words > 359 ? 359 : words;
words -= tlen;
do {
sum2 += sum1 += unaligned_get_u16(data++);
ctx->sum1 += unaligned_get_u16(u16_data++);
ctx->sum2 += ctx->sum1;
} while (--tlen);
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
_reduce(ctx);
}
/* Second reduction step to reduce sums to 16 bits */
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
return (sum2 << 16) | sum1;
}

uint32_t fletcher32(const uint16_t *data, size_t words)
{
fletcher32_ctx_t ctx;
fletcher32_init(&ctx);
fletcher32_update(&ctx, data, words);
return fletcher32_finish(&ctx);
}
40 changes: 40 additions & 0 deletions sys/include/checksum/fletcher32.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
* @file
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
* @author Koen Zandberg <koen@bergzand.net>
*/

#ifndef CHECKSUM_FLETCHER32_H
Expand All @@ -27,6 +28,14 @@
extern "C" {
#endif

/**
* @brief Fletcher's 32 bit checksum context struct
*/
typedef struct {
uint32_t sum1; /**< First sum of the checksum */
uint32_t sum2; /**< Second sum of the checksum */
} fletcher32_ctx_t;

/**
* @brief Fletcher's 32 bit checksum
*
Expand All @@ -43,6 +52,37 @@ extern "C" {
*/
uint32_t fletcher32(const uint16_t *buf, size_t words);

/**
* @brief Initialize a fletcher32 context
*
* Multi-part version of @ref fletcher32.
*
* @param[in] ctx fletcher32 context to initialize
*/
void fletcher32_init(fletcher32_ctx_t *ctx);

/**
* @brief Incrementally update the fletcher32 context with new data. Can be an arbitrary amount of
* times with new data to checksum.
*
* @note @p words is the number of 16 bit words in the buffer
* @note @p data should contain an integer number of 16 bit words
*
* @param[in] ctx fletcher32 context
* @param[in] data Data to add to the context
* @param[in] words Length of the data in 16 bit words
*/
void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words);

/**
* @brief Finalize the checksum operation and return the checksum
*
* @param[in] ctx fletcher32 context
*
* @return 32 bit sized hash in the interval [1..2^32]
*/
uint32_t fletcher32_finish(fletcher32_ctx_t *ctx);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/tests-checksum/tests-checksum-fletcher32.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,28 @@ static void test_checksum_fletcher32_wrap_around(void)
sizeof(wrap_around_data) - 1, expect));
}

static void test_checksum_fletcher32_wrap_around_piecewise(void)
{
/* XXX: not verified with external implementation yet */
uint32_t expect = 0x5bac8c3d;
fletcher32_ctx_t ctx;
fletcher32_init(&ctx);
size_t full_len = sizeof(wrap_around_data) - 1;
size_t initial_len = full_len / 2;
fletcher32_update(&ctx, wrap_around_data, initial_len / 2);
fletcher32_update(&ctx, (wrap_around_data + initial_len), (full_len - initial_len) / 2);
uint32_t result = fletcher32_finish(&ctx);
TEST_ASSERT_EQUAL_INT(result, expect);
}

Test *tests_checksum_fletcher32_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_checksum_fletcher32_empty),
new_TestFixture(test_checksum_fletcher32_0to1_undetected),
new_TestFixture(test_checksum_fletcher32_atof),
new_TestFixture(test_checksum_fletcher32_wrap_around),
new_TestFixture(test_checksum_fletcher32_wrap_around_piecewise),
};

EMB_UNIT_TESTCALLER(checksum_fletcher32_tests, NULL, NULL, fixtures);
Expand Down

0 comments on commit 31df2ba

Please sign in to comment.