commit 395d2c44593d554d337c1c2712bd4fbef2ef8f28
parent 8e9221ac199f4e6ad97a2a535a662e5882845bc2
Author: nolash <dev@holbrook.no>
Date: Thu, 16 Sep 2021 13:37:11 +0200
Add soc and swarm chunk serialization
Diffstat:
10 files changed, 121 insertions(+), 17 deletions(-)
diff --git a/Makefile.dev b/Makefile.dev
@@ -28,6 +28,7 @@ build_base: prep build_keccak
$(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
+ $(CC) -c -o build/chunk.o $(CFLAGS) src/chunk.c
build_secp256k1: prep
@@ -43,7 +44,7 @@ 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
+ ar -rvs build/test/libtestcommon.a build/common.o build/hex.o
build_check: build_base 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
@@ -54,8 +55,8 @@ build_check_keystore: build_base build_keystore build_check_common
$(CC) -I./src -o build/test/check_keystore build/swarm.o build/keystore.o test/check_keystore.c $(CFLAGS_CHECK) ./aux/secp256k1/.libs/libsecp256k1.a -lcheck -lkeccak-tiny -ltestcommon
-build_check_soc: build build_check_common build_check_keystore
- $(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_check_soc: build build_check_common build_soc
+ $(CC) -I./src -o build/test/check_soc build/chunk.o build/swarm.o build/soc.o test/check_soc.c -L./aux/secp256k1/.libs $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -lsecp256k1
build_lib: build
$(CC) -fPIC -rdynamic --shared -o build/lib/libswarm.so build/swarm.o build/swarmfile.o build/bmt.o build/endian.o
diff --git a/src/chunk.c b/src/chunk.c
@@ -0,0 +1,18 @@
+#include <string.h>
+#include <stddef.h>
+
+#include "chunk.h"
+
+unsigned char* chunk_serialize(const swarm_chunk_t *chunk, unsigned char *z, size_t *sz) {
+ int crsr;
+
+ crsr = 0;
+
+ memcpy(z, chunk->span, SWARM_DATA_LENGTH_TYPESIZE);
+ crsr += SWARM_DATA_LENGTH_TYPESIZE;
+
+ memcpy(z + crsr, chunk->payload, chunk->payload_sz);
+ *sz += crsr + chunk->payload_sz;
+
+ return z;
+}
diff --git a/src/chunk.h b/src/chunk.h
@@ -0,0 +1,16 @@
+
+#ifndef _LIBSWARM_CHUNK_H
+#define _LIBSWARM_CHUNK_H
+
+#include "def.h"
+
+typedef struct swarm_chunk {
+ unsigned char hash[SWARM_WORD_SIZE];
+ unsigned char span[SWARM_DATA_LENGTH_TYPESIZE];
+ size_t payload_sz;
+ unsigned char *payload;
+} swarm_chunk_t;
+
+unsigned char* chunk_serialize(const swarm_chunk_t *chunk, unsigned char *z, size_t *sz);
+
+#endif // _LIBSWARM_CHUNK_H
diff --git a/src/def.h b/src/def.h
@@ -0,0 +1,14 @@
+#define SWARM_WORD_SIZE 32
+#define SWARM_DATA_LENGTH_TYPESIZE 8
+#define SWARM_BLOCK_SIZE 4096
+#define SWARM_ADDRESS_SIZE 20
+
+#define SWARM_PRIVATE_KEY_SIZE 32
+#define SWARM_KEY_LABEL_SIZE 20
+#define SWARM_PUBLIC_KEY_SIZE 65
+#define SWARM_SIGNATURE_SIZE 65
+
+#define SWARM_KECCAK_RATE 200-64
+#define SWARM_KECCAK_PADDING 0x01
+
+
diff --git a/src/keys.h b/src/keys.h
@@ -0,0 +1,4 @@
+#ifndef _LIBSWARM_KEYS_H
+#define _LIBSWARM_KEYS_H
+
+
diff --git a/src/keystore.h b/src/keystore.h
@@ -1,10 +1,7 @@
#ifndef _LIBSWARM_SIGN_H
#define _LIBSWARM_SIGN_H
-#define SWARM_PRIVATE_KEY_SIZE 32
-#define SWARM_KEY_LABEL_SIZE 20
-#define SWARM_PUBLIC_KEY_SIZE 65
-#define SWARM_SIGNATURE_SIZE 65
+#include "swarm.h"
typedef struct keystore_key {
unsigned char pk[SWARM_PRIVATE_KEY_SIZE];
diff --git a/src/soc.c b/src/soc.c
@@ -40,10 +40,10 @@ int soc_address(char *z, const char *identifier, const char *address) {
p = z + SWARM_WORD_SIZE;
src = p;
- memcpy(p, topic, SWARM_SOC_IDENTIFIER_SIZE);
+ memcpy(p, identifier, SWARM_SOC_IDENTIFIER_SIZE);
p += SWARM_SOC_IDENTIFIER_SIZE;
- memcpy(p, index, SWARM_ADDRESS_SIZE);
+ memcpy(p, address, SWARM_ADDRESS_SIZE);
r = keccak_hash_btc(z, SWARM_WORD_SIZE, src, src_sz);
if (r < 0) {
@@ -53,6 +53,16 @@ int soc_address(char *z, const char *identifier, const char *address) {
return 0;
}
-int soc_sign() {
+unsigned char* soc_serialize(const soc_chunk_t *chunk, unsigned char *z, size_t *sz) {
+ int crsr;
+ crsr = 0;
+ memcpy(z, chunk, SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE);
+ crsr += SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE;
+
+ chunk_serialize(&chunk->data, z + crsr, sz);
+
+ *sz += crsr;
+
+ return z;
}
diff --git a/src/soc.h b/src/soc.h
@@ -1,11 +1,21 @@
#ifndef _LIBSWARM_SOC_H
#define _LIBSWARM_SOC_H
+#include "swarm.h"
+#include "chunk.h"
+
#define SWARM_SOC_TOPIC_SIZE 20
#define SWARM_SOC_INDEX_SIZE SWARM_WORD_SIZE
#define SWARM_SOC_IDENTIFIER_SIZE SWARM_WORD_SIZE
+typedef struct soc_chunk {
+ unsigned char identifier[SWARM_SOC_IDENTIFIER_SIZE];
+ unsigned char signature[SWARM_SIGNATURE_SIZE];
+ swarm_chunk_t data;
+} soc_chunk_t;
+
int soc_identifier(char *z, const char *topic, const char *index);
int soc_address(char *z, const char *identifier, const char *address);
+unsigned char* soc_serialize(const soc_chunk_t *chunk, unsigned char *z, size_t *sz);
#endif // _LIBSWARM_SOC_H
diff --git a/src/swarm.h b/src/swarm.h
@@ -1,13 +1,7 @@
#ifndef _LIBSWARM_H
#define _LIBSWARM_H
-#define SWARM_WORD_SIZE 32
-#define SWARM_DATA_LENGTH_TYPESIZE 8
-#define SWARM_BLOCK_SIZE 4096
-#define SWARM_ADDRESS_SIZE 20
-
-#define SWARM_KECCAK_RATE 200-64
-#define SWARM_KECCAK_PADDING 0x01
+#include "def.h"
int keccak_hash_btc(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz);
int keccak_hash_btc_v(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz, const unsigned char *vector, size_t vector_sz);
diff --git a/test/check_soc.c b/test/check_soc.c
@@ -4,6 +4,7 @@
#include "soc.h"
#include "hex.h"
#include "common.h"
+#include "def.h"
START_TEST(check_soc_identifier) {
@@ -36,6 +37,44 @@ START_TEST(check_soc_address) {
}
END_TEST
+START_TEST(check_soc_serialize) {
+ soc_chunk_t soc_chunk;
+ long long span;
+ unsigned char wire[4096*2];
+ unsigned char wire_out[4096*2];
+ size_t wire_length;
+ unsigned char *wire_returned;
+ struct block_generator bg;
+ int crsr;
+
+ bg.v = 0;
+ bg.m = 256;
+
+ block_generate(&bg, (char*)soc_chunk.identifier, SWARM_SOC_IDENTIFIER_SIZE);
+ block_generate(&bg, (char*)soc_chunk.signature, SWARM_SIGNATURE_SIZE);
+
+ block_generate(&bg, soc_chunk.data.hash, SWARM_WORD_SIZE);
+ soc_chunk.data.payload_sz = 1324;
+ span = 2435;
+ memcpy(&soc_chunk.data.span, &span, SWARM_DATA_LENGTH_TYPESIZE);
+ block_generate(&bg, (char*)wire, soc_chunk.data.payload_sz);
+ soc_chunk.data.payload = wire;
+ memcpy(wire_out, wire, soc_chunk.data.payload_sz);
+
+ wire_returned = soc_serialize(&soc_chunk, wire_out, &wire_length);
+
+ crsr = 0;
+ ck_assert_mem_eq(&soc_chunk, wire_returned + crsr, SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE);
+ crsr += SWARM_SOC_IDENTIFIER_SIZE + SWARM_SIGNATURE_SIZE;
+ ck_assert_mem_eq(soc_chunk.data.span, wire_returned + crsr, SWARM_DATA_LENGTH_TYPESIZE);
+ crsr += SWARM_DATA_LENGTH_TYPESIZE;
+ ck_assert_mem_eq(soc_chunk.data.payload, wire_returned + crsr, soc_chunk.data.payload_sz);
+ crsr += soc_chunk.data.payload_sz;
+
+ ck_assert_int_eq(crsr, wire_length);
+}
+END_TEST
+
Suite * common_suite(void) {
Suite *s;
TCase *tc;
@@ -44,6 +83,7 @@ Suite * common_suite(void) {
tc = tcase_create("core");
tcase_add_test(tc, check_soc_identifier);
tcase_add_test(tc, check_soc_address);
+ tcase_add_test(tc, check_soc_serialize);
suite_add_tcase(s, tc);
return s;