libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Log | Files | Refs | Submodules | README

commit 53968b627ab387fee306b8090468fa30491fbe01
parent 297b4ff9c9758b62d1754f89b232261ca70a86cc
Author: nolash <dev@holbrook.no>
Date:   Tue, 14 Sep 2021 17:57:05 +0200

Make final hashing recursive, all vectors now correct

Diffstat:
Msrc/file.c | 67++++++++++++++++++++++++++++++-------------------------------------
Msrc/file.h | 1+
Mtest/check_file.c | 48++++++++++++++++++++++++------------------------
3 files changed, 55 insertions(+), 61 deletions(-)

diff --git a/src/file.c b/src/file.c @@ -12,60 +12,52 @@ void filehash_reset(filehash_t *fctx) { fctx->length = 0; } -int filehash_sum(filehash_t *fctx) { +static int filehash_finalize_level(filehash_t *fctx, int level) { bmt_t *bctx; - int r; int l; - int i; + int r; + int next_level; int blocks; - long long blocks_span_length; - int trailer = 0; - int remain_length; - char *target; + int blocks_span_length; + int remainder; bctx = &fctx->bmt_context; - remain_length = fctx->length % SWARM_BLOCK_SIZE; + next_level = level + 1; - target = fctx->buf + _SWARM_WORD_SIZE; - - trailer = 0; - - for (i = 0; i < SWARM_LEVELS; i++) { - if (fctx->ptr[i] == target) { - return fctx->length; - } - l = fctx->ptr[i] - fctx->ptr[i+1]; - blocks = (l / _SWARM_WORD_SIZE); + if (fctx->ptr[level] == fctx->target) { + return fctx->length; + } - if (blocks == 1) { - trailer = 1; - fctx->ptr[i+1] = fctx->ptr[i]; - continue; - } + l = fctx->ptr[level] - fctx->ptr[next_level]; + blocks = (l / _SWARM_WORD_SIZE); - blocks_span_length = 0; + remainder = fctx->length % fctx->spans[level]; - if (trailer == 1) { - blocks--; - blocks_span_length += SWARM_BLOCK_SIZE; - } - - blocks_span_length += blocks * fctx->spans[i]; - if (remain_length > 1) { - blocks_span_length -= SWARM_BLOCK_SIZE - remain_length; + if (blocks == 1) { + fctx->ptr[next_level] = fctx->ptr[level]; + return filehash_finalize_level(fctx, next_level); + } else { + blocks_span_length = (blocks - 1) * fctx->spans[level]; + if (remainder > 0) { + blocks_span_length += remainder; + } else { + blocks_span_length += fctx->spans[level]; } - - bmt_init(bctx, fctx->ptr[i+1], l, (long long)(blocks_span_length)); + bmt_init(bctx, fctx->ptr[next_level], l, (bmt_spansize_t)(blocks_span_length)); r = bmt_sum(bctx); if (r != 0) { return -1; } - memcpy(fctx->ptr[i+1], bctx->buf, _SWARM_WORD_SIZE); - fctx->ptr[i+1] += _SWARM_WORD_SIZE; + memcpy(fctx->ptr[next_level], bctx->buf, _SWARM_WORD_SIZE); + fctx->ptr[next_level] += _SWARM_WORD_SIZE; } + return filehash_finalize_level(fctx, next_level); +} - return -1; + +int filehash_sum(filehash_t *fctx) { + return filehash_finalize_level(fctx, 0); } @@ -79,6 +71,7 @@ void filehash_init(filehash_t *fctx) { fctx->spans[i] = l; l *= _SWARM_BATCH_SIZE; } + fctx->target = fctx->buf + _SWARM_WORD_SIZE; filehash_reset(fctx); } diff --git a/src/file.h b/src/file.h @@ -9,6 +9,7 @@ typedef struct filehash { char buf[SWARM_LEVELS * SWARM_BLOCK_SIZE]; char *ptr[SWARM_LEVELS]; + char *target; long long writes[SWARM_LEVELS]; long long spans[SWARM_LEVELS]; long long length; diff --git a/test/check_file.c b/test/check_file.c @@ -78,35 +78,35 @@ START_TEST(check_file_vectors) { char buf[SWARM_BLOCK_SIZE]; int lengths[] = { -// SWARM_BLOCK_SIZE, -// SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE - 1, -// SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE, -// SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2) - 1, -// SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2), -// SWARM_BLOCK_SIZE * 2, -// SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE, -// SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE - 1, -// SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE, -// SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + (_SWARM_WORD_SIZE * 2), -// SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1), + SWARM_BLOCK_SIZE, + SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE - 1, + SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE, + SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2) - 1, + SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2), + SWARM_BLOCK_SIZE * 2, + SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE, + SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE - 1, + SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE, + SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + (_SWARM_WORD_SIZE * 2), + SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1), SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 2), -// SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE * _SWARM_BATCH_SIZE, + SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE * _SWARM_BATCH_SIZE, }; char *vectors[] = { -// "c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef", -// "91699c83ed93a1f87e326a29ccd8cc775323f9e7260035a5f014c975c5f3cd28", -// "73759673a52c1f1707cbb61337645f4fcbd209cdc53d7e2cedaaa9f44df61285", -// "db1313a727ffc184ae52a70012fbbf7235f551b9f2d2da04bf476abe42a3cb42", -// "ade7af36ac0c7297dc1c11fd7b46981b629c6077bce75300f85b02a6153f161b", -// "29a5fb121ce96194ba8b7b823a1f9c6af87e1791f824940a53b5a7efe3f790d9", -// "3047d841077898c26bbe6be652a2ec590a5d9bd7cd45d290ea42511b48753c09", -// "e5c76afa931e33ac94bce2e754b1bb6407d07f738f67856783d93934ca8fc576", -// "485a526fc74c8a344c43a4545a5987d17af9ab401c0ef1ef63aefcc5c2c086df", -// "624b2abb7aefc0978f891b2a56b665513480e5dc195b4a66cd8def074a6d2e94", -// "b8e1804e37a064d28d161ab5f256cc482b1423d5cd0a6b30fde7b0f51ece9199", + "c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef", + "91699c83ed93a1f87e326a29ccd8cc775323f9e7260035a5f014c975c5f3cd28", + "73759673a52c1f1707cbb61337645f4fcbd209cdc53d7e2cedaaa9f44df61285", + "db1313a727ffc184ae52a70012fbbf7235f551b9f2d2da04bf476abe42a3cb42", + "ade7af36ac0c7297dc1c11fd7b46981b629c6077bce75300f85b02a6153f161b", + "29a5fb121ce96194ba8b7b823a1f9c6af87e1791f824940a53b5a7efe3f790d9", + "3047d841077898c26bbe6be652a2ec590a5d9bd7cd45d290ea42511b48753c09", + "e5c76afa931e33ac94bce2e754b1bb6407d07f738f67856783d93934ca8fc576", + "485a526fc74c8a344c43a4545a5987d17af9ab401c0ef1ef63aefcc5c2c086df", + "624b2abb7aefc0978f891b2a56b665513480e5dc195b4a66cd8def074a6d2e94", + "b8e1804e37a064d28d161ab5f256cc482b1423d5cd0a6b30fde7b0f51ece9199", "59de730bf6c67a941f3b2ffa2f920acfaa1713695ad5deea12b4a121e5f23fa1", -// "522194562123473dcfd7a457b18ee7dee8b7db70ed3cfa2b73f348a992fdfd3b", + "522194562123473dcfd7a457b18ee7dee8b7db70ed3cfa2b73f348a992fdfd3b", }; filehash_init(&fh);