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 3a36efb2a722fda6a64a571342c527c30da36f12
parent 53968b627ab387fee306b8090468fa30491fbe01
Author: nolash <dev@holbrook.no>
Date:   Tue, 14 Sep 2021 18:09:51 +0200

Rename file source file

Diffstat:
MMakefile.dev | 10+++++-----
Dsrc/file.c | 123-------------------------------------------------------------------------------
Dsrc/file.h | 24------------------------
Mtest/check_file.c | 6+++---
4 files changed, 8 insertions(+), 155 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -18,19 +18,19 @@ build_keccak: prep build: prep build_keccak $(CC) -c -o build/bmt.o src/bmt.c $(CFLAGS) -lkeccak-tiny $(CC) -c -o build/endian.o $(CFLAGS) src/endian.c - $(CC) -c -o build/file.o $(CFLAGS) src/file.c + $(CC) -c -o build/swarmfile.o $(CFLAGS) src/swarmfile.c build_check: build $(CC) -c -o build/hex.o $(CFLAGS) src/hex.c $(CC) -c -o build/common.o $(CFLAGS) test/common.c ar -rvs build/test/libtestcommon.a build/common.o build/hex.o - $(CC) -I./src -o build/test/check_bmt build/bmt.o build/endian.o build/file.o test/check_bmt.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon - $(CC) -I./src -o build/test/check_file build/bmt.o build/endian.o build/file.o test/check_file.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon + $(CC) -I./src -o build/test/check_bmt build/bmt.o build/endian.o build/swarmfile.o test/check_bmt.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon + $(CC) -I./src -o build/test/check_file build/bmt.o build/endian.o build/swarmfile.o test/check_file.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon #$(CC) -I./src -o test/check_bmt build/bmt.o build/endian.o test/check_bmt.c build/libkeccak-tiny-small.a $(CFLAGS) -lcheck build_lib: build - $(CC) -fPIC -rdynamic --shared -o build/lib/libswarm.so build/bmt.o build/endian.o - $(AR) -rvs build/lib/libswarm.a build/bmt.o build/endian.o build/libkeccak-tiny-small.o + $(CC) -fPIC -rdynamic --shared -o build/lib/libswarm.so build/swarmfile.o build/bmt.o build/endian.o + $(AR) -rvs build/lib/libswarm.a build/swarmfile.o build/bmt.o build/endian.o build/libkeccak-tiny-small.o .PHONY: test clean diff --git a/src/file.c b/src/file.c @@ -1,123 +0,0 @@ -#include <stddef.h> -#include <string.h> - -#include "file.h" - -void filehash_reset(filehash_t *fctx) { - int i; - for (i = 0; i < SWARM_LEVELS; i++) { - fctx->ptr[i] = fctx->buf; - fctx->writes[i] = 0; - } - fctx->length = 0; -} - -static int filehash_finalize_level(filehash_t *fctx, int level) { - bmt_t *bctx; - int l; - int r; - int next_level; - int blocks; - int blocks_span_length; - int remainder; - - bctx = &fctx->bmt_context; - - next_level = level + 1; - - if (fctx->ptr[level] == fctx->target) { - return fctx->length; - } - - l = fctx->ptr[level] - fctx->ptr[next_level]; - blocks = (l / _SWARM_WORD_SIZE); - - remainder = fctx->length % fctx->spans[level]; - - 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[next_level], l, (bmt_spansize_t)(blocks_span_length)); - r = bmt_sum(bctx); - if (r != 0) { - return -1; - } - memcpy(fctx->ptr[next_level], bctx->buf, _SWARM_WORD_SIZE); - fctx->ptr[next_level] += _SWARM_WORD_SIZE; - } - return filehash_finalize_level(fctx, next_level); -} - - -int filehash_sum(filehash_t *fctx) { - return filehash_finalize_level(fctx, 0); -} - - -void filehash_init(filehash_t *fctx) { - int i; - int l; - - l = SWARM_BLOCK_SIZE; - - for (i = 0; i < SWARM_LEVELS; i++) { - fctx->spans[i] = l; - l *= _SWARM_BATCH_SIZE; - } - fctx->target = fctx->buf + _SWARM_WORD_SIZE; - - filehash_reset(fctx); -} - -static int filehash_write_hash(filehash_t *fctx, int level, const char *data) { - bmt_t *bctx; - int next_level; - int r; - - fctx->writes[level] += 1; - memcpy(fctx->ptr[level], data, _SWARM_WORD_SIZE); - if (fctx->writes[level] % _SWARM_BATCH_SIZE == 0) { - bctx = &fctx->bmt_context; - next_level = level + 1; - bmt_init(bctx, fctx->ptr[next_level], SWARM_BLOCK_SIZE, (long long)fctx->spans[next_level]); - r = bmt_sum(bctx); - if (r != 0) { - return -1; - } - r = filehash_write_hash(fctx, level + 1, bctx->buf); - if (r != 0) { - return -1; - } - fctx->ptr[level] = fctx->ptr[next_level]; - } else { - fctx->ptr[level] += _SWARM_WORD_SIZE; - } - return 0; -} - -int filehash_write(filehash_t *fctx, const char *data, const size_t data_length) { - bmt_t *bctx; - int r; - - bctx = &fctx->bmt_context; - - bmt_init(bctx, data, data_length, (long long)data_length); - r = bmt_sum(bctx); - if (r != 0) { - return -1; - } - r = filehash_write_hash(fctx, 0, bctx->buf); - if (r != 0) { - return -1; - } - fctx->length += data_length; - - return data_length; -} diff --git a/src/file.h b/src/file.h @@ -1,24 +0,0 @@ -#ifndef _LIBSWARM_FILE -#define _LIBSWARM_FILE - -#include "bmt.h" - -#define _SWARM_BATCH_SIZE (int)(SWARM_BLOCK_SIZE / _SWARM_WORD_SIZE) -#define SWARM_LEVELS 9 - -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; - bmt_t bmt_context; -} filehash_t; - -void filehash_reset(filehash_t *filehash_context); -void filehash_init(filehash_t *filehash_context); -int filehash_write(filehash_t *filehash_context, const char *data, const size_t data_length); -int filehash_sum(filehash_t *filehash_content); - -#endif // _LIBSWARM_FILE diff --git a/test/check_file.c b/test/check_file.c @@ -3,7 +3,7 @@ #include <string.h> #include "hex.h" -#include "file.h" +#include "swarmfile.h" #include "common.h" @@ -53,9 +53,9 @@ START_TEST(check_file_write_batch) { for (i = 0; i < _SWARM_BATCH_SIZE; i++) { r = block_generate(&bg, buf, SWARM_BLOCK_SIZE); - //ck_assert_int_eq(r, SWARM_BLOCK_SIZE); + ck_assert_int_eq(r, SWARM_BLOCK_SIZE); r = filehash_write(&fh, buf, SWARM_BLOCK_SIZE); - //ck_assert_int_eq(r, SWARM_BLOCK_SIZE); + ck_assert_int_eq(r, SWARM_BLOCK_SIZE); } ck_assert_int_eq(fh.writes[0], _SWARM_BATCH_SIZE); ck_assert_int_eq(fh.length, _SWARM_BATCH_SIZE * SWARM_BLOCK_SIZE);