Files
dat-thang/inc/sleep.h
T

127 lines
3.3 KiB
C
Raw Normal View History

// ॐ सूक्ष्म शरीर
2025-09-27 14:15:22 -04:00
#ifndef SLEEP_H
#define SLEEP_H
2025-10-02 11:55:48 -04:00
// TODO implement SLEEP
2025-09-27 14:15:22 -04:00
/*metadata.key
metadata.signatures
metadata.bitfield
metadata.tree
metadata.data
content.key
content.signatures
content.bitfield
content.tree*/
2026-10-19 00:12:36 -04:00
#include "keys.h"
2025-10-02 11:55:48 -04:00
#include <stdint.h>
#include <stddef.h>
// test
#include <assert.h>
2025-09-27 14:15:22 -04:00
#ifdef __cplusplus
extern "C" {
#endif
2026-10-19 00:12:36 -04:00
int sleep_init(const char *root);
2025-10-02 11:55:48 -04:00
#define SLEEP_HEADER_SIZE 32u
2026-10-19 00:12:36 -04:00
#define SLEEP_MAGIC_BITFIELD 0x05025700u
#define SLEEP_MAGIC_SIGNATURE 0x05025701u
#define SLEEP_MAGIC_TREE 0x05025702u
// TODO MAGIC_BITFIELD, MAGIC_SIG, MAGIC_TREE
2025-10-02 11:55:48 -04:00
#define SLEEP_ALGO_BLAKE2B "BLAKE2b"
#define SLEEP_ALGO_ED25519 "Ed25519"
#define SLEEP_TREE_ENTRY_SIZE 40u
#define SLEEP_SIG_ENTRY_SIZE 64u
2026-10-19 00:12:36 -04:00
#define SLEEP_BITFIELD_ENTRY_SIZE 3584u
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 {
2025-10-02 11:55:48 -04:00
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
2026-10-19 00:12:36 -04:00
// 3584-byte historical record. The final 256 bytes are opaque for now.
typedef struct SLEEP_PACKED sleep_bitfield_entry {
uint8_t data[1024];
2026-10-19 00:12:36 -04:00
uint8_t tree[2048];
uint8_t index[256];
2026-10-19 00:12:36 -04:00
uint8_t reserved[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 **
2025-10-02 11:55:48 -04:00
// crypto helpers
#ifdef SLEEP_SALT
int sleep_gen_ed25519(uint8_t pubkey[32], uint8_t seckey[64]);
// sign
int sleep_sign_ed25519(const uint8_t *msg, size_t msglen,
const uint8_t seckey[64], uint8_t sig_out[64]);
// verify
int sleep_verify_ed25519(const uint8_t *msg, size_t msglen,
const uint8_t pubkey[32], const uint8_t sig[64]);
// discovery
int sleep_compute_discovery_key(const uint8_t pubkey[32], uint8_t out32[32]);
#endif /* SLEEP_SALT */
// function decs
2026-10-19 00:12:36 -04:00
uint64_t sleep_tree_index(uint64_t depth, uint64_t offset);
uint64_t sleep_tree_depth(uint64_t index);
uint64_t sleep_tree_offset(uint64_t index, uint64_t depth);
uint64_t sleep_tree_parent(uint64_t index);
int sleep_tree_left_child(uint64_t index, uint64_t *out);
int sleep_tree_right_child(uint64_t index, uint64_t *out);
int sleep_tree_sibling(uint64_t index, uint64_t *out);
uint64_t sleep_tree_left_span(uint64_t index);
uint64_t sleep_tree_right_span(uint64_t index);
void sleep_tree_spans(uint64_t index, uint64_t *left, uint64_t *right);
size_t sleep_tree_count(uint64_t index);
size_t sleep_tree_count_leaves(uint64_t index);
size_t sleep_tree_full_roots(uint64_t index, uint64_t *out, size_t capacity);
int sleep_tree_hash_block(const uint8_t *data, size_t len, uint8_t out_hash[32]);
int sleep_tree_hash_parent(const uint8_t left[32], const uint8_t right[32],
uint64_t length, uint8_t out_hash[32]);
2025-10-02 11:55:48 -04:00
#ifdef __cplusplus
}
#endif
2025-10-02 11:55:48 -04:00
#endif /* SLEEP_H */