began sleep.c, worked sleep_crypto.c, sleep_tree.c

renamed sleep.3 for conflicting
added mock visual for client extra/datthang.png
direction: need to save keys, save seckey ~/.datt/secret_keys
.datt datt with extra t decided for testing until port complete
This commit is contained in:
2025-10-06 18:41:50 -04:00
parent 23c037e02e
commit a1da7deba4
12 changed files with 151 additions and 4 deletions

View File

@@ -24,7 +24,7 @@ static inline unsigned int _ctzll_fallback(uint64_t x) {
}
return 1;
}
static inline unsigned int flattree_ctzll(uint64_t x) {
static inline unsigned int tree_ctzll(uint64_t x) {
#if defined(__GNUC__) || defined(__clang__) || define(__MUSL__)
if (x == 0) return 64;
return (unsigned int)__builtin_ctzll(x);
@@ -33,6 +33,78 @@ static inline unsigned int flattree_ctzll(uint64_t x) {
#endif
}
static inline uint64_t sleep_tree_index(uint64_t depth, uint64_t offset) {
if (depth >= 63) {
return UINT64_MAX;
}
return (offset << (depth + 1)) | ((depth == 0) ? 0ULL : ((1ULL << depth) - 1ULL));
}
static inline uint64_t sleep_tree_depth(uint64_t i) {
uint64_t inverted = ~i;
return (uint64_t)tree_ctzll(inverted);
}
static inline int tree_is_even(uint64_t i) {
return (i & 1ULL) == 0ULL; }
static inline int tree_is_odd(uint64_t i) {
return (i & 1ULL) != 0ULL; }
static inline uint64_t sleep_tree_offset(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (tree_is_even(i)) {
return i / 2;
} else {
return i >> (d + 1);
}
}
static inline uint64_t sleep_tree_parent(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
return sleep_tree_index(d, sleep_tree_offset(i) ^ 1ULL);
}
static inline int sleep_tree_left_child(uint64_t i, uint64_t *out) { // set
if (tree_is_even(i)) return 0;
uint64_t d = sleep_tree_depth(i);
if (d == 0) {
*out = i;
return 1;
}
*out = sleep_tree_index(d - 1, sleep_tree_offset(i) << 1);
return 1;
}
static inline int sleep_tree_right_child(uint64_t i, uint64_t *out) { // horus
if (tree_is_even(i)) return 0;
uint64_t d = sleep_tree_depth(i);
if (d == 0) {
*out = i;
return 1;
}
*out = sleep_tree_index(d - 1, (slep_tree_offset(i) << 1) + 1ULL);
return 1;
}
static inline uint64_t sleep_tree_left_span(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (d == 0) return i;
return sleep_tree_offset(i) * (2ULL << d);
}
static inline uint64_t sleep_tree_right_span(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (d == 0) return i;
return (sleep_tree_offset(i) + 1ULL) * (2ULL << d) - 2ULL;
}
static inline void sleep_tree_spans(uint64_t i, uint64_t *left, uint64_t *right) {
*left = sleep_tree_left_span(i);
*right = sleep_tree_right_span(i);
}
// TODO static inline int sleep_tree_full_roots
// भव न सन हृदयं
int sleep_tree_hash_block(const uint8_t *data, size_t len, uint8_t out_hash[32]) {
#ifndef SLEEP_SALT
@@ -86,7 +158,7 @@ int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks,
free(roots);
return -1;
}
leaf.index = flattree_index(0, (uint64_t)i); // 2 * i ... FIXME flattree naming conventions, defined in scope?
leaf.index = sleep_tree_index(0, (uint64_t)i); // 2 * i ...
leaf.size = (uint64_t)block_lens[i];
if (nodes_len >= max_nodes) {
size_t newcap = max_nodes * 2;
@@ -120,5 +192,3 @@ int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks,
free(roots);
return 0;
}