began sleep_tree.c , sleep_tree_entry_t added to sleep.h

This commit is contained in:
2025-10-02 14:08:56 -04:00
parent ad77f1e37f
commit e51f5c7bc8
4 changed files with 131 additions and 6 deletions

11
INSTALL
View File

@@ -1,4 +1,4 @@
x86_64
## x86_64 ** GCC **
deps
NaCl/libsodium
libutp
@@ -11,7 +11,12 @@ open a new tty console and log in as root and then cd to the dir then make insta
CONFIG FLAGS y'all can pass
--
./configure CPPFLAGS="-DSLEEP_SALT"
make SLEEP_SALT=1
--
aarch64
** x86_64 MUSL **
rv64
## aarch64
## rv64

View File

@@ -34,7 +34,7 @@ I like the sound of dat:// better, this is cooler no offense, and the scope of t
"hyperswarm" sounds badass, its just what p2p need(s(ded since 201X)). i wanna be developing hyperswarm. in this implementation its called cesspool. so when people include that particular module in their distros they will package libcess. i've always wanted to write libcess and now here it is, baby.
i'm using C because ez libraries, assert is the ultimate hack, and scheme because macros *shablamo* and s7 lisp-in-C C-in-scheme because hell yeah i specialize in beautiful elegant solutions to your networking problem.
i'm using C because ez libraries, assert is the ultimate hack, and scheme because macros *shablamo* and s7 lisp-in-C because hell yeah i specialize in beautiful elegant solutions to your networking problem.
LICENSE
see LICENSE

View File

@@ -16,18 +16,71 @@ content.tree*/
// test
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SLEEP_HEADER_SIZE 32u
// TODO MAGIC_BITFIELD, MAGIC_SIG, MAGIC_TREE
#define SLEEP_ALGO_BLAKE2B "BLAKE2b"
#define SLEEP_ALGO_ED25519 "Ed25519"
typedef struct sleep_header {
#define SLEEP_TREE_ENTRY_SIZE 40u
#define SLEEP_SIG_ENTRY_SIZE 64u
#define SLEEP_BITFIELD_ENTRY_SIZE 3328u
enum sleep_hash_type {
SLEEP_HASH_LEAF = 0,
SLEEP_HASH_PARENT = 1,
SLEEP_HASH_ROOT = 2
};
// Wire
#if defined(__GNUC__) || defined(__clang__) || defined(__MUSL__)
#define SLEEP_PACKED __attribute__((packed))
#else
#define SLEEP_PACKED
#pragma pack(push,1) // pragma deez
#endif
typedef struct SLEEP_PACKED sleep_header {
uint32_t magic_be;
uint8_t version;
uint16_t entry_size_be;
uint8_t algo_len;
char algo[24];
} sleep_header_t;
// TODO assert must be 32 bytes
// 32 byte hash + 8 byte BE len
typedef struct SLEEP_PACKED sleep_tree_entry {
uint8_t hash[32];
uint64_t length_be;
} sleep_tree_entry_t;
// TODO assert
// 64 bytes
typedef struct SLEEP_PACKED sleep_sig_entry {
uint8_t sig[64];
} sleep_sig_entry_t;
// TODO assert
// 3328 bytes: tree[2048] + data[1024] + index[256]
typedef struct SLEEP_PACKED sleep_bitfield_entry {
uint8_t tree[2048];
uint8_t data[1024];
uint8_t index[256];
} sleep_bitfield_entry_t;
// TODO DOTO ODOT assert it
#if !defined(__GNUC__) && !defined(__clang__)
#pragma pack(pop)
#endif
// TODO BE INT into and out of buffers **
// crypto helpers
#ifdef SLEEP_SALT
@@ -44,5 +97,7 @@ int sleep_compute_discovery_key(const uint8_t pubkey[32], uint8_t out32[32]);
// function decs
#ifdef __cplusplus
}
#endif
#endif /* SLEEP_H */

View File

@@ -0,0 +1,65 @@
/*
Merkle tree
derived from flat-tree implementation DEP-0002, datrs
*/
#include "sleep.h"
#ifdef SLEEP_SALT
#include <sodium.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
// count trailing zeros
static inline unsigned int _ctzll_fallback(uint64_t x) {
if (x == 0) return 64;
unsigned int n = 0;
while ((x & 1ULL) == 0ULL) {
n++;
x >>= 1;
}
return 1;
}
static inline unsigned int flattree_ctzll(uint64_t x) {
#if defined(__GNUC__) || defined(__clang__) || define(__MUSL__)
if (x == 0) return 64;
return (unsigned int)__builtin_ctzll(x);
#else
return _ctzll_fallback(x);
#endif
}
// भव न सन हृदयं
int sleep_tree_hash_block(const uint8_t *data, size_t len, uint8_t out_hash[32]) {
#ifndef SLEEP_SALT
errno = ENOSYS;
return -1;
#else
if (crypto_generichash(out_hash, 32, data, len, NULL, 0) != 0)
return -1;
return 0;
#endif
}
int sleep_tree_hash_parent(const uint8_t left[32], const uint8_t right[32], uint8_t out_hash[32]) {
#ifndef SLEEP_SALT
errno = ENOSYS;
return -1;
#else
uint8_t buf[64];
memcpy(buf, left, 32);
memcpy(buf + 32, right, 32);
if (crypto_generichash(out_hash, 32, buf, sizeof(buf), NULL, 0) != 0)
return -1;
return 0;
#endif
}
int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks, /*FIXME*/, size_t *out_count){
}