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 16cd6ddcedb5bb3c08f049f8bfb5b8f375cd6739
parent 8fc4cf4afebe363c8023b00b7e2cae18d15b5a2d
Author: nolash <dev@holbrook.no>
Date:   Sun, 12 Sep 2021 12:50:43 +0200

WIP revert to keccak-tiny

Diffstat:
MMakefile.dev | 24++++++++++++++++++------
Msrc/bmt.c | 67+++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/bmt.h | 5+++--
Csrc/bmt.c -> src/bmt_xkcp.c | 0
Mtest/check_bmt.c | 8+++++---
5 files changed, 71 insertions(+), 33 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -1,14 +1,26 @@ export LD_LIBRARY_PATH +CFLAGS += -I./src -I./aux/keccak-tiny -L./lib/ -build: - mkdir -p build - gcc -I./src -c -o build/bmt.o src/bmt.c $(CFLAGS) -lkeccak - gcc -I./src -c -o build/endian.o $(CFLAGS) src/endian.c +prep: + mkdir -vp lib build + +build_keccak: prep + $(CC) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" $(CFLAGS) -O3 -march=native -std=c11 -Wextra -Wpedantic -Wall -rdynamic --shared aux/keccak-tiny/keccak-tiny.c -o lib/libkeccak-tiny.so + $(CC) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" $(CFLAGS) -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -rdynamic --shared aux/keccak-tiny/keccak-tiny.c -o lib/libkeccak-tiny-small.so + + +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 build_test: build - gcc -I./src -o test/check_bmt build/*.o test/check_bmt.c $(CFLAGS) -lcheck -lkeccak + $(CC) -I./src -o test/check_bmt build/*.o test/check_bmt.c $(CFLAGS) -lcheck -lkeccak-tiny -.PHONY: test +.PHONY: test clean test: build build_test CK_FORK=no test/check_bmt + +clean: + rm -vrf build/* + rm -v lib/* diff --git a/src/bmt.c b/src/bmt.c @@ -1,55 +1,78 @@ #include <string.h> -#include <XKCP/KeccakHash.h> -#include <XKCP/KangarooTwelve.h> -#include <XKCP/SP800-185.h> +//#include <XKCP/KeccakHash.h> +//#include <XKCP/KangarooTwelve.h> +//#include <XKCP/SP800-185.h> +#include "keccak-tiny.h" #include "endian.h" #include "bmt.h" -#define _DIGEST_INPUT_SIZE _WORD_SIZE * 2 +#define _DIGEST_INPUT_SIZE _WORD_SIZE*2 #define _ROLLUP_TARGET BLOCK_SIZE + _DATA_LENGTH_TYPESIZE + _WORD_SIZE +#define _KECCAK_RATE 200-64 +#define _KECCAK_PADDING 0x01 +extern int hash(uint8_t* out, size_t outlen, const uint8_t* in, size_t inlen, size_t rate, uint8_t delim); static int bmt_rollup(bmt_t *bmt_content) { - char *last_target = bmt_content->ptr + _WORD_SIZE; + char *last_target = bmt_content->w_ptr + _WORD_SIZE; + char *start = bmt_content->w_ptr; + char buf[256]; + int r; while (last_target != 0x00) { - while (bmt_content->ptr != bmt_content->target) { - Keccak_HashInstance instance; - if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) { + while (bmt_content->r_ptr < bmt_content->target) { +// Keccak_HashInstance instance; +// if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) { +// return 1; +// } +// Keccak_HashUpdate(&instance, bmt_content->ptr, _DIGEST_INPUT_SIZE); +// Keccak_HashFinal(&instance, bmt_content->ptr); + r = hash(buf, _WORD_SIZE, bmt_content->r_ptr, _DIGEST_INPUT_SIZE, _KECCAK_RATE, _KECCAK_PADDING); + if (r != 0) { return 1; } - Keccak_HashUpdate(&instance, bmt_content->ptr, _DIGEST_INPUT_SIZE); - Keccak_HashFinal(&instance, bmt_content->ptr); - bmt_content->ptr += _DIGEST_INPUT_SIZE; + memcpy(bmt_content->w_ptr, buf, _WORD_SIZE); + bmt_content->w_ptr += _WORD_SIZE; + bmt_content->r_ptr += _DIGEST_INPUT_SIZE; } - bmt_content->target = (bmt_content->target - bmt_content->ptr) / 2; - if (bmt_content->target < last_target) { + bmt_content->target = start + ((bmt_content->target - start) / 2); + if (bmt_content->target == last_target) { last_target = 0x00; } + bmt_content->r_ptr = start; + bmt_content->w_ptr = start; } - Keccak_HashInstance instance; - if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) { + r = hash(buf, _WORD_SIZE, bmt_content->buf, _DATA_LENGTH_TYPESIZE + _WORD_SIZE, _KECCAK_RATE, _KECCAK_PADDING); + if (r != 0) { return 1; } - Keccak_HashUpdate(&instance, bmt_content->buf, _DATA_LENGTH_TYPESIZE + _WORD_SIZE); - Keccak_HashFinal(&instance, bmt_content->buf); + memcpy(bmt_content->buf, buf, _WORD_SIZE); + +// Keccak_HashInstance instance; +// if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) { +// return 1; +// } +// Keccak_HashUpdate(&instance, bmt_content->buf, _DATA_LENGTH_TYPESIZE + _WORD_SIZE); +// Keccak_HashFinal(&instance, bmt_content->buf); return 0; } void bmt_init(bmt_t *bmt_content, char *input, size_t input_length, long long data_length) { - bmt_content->ptr = (char*)bmt_content->buf+_DATA_LENGTH_TYPESIZE; - bmt_content->target = bmt_content->ptr + BLOCK_SIZE; - memset(bmt_content->buf, 0, BLOCK_SIZE+_DATA_LENGTH_TYPESIZE); + bmt_content->w_ptr = (char*)bmt_content->buf+_DATA_LENGTH_TYPESIZE; + bmt_content->r_ptr = bmt_content->w_ptr; + bmt_content->target = bmt_content->w_ptr + BLOCK_SIZE; + memset(bmt_content->buf, 0, _DATA_LENGTH_TYPESIZE + BLOCK_SIZE); memcpy((char*)bmt_content->buf, &data_length, sizeof(long long)); - to_endian(CONVERT_BIGENDIAN, _DATA_LENGTH_TYPESIZE, bmt_content->buf); - memcpy(bmt_content->ptr, input, input_length); + to_endian(CONVERT_LITTLEENDIAN, _DATA_LENGTH_TYPESIZE, bmt_content->buf); + + memcpy(bmt_content->w_ptr, input, input_length); } diff --git a/src/bmt.h b/src/bmt.h @@ -6,8 +6,9 @@ #define _DATA_LENGTH_TYPESIZE 8 typedef struct bmt { - char buf[BLOCK_SIZE + _DATA_LENGTH_TYPESIZE]; - char *ptr; + char buf[_DATA_LENGTH_TYPESIZE + BLOCK_SIZE]; + char *w_ptr; + char *r_ptr; char *target; } bmt_t; diff --git a/src/bmt.c b/src/bmt_xkcp.c diff --git a/test/check_bmt.c b/test/check_bmt.c @@ -8,12 +8,12 @@ START_TEST(check_bmt_init) { bmt_t bmt_context; char *input = "foo"; char input_length = 3; - char data_length_bytes[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; + char data_length_bytes[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; int r; bmt_init(&bmt_context, input, input_length, 3); ck_assert_mem_eq(bmt_context.buf, data_length_bytes, sizeof(long long)); - ck_assert_mem_eq(bmt_context.ptr, input, 3); + ck_assert_mem_eq(bmt_context.w_ptr, input, 3); } END_TEST @@ -22,9 +22,11 @@ START_TEST(check_bmt_sum) { bmt_t bmt_context; char *input = "foo"; char input_length = 3; + int r; bmt_init(&bmt_context, input, input_length, 3); - bmt_sum(&bmt_context); + r = bmt_sum(&bmt_context); + ck_assert_int_eq(r, 0); } Suite * common_suite(void) {