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

bmt_xkcp.c (1727B)


      1 #include <string.h>
      2 
      3 #include <XKCP/KeccakHash.h>
      4 #include <XKCP/KangarooTwelve.h>
      5 #include <XKCP/SP800-185.h>
      6 #include "endian.h"
      7 
      8 #include "bmt.h"
      9 
     10 #define _DIGEST_INPUT_SIZE _WORD_SIZE * 2
     11 #define _ROLLUP_TARGET BLOCK_SIZE + _DATA_LENGTH_TYPESIZE + _WORD_SIZE
     12 
     13 
     14 // TODO: broken, must be updated to use new pointers
     15 static int bmt_rollup(bmt_t *bmt_content) {
     16 	char *last_target = bmt_content->ptr + _WORD_SIZE;
     17 
     18 	while (last_target != 0x00) {
     19 		while (bmt_content->ptr != bmt_content->target) {
     20 			Keccak_HashInstance instance;
     21 			if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) {
     22 				return 1;
     23 			}
     24 			Keccak_HashUpdate(&instance, bmt_content->ptr, _DIGEST_INPUT_SIZE);
     25 			Keccak_HashFinal(&instance, bmt_content->ptr);
     26 			bmt_content->ptr += _DIGEST_INPUT_SIZE;
     27 		}
     28 		bmt_content->target = (bmt_content->target - bmt_content->ptr) / 2;
     29 		if (bmt_content->target < last_target) {
     30 			last_target = 0x00;
     31 		}
     32 	}
     33 
     34 	Keccak_HashInstance instance;
     35 	if (Keccak_HashInitialize(&instance, 1088, 512, 256, 0x01)) {
     36 		return 1;
     37 	}
     38 	Keccak_HashUpdate(&instance, bmt_content->buf, _DATA_LENGTH_TYPESIZE + _WORD_SIZE);
     39 	Keccak_HashFinal(&instance, bmt_content->buf);
     40 
     41 	return 0;
     42 }
     43 
     44 
     45 void bmt_init(bmt_t *bmt_content, char *input, size_t input_length, long long data_length) {
     46 	bmt_content->ptr = (char*)bmt_content->buf+_DATA_LENGTH_TYPESIZE;
     47 	bmt_content->target = bmt_content->ptr + BLOCK_SIZE;
     48 	memset(bmt_content->buf, 0, BLOCK_SIZE+_DATA_LENGTH_TYPESIZE);
     49 
     50 	memcpy((char*)bmt_content->buf, &data_length, sizeof(long long));
     51 	to_endian(CONVERT_BIGENDIAN, _DATA_LENGTH_TYPESIZE, bmt_content->buf);
     52 
     53 	memcpy(bmt_content->ptr, input, input_length);
     54 }
     55 
     56 
     57 int bmt_sum(bmt_t *bmt_content) {
     58 	return bmt_rollup(bmt_content);
     59 }
     60 
     61