commit a44d4748e79698aa74936a6f9c5dacb3c5a9a943
parent f136952600e679611fe15d4b1b35fa01c05ad0ef
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Wed, 3 Jun 2026 10:25:58 -0700
dist: delete dead legacy kpkg v2 package-format surface
Every dist_kpkg2_* hash/merkle helper plus the v2 header/index/descriptor
codecs (dist_kpkg_write_header/read_header, encode/decode_index_record,
descriptor_emit/parse) and their v2 types/macros (DistKpkgHeader,
DistKpkgIndexRecord, DistKpkgDescriptor, DIST_KPKG_MAGIC/VERSION/HEADER_SIZE/
INDEX_RECORD_SIZE) were self-referential dead code: the live create/verify
paths in src/api/package.c call only dist_kpkg3_*. Remove the v2 surface,
including the v2-only static helpers hash_u64 and parse_u64. Keep the static
descriptor helpers (trim_lead/trim_trail/set_err/parse_u64_strict/parse_hex32/
copy_field/seen_once) that the v3 parser still depends on.
Build: make lib && make bin (zero warnings)
Tests: make test-driver-pkg (182) test-driver-cas (41)
(cherry picked from commit 3d92b31224305bf77d4247bfd0b835fb4b54fc95)
Diffstat:
| M | src/dist/kpkg.c | | | 328 | ------------------------------------------------------------------------------- |
| M | src/dist/kpkg.h | | | 67 | ------------------------------------------------------------------- |
2 files changed, 0 insertions(+), 395 deletions(-)
diff --git a/src/dist/kpkg.c b/src/dist/kpkg.c
@@ -32,12 +32,6 @@ static uint64_t get_u64le(const uint8_t* p) {
return v;
}
-static void hash_u64(DistBlake2b* h, uint64_t v) {
- uint8_t b[8];
- put_u64le(b, v);
- dist_blake2b_update(h, b, sizeof b);
-}
-
static int emit(KitWriter* out, const char* s) {
return kit_writer_write(out, s, strlen(s)) == KIT_OK ? DIST_OK : DIST_ERR;
}
@@ -60,232 +54,6 @@ static int emit_hex(KitWriter* out, const char* key, const uint8_t* h) {
return emit_kv(out, key, hex);
}
-void dist_kpkg2_leaf_hash(uint8_t out[DIST_BLAKE2B_LEN], uint64_t artifact_id,
- uint64_t chunk_index, const uint8_t* raw,
- size_t raw_len) {
- DistBlake2b h;
- static const uint8_t dom[] = "kpkg2 leaf v1";
- dist_blake2b_init(&h, DIST_BLAKE2B_LEN);
- dist_blake2b_update(&h, dom, sizeof dom - 1);
- hash_u64(&h, artifact_id);
- hash_u64(&h, chunk_index);
- hash_u64(&h, (uint64_t)raw_len);
- dist_blake2b_update(&h, raw, raw_len);
- dist_blake2b_final(&h, out);
-}
-
-void dist_kpkg2_node_hash(uint8_t out[DIST_BLAKE2B_LEN],
- const uint8_t left[DIST_BLAKE2B_LEN],
- const uint8_t right[DIST_BLAKE2B_LEN]) {
- DistBlake2b h;
- static const uint8_t dom[] = "kpkg2 node v1";
- dist_blake2b_init(&h, DIST_BLAKE2B_LEN);
- dist_blake2b_update(&h, dom, sizeof dom - 1);
- dist_blake2b_update(&h, left, DIST_BLAKE2B_LEN);
- dist_blake2b_update(&h, right, DIST_BLAKE2B_LEN);
- dist_blake2b_final(&h, out);
-}
-
-void dist_kpkg2_root_hash(uint8_t out[DIST_BLAKE2B_LEN], const char* kind,
- uint64_t artifact_id,
- const uint8_t top[DIST_BLAKE2B_LEN]) {
- DistBlake2b h;
- static const uint8_t dom[] = "kpkg2 root v1";
- (void)artifact_id;
- dist_blake2b_init(&h, DIST_BLAKE2B_LEN);
- dist_blake2b_update(&h, dom, sizeof dom - 1);
- dist_blake2b_update(&h, (const uint8_t*)kind, strlen(kind));
- dist_blake2b_update(&h, top, DIST_BLAKE2B_LEN);
- dist_blake2b_final(&h, out);
-}
-
-void dist_kpkg2_empty_artifact_root(uint8_t out[DIST_BLAKE2B_LEN],
- uint64_t artifact_id) {
- DistBlake2b h;
- static const uint8_t dom[] = "kpkg2 root v1";
- static const uint8_t kind[] = "artifact-empty";
- dist_blake2b_init(&h, DIST_BLAKE2B_LEN);
- dist_blake2b_update(&h, dom, sizeof dom - 1);
- dist_blake2b_update(&h, kind, sizeof kind - 1);
- hash_u64(&h, artifact_id);
- hash_u64(&h, 0);
- dist_blake2b_final(&h, out);
-}
-
-int dist_kpkg2_artifact_root(uint8_t out[DIST_BLAKE2B_LEN],
- uint64_t artifact_id, const uint8_t* raw,
- size_t raw_len, size_t chunk_size) {
- uint8_t level[DIST_MAX_FILES][DIST_BLAKE2B_LEN];
- size_t leaves, i;
- if (chunk_size == 0) return DIST_ERR;
- if (raw_len == 0) {
- dist_kpkg2_empty_artifact_root(out, artifact_id);
- return DIST_OK;
- }
- leaves = (raw_len + chunk_size - 1u) / chunk_size;
- if (leaves > DIST_MAX_FILES) return DIST_ERR;
- for (i = 0; i < leaves; ++i) {
- size_t off = i * chunk_size;
- size_t n = raw_len - off;
- if (n > chunk_size) n = chunk_size;
- dist_kpkg2_leaf_hash(level[i], artifact_id, (uint64_t)i, raw + off, n);
- }
- while (leaves > 1u) {
- size_t outn = 0;
- for (i = 0; i < leaves; i += 2u) {
- if (i + 1u < leaves)
- dist_kpkg2_node_hash(level[outn], level[i], level[i + 1u]);
- else
- memcpy(level[outn], level[i], DIST_BLAKE2B_LEN);
- ++outn;
- }
- leaves = outn;
- }
- dist_kpkg2_root_hash(out, "artifact", artifact_id, level[0]);
- return DIST_OK;
-}
-
-int dist_kpkg2_verify_proof(const uint8_t leaf[DIST_BLAKE2B_LEN],
- uint64_t leaf_index, uint64_t leaf_count,
- const uint8_t* proof, size_t proof_len,
- const uint8_t root[DIST_BLAKE2B_LEN]) {
- uint8_t cur[DIST_BLAKE2B_LEN], tmp[DIST_BLAKE2B_LEN];
- uint64_t idx = leaf_index, count = leaf_count;
- size_t off = 0;
- if (leaf_count == 0 || leaf_index >= leaf_count) return DIST_ERR;
- memcpy(cur, leaf, DIST_BLAKE2B_LEN);
- while (count > 1u) {
- int has_pair = (idx + 1u < count) || (idx & 1u);
- if (has_pair) {
- if (off + DIST_BLAKE2B_LEN > proof_len) return DIST_ERR;
- if (idx & 1u)
- dist_kpkg2_node_hash(tmp, proof + off, cur);
- else
- dist_kpkg2_node_hash(tmp, cur, proof + off);
- memcpy(cur, tmp, DIST_BLAKE2B_LEN);
- off += DIST_BLAKE2B_LEN;
- }
- idx >>= 1;
- count = (count + 1u) >> 1;
- }
- dist_kpkg2_root_hash(tmp, "artifact", 0, cur);
- return off == proof_len && memcmp(tmp, root, DIST_BLAKE2B_LEN) == 0
- ? DIST_OK
- : DIST_ERR;
-}
-
-int dist_kpkg_write_header(KitWriter* out, const DistKpkgHeader* h) {
- uint8_t b[DIST_KPKG_HEADER_SIZE];
- size_t off = 16u;
- memset(b, 0, sizeof b);
- memcpy(b, DIST_KPKG_MAGIC, 8u);
- put_u32le(b + 8u, DIST_KPKG_VERSION);
- put_u32le(b + 12u, DIST_KPKG_HEADER_SIZE);
-#define PUT(v) \
- do { \
- put_u64le(b + off, (v)); \
- off += 8u; \
- } while (0)
- PUT(h->manifest_offset);
- PUT(h->manifest_size);
- PUT(h->signature_offset);
- PUT(h->signature_size);
- PUT(h->descriptor_offset);
- PUT(h->descriptor_size);
- PUT(h->descriptor_signature_offset);
- PUT(h->descriptor_signature_size);
- PUT(h->pubkey_offset);
- PUT(h->pubkey_size);
- PUT(h->index_offset);
- PUT(h->index_size);
- PUT(h->content_offset);
- PUT(h->content_size);
- PUT(h->alignment);
- PUT(h->chunk_size);
-#undef PUT
- return kit_writer_write(out, b, sizeof b) == KIT_OK ? DIST_OK : DIST_ERR;
-}
-
-int dist_kpkg_read_header(const uint8_t* data, size_t len, DistKpkgHeader* h) {
- size_t off = 16u;
- if (len < DIST_KPKG_HEADER_SIZE) return DIST_ERR;
- if (memcmp(data, DIST_KPKG_MAGIC, 8u) != 0) return DIST_ERR;
- if (get_u32le(data + 8u) != DIST_KPKG_VERSION ||
- get_u32le(data + 12u) != DIST_KPKG_HEADER_SIZE)
- return DIST_ERR;
-#define GET(dst) \
- do { \
- (dst) = get_u64le(data + off); \
- off += 8u; \
- } while (0)
- GET(h->manifest_offset);
- GET(h->manifest_size);
- GET(h->signature_offset);
- GET(h->signature_size);
- GET(h->descriptor_offset);
- GET(h->descriptor_size);
- GET(h->descriptor_signature_offset);
- GET(h->descriptor_signature_size);
- GET(h->pubkey_offset);
- GET(h->pubkey_size);
- GET(h->index_offset);
- GET(h->index_size);
- GET(h->content_offset);
- GET(h->content_size);
- GET(h->alignment);
- GET(h->chunk_size);
-#undef GET
- return DIST_OK;
-}
-
-void dist_kpkg_encode_index_record(uint8_t out[DIST_KPKG_INDEX_RECORD_SIZE],
- const DistKpkgIndexRecord* r) {
- memset(out, 0, DIST_KPKG_INDEX_RECORD_SIZE);
- put_u64le(out + 0, r->artifact_id);
- put_u64le(out + 8, r->chunk_index);
- put_u64le(out + 16, r->content_offset);
- put_u64le(out + 24, r->stored_size);
- put_u64le(out + 32, r->raw_size);
- put_u32le(out + 40, r->compression);
- memcpy(out + 48, r->stored_hash, DIST_BLAKE2B_LEN);
- memcpy(out + 80, r->raw_hash, DIST_BLAKE2B_LEN);
- memcpy(out + 112, r->leaf_hash, DIST_BLAKE2B_LEN);
-}
-
-int dist_kpkg_decode_index_record(const uint8_t* data, size_t len,
- DistKpkgIndexRecord* r) {
- if (len < DIST_KPKG_INDEX_RECORD_SIZE) return DIST_ERR;
- r->artifact_id = get_u64le(data + 0);
- r->chunk_index = get_u64le(data + 8);
- r->content_offset = get_u64le(data + 16);
- r->stored_size = get_u64le(data + 24);
- r->raw_size = get_u64le(data + 32);
- r->compression = get_u32le(data + 40);
- memcpy(r->stored_hash, data + 48, DIST_BLAKE2B_LEN);
- memcpy(r->raw_hash, data + 80, DIST_BLAKE2B_LEN);
- memcpy(r->leaf_hash, data + 112, DIST_BLAKE2B_LEN);
- return DIST_OK;
-}
-
-int dist_kpkg_descriptor_emit(KitWriter* out, const DistKpkgDescriptor* d) {
- if (emit(out, "kit-encoding 2\n") != DIST_OK) return DIST_ERR;
- if (emit_hex(out, "package-id", d->package_id) != DIST_OK) return DIST_ERR;
- if (emit_kv(out, "format", "kpkg") != DIST_OK) return DIST_ERR;
- if (emit_kv(out, "hash", "blake2b-merkle-v1") != DIST_OK) return DIST_ERR;
- if (emit_u64(out, "index-offset", d->index_offset) != DIST_OK)
- return DIST_ERR;
- if (emit_u64(out, "index-size", d->index_size) != DIST_OK) return DIST_ERR;
- if (emit_hex(out, "index-root", d->index_root) != DIST_OK) return DIST_ERR;
- if (emit_u64(out, "content-offset", d->content_offset) != DIST_OK)
- return DIST_ERR;
- if (emit_u64(out, "content-size", d->content_size) != DIST_OK)
- return DIST_ERR;
- if (emit_hex(out, "content-root", d->content_root) != DIST_OK)
- return DIST_ERR;
- if (emit_u64(out, "chunk-size", d->chunk_size) != DIST_OK) return DIST_ERR;
- return emit_u64(out, "alignment", d->alignment);
-}
-
static char* trim_lead(char* s) {
while (*s == ' ' || *s == '\t') ++s;
return s;
@@ -303,16 +71,6 @@ static int set_err(char* err, size_t cap, const char* msg) {
return DIST_ERR;
}
-static int parse_u64(const char* s, uint64_t* out) {
- char* end = NULL;
- unsigned long long v;
- if (!*s) return DIST_ERR;
- v = strtoull(s, &end, 10);
- if (!end || *end != '\0') return DIST_ERR;
- *out = (uint64_t)v;
- return DIST_OK;
-}
-
static int parse_u64_strict(const char* s, uint64_t* out) {
uint64_t v = 0;
if (!*s) return DIST_ERR;
@@ -346,92 +104,6 @@ static int seen_once(uint32_t* seen, uint32_t bit) {
return DIST_OK;
}
-int dist_kpkg_descriptor_parse(const uint8_t* data, size_t len,
- DistKpkgDescriptor* d, char* err,
- size_t errcap) {
- size_t pos = 0;
- int first = 1;
- unsigned seen = 0;
- memset(d, 0, sizeof *d);
- while (pos < len) {
- char buf[DESC_LINE_MAX], *t, *eq, *key, *val;
- size_t end = pos, n;
- while (end < len && data[end] != '\n') ++end;
- n = end - pos;
- if (n >= sizeof buf) return set_err(err, errcap, "line too long");
- memcpy(buf, data + pos, n);
- buf[n] = '\0';
- pos = (end < len) ? end + 1 : end;
- trim_trail(buf);
- if (first) {
- first = 0;
- if (strcmp(buf, "kit-encoding 2") != 0)
- return set_err(err, errcap, "bad encoding descriptor magic/version");
- continue;
- }
- t = trim_lead(buf);
- if (*t == '\0' || *t == '#') continue;
- eq = strchr(t, '=');
- if (!eq) return set_err(err, errcap, "expected key = value");
- *eq = '\0';
- key = t;
- trim_trail(key);
- val = trim_lead(eq + 1);
- if (strcmp(key, "package-id") == 0) {
- if (strlen(val) != 2 * DIST_BLAKE2B_LEN ||
- dist_hex_decode(d->package_id, val, DIST_BLAKE2B_LEN) != DIST_OK)
- return set_err(err, errcap, "bad package-id");
- seen |= 1u << 0;
- } else if (strcmp(key, "format") == 0) {
- if (strcmp(val, "kpkg") != 0) return set_err(err, errcap, "bad format");
- seen |= 1u << 1;
- } else if (strcmp(key, "hash") == 0) {
- if (strcmp(val, "blake2b-merkle-v1") != 0)
- return set_err(err, errcap, "bad hash algorithm");
- seen |= 1u << 2;
- } else if (strcmp(key, "index-offset") == 0) {
- if (parse_u64(val, &d->index_offset) != DIST_OK)
- return set_err(err, errcap, "bad index-offset");
- seen |= 1u << 3;
- } else if (strcmp(key, "index-size") == 0) {
- if (parse_u64(val, &d->index_size) != DIST_OK)
- return set_err(err, errcap, "bad index-size");
- seen |= 1u << 4;
- } else if (strcmp(key, "index-root") == 0) {
- if (strlen(val) != 2 * DIST_BLAKE2B_LEN ||
- dist_hex_decode(d->index_root, val, DIST_BLAKE2B_LEN) != DIST_OK)
- return set_err(err, errcap, "bad index-root");
- seen |= 1u << 5;
- } else if (strcmp(key, "content-offset") == 0) {
- if (parse_u64(val, &d->content_offset) != DIST_OK)
- return set_err(err, errcap, "bad content-offset");
- seen |= 1u << 6;
- } else if (strcmp(key, "content-size") == 0) {
- if (parse_u64(val, &d->content_size) != DIST_OK)
- return set_err(err, errcap, "bad content-size");
- seen |= 1u << 7;
- } else if (strcmp(key, "content-root") == 0) {
- if (strlen(val) != 2 * DIST_BLAKE2B_LEN ||
- dist_hex_decode(d->content_root, val, DIST_BLAKE2B_LEN) != DIST_OK)
- return set_err(err, errcap, "bad content-root");
- seen |= 1u << 8;
- } else if (strcmp(key, "chunk-size") == 0) {
- if (parse_u64(val, &d->chunk_size) != DIST_OK)
- return set_err(err, errcap, "bad chunk-size");
- seen |= 1u << 9;
- } else if (strcmp(key, "alignment") == 0) {
- if (parse_u64(val, &d->alignment) != DIST_OK)
- return set_err(err, errcap, "bad alignment");
- seen |= 1u << 10;
- } else {
- return set_err(err, errcap, "unknown encoding descriptor key");
- }
- }
- return seen == 0x7ffu ? DIST_OK
- : set_err(err, errcap,
- "missing required encoding descriptor field");
-}
-
void dist_kpkg3_region_root(uint8_t out[DIST_BLAKE2B_LEN], const char* kind,
const uint8_t* data, size_t len) {
uint8_t region[DIST_BLAKE2B_LEN];
diff --git a/src/dist/kpkg.h b/src/dist/kpkg.h
@@ -7,12 +7,8 @@
#include "dist.h"
-#define DIST_KPKG_MAGIC "kpkg--2\0"
-#define DIST_KPKG_VERSION 2u
-#define DIST_KPKG_HEADER_SIZE 160u
#define DIST_KPKG_ALIGNMENT 16u
#define DIST_KPKG_CHUNK_SIZE_DEFAULT 65536u
-#define DIST_KPKG_INDEX_RECORD_SIZE 144u
#define DIST_KPKG3_MAGIC "kpkg3\0"
#define DIST_KPKG3_VERSION 3u
@@ -29,17 +25,6 @@ typedef enum DistKpkgCompression {
DIST_KPKG_COMP_LZ4_BLOCK_V1 = 1,
} DistKpkgCompression;
-typedef struct DistKpkgHeader {
- uint64_t manifest_offset, manifest_size;
- uint64_t signature_offset, signature_size;
- uint64_t descriptor_offset, descriptor_size;
- uint64_t descriptor_signature_offset, descriptor_signature_size;
- uint64_t pubkey_offset, pubkey_size;
- uint64_t index_offset, index_size;
- uint64_t content_offset, content_size;
- uint64_t alignment, chunk_size;
-} DistKpkgHeader;
-
typedef struct DistKpkg3Header {
uint64_t manifest_offset, manifest_size;
uint64_t signature_offset, signature_size;
@@ -48,18 +33,6 @@ typedef struct DistKpkg3Header {
uint64_t pubkey_offset, pubkey_size;
} DistKpkg3Header;
-typedef struct DistKpkgIndexRecord {
- uint64_t artifact_id;
- uint64_t chunk_index;
- uint64_t content_offset; /* relative to the content region */
- uint64_t stored_size;
- uint64_t raw_size;
- uint32_t compression;
- uint8_t stored_hash[DIST_BLAKE2B_LEN];
- uint8_t raw_hash[DIST_BLAKE2B_LEN];
- uint8_t leaf_hash[DIST_BLAKE2B_LEN];
-} DistKpkgIndexRecord;
-
typedef struct DistKpkg3IndexRecord {
uint8_t blob_id[DIST_BLAKE2B_LEN];
uint64_t chunk_index;
@@ -72,15 +45,6 @@ typedef struct DistKpkg3IndexRecord {
uint8_t leaf_hash[DIST_BLAKE2B_LEN];
} DistKpkg3IndexRecord;
-typedef struct DistKpkgDescriptor {
- uint8_t package_id[DIST_BLAKE2B_LEN];
- uint64_t index_offset, index_size;
- uint8_t index_root[DIST_BLAKE2B_LEN];
- uint64_t content_offset, content_size;
- uint8_t content_root[DIST_BLAKE2B_LEN];
- uint64_t chunk_size, alignment;
-} DistKpkgDescriptor;
-
typedef struct DistKpkg3TreeObject {
uint8_t tree[DIST_BLAKE2B_LEN];
uint64_t offset, size;
@@ -115,37 +79,6 @@ typedef struct DistKpkg3Descriptor {
size_t n_chunk_sources;
} DistKpkg3Descriptor;
-void dist_kpkg2_leaf_hash(uint8_t out[DIST_BLAKE2B_LEN], uint64_t artifact_id,
- uint64_t chunk_index, const uint8_t* raw,
- size_t raw_len);
-void dist_kpkg2_node_hash(uint8_t out[DIST_BLAKE2B_LEN],
- const uint8_t left[DIST_BLAKE2B_LEN],
- const uint8_t right[DIST_BLAKE2B_LEN]);
-void dist_kpkg2_root_hash(uint8_t out[DIST_BLAKE2B_LEN], const char* kind,
- uint64_t artifact_id,
- const uint8_t top[DIST_BLAKE2B_LEN]);
-void dist_kpkg2_empty_artifact_root(uint8_t out[DIST_BLAKE2B_LEN],
- uint64_t artifact_id);
-int dist_kpkg2_artifact_root(uint8_t out[DIST_BLAKE2B_LEN],
- uint64_t artifact_id, const uint8_t* raw,
- size_t raw_len, size_t chunk_size);
-int dist_kpkg2_verify_proof(const uint8_t leaf[DIST_BLAKE2B_LEN],
- uint64_t leaf_index, uint64_t leaf_count,
- const uint8_t* proof, size_t proof_len,
- const uint8_t root[DIST_BLAKE2B_LEN]);
-
-int dist_kpkg_write_header(KitWriter* out, const DistKpkgHeader* h);
-int dist_kpkg_read_header(const uint8_t* data, size_t len, DistKpkgHeader* h);
-
-void dist_kpkg_encode_index_record(uint8_t out[DIST_KPKG_INDEX_RECORD_SIZE],
- const DistKpkgIndexRecord* r);
-int dist_kpkg_decode_index_record(const uint8_t* data, size_t len,
- DistKpkgIndexRecord* r);
-
-int dist_kpkg_descriptor_emit(KitWriter* out, const DistKpkgDescriptor* d);
-int dist_kpkg_descriptor_parse(const uint8_t* data, size_t len,
- DistKpkgDescriptor* d, char* err, size_t errcap);
-
void dist_kpkg3_region_root(uint8_t out[DIST_BLAKE2B_LEN], const char* kind,
const uint8_t* data, size_t len);
int dist_kpkg3_write_header(KitWriter* out, const DistKpkg3Header* h);