navid/veins / commit
28ba6a798dc1
Navid M <navidmomtahen@gmail.com> · 2026-11-25
parents: cba7a7a323b2
Browse files at this commit · Download source
--- a/src/src/chunking/fastcdc.sx+++ b/src/src/chunking/fastcdc.sx@@ -22,12 +22,8 @@ MASK_L: u64 = 0x3fff ) -fn gear_at(c: u8) u64 = {- val o = @get(gear.GEAR, c as usize)- if o is some v {- return v- }- return 0+fn gear_at(c: u8) u64 = inline trust {+ return @load64(@ptradd(gear.GEAR_RAW, (c as i64) * 8)) as u64 } fn min_usize(a: usize, b: usize) usize = {@@ -39,7 +35,7 @@ /// Return the length of the next chunk at `data`, given that at least /// `len` bytes remain. The result is in `1..=min(len, MAX_SIZE)`.-pub fn next_len(data: ref u8, len: usize) usize = {+pub fn next_len(data: ref u8, len: usize) usize = trust { pre { progress: len > 0 }@@ -51,7 +47,7 @@ val fp: mut u64 = 0 val i: mut usize = MIN_SIZE for i < normal {- val c: u8 = trust @load8(@ptradd(data, i as i64))+ val c: u8 = @load8(@ptradd(data, i as i64)) fp = (fp >> 1) + gear_at(c) if (fp & MASK_S) == 0 { return i + 1@@ -59,7 +55,7 @@ i++ } for i < limit {- val c2: u8 = trust @load8(@ptradd(data, i as i64))+ val c2: u8 = @load8(@ptradd(data, i as i64)) fp = (fp >> 1) + gear_at(c2) if (fp & MASK_L) == 0 { return i + 1@@ -70,11 +66,11 @@ } /// Split a byte span into chunk lengths.-pub fn split(data: ref u8, len: usize) list[usize] = {+pub fn split(data: ref u8, len: usize) list[usize] = trust { val out: mut list[usize] = lists.new[usize]() val off: mut usize = 0 for off < len {- val n: usize = next_len(trust @ptradd(data, off as i64), len - off)+ val n: usize = next_len(@ptradd(data, off as i64), len - off) @append(out, n) off = off + n }Binary files a/src/src/chunking/gear.bin and b/src/src/chunking/gear.bin differ--- a/src/src/chunking/gear.sx+++ b/src/src/chunking/gear.sx@@ -73,3 +73,7 @@ 0x40d773d869517dc2, 0xdb50fd3858eb7ee6, 0x5af4344ac6a899c7, 0xb055dc42b7329b9b, 0x2314a1c54ecfe1c7, 0x0eaeed2e3c60c346, 0xfe5614b9ebe6329b, 0x8592445ff13752f9, ]++/// Byte view of the frozen table for fast FFI-free indexing.+pub val GEAR_RAW: ref u8 = @embed("gear.bin")+--- a/src/src/crypto/blake3.sx+++ b/src/src/crypto/blake3.sx@@ -229,9 +229,18 @@ val base: ref u64 = @ptradd(stack, (depth * 64) as i64) as ref u64 copy_words(out, base, 8) }++extern (C) fn vein_blake3(data: ref u8, len: usize, out: ref u8) void = "vein_blake3" /// Hash `len` bytes at `data` and write the 32-byte digest to `out`.+///+/// The production build links the scalar C implementation. The pure+/// implementation below is retained for format tests and fallback use. pub fn hash(data: ref u8, len: usize, out: ref u8) void = trust {+ vein_blake3(data, len, out)+}++pub fn hash_pure(data: ref u8, len: usize, out: ref u8) void = trust { val stack: ref u64 = @alloc((MAX_STACK_DEPTH + 1) * 64) as ref u64 val scratch: ref u64 = @alloc(128) as ref u64 val out_cv: ref u64 = @alloc(64) as ref u64--- /dev/null+++ b/src/src/crypto/blake3_c.c@@ -0,0 +1,205 @@+/*+ * Minimal scalar BLAKE3 (unkeyed, 256-bit output).+ *+ * This is a direct C transcription of the format's reference implementation.+ * It is linked into vn to keep object-ID hashing practical during large+ * imports. The pure Spectre implementation remains in blake3.sx for tests and+ * for environments where the C object is not linked.+ */++#include <stddef.h>+#include <stdint.h>+#include <string.h>++static const uint32_t IV[8] = {+ 0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au,+ 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u,+};++static const uint8_t MSG[16 * 7] = {+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,+ 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8,+ 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1,+ 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6,+ 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4,+ 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7,+ 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13,+};++enum {+ CHUNK_START = 1,+ CHUNK_END = 2,+ PARENT = 4,+ ROOT = 8,+};++static inline uint32_t rotr32(uint32_t x, unsigned n) {+ return (x >> n) | (x << (32 - n));+}++static inline void g(uint32_t s[16], int a, int b, int c, int d, uint32_t mx, uint32_t my) {+ uint32_t x0 = s[a] + s[b] + mx;+ uint32_t x3 = rotr32(s[d] ^ x0, 16);+ uint32_t x2 = s[c] + x3;+ uint32_t x1 = rotr32(s[b] ^ x2, 12);+ x0 = x0 + x1 + my;+ x3 = rotr32(x3 ^ x0, 8);+ x2 = x2 + x3;+ x1 = rotr32(x1 ^ x2, 7);+ s[a] = x0;+ s[b] = x1;+ s[c] = x2;+ s[d] = x3;+}++static void compress_into(+ const uint32_t cv[8],+ const uint32_t block[16],+ uint64_t counter,+ uint32_t block_len,+ uint32_t flags,+ uint32_t out[16]+) {+ for (int i = 0; i < 8; i++) out[i] = cv[i];+ out[8] = IV[0];+ out[9] = IV[1];+ out[10] = IV[2];+ out[11] = IV[3];+ out[12] = (uint32_t)counter;+ out[13] = (uint32_t)(counter >> 32);+ out[14] = block_len;+ out[15] = flags;++ for (int r = 0; r < 7; r++) {+ const uint8_t *m = MSG + r * 16;+ g(out, 0, 4, 8, 12, block[m[0]], block[m[1]]);+ g(out, 1, 5, 9, 13, block[m[2]], block[m[3]]);+ g(out, 2, 6, 10, 14, block[m[4]], block[m[5]]);+ g(out, 3, 7, 11, 15, block[m[6]], block[m[7]]);+ g(out, 0, 5, 10, 15, block[m[8]], block[m[9]]);+ g(out, 1, 6, 11, 12, block[m[10]], block[m[11]]);+ g(out, 2, 7, 8, 13, block[m[12]], block[m[13]]);+ g(out, 3, 4, 9, 14, block[m[14]], block[m[15]]);+ }++ for (int i = 0; i < 8; i++) {+ uint32_t lo = out[i];+ uint32_t hi = out[i + 8];+ out[i] = lo ^ hi;+ out[i + 8] = hi ^ cv[i];+ }+}++static void load_block(const uint8_t *src, size_t len, uint32_t block[16]) {+ uint8_t tmp[64];+ memset(tmp, 0, sizeof(tmp));+ if (len > 0) memcpy(tmp, src, len);+ for (int i = 0; i < 16; i++) {+ block[i] = (uint32_t)tmp[i * 4]+ | ((uint32_t)tmp[i * 4 + 1] << 8)+ | ((uint32_t)tmp[i * 4 + 2] << 16)+ | ((uint32_t)tmp[i * 4 + 3] << 24);+ }+}++static void chunk_cv(+ const uint8_t *data,+ size_t len,+ uint64_t chunk_index,+ uint32_t cv[8],+ uint32_t final_block[16],+ uint32_t *final_flags,+ uint32_t *final_len,+ uint64_t *final_counter+) {+ size_t blocks = len == 0 ? 1 : (len + 63) / 64;+ for (int i = 0; i < 8; i++) cv[i] = IV[i];++ uint32_t state[16];+ for (size_t i = 0; i + 1 < blocks; i++) {+ uint32_t block[16];+ load_block(data + i * 64, 64, block);+ uint32_t flags = (i == 0) ? CHUNK_START : 0;+ compress_into(cv, block, chunk_index, 64, flags, state);+ for (int j = 0; j < 8; j++) cv[j] = state[j];+ }++ size_t last = blocks - 1;+ size_t last_len = len - last * 64;+ load_block(data + last * 64, last_len, final_block);+ *final_flags = CHUNK_END | ((last == 0) ? CHUNK_START : 0);+ *final_len = (uint32_t)last_len;+ *final_counter = chunk_index;+}++static void output_cv(+ const uint32_t cv[8],+ const uint32_t block[16],+ uint64_t counter,+ uint32_t block_len,+ uint32_t flags,+ uint32_t out_cv[8]+) {+ uint32_t state[16];+ compress_into(cv, block, counter, block_len, flags, state);+ for (int i = 0; i < 8; i++) out_cv[i] = state[i];+}++static void parent_cv(const uint32_t left[8], const uint32_t right[8], uint32_t out_cv[8]) {+ uint32_t block[16];+ for (int i = 0; i < 8; i++) block[i] = left[i];+ for (int i = 0; i < 8; i++) block[i + 8] = right[i];+ output_cv(IV, block, 0, 64, PARENT, out_cv);+}++void vein_blake3(const uint8_t *data, size_t len, uint8_t out[32]) {+ uint32_t stack[54][8];+ int depth = 0;+ uint64_t chunks = len == 0 ? 1 : (uint64_t)((len + 1023) / 1024);++ for (uint64_t i = 0; i + 1 < chunks; i++) {+ uint32_t cv[8], block[16], flags, block_len;+ uint64_t counter;+ chunk_cv(data + (size_t)i * 1024, 1024, i, cv, block, &flags, &block_len, &counter);+ uint32_t new_cv[8];+ output_cv(cv, block, counter, block_len, flags, new_cv);+ uint64_t total = i + 1;+ while ((total & 1) == 0) {+ depth--;+ parent_cv(stack[depth], new_cv, new_cv);+ total >>= 1;+ }+ memcpy(stack[depth], new_cv, sizeof(new_cv));+ depth++;+ }++ uint64_t last_index = chunks - 1;+ size_t last_off = (size_t)last_index * 1024;+ size_t last_len = len - last_off;+ uint32_t cv[8], block[16], flags, block_len;+ uint64_t counter;+ chunk_cv(data + last_off, last_len, last_index, cv, block, &flags, &block_len, &counter);++ uint32_t cur_cv[8];+ while (depth > 0) {+ output_cv(cv, block, counter, block_len, flags, cur_cv);+ depth--;+ for (int i = 0; i < 8; i++) {+ block[i] = stack[depth][i];+ block[i + 8] = cur_cv[i];+ cv[i] = IV[i];+ }+ flags = PARENT;+ block_len = 64;+ counter = 0;+ }++ uint32_t state[16];+ compress_into(cv, block, counter, block_len, flags | ROOT, state);+ for (int i = 0; i < 8; i++) {+ out[i * 4] = (uint8_t)(state[i] & 255);+ out[i * 4 + 1] = (uint8_t)((state[i] >> 8) & 255);+ out[i * 4 + 2] = (uint8_t)((state[i] >> 16) & 255);+ out[i * 4 + 3] = (uint8_t)((state[i] >> 24) & 255);+ }+}--- a/src/src/git/import.sx+++ b/src/src/git/import.sx@@ -28,6 +28,10 @@ val refs = use("refs.sx") val oidmap = use("oidmap.sx") val gitmap = use("map.sx")++when linux {+ extern (C) fn malloc_trim(pad: usize) i32 = "malloc_trim"+} pub type ImportStats = { commits: mut usize@@ -179,7 +183,7 @@ fn import_blob(ctx: mut ImportCtx, oid: bytes.Bytes) option[objid.Id] = trust { val existing = get_id(ctx.blobs_map, oid) if existing is some id {- return some id+ return some dup_id(id) } val obj_opt = object.read(ctx.git, oid) val obj: mut object.Object = object.Object{kind: 0, body: bytes.empty(), alloc: bytes.empty()}@@ -215,7 +219,7 @@ fn import_tree(ctx: mut ImportCtx, oid: bytes.Bytes) option[objid.Id] = trust { val existing = get_id(ctx.trees_map, oid) if existing is some id {- return some id+ return some dup_id(id) } val obj_opt = object.read(ctx.git, oid) val obj: mut object.Object = object.Object{kind: 0, body: bytes.empty(), alloc: bytes.empty()}@@ -284,6 +288,10 @@ if te.name.ptr != none { val np: mut ref void = te.name.ptr as ref void @free(np)+ }+ if te.target.ptr != none {+ val ip: mut ref void = te.target.ptr as ref void+ @free(ip) } val tp: mut ref void = te as ref void @free(tp)@@ -307,6 +315,10 @@ if te.name.ptr != none { val np2: mut ref void = te.name.ptr as ref void @free(np2)+ }+ if te.target.ptr != none {+ val ip2: mut ref void = te.target.ptr as ref void+ @free(ip2) } val tp2: mut ref void = te as ref void @free(tp2)@@ -568,13 +580,13 @@ } /// Flush object batches as packs during large imports to bound peak memory.-val IMPORT_BATCH_BYTES: usize = 268435456+val IMPORT_BATCH_BYTES: usize = 67108864 fn should_flush(ctx: ImportCtx) bool = { return ctx.object_bytes >= IMPORT_BATCH_BYTES } -fn flush_batch(ctx: mut ImportCtx, repo: layout.Repo) bool = {+fn flush_batch(ctx: mut ImportCtx, repo: layout.Repo) bool = trust { if @len(ctx.objects) == 0 { return true }@@ -582,6 +594,9 @@ raw.free_objects(ctx.objects) ctx.objects = raw.empty_list() ctx.object_bytes = 0+ when linux {+ val _ = malloc_trim(0)+ } return wrote } @@ -870,6 +885,10 @@ if tr is none { build_failed = true break+ }+ if tr is some tid {+ val tp: mut ref void = tid.ptr as ref void+ @free(tp) } if should_flush(ctx) { if !flush_batch(ctx, repo) {--- a/src/src/object/blob.sx+++ b/src/src/object/blob.sx@@ -231,7 +231,7 @@ /// Chunk and manifest a file. All produced objects, including chunks, are /// returned in `objects`; the caller owns them.-pub fn build(data: ref u8, len: usize) ref BlobBuild = {+pub fn build(data: ref u8, len: usize) ref BlobBuild = trust { val objects: mut list[ref raw.RawObject] = raw.empty_list() if len <= prim.INLINE_THRESHOLD { val b: mut buf.Buf = buf.new(len + 16)@@ -248,7 +248,7 @@ val level: mut list[ref NodeRef] = lists.new[ref NodeRef]() val off: mut usize = 0 for off < len {- val n: usize = fastcdc.next_len(trust @ptradd(data, off as i64), len - off)+ val n: usize = fastcdc.next_len(@ptradd(data, off as i64), len - off) val chunk: bytes.Bytes = bytes.Bytes.slice(bytes.bytes(data, len), off, off + n) val cid: objid.Id = objid.object_id(types.CHUNK, chunk) @append(objects, raw.raw(types.CHUNK, bytes.Bytes.dup(chunk)))--- a/src/sx.mod+++ b/src/sx.mod@@ -3,6 +3,7 @@ spectre_version "v0.0.18" build release {+ action "mkdir -p $OUT && cc -O3 -c ./src/crypto/blake3_c.c -o $OUT/blake3_c.o && spectre ./src/main.sx -release -link-obj:$OUT/blake3_c.o -o ./vn" flags "-release" output "./vn" windows {@@ -11,6 +12,7 @@ } build dev {+ action "mkdir -p $OUT && cc -O3 -c ./src/crypto/blake3_c.c -o $OUT/blake3_c.o && spectre ./src/main.sx -link-obj:$OUT/blake3_c.o -o ./vn-dev" output "./vn-dev" windows { output "vn-dev.exe"