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 6a68d5546652a99241ee3f0473eb4f9f677a2550
parent e414b99c0d07bd35469cd64838a1aafc302f75aa
Author: nolash <dev@holbrook.no>
Date:   Thu, 16 Sep 2021 08:30:37 +0200

Add soc identifier create, test file, refactor keccak interface

Diffstat:
M.gitmodules | 6++++++
ADEPENDENCIES | 9+++++++++
MMakefile.dev | 32++++++++++++++++++++++++--------
AROADMAP | 6++++++
MVERSION | 2+-
Aaux/secp256k1 | 1+
Aaux/tiny-bignum-c | 1+
Alicenses/CC0 | 26++++++++++++++++++++++++++
RLICENSE -> licenses/GPL3 | 0
Alicenses/MIT | 19+++++++++++++++++++
Msrc/bmt.c | 29++++++++++++++---------------
Msrc/bmt.h | 8+++++---
Asrc/soc.c | 30++++++++++++++++++++++++++++++
Asrc/soc.h | 9+++++++++
Asrc/swarm.c | 9+++++++++
Asrc/swarm.h | 13+++++++++++++
Mtest/check_bmt.c | 2++
Atest/check_soc.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
18 files changed, 225 insertions(+), 27 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -1,3 +1,9 @@ [submodule "aux/keccak-tiny"] path = aux/keccak-tiny url = https://gitlab.com/nolash/keccak-tiny.git +[submodule "aux/tiny-bignum-c"] + path = aux/tiny-bignum-c + url = https://github.com/kokke/tiny-bignum-c +[submodule "aux/secp256k1"] + path = aux/secp256k1 + url = https://gitlab.com/nolash/secp256k1 diff --git a/DEPENDENCIES b/DEPENDENCIES @@ -0,0 +1,9 @@ +- libsecp256k1 + * Source: bitcoin-core, github.com + * License: MIT +- keccak-tiny + * Source: David Leon Gil, github.com + * License: CC0 +- tiny-bignum-c + * Source: kokke, github.com + * License: In public domain diff --git a/Makefile.dev b/Makefile.dev @@ -23,29 +23,45 @@ build_keccak: prep $(CC) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" $(CFLAGS) -Os -march=native -std=c11 -Wextra -Wpedantic -Wall $(KECCAK_DIR)/keccak-tiny.c -c -o build/libkeccak-tiny-small.o $(CC) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" $(CFLAGS) -Os -march=native -std=c11 -Wextra -Wpedantic -Wall --static $(KECCAK_DIR)/keccak-tiny.c -c -o build/libkeccak-tiny-small.a -build: prep build_keccak +build_base: 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/swarmfile.o $(CFLAGS) src/swarmfile.c + $(CC) -c -o build/swarm.o $(CFLAGS) src/swarm.c -build_check: build +build_soc: prep build_keccak + $(CC) -c -o build/soc.o src/soc.c $(CFLAGS) -lkeccak-tiny + +build: build_base + +build_check_common: $(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/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_check: build build_check_common + $(CC) -I./src -o build/test/check_bmt build/swarm.o 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/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_file.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon + +build_check_soc: build build_check_common + $(CC) -I./src -o build/test/check_soc build/swarm.o build/soc.o test/check_soc.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon + build_lib: build - $(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 + $(CC) -fPIC -rdynamic --shared -o build/lib/libswarm.so build/swarm.o build/swarmfile.o build/bmt.o build/endian.o + $(AR) -rvs build/lib/libswarm.a build/swarmfile.o build/swarm.o build/bmt.o build/endian.o build/libkeccak-tiny-small.o .PHONY: test clean -check: build build_check +check_base: build build_check LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_bmt LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_file +check_soc: build build_check_soc + LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_soc + +check: check_base check_soc + test: check clean: diff --git a/ROADMAP b/ROADMAP @@ -0,0 +1,6 @@ +- 0.0.1 + * BMT and file hashers +- 0.0.2 + * Single-owner chunks +- 0.0.3 + * Postage stamps diff --git a/VERSION b/VERSION @@ -1 +1 @@ -0.0.1 +0.0.2-alpha.1 diff --git a/aux/secp256k1 b/aux/secp256k1 @@ -0,0 +1 @@ +Subproject commit 2a3a97c665475bc00d5d60f2f04830202983a631 diff --git a/aux/tiny-bignum-c b/aux/tiny-bignum-c @@ -0,0 +1 @@ +Subproject commit 201fb2027e5b98dbc32e432aa369c87f37cc54fc diff --git a/licenses/CC0 b/licenses/CC0 @@ -0,0 +1,26 @@ +Statement of Purpose +The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: + +the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; +moral rights retained by the original author(s) and/or performer(s); +publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; +rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; +rights protecting the extraction, dissemination, use and reuse of data in a Work; +database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and +other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. +2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. + +4. Limitations and Disclaimers. + +No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. +Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. +Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. +Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. diff --git a/LICENSE b/licenses/GPL3 diff --git a/licenses/MIT b/licenses/MIT @@ -0,0 +1,19 @@ +Copyright (c) 2013 Pieter Wuille + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/bmt.c b/src/bmt.c @@ -4,29 +4,28 @@ #include "keccak-tiny.h" #include "endian.h" +#include "swarm.h" #include "bmt.h" -#define _SWARM_DIGEST_INPUT_SIZE _SWARM_WORD_SIZE*2 -#define _SWARM_ROLLUP_TARGET SWARM_BLOCK_SIZE + _SWARM_DATA_LENGTH_TYPESIZE + _SWARM_WORD_SIZE -#define _KECCAK_RATE 200-64 -#define _KECCAK_PADDING 0x01 +#define _SWARM_DIGEST_INPUT_SIZE SWARM_WORD_SIZE*2 +#define _SWARM_ROLLUP_TARGET SWARM_BLOCK_SIZE + SWARM_DATA_LENGTH_TYPESIZE + SWARM_WORD_SIZE extern void dlog(int p, int v); static int bmt_rollup(bmt_t *bctx) { - char *last_target = bctx->w_ptr + _SWARM_WORD_SIZE; + char *last_target = bctx->w_ptr + SWARM_WORD_SIZE; char *start = bctx->w_ptr; char buf[256]; int r; while (last_target != 0x00) { 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); + r = keccak_hash_btc((unsigned char*)buf, SWARM_WORD_SIZE, (unsigned char*)bctx->r_ptr, _SWARM_DIGEST_INPUT_SIZE); if (r < 0) { return 1; } - memcpy(bctx->w_ptr, buf, _SWARM_WORD_SIZE); - bctx->w_ptr += _SWARM_WORD_SIZE; + memcpy(bctx->w_ptr, buf, SWARM_WORD_SIZE); + bctx->w_ptr += SWARM_WORD_SIZE; bctx->r_ptr += _SWARM_DIGEST_INPUT_SIZE; } bctx->target = start + ((bctx->target - start) / 2); @@ -37,23 +36,23 @@ static int bmt_rollup(bmt_t *bctx) { bctx->w_ptr = start; } - r = keccak_hash((unsigned char*)buf, _SWARM_WORD_SIZE, (unsigned char*)bctx->buf, _SWARM_DATA_LENGTH_TYPESIZE + _SWARM_WORD_SIZE, _KECCAK_RATE, _KECCAK_PADDING); + r = keccak_hash_btc((unsigned char*)buf, SWARM_WORD_SIZE, (unsigned char*)bctx->buf, SWARM_DATA_LENGTH_TYPESIZE + SWARM_WORD_SIZE); if (r < 0) { return 1; } - memcpy(bctx->buf, buf, _SWARM_WORD_SIZE); + memcpy(bctx->buf, buf, SWARM_WORD_SIZE); return 0; } 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->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); + memset(bctx->buf, 0, SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); memcpy((char*)bctx->buf, &data_length, sizeof(bmt_spansize_t)); - to_endian(CONVERT_LITTLEENDIAN, _SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)bctx->buf); + to_endian(CONVERT_LITTLEENDIAN, SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)bctx->buf); memcpy(bctx->w_ptr, input, input_length); } @@ -73,7 +72,7 @@ int bmt_hash(char *zOut, const char *input, const size_t input_length, const bmt bmt_t bctx; bmt_init(&bctx, input, input_length, data_length); r = bmt_sum(&bctx); - memcpy(zOut, bctx.buf, _SWARM_WORD_SIZE); + memcpy(zOut, bctx.buf, SWARM_WORD_SIZE); bmt_free(&bctx); return r; } diff --git a/src/bmt.h b/src/bmt.h @@ -1,9 +1,11 @@ #ifndef _LIBSWARM_BMT #define _LIBSWARM_BMT -#define SWARM_BLOCK_SIZE 4096 -#define _SWARM_WORD_SIZE 32 -#define _SWARM_DATA_LENGTH_TYPESIZE 8 +#include "swarm.h" + +#define _SWARM_WORD_SIZE SWARM_WORD_SIZE +#define _SWARM_DATA_LENGTH_TYPESIZE SWARM_DATA_LENGTH_TYPESIZE + typedef struct bmt { //char buf[_SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE]; diff --git a/src/soc.c b/src/soc.c @@ -0,0 +1,30 @@ +#include <string.h> + +#include "keccak-tiny.h" + +#include "swarm.h" +#include "soc.h" + +// z must be minimum 84 bytes long (32 bytes for out, 20 bytes for topic, 32 bytes for index) +int soc_identifier(char *z, const char *topic, const char *index) { + int r; + char *p; + char *src; + size_t src_sz; + + src_sz = SWARM_SOC_TOPIC_SIZE + SWARM_SOC_INDEX_SIZE; + + p = z + SWARM_WORD_SIZE; + src = p; + memcpy(p, topic, SWARM_SOC_TOPIC_SIZE); + + p += SWARM_SOC_TOPIC_SIZE; + memcpy(p, index, SWARM_SOC_INDEX_SIZE); + + r = keccak_hash_btc(z, SWARM_WORD_SIZE, src, src_sz); + if (r < 0) { + return 1; + } + + return 0; +} diff --git a/src/soc.h b/src/soc.h @@ -0,0 +1,9 @@ +#ifndef _LIBSWARM_SOC_H +#define _LIBSWARM_SOC_H + +#define SWARM_SOC_TOPIC_SIZE 20 +#define SWARM_SOC_INDEX_SIZE 32 + +int soc_identifier(char *z, const char *topic, const char *index); + +#endif // _LIBSWARM_SOC_H diff --git a/src/swarm.c b/src/swarm.c @@ -0,0 +1,9 @@ +#include <stdint.h> +#include <stddef.h> + +#include "keccak-tiny.h" +#include "swarm.h" + +int keccak_hash_btc(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz) { + return keccak_hash((uint8_t*)out, out_sz, (uint8_t*)in, in_sz, SWARM_KECCAK_RATE, SWARM_KECCAK_PADDING); +} diff --git a/src/swarm.h b/src/swarm.h @@ -0,0 +1,13 @@ +#ifndef _LIBSWARM_H +#define _LIBSWARM_H + +#define SWARM_WORD_SIZE 32 +#define SWARM_DATA_LENGTH_TYPESIZE 8 +#define SWARM_BLOCK_SIZE 4096 + +#define SWARM_KECCAK_RATE 200-64 +#define SWARM_KECCAK_PADDING 0x01 + +int keccak_hash_btc(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz); + +#endif // _LIBSWARM_H diff --git a/test/check_bmt.c b/test/check_bmt.c @@ -37,6 +37,7 @@ START_TEST(check_bmt_sum_foo) { } END_TEST + START_TEST(check_bmt_sum_vector) { bmt_t bmt_context; int r; @@ -84,6 +85,7 @@ START_TEST(check_bmt_sum_vector) { } END_TEST + Suite * common_suite(void) { Suite *s; TCase *tc; diff --git a/test/check_soc.c b/test/check_soc.c @@ -0,0 +1,50 @@ +#include <check.h> +#include <stdlib.h> + +#include "soc.h" +#include "hex.h" +#include "common.h" + + +START_TEST(check_soc_identifier) { + int i; + int r; + char out[84]; + char in[52]; + + for (i = 0; i < 52; i++) { + in[i] = i; + } + + r = soc_identifier(out, in, in+20); + ck_assert_int_eq(r, 0); +} +END_TEST + +Suite * common_suite(void) { + Suite *s; + TCase *tc; + + s = suite_create("soc"); + tc = tcase_create("core"); + tcase_add_test(tc, check_soc_identifier); + suite_add_tcase(s, tc); + + return s; +} + +int main(void) { + int n_fail; + + Suite *s; + SRunner *sr; + + s = common_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_VERBOSE); + n_fail = srunner_ntests_failed(sr); + srunner_free(sr); + + return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}