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 e550a5cba68e8db5e658a41336548ce9ec4672b6
parent ceb0eb3392cb2c1c426f1747b02c140901541e54
Author: nolash <dev@holbrook.no>
Date:   Wed, 15 Sep 2021 20:15:24 +0200

Add wasm target

Diffstat:
MMakefile.dev | 26++++++++++++++++++++++++--
Msrc/bmt.c | 71+++++++++++++++++++++++++++++++++++++++++++----------------------------
Msrc/bmt.h | 3++-
Mtest/check_file.c | 2+-
4 files changed, 70 insertions(+), 32 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -1,12 +1,20 @@ KECCAK_DIR = ./aux/keccak-tiny -CFLAGS += -I./src -I$(KECCAK_DIR) -L./build/lib/ -L./build/ +INCLUDE = -I./src -I$(KECCAK_DIR) -L./build/lib/ +LIBS += -L./build/ +CFLAGS += $(INCLUDE) $(LIBS) CFLAGS_CHECK = $(CFLAGS) -L./build/test prefix = /usr/local includedir = $(prefix)/include libdir = $(prefix)/lib +wasm_cc = /usr/bin/clang +wasm_libcdir = /opt/wasi-libc +wasm_libdir = $(wasm_libcdir)/lib/wasm32-wasi +wasm_target = wasm32-unknown-wasi +wasm_cflags = -I./src/wasm -m32 --target=$(wasm_target) -Wl,--import-memory -Wl,--import-table -Wl,--allow-undefined -Wl,--no-entry -nostdlib -nostartfiles +wasm_cflags_stdlib = $(wasm_cflags) --sysroot $(wasm_libcdir) prep: - mkdir -vp build/lib build/include build/test + mkdir -vp build/lib build/include build/test build/wasm 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 $(KECCAK_DIR)/keccak-tiny.c -o build/libkeccak-tiny.so @@ -52,3 +60,17 @@ install: install -vDm 0644 build/include/*.h $(includedir)/ install -vDm 0644 build/lib/*.a $(libdir)/ install -vDm 0644 build/lib/*.so $(libdir)/ + +wasm_prep: + mkdir -vp build/wasm + +wasm: wasm_prep + #clang $(CFLAGS) $(wasm_cflags_stdlib) -static -Wl,--export="bmt_hash" -Wl,--export="bmt_hash_heap_init" -Wl,--export="bmt_hash_heap_free" -o build/wasm/swarm.wasm src/wasm.c build/lib/libswarm.a + clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export-all -Os -o build/wasm/common_o.wasm src/wasm/common.c + clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export-all -Os -o build/wasm/wasm_o.wasm src/wasm/wasm.c + clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export="bmt_sum" -Os -Wl,--export="bmt_init_ptr" -Wl,--export="bmt_hash" -o build/wasm/bmt_o.wasm -c src/bmt.c + clang $(CFLAGS) $(wasm_cflags) -c -Wl,--export="to_endian" -Os -o build/wasm/endian_o.wasm src/endian.c + clang $(CFLAGS) $(wasm_cflags_stdlib) -c -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="keccak_hash_heap_init" -Wl,--export="keccak_hash" -o build/wasm/keccak_o.wasm aux/keccak-tiny/keccak-tiny.c + clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="keccak_hash_heap_init" -Wl,--export="keccak_hash_heap_free" -Wl,--export="keccak_hash" -o build/wasm/keccak.wasm build/wasm/keccak_o.wasm build/wasm/wasm_o.wasm $(wasm_libdir)/libc.a + clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="bmt_hash" -Wl,--export="bmt_hash_free" -Wl,--export="bmt_hash_heap" -o build/wasm/swarm.wasm build/wasm/endian_o.wasm build/wasm/keccak_o.wasm build/wasm/bmt_o.wasm build/wasm/wasm_o.wasm $(wasm_libdir)/libc.a + diff --git a/src/bmt.c b/src/bmt.c @@ -1,4 +1,5 @@ #include <string.h> +#include <stdlib.h> #include "keccak-tiny.h" #include "endian.h" @@ -10,55 +11,69 @@ #define _KECCAK_RATE 200-64 #define _KECCAK_PADDING 0x01 -static int bmt_rollup(bmt_t *bmt_content) { - char *last_target = bmt_content->w_ptr + _SWARM_WORD_SIZE; - char *start = bmt_content->w_ptr; +extern void dlog(int p, int v); + +static int bmt_rollup(bmt_t *bctx) { + char *last_target = bctx->w_ptr + _SWARM_WORD_SIZE; + char *start = bctx->w_ptr; char buf[256]; int r; while (last_target != 0x00) { - while (bmt_content->r_ptr < bmt_content->target) { - r = keccak_hash((unsigned char*)buf, _SWARM_WORD_SIZE, (unsigned char*)bmt_content->r_ptr, _SWARM_DIGEST_INPUT_SIZE, _KECCAK_RATE, _KECCAK_PADDING); - if (r != 0) { + while (bctx->r_ptr < bctx->target) { + r = keccak_hash((unsigned char*)buf, _SWARM_WORD_SIZE, (unsigned char*)bctx->r_ptr, _SWARM_DIGEST_INPUT_SIZE, _KECCAK_RATE, _KECCAK_PADDING); + if (r < 0) { return 1; } - memcpy(bmt_content->w_ptr, buf, _SWARM_WORD_SIZE); - bmt_content->w_ptr += _SWARM_WORD_SIZE; - bmt_content->r_ptr += _SWARM_DIGEST_INPUT_SIZE; + memcpy(bctx->w_ptr, buf, _SWARM_WORD_SIZE); + bctx->w_ptr += _SWARM_WORD_SIZE; + bctx->r_ptr += _SWARM_DIGEST_INPUT_SIZE; } - bmt_content->target = start + ((bmt_content->target - start) / 2); - if (bmt_content->target == last_target) { + bctx->target = start + ((bctx->target - start) / 2); + if (bctx->target == last_target) { last_target = 0x00; } - bmt_content->r_ptr = start; - bmt_content->w_ptr = start; + bctx->r_ptr = start; + bctx->w_ptr = start; } - r = keccak_hash((unsigned char*)buf, _SWARM_WORD_SIZE, (unsigned char*)bmt_content->buf, _SWARM_DATA_LENGTH_TYPESIZE + _SWARM_WORD_SIZE, _KECCAK_RATE, _KECCAK_PADDING); - if (r != 0) { + r = keccak_hash((unsigned char*)buf, _SWARM_WORD_SIZE, (unsigned char*)bctx->buf, _SWARM_DATA_LENGTH_TYPESIZE + _SWARM_WORD_SIZE, _KECCAK_RATE, _KECCAK_PADDING); + if (r < 0) { return 1; } - memcpy(bmt_content->buf, buf, _SWARM_WORD_SIZE); - + memcpy(bctx->buf, buf, _SWARM_WORD_SIZE); return 0; } -void bmt_init(bmt_t *bmt_content, const char *input, const size_t input_length, const bmt_spansize_t data_length) { - bmt_content->w_ptr = (char*)bmt_content->buf+_SWARM_DATA_LENGTH_TYPESIZE; - bmt_content->r_ptr = bmt_content->w_ptr; - bmt_content->target = bmt_content->w_ptr + SWARM_BLOCK_SIZE; - - memset(bmt_content->buf, 0, _SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); +void bmt_init(bmt_t *bctx, const char *input, const size_t input_length, const bmt_spansize_t data_length) { + bctx->buf = malloc(_SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); + bctx->w_ptr = bctx->buf + _SWARM_DATA_LENGTH_TYPESIZE; + bctx->r_ptr = bctx->w_ptr; + bctx->target = bctx->w_ptr + SWARM_BLOCK_SIZE; + memset(bctx->buf, 0, _SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); - memcpy((char*)bmt_content->buf, &data_length, sizeof(bmt_spansize_t)); - to_endian(CONVERT_LITTLEENDIAN, _SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)bmt_content->buf); + memcpy((char*)bctx->buf, &data_length, sizeof(bmt_spansize_t)); + to_endian(CONVERT_LITTLEENDIAN, _SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)bctx->buf); - memcpy(bmt_content->w_ptr, input, input_length); + memcpy(bctx->w_ptr, input, input_length); } +int bmt_sum(bmt_t *bctx) { + return bmt_rollup(bctx); +} -int bmt_sum(bmt_t *bmt_content) { - return bmt_rollup(bmt_content); +void bmt_free(bmt_t *bctx) { + free(bctx->buf); } +int bmt_hash(char *zOut, const char *input, const size_t input_length, const bmt_spansize_t data_length) { + int i; + int r; + bmt_t bctx; + bmt_init(&bctx, input, input_length, data_length); + r = bmt_sum(&bctx); + memcpy(zOut, bctx.buf, _SWARM_WORD_SIZE); + bmt_free(&bctx); + return r; +} diff --git a/src/bmt.h b/src/bmt.h @@ -6,7 +6,8 @@ #define _SWARM_DATA_LENGTH_TYPESIZE 8 typedef struct bmt { - char buf[_SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE]; + //char buf[_SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE]; + char *buf; char *w_ptr; char *r_ptr; char *target; diff --git a/test/check_file.c b/test/check_file.c @@ -137,7 +137,7 @@ START_TEST(check_file_callback_intermediate) { file_callback_dir_fd = open(cbdir, O_DIRECTORY | O_SYNC); - filehash_init_callback(&fh, file_callback, callback_static_foo); + filehash_init_callback(&fh, file_callback, (char*)callback_static_foo); bg.v = 0; bg.m = 255;